1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright(c) 2009 Intel Corporation. All rights reserved.
5 * Maintained at www.Open-FCoE.org
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/scatterlist.h>
11 #include <linux/crc32.h>
12 #include <linux/module.h>
14 #include <scsi/libfc.h>
16 #include "fc_encode.h"
19 MODULE_AUTHOR("Open-FCoE.org");
20 MODULE_DESCRIPTION("libfc");
21 MODULE_LICENSE("GPL v2");
23 unsigned int fc_debug_logging
;
24 module_param_named(debug_logging
, fc_debug_logging
, int, S_IRUGO
|S_IWUSR
);
25 MODULE_PARM_DESC(debug_logging
, "a bit mask of logging levels");
27 DEFINE_MUTEX(fc_prov_mutex
);
28 static LIST_HEAD(fc_local_ports
);
29 struct blocking_notifier_head fc_lport_notifier_head
=
30 BLOCKING_NOTIFIER_INIT(fc_lport_notifier_head
);
31 EXPORT_SYMBOL(fc_lport_notifier_head
);
34 * Providers which primarily send requests and PRLIs.
36 struct fc4_prov
*fc_active_prov
[FC_FC4_PROV_SIZE
] = {
37 [0] = &fc_rport_t0_prov
,
38 [FC_TYPE_FCP
] = &fc_rport_fcp_init
,
42 * Providers which receive requests.
44 struct fc4_prov
*fc_passive_prov
[FC_FC4_PROV_SIZE
] = {
45 [FC_TYPE_ELS
] = &fc_lport_els_prov
,
49 * libfc_init() - Initialize libfc.ko
51 static int __init
libfc_init(void)
59 rc
= fc_setup_exch_mgr();
61 goto destroy_pkt_cache
;
63 rc
= fc_setup_rport();
69 fc_destroy_exch_mgr();
74 module_init(libfc_init
);
77 * libfc_exit() - Tear down libfc.ko
79 static void __exit
libfc_exit(void)
82 fc_destroy_exch_mgr();
85 module_exit(libfc_exit
);
88 * fc_copy_buffer_to_sglist() - This routine copies the data of a buffer
89 * into a scatter-gather list (SG list).
91 * @buf: pointer to the data buffer.
92 * @len: the byte-length of the data buffer.
93 * @sg: pointer to the pointer of the SG list.
94 * @nents: pointer to the remaining number of entries in the SG list.
95 * @offset: pointer to the current offset in the SG list.
96 * @crc: pointer to the 32-bit crc value.
97 * If crc is NULL, CRC is not calculated.
99 u32
fc_copy_buffer_to_sglist(void *buf
, size_t len
,
100 struct scatterlist
*sg
,
101 u32
*nents
, size_t *offset
,
104 size_t remaining
= len
;
107 while (remaining
> 0 && sg
) {
108 size_t off
, sg_bytes
;
111 if (*offset
>= sg
->length
) {
113 * Check for end and drop resources
114 * from the last iteration.
119 *offset
-= sg
->length
;
123 sg_bytes
= min(remaining
, sg
->length
- *offset
);
126 * The scatterlist item may be bigger than PAGE_SIZE,
127 * but we are limited to mapping PAGE_SIZE at a time.
129 off
= *offset
+ sg
->offset
;
130 sg_bytes
= min(sg_bytes
,
131 (size_t)(PAGE_SIZE
- (off
& ~PAGE_MASK
)));
132 page_addr
= kmap_atomic(sg_page(sg
) + (off
>> PAGE_SHIFT
));
134 *crc
= crc32(*crc
, buf
, sg_bytes
);
135 memcpy((char *)page_addr
+ (off
& ~PAGE_MASK
), buf
, sg_bytes
);
136 kunmap_atomic(page_addr
);
139 remaining
-= sg_bytes
;
140 copy_len
+= sg_bytes
;
146 * fc_fill_hdr() - fill FC header fields based on request
147 * @fp: reply frame containing header to be filled in
148 * @in_fp: request frame containing header to use in filling in reply
149 * @r_ctl: R_CTL value for header
150 * @f_ctl: F_CTL value for header, with 0 pad
151 * @seq_cnt: sequence count for the header, ignored if frame has a sequence
152 * @parm_offset: parameter / offset value
154 void fc_fill_hdr(struct fc_frame
*fp
, const struct fc_frame
*in_fp
,
155 enum fc_rctl r_ctl
, u32 f_ctl
, u16 seq_cnt
, u32 parm_offset
)
157 struct fc_frame_header
*fh
;
158 struct fc_frame_header
*in_fh
;
162 fh
= __fc_frame_header_get(fp
);
163 in_fh
= __fc_frame_header_get(in_fp
);
165 if (f_ctl
& FC_FC_END_SEQ
) {
166 fill
= -fr_len(fp
) & 3;
168 /* TODO, this may be a problem with fragmented skb */
169 skb_put_zero(fp_skb(fp
), fill
);
172 fr_eof(fp
) = FC_EOF_T
;
174 WARN_ON(fr_len(fp
) % 4 != 0); /* no pad to non last frame */
175 fr_eof(fp
) = FC_EOF_N
;
178 fh
->fh_r_ctl
= r_ctl
;
179 memcpy(fh
->fh_d_id
, in_fh
->fh_s_id
, sizeof(fh
->fh_d_id
));
180 memcpy(fh
->fh_s_id
, in_fh
->fh_d_id
, sizeof(fh
->fh_s_id
));
181 fh
->fh_type
= in_fh
->fh_type
;
182 hton24(fh
->fh_f_ctl
, f_ctl
);
183 fh
->fh_ox_id
= in_fh
->fh_ox_id
;
184 fh
->fh_rx_id
= in_fh
->fh_rx_id
;
187 fh
->fh_parm_offset
= htonl(parm_offset
);
192 fh
->fh_seq_id
= sp
->id
;
197 fh
->fh_seq_cnt
= ntohs(seq_cnt
);
198 fr_sof(fp
) = seq_cnt
? FC_SOF_N3
: FC_SOF_I3
;
199 fr_encaps(fp
) = fr_encaps(in_fp
);
201 EXPORT_SYMBOL(fc_fill_hdr
);
204 * fc_fill_reply_hdr() - fill FC reply header fields based on request
205 * @fp: reply frame containing header to be filled in
206 * @in_fp: request frame containing header to use in filling in reply
207 * @r_ctl: R_CTL value for reply
208 * @parm_offset: parameter / offset value
210 void fc_fill_reply_hdr(struct fc_frame
*fp
, const struct fc_frame
*in_fp
,
211 enum fc_rctl r_ctl
, u32 parm_offset
)
217 fr_seq(fp
) = fc_seq_start_next(sp
);
218 fc_fill_hdr(fp
, in_fp
, r_ctl
, FC_FCTL_RESP
, 0, parm_offset
);
220 EXPORT_SYMBOL(fc_fill_reply_hdr
);
223 * fc_fc4_conf_lport_params() - Modify "service_params" of specified lport
224 * if there is service provider (target provider) registered with libfc
225 * for specified "fc_ft_type"
226 * @lport: Local port which service_params needs to be modified
227 * @type: FC-4 type, such as FC_TYPE_FCP
229 void fc_fc4_conf_lport_params(struct fc_lport
*lport
, enum fc_fh_type type
)
231 struct fc4_prov
*prov_entry
;
232 BUG_ON(type
>= FC_FC4_PROV_SIZE
);
234 prov_entry
= fc_passive_prov
[type
];
235 if (type
== FC_TYPE_FCP
) {
236 if (prov_entry
&& prov_entry
->recv
)
237 lport
->service_params
|= FCP_SPPF_TARG_FCN
;
241 void fc_lport_iterate(void (*notify
)(struct fc_lport
*, void *), void *arg
)
243 struct fc_lport
*lport
;
245 mutex_lock(&fc_prov_mutex
);
246 list_for_each_entry(lport
, &fc_local_ports
, lport_list
)
248 mutex_unlock(&fc_prov_mutex
);
250 EXPORT_SYMBOL(fc_lport_iterate
);
253 * fc_fc4_register_provider() - register FC-4 upper-level provider.
254 * @type: FC-4 type, such as FC_TYPE_FCP
255 * @prov: structure describing provider including ops vector.
257 * Returns 0 on success, negative error otherwise.
259 int fc_fc4_register_provider(enum fc_fh_type type
, struct fc4_prov
*prov
)
261 struct fc4_prov
**prov_entry
;
264 if (type
>= FC_FC4_PROV_SIZE
)
266 mutex_lock(&fc_prov_mutex
);
267 prov_entry
= (prov
->recv
? fc_passive_prov
: fc_active_prov
) + type
;
272 mutex_unlock(&fc_prov_mutex
);
275 EXPORT_SYMBOL(fc_fc4_register_provider
);
278 * fc_fc4_deregister_provider() - deregister FC-4 upper-level provider.
279 * @type: FC-4 type, such as FC_TYPE_FCP
280 * @prov: structure describing provider including ops vector.
282 void fc_fc4_deregister_provider(enum fc_fh_type type
, struct fc4_prov
*prov
)
284 BUG_ON(type
>= FC_FC4_PROV_SIZE
);
285 mutex_lock(&fc_prov_mutex
);
287 RCU_INIT_POINTER(fc_passive_prov
[type
], NULL
);
289 RCU_INIT_POINTER(fc_active_prov
[type
], NULL
);
290 mutex_unlock(&fc_prov_mutex
);
293 EXPORT_SYMBOL(fc_fc4_deregister_provider
);
296 * fc_fc4_add_lport() - add new local port to list and run notifiers.
297 * @lport: The new local port.
299 void fc_fc4_add_lport(struct fc_lport
*lport
)
301 mutex_lock(&fc_prov_mutex
);
302 list_add_tail(&lport
->lport_list
, &fc_local_ports
);
303 blocking_notifier_call_chain(&fc_lport_notifier_head
,
304 FC_LPORT_EV_ADD
, lport
);
305 mutex_unlock(&fc_prov_mutex
);
309 * fc_fc4_del_lport() - remove local port from list and run notifiers.
310 * @lport: The new local port.
312 void fc_fc4_del_lport(struct fc_lport
*lport
)
314 mutex_lock(&fc_prov_mutex
);
315 list_del(&lport
->lport_list
);
316 blocking_notifier_call_chain(&fc_lport_notifier_head
,
317 FC_LPORT_EV_DEL
, lport
);
318 mutex_unlock(&fc_prov_mutex
);