[SCSI] libfc: add local port hook for provider session lookup
[deliverable/linux.git] / drivers / scsi / libfc / fc_libfc.c
CommitLineData
8866a5d9
RL
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>
24f089e2 26#include <scsi/fc_encode.h>
8866a5d9
RL
27
28#include "fc_libfc.h"
29
30MODULE_AUTHOR("Open-FCoE.org");
31MODULE_DESCRIPTION("libfc");
32MODULE_LICENSE("GPL v2");
33
34unsigned int fc_debug_logging;
35module_param_named(debug_logging, fc_debug_logging, int, S_IRUGO|S_IWUSR);
36MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
93e6d5ab 37
96ad8464
JE
38DEFINE_MUTEX(fc_prov_mutex);
39
40/*
41 * Providers which primarily send requests and PRLIs.
42 */
43struct fc4_prov *fc_active_prov[FC_FC4_PROV_SIZE] = {
44 [0] = &fc_rport_t0_prov,
45 [FC_TYPE_FCP] = &fc_rport_fcp_init,
46};
47
48/*
49 * Providers which receive requests.
50 */
51struct fc4_prov *fc_passive_prov[FC_FC4_PROV_SIZE] = {
52 [FC_TYPE_ELS] = &fc_lport_els_prov,
53};
54
93e6d5ab
RL
55/**
56 * libfc_init() - Initialize libfc.ko
57 */
58static int __init libfc_init(void)
59{
60 int rc = 0;
61
62 rc = fc_setup_fcp();
63 if (rc)
64 return rc;
65
66 rc = fc_setup_exch_mgr();
67 if (rc)
68 goto destroy_pkt_cache;
69
70 rc = fc_setup_rport();
71 if (rc)
72 goto destroy_em;
73
74 return rc;
75destroy_em:
76 fc_destroy_exch_mgr();
77destroy_pkt_cache:
78 fc_destroy_fcp();
79 return rc;
80}
81module_init(libfc_init);
82
83/**
84 * libfc_exit() - Tear down libfc.ko
85 */
86static void __exit libfc_exit(void)
87{
88 fc_destroy_fcp();
89 fc_destroy_exch_mgr();
90 fc_destroy_rport();
91}
92module_exit(libfc_exit);
58682874
RL
93
94/**
95 * fc_copy_buffer_to_sglist() - This routine copies the data of a buffer
3a3b42bf 96 * into a scatter-gather list (SG list).
58682874
RL
97 *
98 * @buf: pointer to the data buffer.
99 * @len: the byte-length of the data buffer.
100 * @sg: pointer to the pointer of the SG list.
101 * @nents: pointer to the remaining number of entries in the SG list.
102 * @offset: pointer to the current offset in the SG list.
103 * @km_type: dedicated page table slot type for kmap_atomic.
104 * @crc: pointer to the 32-bit crc value.
3a3b42bf 105 * If crc is NULL, CRC is not calculated.
58682874
RL
106 */
107u32 fc_copy_buffer_to_sglist(void *buf, size_t len,
108 struct scatterlist *sg,
109 u32 *nents, size_t *offset,
110 enum km_type km_type, u32 *crc)
111{
112 size_t remaining = len;
113 u32 copy_len = 0;
114
115 while (remaining > 0 && sg) {
116 size_t off, sg_bytes;
117 void *page_addr;
118
119 if (*offset >= sg->length) {
120 /*
121 * Check for end and drop resources
122 * from the last iteration.
123 */
124 if (!(*nents))
125 break;
126 --(*nents);
127 *offset -= sg->length;
128 sg = sg_next(sg);
129 continue;
130 }
131 sg_bytes = min(remaining, sg->length - *offset);
132
133 /*
134 * The scatterlist item may be bigger than PAGE_SIZE,
135 * but we are limited to mapping PAGE_SIZE at a time.
136 */
137 off = *offset + sg->offset;
138 sg_bytes = min(sg_bytes,
139 (size_t)(PAGE_SIZE - (off & ~PAGE_MASK)));
140 page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT),
141 km_type);
142 if (crc)
143 *crc = crc32(*crc, buf, sg_bytes);
144 memcpy((char *)page_addr + (off & ~PAGE_MASK), buf, sg_bytes);
145 kunmap_atomic(page_addr, km_type);
146 buf += sg_bytes;
147 *offset += sg_bytes;
148 remaining -= sg_bytes;
149 copy_len += sg_bytes;
150 }
151 return copy_len;
152}
24f089e2
JE
153
154/**
155 * fc_fill_hdr() - fill FC header fields based on request
156 * @fp: reply frame containing header to be filled in
157 * @in_fp: request frame containing header to use in filling in reply
158 * @r_ctl: R_CTL value for header
159 * @f_ctl: F_CTL value for header, with 0 pad
160 * @seq_cnt: sequence count for the header, ignored if frame has a sequence
161 * @parm_offset: parameter / offset value
162 */
163void fc_fill_hdr(struct fc_frame *fp, const struct fc_frame *in_fp,
164 enum fc_rctl r_ctl, u32 f_ctl, u16 seq_cnt, u32 parm_offset)
165{
166 struct fc_frame_header *fh;
167 struct fc_frame_header *in_fh;
168 struct fc_seq *sp;
169 u32 fill;
170
171 fh = __fc_frame_header_get(fp);
172 in_fh = __fc_frame_header_get(in_fp);
173
174 if (f_ctl & FC_FC_END_SEQ) {
175 fill = -fr_len(fp) & 3;
176 if (fill) {
177 /* TODO, this may be a problem with fragmented skb */
178 memset(skb_put(fp_skb(fp), fill), 0, fill);
179 f_ctl |= fill;
180 }
181 fr_eof(fp) = FC_EOF_T;
182 } else {
183 WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
184 fr_eof(fp) = FC_EOF_N;
185 }
186
187 fh->fh_r_ctl = r_ctl;
188 memcpy(fh->fh_d_id, in_fh->fh_s_id, sizeof(fh->fh_d_id));
189 memcpy(fh->fh_s_id, in_fh->fh_d_id, sizeof(fh->fh_s_id));
190 fh->fh_type = in_fh->fh_type;
191 hton24(fh->fh_f_ctl, f_ctl);
192 fh->fh_ox_id = in_fh->fh_ox_id;
193 fh->fh_rx_id = in_fh->fh_rx_id;
194 fh->fh_cs_ctl = 0;
195 fh->fh_df_ctl = 0;
196 fh->fh_parm_offset = htonl(parm_offset);
197
198 sp = fr_seq(in_fp);
199 if (sp) {
200 fr_seq(fp) = sp;
201 fh->fh_seq_id = sp->id;
202 seq_cnt = sp->cnt;
203 } else {
204 fh->fh_seq_id = 0;
205 }
206 fh->fh_seq_cnt = ntohs(seq_cnt);
207 fr_sof(fp) = seq_cnt ? FC_SOF_N3 : FC_SOF_I3;
208 fr_encaps(fp) = fr_encaps(in_fp);
209}
210EXPORT_SYMBOL(fc_fill_hdr);
211
212/**
213 * fc_fill_reply_hdr() - fill FC reply header fields based on request
214 * @fp: reply frame containing header to be filled in
215 * @in_fp: request frame containing header to use in filling in reply
216 * @r_ctl: R_CTL value for reply
217 * @parm_offset: parameter / offset value
218 */
219void fc_fill_reply_hdr(struct fc_frame *fp, const struct fc_frame *in_fp,
220 enum fc_rctl r_ctl, u32 parm_offset)
221{
222 struct fc_seq *sp;
223
224 sp = fr_seq(in_fp);
225 if (sp)
226 fr_seq(fp) = fr_dev(in_fp)->tt.seq_start_next(sp);
227 fc_fill_hdr(fp, in_fp, r_ctl, FC_FCTL_RESP, 0, parm_offset);
228}
229EXPORT_SYMBOL(fc_fill_reply_hdr);
96ad8464
JE
230
231/**
232 * fc_fc4_register_provider() - register FC-4 upper-level provider.
233 * @type: FC-4 type, such as FC_TYPE_FCP
234 * @prov: structure describing provider including ops vector.
235 *
236 * Returns 0 on success, negative error otherwise.
237 */
238int fc_fc4_register_provider(enum fc_fh_type type, struct fc4_prov *prov)
239{
240 struct fc4_prov **prov_entry;
241 int ret = 0;
242
243 if (type >= FC_FC4_PROV_SIZE)
244 return -EINVAL;
245 mutex_lock(&fc_prov_mutex);
246 prov_entry = (prov->recv ? fc_passive_prov : fc_active_prov) + type;
247 if (*prov_entry)
248 ret = -EBUSY;
249 else
250 *prov_entry = prov;
251 mutex_unlock(&fc_prov_mutex);
252 return ret;
253}
254EXPORT_SYMBOL(fc_fc4_register_provider);
255
256/**
257 * fc_fc4_deregister_provider() - deregister FC-4 upper-level provider.
258 * @type: FC-4 type, such as FC_TYPE_FCP
259 * @prov: structure describing provider including ops vector.
260 */
261void fc_fc4_deregister_provider(enum fc_fh_type type, struct fc4_prov *prov)
262{
263 BUG_ON(type >= FC_FC4_PROV_SIZE);
264 mutex_lock(&fc_prov_mutex);
265 if (prov->recv)
266 rcu_assign_pointer(fc_passive_prov[type], NULL);
267 else
268 rcu_assign_pointer(fc_active_prov[type], NULL);
269 mutex_unlock(&fc_prov_mutex);
270 synchronize_rcu();
271}
272EXPORT_SYMBOL(fc_fc4_deregister_provider);
This page took 0.18597 seconds and 5 git commands to generate.