1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/kernel.h>
21#include <linux/types.h>
22#include <linux/scatterlist.h>
23#include <linux/crc32.h>
24#include <linux/module.h>
25
26#include <scsi/libfc.h>
27#include <scsi/fc_encode.h>
28
29#include "fc_libfc.h"
30
31MODULE_AUTHOR("Open-FCoE.org");
32MODULE_DESCRIPTION("libfc");
33MODULE_LICENSE("GPL v2");
34
35unsigned int fc_debug_logging;
36module_param_named(debug_logging, fc_debug_logging, int, S_IRUGO|S_IWUSR);
37MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
38
39DEFINE_MUTEX(fc_prov_mutex);
40static LIST_HEAD(fc_local_ports);
41struct blocking_notifier_head fc_lport_notifier_head =
42 BLOCKING_NOTIFIER_INIT(fc_lport_notifier_head);
43EXPORT_SYMBOL(fc_lport_notifier_head);
44
45
46
47
48struct fc4_prov *fc_active_prov[FC_FC4_PROV_SIZE] = {
49 [0] = &fc_rport_t0_prov,
50 [FC_TYPE_FCP] = &fc_rport_fcp_init,
51};
52
53
54
55
56struct fc4_prov *fc_passive_prov[FC_FC4_PROV_SIZE] = {
57 [FC_TYPE_ELS] = &fc_lport_els_prov,
58};
59
60
61
62
63static int __init libfc_init(void)
64{
65 int rc = 0;
66
67 rc = fc_setup_fcp();
68 if (rc)
69 return rc;
70
71 rc = fc_setup_exch_mgr();
72 if (rc)
73 goto destroy_pkt_cache;
74
75 rc = fc_setup_rport();
76 if (rc)
77 goto destroy_em;
78
79 return rc;
80destroy_em:
81 fc_destroy_exch_mgr();
82destroy_pkt_cache:
83 fc_destroy_fcp();
84 return rc;
85}
86module_init(libfc_init);
87
88
89
90
91static void __exit libfc_exit(void)
92{
93 fc_destroy_fcp();
94 fc_destroy_exch_mgr();
95 fc_destroy_rport();
96}
97module_exit(libfc_exit);
98
99
100
101
102
103
104
105
106
107
108
109
110
111u32 fc_copy_buffer_to_sglist(void *buf, size_t len,
112 struct scatterlist *sg,
113 u32 *nents, size_t *offset,
114 u32 *crc)
115{
116 size_t remaining = len;
117 u32 copy_len = 0;
118
119 while (remaining > 0 && sg) {
120 size_t off, sg_bytes;
121 void *page_addr;
122
123 if (*offset >= sg->length) {
124
125
126
127
128 if (!(*nents))
129 break;
130 --(*nents);
131 *offset -= sg->length;
132 sg = sg_next(sg);
133 continue;
134 }
135 sg_bytes = min(remaining, sg->length - *offset);
136
137
138
139
140
141 off = *offset + sg->offset;
142 sg_bytes = min(sg_bytes,
143 (size_t)(PAGE_SIZE - (off & ~PAGE_MASK)));
144 page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT));
145 if (crc)
146 *crc = crc32(*crc, buf, sg_bytes);
147 memcpy((char *)page_addr + (off & ~PAGE_MASK), buf, sg_bytes);
148 kunmap_atomic(page_addr);
149 buf += sg_bytes;
150 *offset += sg_bytes;
151 remaining -= sg_bytes;
152 copy_len += sg_bytes;
153 }
154 return copy_len;
155}
156
157
158
159
160
161
162
163
164
165
166void fc_fill_hdr(struct fc_frame *fp, const struct fc_frame *in_fp,
167 enum fc_rctl r_ctl, u32 f_ctl, u16 seq_cnt, u32 parm_offset)
168{
169 struct fc_frame_header *fh;
170 struct fc_frame_header *in_fh;
171 struct fc_seq *sp;
172 u32 fill;
173
174 fh = __fc_frame_header_get(fp);
175 in_fh = __fc_frame_header_get(in_fp);
176
177 if (f_ctl & FC_FC_END_SEQ) {
178 fill = -fr_len(fp) & 3;
179 if (fill) {
180
181 skb_put_zero(fp_skb(fp), fill);
182 f_ctl |= fill;
183 }
184 fr_eof(fp) = FC_EOF_T;
185 } else {
186 WARN_ON(fr_len(fp) % 4 != 0);
187 fr_eof(fp) = FC_EOF_N;
188 }
189
190 fh->fh_r_ctl = r_ctl;
191 memcpy(fh->fh_d_id, in_fh->fh_s_id, sizeof(fh->fh_d_id));
192 memcpy(fh->fh_s_id, in_fh->fh_d_id, sizeof(fh->fh_s_id));
193 fh->fh_type = in_fh->fh_type;
194 hton24(fh->fh_f_ctl, f_ctl);
195 fh->fh_ox_id = in_fh->fh_ox_id;
196 fh->fh_rx_id = in_fh->fh_rx_id;
197 fh->fh_cs_ctl = 0;
198 fh->fh_df_ctl = 0;
199 fh->fh_parm_offset = htonl(parm_offset);
200
201 sp = fr_seq(in_fp);
202 if (sp) {
203 fr_seq(fp) = sp;
204 fh->fh_seq_id = sp->id;
205 seq_cnt = sp->cnt;
206 } else {
207 fh->fh_seq_id = 0;
208 }
209 fh->fh_seq_cnt = ntohs(seq_cnt);
210 fr_sof(fp) = seq_cnt ? FC_SOF_N3 : FC_SOF_I3;
211 fr_encaps(fp) = fr_encaps(in_fp);
212}
213EXPORT_SYMBOL(fc_fill_hdr);
214
215
216
217
218
219
220
221
222void fc_fill_reply_hdr(struct fc_frame *fp, const struct fc_frame *in_fp,
223 enum fc_rctl r_ctl, u32 parm_offset)
224{
225 struct fc_seq *sp;
226
227 sp = fr_seq(in_fp);
228 if (sp)
229 fr_seq(fp) = fc_seq_start_next(sp);
230 fc_fill_hdr(fp, in_fp, r_ctl, FC_FCTL_RESP, 0, parm_offset);
231}
232EXPORT_SYMBOL(fc_fill_reply_hdr);
233
234
235
236
237
238
239
240
241void fc_fc4_conf_lport_params(struct fc_lport *lport, enum fc_fh_type type)
242{
243 struct fc4_prov *prov_entry;
244 BUG_ON(type >= FC_FC4_PROV_SIZE);
245 BUG_ON(!lport);
246 prov_entry = fc_passive_prov[type];
247 if (type == FC_TYPE_FCP) {
248 if (prov_entry && prov_entry->recv)
249 lport->service_params |= FCP_SPPF_TARG_FCN;
250 }
251}
252
253void fc_lport_iterate(void (*notify)(struct fc_lport *, void *), void *arg)
254{
255 struct fc_lport *lport;
256
257 mutex_lock(&fc_prov_mutex);
258 list_for_each_entry(lport, &fc_local_ports, lport_list)
259 notify(lport, arg);
260 mutex_unlock(&fc_prov_mutex);
261}
262EXPORT_SYMBOL(fc_lport_iterate);
263
264
265
266
267
268
269
270
271int fc_fc4_register_provider(enum fc_fh_type type, struct fc4_prov *prov)
272{
273 struct fc4_prov **prov_entry;
274 int ret = 0;
275
276 if (type >= FC_FC4_PROV_SIZE)
277 return -EINVAL;
278 mutex_lock(&fc_prov_mutex);
279 prov_entry = (prov->recv ? fc_passive_prov : fc_active_prov) + type;
280 if (*prov_entry)
281 ret = -EBUSY;
282 else
283 *prov_entry = prov;
284 mutex_unlock(&fc_prov_mutex);
285 return ret;
286}
287EXPORT_SYMBOL(fc_fc4_register_provider);
288
289
290
291
292
293
294void fc_fc4_deregister_provider(enum fc_fh_type type, struct fc4_prov *prov)
295{
296 BUG_ON(type >= FC_FC4_PROV_SIZE);
297 mutex_lock(&fc_prov_mutex);
298 if (prov->recv)
299 RCU_INIT_POINTER(fc_passive_prov[type], NULL);
300 else
301 RCU_INIT_POINTER(fc_active_prov[type], NULL);
302 mutex_unlock(&fc_prov_mutex);
303 synchronize_rcu();
304}
305EXPORT_SYMBOL(fc_fc4_deregister_provider);
306
307
308
309
310
311void fc_fc4_add_lport(struct fc_lport *lport)
312{
313 mutex_lock(&fc_prov_mutex);
314 list_add_tail(&lport->lport_list, &fc_local_ports);
315 blocking_notifier_call_chain(&fc_lport_notifier_head,
316 FC_LPORT_EV_ADD, lport);
317 mutex_unlock(&fc_prov_mutex);
318}
319
320
321
322
323
324void fc_fc4_del_lport(struct fc_lport *lport)
325{
326 mutex_lock(&fc_prov_mutex);
327 list_del(&lport->lport_list);
328 blocking_notifier_call_chain(&fc_lport_notifier_head,
329 FC_LPORT_EV_DEL, lport);
330 mutex_unlock(&fc_prov_mutex);
331}
332