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/version.h>
23 #include <generated/utsrelease.h>
24 #include <linux/utsname.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/kthread.h>
28 #include <linux/types.h>
29 #include <linux/string.h>
30 #include <linux/configfs.h>
31 #include <linux/ctype.h>
32 #include <linux/hash.h>
33 #include <linux/rcupdate.h>
34 #include <linux/rculist.h>
35 #include <linux/kref.h>
36 #include <asm/unaligned.h>
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_cmnd.h>
41 #include <scsi/libfc.h>
43 #include <target/target_core_base.h>
44 #include <target/target_core_transport.h>
45 #include <target/target_core_fabric_ops.h>
46 #include <target/target_core_device.h>
47 #include <target/target_core_tpg.h>
48 #include <target/target_core_configfs.h>
49 #include <target/target_core_base.h>
50 #include <target/configfs_macros.h>
52 #include <scsi/libfc.h>
55 static void ft_sess_delete_all(struct ft_tport
*);
58 * Lookup or allocate target local port.
59 * Caller holds ft_lport_lock.
61 static struct ft_tport
*ft_tport_create(struct fc_lport
*lport
)
64 struct ft_tport
*tport
;
67 tport
= rcu_dereference(lport
->prov
[FC_TYPE_FCP
]);
68 if (tport
&& tport
->tpg
)
71 tpg
= ft_lport_find_tpg(lport
);
80 tport
= kzalloc(sizeof(*tport
), GFP_KERNEL
);
87 for (i
= 0; i
< FT_SESS_HASH_SIZE
; i
++)
88 INIT_HLIST_HEAD(&tport
->hash
[i
]);
90 rcu_assign_pointer(lport
->prov
[FC_TYPE_FCP
], tport
);
97 static void ft_tport_rcu_free(struct rcu_head
*rcu
)
99 struct ft_tport
*tport
= container_of(rcu
, struct ft_tport
, rcu
);
105 * Delete a target local port.
106 * Caller holds ft_lport_lock.
108 static void ft_tport_delete(struct ft_tport
*tport
)
110 struct fc_lport
*lport
;
113 ft_sess_delete_all(tport
);
114 lport
= tport
->lport
;
115 BUG_ON(tport
!= lport
->prov
[FC_TYPE_FCP
]);
116 rcu_assign_pointer(lport
->prov
[FC_TYPE_FCP
], NULL
);
123 call_rcu(&tport
->rcu
, ft_tport_rcu_free
);
128 * Called thru fc_lport_iterate().
130 void ft_lport_add(struct fc_lport
*lport
, void *arg
)
132 mutex_lock(&ft_lport_lock
);
133 ft_tport_create(lport
);
134 mutex_unlock(&ft_lport_lock
);
139 * Called thru fc_lport_iterate().
141 void ft_lport_del(struct fc_lport
*lport
, void *arg
)
143 struct ft_tport
*tport
;
145 mutex_lock(&ft_lport_lock
);
146 tport
= lport
->prov
[FC_TYPE_FCP
];
148 ft_tport_delete(tport
);
149 mutex_unlock(&ft_lport_lock
);
153 * Notification of local port change from libfc.
154 * Create or delete local port and associated tport.
156 int ft_lport_notify(struct notifier_block
*nb
, unsigned long event
, void *arg
)
158 struct fc_lport
*lport
= arg
;
161 case FC_LPORT_EV_ADD
:
162 ft_lport_add(lport
, NULL
);
164 case FC_LPORT_EV_DEL
:
165 ft_lport_del(lport
, NULL
);
172 * Hash function for FC_IDs.
174 static u32
ft_sess_hash(u32 port_id
)
176 return hash_32(port_id
, FT_SESS_HASH_BITS
);
180 * Find session in local port.
181 * Sessions and hash lists are RCU-protected.
182 * A reference is taken which must be eventually freed.
184 static struct ft_sess
*ft_sess_get(struct fc_lport
*lport
, u32 port_id
)
186 struct ft_tport
*tport
;
187 struct hlist_head
*head
;
188 struct hlist_node
*pos
;
189 struct ft_sess
*sess
;
192 tport
= rcu_dereference(lport
->prov
[FC_TYPE_FCP
]);
196 head
= &tport
->hash
[ft_sess_hash(port_id
)];
197 hlist_for_each_entry_rcu(sess
, pos
, head
, hash
) {
198 if (sess
->port_id
== port_id
) {
199 kref_get(&sess
->kref
);
201 FT_SESS_DBG("port_id %x found %p\n", port_id
, sess
);
207 FT_SESS_DBG("port_id %x not found\n", port_id
);
212 * Allocate session and enter it in the hash for the local port.
213 * Caller holds ft_lport_lock.
215 static struct ft_sess
*ft_sess_create(struct ft_tport
*tport
, u32 port_id
,
216 struct ft_node_acl
*acl
)
218 struct ft_sess
*sess
;
219 struct hlist_head
*head
;
220 struct hlist_node
*pos
;
222 head
= &tport
->hash
[ft_sess_hash(port_id
)];
223 hlist_for_each_entry_rcu(sess
, pos
, head
, hash
)
224 if (sess
->port_id
== port_id
)
227 sess
= kzalloc(sizeof(*sess
), GFP_KERNEL
);
231 sess
->se_sess
= transport_init_session();
232 if (IS_ERR(sess
->se_sess
)) {
236 sess
->se_sess
->se_node_acl
= &acl
->se_node_acl
;
238 sess
->port_id
= port_id
;
239 kref_init(&sess
->kref
); /* ref for table entry */
240 hlist_add_head_rcu(&sess
->hash
, head
);
243 FT_SESS_DBG("port_id %x sess %p\n", port_id
, sess
);
245 transport_register_session(&tport
->tpg
->se_tpg
, &acl
->se_node_acl
,
246 sess
->se_sess
, sess
);
251 * Unhash the session.
252 * Caller holds ft_lport_lock.
254 static void ft_sess_unhash(struct ft_sess
*sess
)
256 struct ft_tport
*tport
= sess
->tport
;
258 hlist_del_rcu(&sess
->hash
);
259 BUG_ON(!tport
->sess_count
);
266 * Delete session from hash.
267 * Caller holds ft_lport_lock.
269 static struct ft_sess
*ft_sess_delete(struct ft_tport
*tport
, u32 port_id
)
271 struct hlist_head
*head
;
272 struct hlist_node
*pos
;
273 struct ft_sess
*sess
;
275 head
= &tport
->hash
[ft_sess_hash(port_id
)];
276 hlist_for_each_entry_rcu(sess
, pos
, head
, hash
) {
277 if (sess
->port_id
== port_id
) {
278 ft_sess_unhash(sess
);
286 * Delete all sessions from tport.
287 * Caller holds ft_lport_lock.
289 static void ft_sess_delete_all(struct ft_tport
*tport
)
291 struct hlist_head
*head
;
292 struct hlist_node
*pos
;
293 struct ft_sess
*sess
;
295 for (head
= tport
->hash
;
296 head
< &tport
->hash
[FT_SESS_HASH_SIZE
]; head
++) {
297 hlist_for_each_entry_rcu(sess
, pos
, head
, hash
) {
298 ft_sess_unhash(sess
);
299 transport_deregister_session_configfs(sess
->se_sess
);
300 ft_sess_put(sess
); /* release from table */
306 * TCM ops for sessions.
310 * Determine whether session is allowed to be shutdown in the current context.
311 * Returns non-zero if the session should be shutdown.
313 int ft_sess_shutdown(struct se_session
*se_sess
)
315 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
317 FT_SESS_DBG("port_id %x\n", sess
->port_id
);
322 * Remove session and send PRLO.
323 * This is called when the ACL is being deleted or queue depth is changing.
325 void ft_sess_close(struct se_session
*se_sess
)
327 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
328 struct fc_lport
*lport
;
331 mutex_lock(&ft_lport_lock
);
332 lport
= sess
->tport
->lport
;
333 port_id
= sess
->port_id
;
335 mutex_unlock(&ft_lport_lock
);
338 FT_SESS_DBG("port_id %x\n", port_id
);
339 ft_sess_unhash(sess
);
340 mutex_unlock(&ft_lport_lock
);
341 transport_deregister_session_configfs(se_sess
);
343 /* XXX Send LOGO or PRLO */
344 synchronize_rcu(); /* let transport deregister happen */
347 void ft_sess_stop(struct se_session
*se_sess
, int sess_sleep
, int conn_sleep
)
349 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
351 FT_SESS_DBG("port_id %x\n", sess
->port_id
);
354 int ft_sess_logged_in(struct se_session
*se_sess
)
356 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
358 return sess
->port_id
!= -1;
361 u32
ft_sess_get_index(struct se_session
*se_sess
)
363 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
365 return sess
->port_id
; /* XXX TBD probably not what is needed */
368 u32
ft_sess_get_port_name(struct se_session
*se_sess
,
369 unsigned char *buf
, u32 len
)
371 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
373 return ft_format_wwn(buf
, len
, sess
->port_name
);
376 void ft_sess_set_erl0(struct se_session
*se_sess
)
378 /* XXX TBD called when out of memory */
382 * libfc ops involving sessions.
385 static int ft_prli_locked(struct fc_rport_priv
*rdata
, u32 spp_len
,
386 const struct fc_els_spp
*rspp
, struct fc_els_spp
*spp
)
388 struct ft_tport
*tport
;
389 struct ft_sess
*sess
;
390 struct ft_node_acl
*acl
;
393 tport
= ft_tport_create(rdata
->local_port
);
395 return 0; /* not a target for this local port */
397 acl
= ft_acl_get(tport
->tpg
, rdata
);
404 if (rspp
->spp_flags
& (FC_SPP_OPA_VAL
| FC_SPP_RPA_VAL
))
405 return FC_SPP_RESP_NO_PA
;
408 * If both target and initiator bits are off, the SPP is invalid.
410 fcp_parm
= ntohl(rspp
->spp_params
);
411 if (!(fcp_parm
& (FCP_SPPF_INIT_FCN
| FCP_SPPF_TARG_FCN
)))
412 return FC_SPP_RESP_INVL
;
415 * Create session (image pair) only if requested by
416 * EST_IMG_PAIR flag and if the requestor is an initiator.
418 if (rspp
->spp_flags
& FC_SPP_EST_IMG_PAIR
) {
419 spp
->spp_flags
|= FC_SPP_EST_IMG_PAIR
;
420 if (!(fcp_parm
& FCP_SPPF_INIT_FCN
))
421 return FC_SPP_RESP_CONF
;
422 sess
= ft_sess_create(tport
, rdata
->ids
.port_id
, acl
);
424 return FC_SPP_RESP_RES
;
427 sess
->params
= fcp_parm
;
428 sess
->port_name
= rdata
->ids
.port_name
;
429 sess
->max_frame
= rdata
->maxframe_size
;
431 /* XXX TBD - clearing actions. unit attn, see 4.10 */
435 * OR in our service parameters with other provider (initiator), if any.
436 * TBD XXX - indicate RETRY capability?
439 fcp_parm
= ntohl(spp
->spp_params
);
440 spp
->spp_params
= htonl(fcp_parm
| FCP_SPPF_TARG_FCN
);
441 return FC_SPP_RESP_ACK
;
445 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
446 * @rdata: remote port private
447 * @spp_len: service parameter page length
448 * @rspp: received service parameter page (NULL for outgoing PRLI)
449 * @spp: response service parameter page
451 * Returns spp response code.
453 static int ft_prli(struct fc_rport_priv
*rdata
, u32 spp_len
,
454 const struct fc_els_spp
*rspp
, struct fc_els_spp
*spp
)
458 mutex_lock(&ft_lport_lock
);
459 ret
= ft_prli_locked(rdata
, spp_len
, rspp
, spp
);
460 mutex_unlock(&ft_lport_lock
);
461 FT_SESS_DBG("port_id %x flags %x ret %x\n",
462 rdata
->ids
.port_id
, rspp
? rspp
->spp_flags
: 0, ret
);
466 static void ft_sess_rcu_free(struct rcu_head
*rcu
)
468 struct ft_sess
*sess
= container_of(rcu
, struct ft_sess
, rcu
);
470 transport_deregister_session(sess
->se_sess
);
474 static void ft_sess_free(struct kref
*kref
)
476 struct ft_sess
*sess
= container_of(kref
, struct ft_sess
, kref
);
478 call_rcu(&sess
->rcu
, ft_sess_rcu_free
);
481 void ft_sess_put(struct ft_sess
*sess
)
483 int sess_held
= atomic_read(&sess
->kref
.refcount
);
486 kref_put(&sess
->kref
, ft_sess_free
);
489 static void ft_prlo(struct fc_rport_priv
*rdata
)
491 struct ft_sess
*sess
;
492 struct ft_tport
*tport
;
494 mutex_lock(&ft_lport_lock
);
495 tport
= rcu_dereference(rdata
->local_port
->prov
[FC_TYPE_FCP
]);
497 mutex_unlock(&ft_lport_lock
);
500 sess
= ft_sess_delete(tport
, rdata
->ids
.port_id
);
502 mutex_unlock(&ft_lport_lock
);
505 mutex_unlock(&ft_lport_lock
);
506 transport_deregister_session_configfs(sess
->se_sess
);
507 ft_sess_put(sess
); /* release from table */
509 /* XXX TBD - clearing actions. unit attn, see 4.10 */
513 * Handle incoming FCP request.
514 * Caller has verified that the frame is type FCP.
516 static void ft_recv(struct fc_lport
*lport
, struct fc_frame
*fp
)
518 struct ft_sess
*sess
;
519 u32 sid
= fc_frame_sid(fp
);
521 FT_SESS_DBG("sid %x\n", sid
);
523 sess
= ft_sess_get(lport
, sid
);
525 FT_SESS_DBG("sid %x sess lookup failed\n", sid
);
526 /* TBD XXX - if FCP_CMND, send PRLO */
530 ft_recv_req(sess
, fp
); /* must do ft_sess_put() */
534 * Provider ops for libfc.
536 struct fc4_prov ft_prov
= {
540 .module
= THIS_MODULE
,