]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/target/tcm_fc/tfc_sess.c
2 * Copyright (c) 2010 Cisco Systems, Inc.
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.
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
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.
18 /* XXX TBD some includes may be extraneous */
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
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>
41 #include <target/target_core_base.h>
42 #include <target/target_core_fabric.h>
43 #include <target/target_core_configfs.h>
44 #include <target/configfs_macros.h>
48 static void ft_sess_delete_all(struct ft_tport
*);
51 * Lookup or allocate target local port.
52 * Caller holds ft_lport_lock.
54 static struct ft_tport
*ft_tport_create(struct fc_lport
*lport
)
57 struct ft_tport
*tport
;
60 tport
= rcu_dereference_protected(lport
->prov
[FC_TYPE_FCP
],
61 lockdep_is_held(&ft_lport_lock
));
62 if (tport
&& tport
->tpg
)
65 tpg
= ft_lport_find_tpg(lport
);
74 tport
= kzalloc(sizeof(*tport
), GFP_KERNEL
);
81 for (i
= 0; i
< FT_SESS_HASH_SIZE
; i
++)
82 INIT_HLIST_HEAD(&tport
->hash
[i
]);
84 rcu_assign_pointer(lport
->prov
[FC_TYPE_FCP
], tport
);
89 * Delete a target local port.
90 * Caller holds ft_lport_lock.
92 static void ft_tport_delete(struct ft_tport
*tport
)
94 struct fc_lport
*lport
;
97 ft_sess_delete_all(tport
);
99 BUG_ON(tport
!= lport
->prov
[FC_TYPE_FCP
]);
100 rcu_assign_pointer(lport
->prov
[FC_TYPE_FCP
], NULL
);
107 kfree_rcu(tport
, rcu
);
112 * Called thru fc_lport_iterate().
114 void ft_lport_add(struct fc_lport
*lport
, void *arg
)
116 mutex_lock(&ft_lport_lock
);
117 ft_tport_create(lport
);
118 mutex_unlock(&ft_lport_lock
);
123 * Called thru fc_lport_iterate().
125 void ft_lport_del(struct fc_lport
*lport
, void *arg
)
127 struct ft_tport
*tport
;
129 mutex_lock(&ft_lport_lock
);
130 tport
= lport
->prov
[FC_TYPE_FCP
];
132 ft_tport_delete(tport
);
133 mutex_unlock(&ft_lport_lock
);
137 * Notification of local port change from libfc.
138 * Create or delete local port and associated tport.
140 int ft_lport_notify(struct notifier_block
*nb
, unsigned long event
, void *arg
)
142 struct fc_lport
*lport
= arg
;
145 case FC_LPORT_EV_ADD
:
146 ft_lport_add(lport
, NULL
);
148 case FC_LPORT_EV_DEL
:
149 ft_lport_del(lport
, NULL
);
156 * Hash function for FC_IDs.
158 static u32
ft_sess_hash(u32 port_id
)
160 return hash_32(port_id
, FT_SESS_HASH_BITS
);
164 * Find session in local port.
165 * Sessions and hash lists are RCU-protected.
166 * A reference is taken which must be eventually freed.
168 static struct ft_sess
*ft_sess_get(struct fc_lport
*lport
, u32 port_id
)
170 struct ft_tport
*tport
;
171 struct hlist_head
*head
;
172 struct ft_sess
*sess
;
175 tport
= rcu_dereference(lport
->prov
[FC_TYPE_FCP
]);
179 head
= &tport
->hash
[ft_sess_hash(port_id
)];
180 hlist_for_each_entry_rcu(sess
, head
, hash
) {
181 if (sess
->port_id
== port_id
) {
182 kref_get(&sess
->kref
);
184 pr_debug("port_id %x found %p\n", port_id
, sess
);
190 pr_debug("port_id %x not found\n", port_id
);
195 * Allocate session and enter it in the hash for the local port.
196 * Caller holds ft_lport_lock.
198 static struct ft_sess
*ft_sess_create(struct ft_tport
*tport
, u32 port_id
,
199 struct ft_node_acl
*acl
)
201 struct ft_sess
*sess
;
202 struct hlist_head
*head
;
204 head
= &tport
->hash
[ft_sess_hash(port_id
)];
205 hlist_for_each_entry_rcu(sess
, head
, hash
)
206 if (sess
->port_id
== port_id
)
209 sess
= kzalloc(sizeof(*sess
), GFP_KERNEL
);
213 sess
->se_sess
= transport_init_session();
214 if (IS_ERR(sess
->se_sess
)) {
218 sess
->se_sess
->se_node_acl
= &acl
->se_node_acl
;
220 sess
->port_id
= port_id
;
221 kref_init(&sess
->kref
); /* ref for table entry */
222 hlist_add_head_rcu(&sess
->hash
, head
);
225 pr_debug("port_id %x sess %p\n", port_id
, sess
);
227 transport_register_session(&tport
->tpg
->se_tpg
, &acl
->se_node_acl
,
228 sess
->se_sess
, sess
);
233 * Unhash the session.
234 * Caller holds ft_lport_lock.
236 static void ft_sess_unhash(struct ft_sess
*sess
)
238 struct ft_tport
*tport
= sess
->tport
;
240 hlist_del_rcu(&sess
->hash
);
241 BUG_ON(!tport
->sess_count
);
248 * Delete session from hash.
249 * Caller holds ft_lport_lock.
251 static struct ft_sess
*ft_sess_delete(struct ft_tport
*tport
, u32 port_id
)
253 struct hlist_head
*head
;
254 struct ft_sess
*sess
;
256 head
= &tport
->hash
[ft_sess_hash(port_id
)];
257 hlist_for_each_entry_rcu(sess
, head
, hash
) {
258 if (sess
->port_id
== port_id
) {
259 ft_sess_unhash(sess
);
267 * Delete all sessions from tport.
268 * Caller holds ft_lport_lock.
270 static void ft_sess_delete_all(struct ft_tport
*tport
)
272 struct hlist_head
*head
;
273 struct ft_sess
*sess
;
275 for (head
= tport
->hash
;
276 head
< &tport
->hash
[FT_SESS_HASH_SIZE
]; head
++) {
277 hlist_for_each_entry_rcu(sess
, head
, hash
) {
278 ft_sess_unhash(sess
);
279 transport_deregister_session_configfs(sess
->se_sess
);
280 ft_sess_put(sess
); /* release from table */
286 * TCM ops for sessions.
290 * Determine whether session is allowed to be shutdown in the current context.
291 * Returns non-zero if the session should be shutdown.
293 int ft_sess_shutdown(struct se_session
*se_sess
)
295 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
297 pr_debug("port_id %x\n", sess
->port_id
);
302 * Remove session and send PRLO.
303 * This is called when the ACL is being deleted or queue depth is changing.
305 void ft_sess_close(struct se_session
*se_sess
)
307 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
310 mutex_lock(&ft_lport_lock
);
311 port_id
= sess
->port_id
;
313 mutex_unlock(&ft_lport_lock
);
316 pr_debug("port_id %x\n", port_id
);
317 ft_sess_unhash(sess
);
318 mutex_unlock(&ft_lport_lock
);
319 transport_deregister_session_configfs(se_sess
);
321 /* XXX Send LOGO or PRLO */
322 synchronize_rcu(); /* let transport deregister happen */
325 u32
ft_sess_get_index(struct se_session
*se_sess
)
327 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
329 return sess
->port_id
; /* XXX TBD probably not what is needed */
332 u32
ft_sess_get_port_name(struct se_session
*se_sess
,
333 unsigned char *buf
, u32 len
)
335 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
337 return ft_format_wwn(buf
, len
, sess
->port_name
);
341 * libfc ops involving sessions.
344 static int ft_prli_locked(struct fc_rport_priv
*rdata
, u32 spp_len
,
345 const struct fc_els_spp
*rspp
, struct fc_els_spp
*spp
)
347 struct ft_tport
*tport
;
348 struct ft_sess
*sess
;
349 struct ft_node_acl
*acl
;
352 tport
= ft_tport_create(rdata
->local_port
);
354 goto not_target
; /* not a target for this local port */
356 acl
= ft_acl_get(tport
->tpg
, rdata
);
358 goto not_target
; /* no target for this remote */
363 if (rspp
->spp_flags
& (FC_SPP_OPA_VAL
| FC_SPP_RPA_VAL
))
364 return FC_SPP_RESP_NO_PA
;
367 * If both target and initiator bits are off, the SPP is invalid.
369 fcp_parm
= ntohl(rspp
->spp_params
);
370 if (!(fcp_parm
& (FCP_SPPF_INIT_FCN
| FCP_SPPF_TARG_FCN
)))
371 return FC_SPP_RESP_INVL
;
374 * Create session (image pair) only if requested by
375 * EST_IMG_PAIR flag and if the requestor is an initiator.
377 if (rspp
->spp_flags
& FC_SPP_EST_IMG_PAIR
) {
378 spp
->spp_flags
|= FC_SPP_EST_IMG_PAIR
;
379 if (!(fcp_parm
& FCP_SPPF_INIT_FCN
))
380 return FC_SPP_RESP_CONF
;
381 sess
= ft_sess_create(tport
, rdata
->ids
.port_id
, acl
);
383 return FC_SPP_RESP_RES
;
386 sess
->params
= fcp_parm
;
387 sess
->port_name
= rdata
->ids
.port_name
;
388 sess
->max_frame
= rdata
->maxframe_size
;
390 /* XXX TBD - clearing actions. unit attn, see 4.10 */
394 * OR in our service parameters with other provider (initiator), if any.
397 fcp_parm
= ntohl(spp
->spp_params
);
398 fcp_parm
&= ~FCP_SPPF_RETRY
;
399 spp
->spp_params
= htonl(fcp_parm
| FCP_SPPF_TARG_FCN
);
400 return FC_SPP_RESP_ACK
;
403 fcp_parm
= ntohl(spp
->spp_params
);
404 fcp_parm
&= ~FCP_SPPF_TARG_FCN
;
405 spp
->spp_params
= htonl(fcp_parm
);
410 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
411 * @rdata: remote port private
412 * @spp_len: service parameter page length
413 * @rspp: received service parameter page (NULL for outgoing PRLI)
414 * @spp: response service parameter page
416 * Returns spp response code.
418 static int ft_prli(struct fc_rport_priv
*rdata
, u32 spp_len
,
419 const struct fc_els_spp
*rspp
, struct fc_els_spp
*spp
)
423 mutex_lock(&ft_lport_lock
);
424 ret
= ft_prli_locked(rdata
, spp_len
, rspp
, spp
);
425 mutex_unlock(&ft_lport_lock
);
426 pr_debug("port_id %x flags %x ret %x\n",
427 rdata
->ids
.port_id
, rspp
? rspp
->spp_flags
: 0, ret
);
431 static void ft_sess_rcu_free(struct rcu_head
*rcu
)
433 struct ft_sess
*sess
= container_of(rcu
, struct ft_sess
, rcu
);
438 static void ft_sess_free(struct kref
*kref
)
440 struct ft_sess
*sess
= container_of(kref
, struct ft_sess
, kref
);
442 transport_deregister_session(sess
->se_sess
);
443 call_rcu(&sess
->rcu
, ft_sess_rcu_free
);
446 void ft_sess_put(struct ft_sess
*sess
)
448 int sess_held
= atomic_read(&sess
->kref
.refcount
);
451 kref_put(&sess
->kref
, ft_sess_free
);
454 static void ft_prlo(struct fc_rport_priv
*rdata
)
456 struct ft_sess
*sess
;
457 struct ft_tport
*tport
;
459 mutex_lock(&ft_lport_lock
);
460 tport
= rcu_dereference_protected(rdata
->local_port
->prov
[FC_TYPE_FCP
],
461 lockdep_is_held(&ft_lport_lock
));
464 mutex_unlock(&ft_lport_lock
);
467 sess
= ft_sess_delete(tport
, rdata
->ids
.port_id
);
469 mutex_unlock(&ft_lport_lock
);
472 mutex_unlock(&ft_lport_lock
);
473 transport_deregister_session_configfs(sess
->se_sess
);
474 ft_sess_put(sess
); /* release from table */
476 /* XXX TBD - clearing actions. unit attn, see 4.10 */
480 * Handle incoming FCP request.
481 * Caller has verified that the frame is type FCP.
483 static void ft_recv(struct fc_lport
*lport
, struct fc_frame
*fp
)
485 struct ft_sess
*sess
;
486 u32 sid
= fc_frame_sid(fp
);
488 pr_debug("sid %x\n", sid
);
490 sess
= ft_sess_get(lport
, sid
);
492 pr_debug("sid %x sess lookup failed\n", sid
);
493 /* TBD XXX - if FCP_CMND, send PRLO */
497 ft_recv_req(sess
, fp
); /* must do ft_sess_put() */
501 * Provider ops for libfc.
503 struct fc4_prov ft_prov
= {
507 .module
= THIS_MODULE
,