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/configfs_macros.h>
53 static void ft_sess_delete_all(struct ft_tport *);
56 * Lookup or allocate target local port.
57 * Caller holds ft_lport_lock.
59 static struct ft_tport *ft_tport_create(struct fc_lport *lport)
62 struct ft_tport *tport;
65 tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
66 if (tport && tport->tpg)
69 tpg = ft_lport_find_tpg(lport);
78 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
85 for (i = 0; i < FT_SESS_HASH_SIZE; i++)
86 INIT_HLIST_HEAD(&tport->hash[i]);
88 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], tport);
95 static void ft_tport_rcu_free(struct rcu_head *rcu)
97 struct ft_tport *tport = container_of(rcu, struct ft_tport, rcu);
103 * Delete a target local port.
104 * Caller holds ft_lport_lock.
106 static void ft_tport_delete(struct ft_tport *tport)
108 struct fc_lport *lport;
111 ft_sess_delete_all(tport);
112 lport = tport->lport;
113 BUG_ON(tport != lport->prov[FC_TYPE_FCP]);
114 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], NULL);
121 call_rcu(&tport->rcu, ft_tport_rcu_free);
126 * Called thru fc_lport_iterate().
128 void ft_lport_add(struct fc_lport *lport, void *arg)
130 mutex_lock(&ft_lport_lock);
131 ft_tport_create(lport);
132 mutex_unlock(&ft_lport_lock);
137 * Called thru fc_lport_iterate().
139 void ft_lport_del(struct fc_lport *lport, void *arg)
141 struct ft_tport *tport;
143 mutex_lock(&ft_lport_lock);
144 tport = lport->prov[FC_TYPE_FCP];
146 ft_tport_delete(tport);
147 mutex_unlock(&ft_lport_lock);
151 * Notification of local port change from libfc.
152 * Create or delete local port and associated tport.
154 int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg)
156 struct fc_lport *lport = arg;
159 case FC_LPORT_EV_ADD:
160 ft_lport_add(lport, NULL);
162 case FC_LPORT_EV_DEL:
163 ft_lport_del(lport, NULL);
170 * Hash function for FC_IDs.
172 static u32 ft_sess_hash(u32 port_id)
174 return hash_32(port_id, FT_SESS_HASH_BITS);
178 * Find session in local port.
179 * Sessions and hash lists are RCU-protected.
180 * A reference is taken which must be eventually freed.
182 static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
184 struct ft_tport *tport;
185 struct hlist_head *head;
186 struct hlist_node *pos;
187 struct ft_sess *sess;
190 tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
194 head = &tport->hash[ft_sess_hash(port_id)];
195 hlist_for_each_entry_rcu(sess, pos, head, hash) {
196 if (sess->port_id == port_id) {
197 kref_get(&sess->kref);
199 FT_SESS_DBG("port_id %x found %p\n", port_id, sess);
205 FT_SESS_DBG("port_id %x not found\n", port_id);
210 * Allocate session and enter it in the hash for the local port.
211 * Caller holds ft_lport_lock.
213 static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
214 struct ft_node_acl *acl)
216 struct ft_sess *sess;
217 struct hlist_head *head;
218 struct hlist_node *pos;
220 head = &tport->hash[ft_sess_hash(port_id)];
221 hlist_for_each_entry_rcu(sess, pos, head, hash)
222 if (sess->port_id == port_id)
225 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
229 sess->se_sess = transport_init_session();
230 if (IS_ERR(sess->se_sess)) {
234 sess->se_sess->se_node_acl = &acl->se_node_acl;
236 sess->port_id = port_id;
237 kref_init(&sess->kref); /* ref for table entry */
238 hlist_add_head_rcu(&sess->hash, head);
241 FT_SESS_DBG("port_id %x sess %p\n", port_id, sess);
243 transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl,
244 sess->se_sess, sess);
249 * Unhash the session.
250 * Caller holds ft_lport_lock.
252 static void ft_sess_unhash(struct ft_sess *sess)
254 struct ft_tport *tport = sess->tport;
256 hlist_del_rcu(&sess->hash);
257 BUG_ON(!tport->sess_count);
264 * Delete session from hash.
265 * Caller holds ft_lport_lock.
267 static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
269 struct hlist_head *head;
270 struct hlist_node *pos;
271 struct ft_sess *sess;
273 head = &tport->hash[ft_sess_hash(port_id)];
274 hlist_for_each_entry_rcu(sess, pos, head, hash) {
275 if (sess->port_id == port_id) {
276 ft_sess_unhash(sess);
284 * Delete all sessions from tport.
285 * Caller holds ft_lport_lock.
287 static void ft_sess_delete_all(struct ft_tport *tport)
289 struct hlist_head *head;
290 struct hlist_node *pos;
291 struct ft_sess *sess;
293 for (head = tport->hash;
294 head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
295 hlist_for_each_entry_rcu(sess, pos, head, hash) {
296 ft_sess_unhash(sess);
297 transport_deregister_session_configfs(sess->se_sess);
298 ft_sess_put(sess); /* release from table */
304 * TCM ops for sessions.
308 * Determine whether session is allowed to be shutdown in the current context.
309 * Returns non-zero if the session should be shutdown.
311 int ft_sess_shutdown(struct se_session *se_sess)
313 struct ft_sess *sess = se_sess->fabric_sess_ptr;
315 FT_SESS_DBG("port_id %x\n", sess->port_id);
320 * Remove session and send PRLO.
321 * This is called when the ACL is being deleted or queue depth is changing.
323 void ft_sess_close(struct se_session *se_sess)
325 struct ft_sess *sess = se_sess->fabric_sess_ptr;
326 struct fc_lport *lport;
329 mutex_lock(&ft_lport_lock);
330 lport = sess->tport->lport;
331 port_id = sess->port_id;
333 mutex_unlock(&ft_lport_lock);
336 FT_SESS_DBG("port_id %x\n", port_id);
337 ft_sess_unhash(sess);
338 mutex_unlock(&ft_lport_lock);
339 transport_deregister_session_configfs(se_sess);
341 /* XXX Send LOGO or PRLO */
342 synchronize_rcu(); /* let transport deregister happen */
345 void ft_sess_stop(struct se_session *se_sess, int sess_sleep, int conn_sleep)
347 struct ft_sess *sess = se_sess->fabric_sess_ptr;
349 FT_SESS_DBG("port_id %x\n", sess->port_id);
352 int ft_sess_logged_in(struct se_session *se_sess)
354 struct ft_sess *sess = se_sess->fabric_sess_ptr;
356 return sess->port_id != -1;
359 u32 ft_sess_get_index(struct se_session *se_sess)
361 struct ft_sess *sess = se_sess->fabric_sess_ptr;
363 return sess->port_id; /* XXX TBD probably not what is needed */
366 u32 ft_sess_get_port_name(struct se_session *se_sess,
367 unsigned char *buf, u32 len)
369 struct ft_sess *sess = se_sess->fabric_sess_ptr;
371 return ft_format_wwn(buf, len, sess->port_name);
374 void ft_sess_set_erl0(struct se_session *se_sess)
376 /* XXX TBD called when out of memory */
380 * libfc ops involving sessions.
383 static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
384 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
386 struct ft_tport *tport;
387 struct ft_sess *sess;
388 struct ft_node_acl *acl;
391 tport = ft_tport_create(rdata->local_port);
393 return 0; /* not a target for this local port */
395 acl = ft_acl_get(tport->tpg, rdata);
402 if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
403 return FC_SPP_RESP_NO_PA;
406 * If both target and initiator bits are off, the SPP is invalid.
408 fcp_parm = ntohl(rspp->spp_params);
409 if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
410 return FC_SPP_RESP_INVL;
413 * Create session (image pair) only if requested by
414 * EST_IMG_PAIR flag and if the requestor is an initiator.
416 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
417 spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
418 if (!(fcp_parm & FCP_SPPF_INIT_FCN))
419 return FC_SPP_RESP_CONF;
420 sess = ft_sess_create(tport, rdata->ids.port_id, acl);
422 return FC_SPP_RESP_RES;
425 sess->params = fcp_parm;
426 sess->port_name = rdata->ids.port_name;
427 sess->max_frame = rdata->maxframe_size;
429 /* XXX TBD - clearing actions. unit attn, see 4.10 */
433 * OR in our service parameters with other provider (initiator), if any.
434 * TBD XXX - indicate RETRY capability?
437 fcp_parm = ntohl(spp->spp_params);
438 spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
439 return FC_SPP_RESP_ACK;
443 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
444 * @rdata: remote port private
445 * @spp_len: service parameter page length
446 * @rspp: received service parameter page (NULL for outgoing PRLI)
447 * @spp: response service parameter page
449 * Returns spp response code.
451 static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
452 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
456 mutex_lock(&ft_lport_lock);
457 ret = ft_prli_locked(rdata, spp_len, rspp, spp);
458 mutex_unlock(&ft_lport_lock);
459 FT_SESS_DBG("port_id %x flags %x ret %x\n",
460 rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
464 static void ft_sess_rcu_free(struct rcu_head *rcu)
466 struct ft_sess *sess = container_of(rcu, struct ft_sess, rcu);
468 transport_deregister_session(sess->se_sess);
472 static void ft_sess_free(struct kref *kref)
474 struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
476 call_rcu(&sess->rcu, ft_sess_rcu_free);
479 void ft_sess_put(struct ft_sess *sess)
481 int sess_held = atomic_read(&sess->kref.refcount);
484 kref_put(&sess->kref, ft_sess_free);
487 static void ft_prlo(struct fc_rport_priv *rdata)
489 struct ft_sess *sess;
490 struct ft_tport *tport;
492 mutex_lock(&ft_lport_lock);
493 tport = rcu_dereference(rdata->local_port->prov[FC_TYPE_FCP]);
495 mutex_unlock(&ft_lport_lock);
498 sess = ft_sess_delete(tport, rdata->ids.port_id);
500 mutex_unlock(&ft_lport_lock);
503 mutex_unlock(&ft_lport_lock);
504 transport_deregister_session_configfs(sess->se_sess);
505 ft_sess_put(sess); /* release from table */
507 /* XXX TBD - clearing actions. unit attn, see 4.10 */
511 * Handle incoming FCP request.
512 * Caller has verified that the frame is type FCP.
514 static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
516 struct ft_sess *sess;
517 u32 sid = fc_frame_sid(fp);
519 FT_SESS_DBG("sid %x\n", sid);
521 sess = ft_sess_get(lport, sid);
523 FT_SESS_DBG("sid %x sess lookup failed\n", sid);
524 /* TBD XXX - if FCP_CMND, send PRLO */
528 ft_recv_req(sess, fp); /* must do ft_sess_put() */
532 * Provider ops for libfc.
534 struct fc4_prov ft_prov = {
538 .module = THIS_MODULE,