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 <generated/utsrelease.h>
23 #include <linux/utsname.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/kthread.h>
27 #include <linux/types.h>
28 #include <linux/string.h>
29 #include <linux/configfs.h>
30 #include <linux/ctype.h>
31 #include <linux/hash.h>
32 #include <linux/rcupdate.h>
33 #include <linux/rculist.h>
34 #include <linux/kref.h>
35 #include <asm/unaligned.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_device.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/libfc.h>
42 #include <target/target_core_base.h>
43 #include <target/target_core_transport.h>
44 #include <target/target_core_fabric_ops.h>
45 #include <target/target_core_device.h>
46 #include <target/target_core_tpg.h>
47 #include <target/target_core_configfs.h>
48 #include <target/configfs_macros.h>
52 static void ft_sess_delete_all(struct ft_tport
*);
55 * Lookup or allocate target local port.
56 * Caller holds ft_lport_lock.
58 static struct ft_tport
*ft_tport_create(struct fc_lport
*lport
)
61 struct ft_tport
*tport
;
64 tport
= rcu_dereference(lport
->prov
[FC_TYPE_FCP
]);
65 if (tport
&& tport
->tpg
)
68 tpg
= ft_lport_find_tpg(lport
);
77 tport
= kzalloc(sizeof(*tport
), GFP_KERNEL
);
84 for (i
= 0; i
< FT_SESS_HASH_SIZE
; i
++)
85 INIT_HLIST_HEAD(&tport
->hash
[i
]);
87 rcu_assign_pointer(lport
->prov
[FC_TYPE_FCP
], tport
);
94 static void ft_tport_rcu_free(struct rcu_head
*rcu
)
96 struct ft_tport
*tport
= container_of(rcu
, struct ft_tport
, rcu
);
102 * Delete a target local port.
103 * Caller holds ft_lport_lock.
105 static void ft_tport_delete(struct ft_tport
*tport
)
107 struct fc_lport
*lport
;
110 ft_sess_delete_all(tport
);
111 lport
= tport
->lport
;
112 BUG_ON(tport
!= lport
->prov
[FC_TYPE_FCP
]);
113 rcu_assign_pointer(lport
->prov
[FC_TYPE_FCP
], NULL
);
120 call_rcu(&tport
->rcu
, ft_tport_rcu_free
);
125 * Called thru fc_lport_iterate().
127 void ft_lport_add(struct fc_lport
*lport
, void *arg
)
129 mutex_lock(&ft_lport_lock
);
130 ft_tport_create(lport
);
131 mutex_unlock(&ft_lport_lock
);
136 * Called thru fc_lport_iterate().
138 void ft_lport_del(struct fc_lport
*lport
, void *arg
)
140 struct ft_tport
*tport
;
142 mutex_lock(&ft_lport_lock
);
143 tport
= lport
->prov
[FC_TYPE_FCP
];
145 ft_tport_delete(tport
);
146 mutex_unlock(&ft_lport_lock
);
150 * Notification of local port change from libfc.
151 * Create or delete local port and associated tport.
153 int ft_lport_notify(struct notifier_block
*nb
, unsigned long event
, void *arg
)
155 struct fc_lport
*lport
= arg
;
158 case FC_LPORT_EV_ADD
:
159 ft_lport_add(lport
, NULL
);
161 case FC_LPORT_EV_DEL
:
162 ft_lport_del(lport
, NULL
);
169 * Hash function for FC_IDs.
171 static u32
ft_sess_hash(u32 port_id
)
173 return hash_32(port_id
, FT_SESS_HASH_BITS
);
177 * Find session in local port.
178 * Sessions and hash lists are RCU-protected.
179 * A reference is taken which must be eventually freed.
181 static struct ft_sess
*ft_sess_get(struct fc_lport
*lport
, u32 port_id
)
183 struct ft_tport
*tport
;
184 struct hlist_head
*head
;
185 struct hlist_node
*pos
;
186 struct ft_sess
*sess
;
189 tport
= rcu_dereference(lport
->prov
[FC_TYPE_FCP
]);
193 head
= &tport
->hash
[ft_sess_hash(port_id
)];
194 hlist_for_each_entry_rcu(sess
, pos
, head
, hash
) {
195 if (sess
->port_id
== port_id
) {
196 kref_get(&sess
->kref
);
198 pr_debug("port_id %x found %p\n", port_id
, sess
);
204 pr_debug("port_id %x not found\n", port_id
);
209 * Allocate session and enter it in the hash for the local port.
210 * Caller holds ft_lport_lock.
212 static struct ft_sess
*ft_sess_create(struct ft_tport
*tport
, u32 port_id
,
213 struct ft_node_acl
*acl
)
215 struct ft_sess
*sess
;
216 struct hlist_head
*head
;
217 struct hlist_node
*pos
;
219 head
= &tport
->hash
[ft_sess_hash(port_id
)];
220 hlist_for_each_entry_rcu(sess
, pos
, head
, hash
)
221 if (sess
->port_id
== port_id
)
224 sess
= kzalloc(sizeof(*sess
), GFP_KERNEL
);
228 sess
->se_sess
= transport_init_session();
229 if (IS_ERR(sess
->se_sess
)) {
233 sess
->se_sess
->se_node_acl
= &acl
->se_node_acl
;
235 sess
->port_id
= port_id
;
236 kref_init(&sess
->kref
); /* ref for table entry */
237 hlist_add_head_rcu(&sess
->hash
, head
);
240 pr_debug("port_id %x sess %p\n", port_id
, sess
);
242 transport_register_session(&tport
->tpg
->se_tpg
, &acl
->se_node_acl
,
243 sess
->se_sess
, sess
);
248 * Unhash the session.
249 * Caller holds ft_lport_lock.
251 static void ft_sess_unhash(struct ft_sess
*sess
)
253 struct ft_tport
*tport
= sess
->tport
;
255 hlist_del_rcu(&sess
->hash
);
256 BUG_ON(!tport
->sess_count
);
263 * Delete session from hash.
264 * Caller holds ft_lport_lock.
266 static struct ft_sess
*ft_sess_delete(struct ft_tport
*tport
, u32 port_id
)
268 struct hlist_head
*head
;
269 struct hlist_node
*pos
;
270 struct ft_sess
*sess
;
272 head
= &tport
->hash
[ft_sess_hash(port_id
)];
273 hlist_for_each_entry_rcu(sess
, pos
, head
, hash
) {
274 if (sess
->port_id
== port_id
) {
275 ft_sess_unhash(sess
);
283 * Delete all sessions from tport.
284 * Caller holds ft_lport_lock.
286 static void ft_sess_delete_all(struct ft_tport
*tport
)
288 struct hlist_head
*head
;
289 struct hlist_node
*pos
;
290 struct ft_sess
*sess
;
292 for (head
= tport
->hash
;
293 head
< &tport
->hash
[FT_SESS_HASH_SIZE
]; head
++) {
294 hlist_for_each_entry_rcu(sess
, pos
, head
, hash
) {
295 ft_sess_unhash(sess
);
296 transport_deregister_session_configfs(sess
->se_sess
);
297 ft_sess_put(sess
); /* release from table */
303 * TCM ops for sessions.
307 * Determine whether session is allowed to be shutdown in the current context.
308 * Returns non-zero if the session should be shutdown.
310 int ft_sess_shutdown(struct se_session
*se_sess
)
312 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
314 pr_debug("port_id %x\n", sess
->port_id
);
319 * Remove session and send PRLO.
320 * This is called when the ACL is being deleted or queue depth is changing.
322 void ft_sess_close(struct se_session
*se_sess
)
324 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
325 struct fc_lport
*lport
;
328 mutex_lock(&ft_lport_lock
);
329 lport
= sess
->tport
->lport
;
330 port_id
= sess
->port_id
;
332 mutex_unlock(&ft_lport_lock
);
335 pr_debug("port_id %x\n", port_id
);
336 ft_sess_unhash(sess
);
337 mutex_unlock(&ft_lport_lock
);
338 transport_deregister_session_configfs(se_sess
);
340 /* XXX Send LOGO or PRLO */
341 synchronize_rcu(); /* let transport deregister happen */
344 void ft_sess_stop(struct se_session
*se_sess
, int sess_sleep
, int conn_sleep
)
346 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
348 pr_debug("port_id %x\n", sess
->port_id
);
351 int ft_sess_logged_in(struct se_session
*se_sess
)
353 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
355 return sess
->port_id
!= -1;
358 u32
ft_sess_get_index(struct se_session
*se_sess
)
360 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
362 return sess
->port_id
; /* XXX TBD probably not what is needed */
365 u32
ft_sess_get_port_name(struct se_session
*se_sess
,
366 unsigned char *buf
, u32 len
)
368 struct ft_sess
*sess
= se_sess
->fabric_sess_ptr
;
370 return ft_format_wwn(buf
, len
, sess
->port_name
);
373 void ft_sess_set_erl0(struct se_session
*se_sess
)
375 /* XXX TBD called when out of memory */
379 * libfc ops involving sessions.
382 static int ft_prli_locked(struct fc_rport_priv
*rdata
, u32 spp_len
,
383 const struct fc_els_spp
*rspp
, struct fc_els_spp
*spp
)
385 struct ft_tport
*tport
;
386 struct ft_sess
*sess
;
387 struct ft_node_acl
*acl
;
390 tport
= ft_tport_create(rdata
->local_port
);
392 return 0; /* not a target for this local port */
394 acl
= ft_acl_get(tport
->tpg
, rdata
);
401 if (rspp
->spp_flags
& (FC_SPP_OPA_VAL
| FC_SPP_RPA_VAL
))
402 return FC_SPP_RESP_NO_PA
;
405 * If both target and initiator bits are off, the SPP is invalid.
407 fcp_parm
= ntohl(rspp
->spp_params
);
408 if (!(fcp_parm
& (FCP_SPPF_INIT_FCN
| FCP_SPPF_TARG_FCN
)))
409 return FC_SPP_RESP_INVL
;
412 * Create session (image pair) only if requested by
413 * EST_IMG_PAIR flag and if the requestor is an initiator.
415 if (rspp
->spp_flags
& FC_SPP_EST_IMG_PAIR
) {
416 spp
->spp_flags
|= FC_SPP_EST_IMG_PAIR
;
417 if (!(fcp_parm
& FCP_SPPF_INIT_FCN
))
418 return FC_SPP_RESP_CONF
;
419 sess
= ft_sess_create(tport
, rdata
->ids
.port_id
, acl
);
421 return FC_SPP_RESP_RES
;
424 sess
->params
= fcp_parm
;
425 sess
->port_name
= rdata
->ids
.port_name
;
426 sess
->max_frame
= rdata
->maxframe_size
;
428 /* XXX TBD - clearing actions. unit attn, see 4.10 */
432 * OR in our service parameters with other provider (initiator), if any.
433 * TBD XXX - indicate RETRY capability?
436 fcp_parm
= ntohl(spp
->spp_params
);
437 spp
->spp_params
= htonl(fcp_parm
| FCP_SPPF_TARG_FCN
);
438 return FC_SPP_RESP_ACK
;
442 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
443 * @rdata: remote port private
444 * @spp_len: service parameter page length
445 * @rspp: received service parameter page (NULL for outgoing PRLI)
446 * @spp: response service parameter page
448 * Returns spp response code.
450 static int ft_prli(struct fc_rport_priv
*rdata
, u32 spp_len
,
451 const struct fc_els_spp
*rspp
, struct fc_els_spp
*spp
)
455 mutex_lock(&ft_lport_lock
);
456 ret
= ft_prli_locked(rdata
, spp_len
, rspp
, spp
);
457 mutex_unlock(&ft_lport_lock
);
458 pr_debug("port_id %x flags %x ret %x\n",
459 rdata
->ids
.port_id
, rspp
? rspp
->spp_flags
: 0, ret
);
463 static void ft_sess_rcu_free(struct rcu_head
*rcu
)
465 struct ft_sess
*sess
= container_of(rcu
, struct ft_sess
, rcu
);
467 transport_deregister_session(sess
->se_sess
);
471 static void ft_sess_free(struct kref
*kref
)
473 struct ft_sess
*sess
= container_of(kref
, struct ft_sess
, kref
);
475 call_rcu(&sess
->rcu
, ft_sess_rcu_free
);
478 void ft_sess_put(struct ft_sess
*sess
)
480 int sess_held
= atomic_read(&sess
->kref
.refcount
);
483 kref_put(&sess
->kref
, ft_sess_free
);
486 static void ft_prlo(struct fc_rport_priv
*rdata
)
488 struct ft_sess
*sess
;
489 struct ft_tport
*tport
;
491 mutex_lock(&ft_lport_lock
);
492 tport
= rcu_dereference(rdata
->local_port
->prov
[FC_TYPE_FCP
]);
494 mutex_unlock(&ft_lport_lock
);
497 sess
= ft_sess_delete(tport
, rdata
->ids
.port_id
);
499 mutex_unlock(&ft_lport_lock
);
502 mutex_unlock(&ft_lport_lock
);
503 transport_deregister_session_configfs(sess
->se_sess
);
504 ft_sess_put(sess
); /* release from table */
506 /* XXX TBD - clearing actions. unit attn, see 4.10 */
510 * Handle incoming FCP request.
511 * Caller has verified that the frame is type FCP.
513 static void ft_recv(struct fc_lport
*lport
, struct fc_frame
*fp
)
515 struct ft_sess
*sess
;
516 u32 sid
= fc_frame_sid(fp
);
518 pr_debug("sid %x\n", sid
);
520 sess
= ft_sess_get(lport
, sid
);
522 pr_debug("sid %x sess lookup failed\n", sid
);
523 /* TBD XXX - if FCP_CMND, send PRLO */
527 ft_recv_req(sess
, fp
); /* must do ft_sess_put() */
531 * Provider ops for libfc.
533 struct fc4_prov ft_prov
= {
537 .module
= THIS_MODULE
,