Commit | Line | Data |
---|---|---|
3699d92a KP |
1 | /* |
2 | * Copyright (c) 2010 Cisco Systems, Inc. | |
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 | ||
18 | /* XXX TBD some includes may be extraneous */ | |
19 | ||
20 | #include <linux/module.h> | |
21 | #include <linux/moduleparam.h> | |
3699d92a KP |
22 | #include <linux/utsname.h> |
23 | #include <linux/init.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/kthread.h> | |
26 | #include <linux/types.h> | |
27 | #include <linux/string.h> | |
28 | #include <linux/configfs.h> | |
29 | #include <linux/ctype.h> | |
30 | #include <linux/hash.h> | |
31 | #include <linux/rcupdate.h> | |
32 | #include <linux/rculist.h> | |
33 | #include <linux/kref.h> | |
34 | #include <asm/unaligned.h> | |
35 | #include <scsi/scsi.h> | |
36 | #include <scsi/scsi_host.h> | |
37 | #include <scsi/scsi_device.h> | |
38 | #include <scsi/scsi_cmnd.h> | |
39 | #include <scsi/libfc.h> | |
40 | ||
41 | #include <target/target_core_base.h> | |
c4795fb2 | 42 | #include <target/target_core_fabric.h> |
3699d92a KP |
43 | #include <target/configfs_macros.h> |
44 | ||
3699d92a KP |
45 | #include "tcm_fc.h" |
46 | ||
47 | static void ft_sess_delete_all(struct ft_tport *); | |
48 | ||
49 | /* | |
50 | * Lookup or allocate target local port. | |
51 | * Caller holds ft_lport_lock. | |
52 | */ | |
e3d4440c | 53 | static struct ft_tport *ft_tport_get(struct fc_lport *lport) |
3699d92a KP |
54 | { |
55 | struct ft_tpg *tpg; | |
56 | struct ft_tport *tport; | |
57 | int i; | |
58 | ||
863555be MR |
59 | tport = rcu_dereference_protected(lport->prov[FC_TYPE_FCP], |
60 | lockdep_is_held(&ft_lport_lock)); | |
3699d92a KP |
61 | if (tport && tport->tpg) |
62 | return tport; | |
63 | ||
64 | tpg = ft_lport_find_tpg(lport); | |
65 | if (!tpg) | |
66 | return NULL; | |
67 | ||
68 | if (tport) { | |
69 | tport->tpg = tpg; | |
2c42be2d | 70 | tpg->tport = tport; |
3699d92a KP |
71 | return tport; |
72 | } | |
73 | ||
74 | tport = kzalloc(sizeof(*tport), GFP_KERNEL); | |
75 | if (!tport) | |
76 | return NULL; | |
77 | ||
78 | tport->lport = lport; | |
79 | tport->tpg = tpg; | |
80 | tpg->tport = tport; | |
81 | for (i = 0; i < FT_SESS_HASH_SIZE; i++) | |
82 | INIT_HLIST_HEAD(&tport->hash[i]); | |
83 | ||
84 | rcu_assign_pointer(lport->prov[FC_TYPE_FCP], tport); | |
85 | return tport; | |
86 | } | |
87 | ||
3699d92a KP |
88 | /* |
89 | * Delete a target local port. | |
90 | * Caller holds ft_lport_lock. | |
91 | */ | |
92 | static void ft_tport_delete(struct ft_tport *tport) | |
93 | { | |
94 | struct fc_lport *lport; | |
95 | struct ft_tpg *tpg; | |
96 | ||
97 | ft_sess_delete_all(tport); | |
98 | lport = tport->lport; | |
99 | BUG_ON(tport != lport->prov[FC_TYPE_FCP]); | |
c04047ec | 100 | RCU_INIT_POINTER(lport->prov[FC_TYPE_FCP], NULL); |
3699d92a KP |
101 | |
102 | tpg = tport->tpg; | |
103 | if (tpg) { | |
104 | tpg->tport = NULL; | |
105 | tport->tpg = NULL; | |
106 | } | |
a6c76da8 | 107 | kfree_rcu(tport, rcu); |
3699d92a KP |
108 | } |
109 | ||
110 | /* | |
111 | * Add local port. | |
112 | * Called thru fc_lport_iterate(). | |
113 | */ | |
114 | void ft_lport_add(struct fc_lport *lport, void *arg) | |
115 | { | |
116 | mutex_lock(&ft_lport_lock); | |
e3d4440c | 117 | ft_tport_get(lport); |
3699d92a KP |
118 | mutex_unlock(&ft_lport_lock); |
119 | } | |
120 | ||
121 | /* | |
122 | * Delete local port. | |
123 | * Called thru fc_lport_iterate(). | |
124 | */ | |
125 | void ft_lport_del(struct fc_lport *lport, void *arg) | |
126 | { | |
127 | struct ft_tport *tport; | |
128 | ||
129 | mutex_lock(&ft_lport_lock); | |
130 | tport = lport->prov[FC_TYPE_FCP]; | |
131 | if (tport) | |
132 | ft_tport_delete(tport); | |
133 | mutex_unlock(&ft_lport_lock); | |
134 | } | |
135 | ||
136 | /* | |
137 | * Notification of local port change from libfc. | |
138 | * Create or delete local port and associated tport. | |
139 | */ | |
140 | int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg) | |
141 | { | |
142 | struct fc_lport *lport = arg; | |
143 | ||
144 | switch (event) { | |
145 | case FC_LPORT_EV_ADD: | |
146 | ft_lport_add(lport, NULL); | |
147 | break; | |
148 | case FC_LPORT_EV_DEL: | |
149 | ft_lport_del(lport, NULL); | |
150 | break; | |
151 | } | |
152 | return NOTIFY_DONE; | |
153 | } | |
154 | ||
155 | /* | |
156 | * Hash function for FC_IDs. | |
157 | */ | |
158 | static u32 ft_sess_hash(u32 port_id) | |
159 | { | |
160 | return hash_32(port_id, FT_SESS_HASH_BITS); | |
161 | } | |
162 | ||
163 | /* | |
164 | * Find session in local port. | |
165 | * Sessions and hash lists are RCU-protected. | |
166 | * A reference is taken which must be eventually freed. | |
167 | */ | |
168 | static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id) | |
169 | { | |
170 | struct ft_tport *tport; | |
171 | struct hlist_head *head; | |
3699d92a KP |
172 | struct ft_sess *sess; |
173 | ||
174 | rcu_read_lock(); | |
175 | tport = rcu_dereference(lport->prov[FC_TYPE_FCP]); | |
176 | if (!tport) | |
177 | goto out; | |
178 | ||
179 | head = &tport->hash[ft_sess_hash(port_id)]; | |
b67bfe0d | 180 | hlist_for_each_entry_rcu(sess, head, hash) { |
3699d92a KP |
181 | if (sess->port_id == port_id) { |
182 | kref_get(&sess->kref); | |
183 | rcu_read_unlock(); | |
6708bb27 | 184 | pr_debug("port_id %x found %p\n", port_id, sess); |
3699d92a KP |
185 | return sess; |
186 | } | |
187 | } | |
188 | out: | |
189 | rcu_read_unlock(); | |
6708bb27 | 190 | pr_debug("port_id %x not found\n", port_id); |
3699d92a KP |
191 | return NULL; |
192 | } | |
193 | ||
194 | /* | |
195 | * Allocate session and enter it in the hash for the local port. | |
196 | * Caller holds ft_lport_lock. | |
197 | */ | |
198 | static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id, | |
199 | struct ft_node_acl *acl) | |
200 | { | |
201 | struct ft_sess *sess; | |
202 | struct hlist_head *head; | |
3699d92a KP |
203 | |
204 | head = &tport->hash[ft_sess_hash(port_id)]; | |
b67bfe0d | 205 | hlist_for_each_entry_rcu(sess, head, hash) |
3699d92a KP |
206 | if (sess->port_id == port_id) |
207 | return sess; | |
208 | ||
209 | sess = kzalloc(sizeof(*sess), GFP_KERNEL); | |
210 | if (!sess) | |
211 | return NULL; | |
212 | ||
5f544cfa | 213 | sess->se_sess = transport_init_session_tags(TCM_FC_DEFAULT_TAGS, |
e70beee7 NB |
214 | sizeof(struct ft_cmd), |
215 | TARGET_PROT_NORMAL); | |
552523dc | 216 | if (IS_ERR(sess->se_sess)) { |
3699d92a KP |
217 | kfree(sess); |
218 | return NULL; | |
219 | } | |
220 | sess->se_sess->se_node_acl = &acl->se_node_acl; | |
221 | sess->tport = tport; | |
222 | sess->port_id = port_id; | |
223 | kref_init(&sess->kref); /* ref for table entry */ | |
224 | hlist_add_head_rcu(&sess->hash, head); | |
225 | tport->sess_count++; | |
226 | ||
6708bb27 | 227 | pr_debug("port_id %x sess %p\n", port_id, sess); |
3699d92a KP |
228 | |
229 | transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl, | |
230 | sess->se_sess, sess); | |
231 | return sess; | |
232 | } | |
233 | ||
234 | /* | |
235 | * Unhash the session. | |
236 | * Caller holds ft_lport_lock. | |
237 | */ | |
238 | static void ft_sess_unhash(struct ft_sess *sess) | |
239 | { | |
240 | struct ft_tport *tport = sess->tport; | |
241 | ||
242 | hlist_del_rcu(&sess->hash); | |
243 | BUG_ON(!tport->sess_count); | |
244 | tport->sess_count--; | |
245 | sess->port_id = -1; | |
246 | sess->params = 0; | |
247 | } | |
248 | ||
249 | /* | |
250 | * Delete session from hash. | |
251 | * Caller holds ft_lport_lock. | |
252 | */ | |
253 | static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id) | |
254 | { | |
255 | struct hlist_head *head; | |
3699d92a KP |
256 | struct ft_sess *sess; |
257 | ||
258 | head = &tport->hash[ft_sess_hash(port_id)]; | |
b67bfe0d | 259 | hlist_for_each_entry_rcu(sess, head, hash) { |
3699d92a KP |
260 | if (sess->port_id == port_id) { |
261 | ft_sess_unhash(sess); | |
262 | return sess; | |
263 | } | |
264 | } | |
265 | return NULL; | |
266 | } | |
267 | ||
268 | /* | |
269 | * Delete all sessions from tport. | |
270 | * Caller holds ft_lport_lock. | |
271 | */ | |
272 | static void ft_sess_delete_all(struct ft_tport *tport) | |
273 | { | |
274 | struct hlist_head *head; | |
3699d92a KP |
275 | struct ft_sess *sess; |
276 | ||
277 | for (head = tport->hash; | |
278 | head < &tport->hash[FT_SESS_HASH_SIZE]; head++) { | |
b67bfe0d | 279 | hlist_for_each_entry_rcu(sess, head, hash) { |
3699d92a KP |
280 | ft_sess_unhash(sess); |
281 | transport_deregister_session_configfs(sess->se_sess); | |
282 | ft_sess_put(sess); /* release from table */ | |
283 | } | |
284 | } | |
285 | } | |
286 | ||
287 | /* | |
288 | * TCM ops for sessions. | |
289 | */ | |
290 | ||
291 | /* | |
292 | * Determine whether session is allowed to be shutdown in the current context. | |
293 | * Returns non-zero if the session should be shutdown. | |
294 | */ | |
295 | int ft_sess_shutdown(struct se_session *se_sess) | |
296 | { | |
297 | struct ft_sess *sess = se_sess->fabric_sess_ptr; | |
298 | ||
6708bb27 | 299 | pr_debug("port_id %x\n", sess->port_id); |
3699d92a KP |
300 | return 1; |
301 | } | |
302 | ||
303 | /* | |
304 | * Remove session and send PRLO. | |
305 | * This is called when the ACL is being deleted or queue depth is changing. | |
306 | */ | |
307 | void ft_sess_close(struct se_session *se_sess) | |
308 | { | |
309 | struct ft_sess *sess = se_sess->fabric_sess_ptr; | |
3699d92a KP |
310 | u32 port_id; |
311 | ||
312 | mutex_lock(&ft_lport_lock); | |
3699d92a KP |
313 | port_id = sess->port_id; |
314 | if (port_id == -1) { | |
7c7cf3b9 | 315 | mutex_unlock(&ft_lport_lock); |
3699d92a KP |
316 | return; |
317 | } | |
6708bb27 | 318 | pr_debug("port_id %x\n", port_id); |
3699d92a KP |
319 | ft_sess_unhash(sess); |
320 | mutex_unlock(&ft_lport_lock); | |
321 | transport_deregister_session_configfs(se_sess); | |
322 | ft_sess_put(sess); | |
323 | /* XXX Send LOGO or PRLO */ | |
324 | synchronize_rcu(); /* let transport deregister happen */ | |
325 | } | |
326 | ||
3699d92a KP |
327 | u32 ft_sess_get_index(struct se_session *se_sess) |
328 | { | |
329 | struct ft_sess *sess = se_sess->fabric_sess_ptr; | |
330 | ||
331 | return sess->port_id; /* XXX TBD probably not what is needed */ | |
332 | } | |
333 | ||
334 | u32 ft_sess_get_port_name(struct se_session *se_sess, | |
335 | unsigned char *buf, u32 len) | |
336 | { | |
337 | struct ft_sess *sess = se_sess->fabric_sess_ptr; | |
338 | ||
339 | return ft_format_wwn(buf, len, sess->port_name); | |
340 | } | |
341 | ||
3699d92a KP |
342 | /* |
343 | * libfc ops involving sessions. | |
344 | */ | |
345 | ||
346 | static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len, | |
347 | const struct fc_els_spp *rspp, struct fc_els_spp *spp) | |
348 | { | |
349 | struct ft_tport *tport; | |
350 | struct ft_sess *sess; | |
351 | struct ft_node_acl *acl; | |
352 | u32 fcp_parm; | |
353 | ||
e3d4440c | 354 | tport = ft_tport_get(rdata->local_port); |
3699d92a | 355 | if (!tport) |
edec8dfe | 356 | goto not_target; /* not a target for this local port */ |
3699d92a KP |
357 | |
358 | acl = ft_acl_get(tport->tpg, rdata); | |
359 | if (!acl) | |
edec8dfe | 360 | goto not_target; /* no target for this remote */ |
3699d92a KP |
361 | |
362 | if (!rspp) | |
363 | goto fill; | |
364 | ||
365 | if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL)) | |
366 | return FC_SPP_RESP_NO_PA; | |
367 | ||
368 | /* | |
369 | * If both target and initiator bits are off, the SPP is invalid. | |
370 | */ | |
371 | fcp_parm = ntohl(rspp->spp_params); | |
372 | if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN))) | |
373 | return FC_SPP_RESP_INVL; | |
374 | ||
375 | /* | |
376 | * Create session (image pair) only if requested by | |
377 | * EST_IMG_PAIR flag and if the requestor is an initiator. | |
378 | */ | |
379 | if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) { | |
380 | spp->spp_flags |= FC_SPP_EST_IMG_PAIR; | |
381 | if (!(fcp_parm & FCP_SPPF_INIT_FCN)) | |
382 | return FC_SPP_RESP_CONF; | |
383 | sess = ft_sess_create(tport, rdata->ids.port_id, acl); | |
384 | if (!sess) | |
385 | return FC_SPP_RESP_RES; | |
386 | if (!sess->params) | |
387 | rdata->prli_count++; | |
388 | sess->params = fcp_parm; | |
389 | sess->port_name = rdata->ids.port_name; | |
390 | sess->max_frame = rdata->maxframe_size; | |
391 | ||
392 | /* XXX TBD - clearing actions. unit attn, see 4.10 */ | |
393 | } | |
394 | ||
395 | /* | |
396 | * OR in our service parameters with other provider (initiator), if any. | |
3699d92a KP |
397 | */ |
398 | fill: | |
399 | fcp_parm = ntohl(spp->spp_params); | |
f2eeba21 | 400 | fcp_parm &= ~FCP_SPPF_RETRY; |
3699d92a KP |
401 | spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN); |
402 | return FC_SPP_RESP_ACK; | |
edec8dfe MR |
403 | |
404 | not_target: | |
405 | fcp_parm = ntohl(spp->spp_params); | |
406 | fcp_parm &= ~FCP_SPPF_TARG_FCN; | |
407 | spp->spp_params = htonl(fcp_parm); | |
408 | return 0; | |
3699d92a KP |
409 | } |
410 | ||
411 | /** | |
412 | * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target | |
413 | * @rdata: remote port private | |
414 | * @spp_len: service parameter page length | |
415 | * @rspp: received service parameter page (NULL for outgoing PRLI) | |
416 | * @spp: response service parameter page | |
417 | * | |
418 | * Returns spp response code. | |
419 | */ | |
420 | static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len, | |
421 | const struct fc_els_spp *rspp, struct fc_els_spp *spp) | |
422 | { | |
423 | int ret; | |
424 | ||
425 | mutex_lock(&ft_lport_lock); | |
426 | ret = ft_prli_locked(rdata, spp_len, rspp, spp); | |
427 | mutex_unlock(&ft_lport_lock); | |
6708bb27 | 428 | pr_debug("port_id %x flags %x ret %x\n", |
3699d92a KP |
429 | rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret); |
430 | return ret; | |
431 | } | |
432 | ||
3699d92a KP |
433 | static void ft_sess_free(struct kref *kref) |
434 | { | |
435 | struct ft_sess *sess = container_of(kref, struct ft_sess, kref); | |
436 | ||
9f4ad44b | 437 | transport_deregister_session(sess->se_sess); |
a6ad57ef | 438 | kfree_rcu(sess, rcu); |
3699d92a KP |
439 | } |
440 | ||
441 | void ft_sess_put(struct ft_sess *sess) | |
442 | { | |
443 | int sess_held = atomic_read(&sess->kref.refcount); | |
444 | ||
445 | BUG_ON(!sess_held); | |
446 | kref_put(&sess->kref, ft_sess_free); | |
447 | } | |
448 | ||
449 | static void ft_prlo(struct fc_rport_priv *rdata) | |
450 | { | |
451 | struct ft_sess *sess; | |
452 | struct ft_tport *tport; | |
453 | ||
454 | mutex_lock(&ft_lport_lock); | |
08a16208 DE |
455 | tport = rcu_dereference_protected(rdata->local_port->prov[FC_TYPE_FCP], |
456 | lockdep_is_held(&ft_lport_lock)); | |
457 | ||
3699d92a KP |
458 | if (!tport) { |
459 | mutex_unlock(&ft_lport_lock); | |
460 | return; | |
461 | } | |
462 | sess = ft_sess_delete(tport, rdata->ids.port_id); | |
463 | if (!sess) { | |
464 | mutex_unlock(&ft_lport_lock); | |
465 | return; | |
466 | } | |
467 | mutex_unlock(&ft_lport_lock); | |
468 | transport_deregister_session_configfs(sess->se_sess); | |
469 | ft_sess_put(sess); /* release from table */ | |
470 | rdata->prli_count--; | |
471 | /* XXX TBD - clearing actions. unit attn, see 4.10 */ | |
472 | } | |
473 | ||
474 | /* | |
475 | * Handle incoming FCP request. | |
476 | * Caller has verified that the frame is type FCP. | |
477 | */ | |
478 | static void ft_recv(struct fc_lport *lport, struct fc_frame *fp) | |
479 | { | |
480 | struct ft_sess *sess; | |
481 | u32 sid = fc_frame_sid(fp); | |
482 | ||
6708bb27 | 483 | pr_debug("sid %x\n", sid); |
3699d92a KP |
484 | |
485 | sess = ft_sess_get(lport, sid); | |
486 | if (!sess) { | |
6708bb27 | 487 | pr_debug("sid %x sess lookup failed\n", sid); |
3699d92a KP |
488 | /* TBD XXX - if FCP_CMND, send PRLO */ |
489 | fc_frame_free(fp); | |
490 | return; | |
491 | } | |
492 | ft_recv_req(sess, fp); /* must do ft_sess_put() */ | |
493 | } | |
494 | ||
495 | /* | |
496 | * Provider ops for libfc. | |
497 | */ | |
498 | struct fc4_prov ft_prov = { | |
499 | .prli = ft_prli, | |
500 | .prlo = ft_prlo, | |
501 | .recv = ft_recv, | |
502 | .module = THIS_MODULE, | |
503 | }; |