target: Drop unused legacy target_core_fabric_ops API callers
[linux-2.6/btrfs-unstable.git] / drivers / target / tcm_fc / tfc_sess.c
bloba1d1f3c864478045a6794ef42e7143ae37f802b9
1 /*
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
11 * more details.
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_fabric.h>
44 #include <target/target_core_configfs.h>
45 #include <target/configfs_macros.h>
47 #include "tcm_fc.h"
49 static void ft_sess_delete_all(struct ft_tport *);
52 * Lookup or allocate target local port.
53 * Caller holds ft_lport_lock.
55 static struct ft_tport *ft_tport_create(struct fc_lport *lport)
57 struct ft_tpg *tpg;
58 struct ft_tport *tport;
59 int i;
61 tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
62 if (tport && tport->tpg)
63 return tport;
65 tpg = ft_lport_find_tpg(lport);
66 if (!tpg)
67 return NULL;
69 if (tport) {
70 tport->tpg = tpg;
71 return tport;
74 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
75 if (!tport)
76 return NULL;
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]);
84 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], tport);
85 return tport;
89 * Free tport via RCU.
91 static void ft_tport_rcu_free(struct rcu_head *rcu)
93 struct ft_tport *tport = container_of(rcu, struct ft_tport, rcu);
95 kfree(tport);
99 * Delete a target local port.
100 * Caller holds ft_lport_lock.
102 static void ft_tport_delete(struct ft_tport *tport)
104 struct fc_lport *lport;
105 struct ft_tpg *tpg;
107 ft_sess_delete_all(tport);
108 lport = tport->lport;
109 BUG_ON(tport != lport->prov[FC_TYPE_FCP]);
110 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], NULL);
112 tpg = tport->tpg;
113 if (tpg) {
114 tpg->tport = NULL;
115 tport->tpg = NULL;
117 call_rcu(&tport->rcu, ft_tport_rcu_free);
121 * Add local port.
122 * Called thru fc_lport_iterate().
124 void ft_lport_add(struct fc_lport *lport, void *arg)
126 mutex_lock(&ft_lport_lock);
127 ft_tport_create(lport);
128 mutex_unlock(&ft_lport_lock);
132 * Delete local port.
133 * Called thru fc_lport_iterate().
135 void ft_lport_del(struct fc_lport *lport, void *arg)
137 struct ft_tport *tport;
139 mutex_lock(&ft_lport_lock);
140 tport = lport->prov[FC_TYPE_FCP];
141 if (tport)
142 ft_tport_delete(tport);
143 mutex_unlock(&ft_lport_lock);
147 * Notification of local port change from libfc.
148 * Create or delete local port and associated tport.
150 int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg)
152 struct fc_lport *lport = arg;
154 switch (event) {
155 case FC_LPORT_EV_ADD:
156 ft_lport_add(lport, NULL);
157 break;
158 case FC_LPORT_EV_DEL:
159 ft_lport_del(lport, NULL);
160 break;
162 return NOTIFY_DONE;
166 * Hash function for FC_IDs.
168 static u32 ft_sess_hash(u32 port_id)
170 return hash_32(port_id, FT_SESS_HASH_BITS);
174 * Find session in local port.
175 * Sessions and hash lists are RCU-protected.
176 * A reference is taken which must be eventually freed.
178 static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
180 struct ft_tport *tport;
181 struct hlist_head *head;
182 struct hlist_node *pos;
183 struct ft_sess *sess;
185 rcu_read_lock();
186 tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
187 if (!tport)
188 goto out;
190 head = &tport->hash[ft_sess_hash(port_id)];
191 hlist_for_each_entry_rcu(sess, pos, head, hash) {
192 if (sess->port_id == port_id) {
193 kref_get(&sess->kref);
194 rcu_read_unlock();
195 pr_debug("port_id %x found %p\n", port_id, sess);
196 return sess;
199 out:
200 rcu_read_unlock();
201 pr_debug("port_id %x not found\n", port_id);
202 return NULL;
206 * Allocate session and enter it in the hash for the local port.
207 * Caller holds ft_lport_lock.
209 static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
210 struct ft_node_acl *acl)
212 struct ft_sess *sess;
213 struct hlist_head *head;
214 struct hlist_node *pos;
216 head = &tport->hash[ft_sess_hash(port_id)];
217 hlist_for_each_entry_rcu(sess, pos, head, hash)
218 if (sess->port_id == port_id)
219 return sess;
221 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
222 if (!sess)
223 return NULL;
225 sess->se_sess = transport_init_session();
226 if (IS_ERR(sess->se_sess)) {
227 kfree(sess);
228 return NULL;
230 sess->se_sess->se_node_acl = &acl->se_node_acl;
231 sess->tport = tport;
232 sess->port_id = port_id;
233 kref_init(&sess->kref); /* ref for table entry */
234 hlist_add_head_rcu(&sess->hash, head);
235 tport->sess_count++;
237 pr_debug("port_id %x sess %p\n", port_id, sess);
239 transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl,
240 sess->se_sess, sess);
241 return sess;
245 * Unhash the session.
246 * Caller holds ft_lport_lock.
248 static void ft_sess_unhash(struct ft_sess *sess)
250 struct ft_tport *tport = sess->tport;
252 hlist_del_rcu(&sess->hash);
253 BUG_ON(!tport->sess_count);
254 tport->sess_count--;
255 sess->port_id = -1;
256 sess->params = 0;
260 * Delete session from hash.
261 * Caller holds ft_lport_lock.
263 static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
265 struct hlist_head *head;
266 struct hlist_node *pos;
267 struct ft_sess *sess;
269 head = &tport->hash[ft_sess_hash(port_id)];
270 hlist_for_each_entry_rcu(sess, pos, head, hash) {
271 if (sess->port_id == port_id) {
272 ft_sess_unhash(sess);
273 return sess;
276 return NULL;
280 * Delete all sessions from tport.
281 * Caller holds ft_lport_lock.
283 static void ft_sess_delete_all(struct ft_tport *tport)
285 struct hlist_head *head;
286 struct hlist_node *pos;
287 struct ft_sess *sess;
289 for (head = tport->hash;
290 head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
291 hlist_for_each_entry_rcu(sess, pos, head, hash) {
292 ft_sess_unhash(sess);
293 transport_deregister_session_configfs(sess->se_sess);
294 ft_sess_put(sess); /* release from table */
300 * TCM ops for sessions.
304 * Determine whether session is allowed to be shutdown in the current context.
305 * Returns non-zero if the session should be shutdown.
307 int ft_sess_shutdown(struct se_session *se_sess)
309 struct ft_sess *sess = se_sess->fabric_sess_ptr;
311 pr_debug("port_id %x\n", sess->port_id);
312 return 1;
316 * Remove session and send PRLO.
317 * This is called when the ACL is being deleted or queue depth is changing.
319 void ft_sess_close(struct se_session *se_sess)
321 struct ft_sess *sess = se_sess->fabric_sess_ptr;
322 struct fc_lport *lport;
323 u32 port_id;
325 mutex_lock(&ft_lport_lock);
326 lport = sess->tport->lport;
327 port_id = sess->port_id;
328 if (port_id == -1) {
329 mutex_unlock(&ft_lport_lock);
330 return;
332 pr_debug("port_id %x\n", port_id);
333 ft_sess_unhash(sess);
334 mutex_unlock(&ft_lport_lock);
335 transport_deregister_session_configfs(se_sess);
336 ft_sess_put(sess);
337 /* XXX Send LOGO or PRLO */
338 synchronize_rcu(); /* let transport deregister happen */
341 u32 ft_sess_get_index(struct se_session *se_sess)
343 struct ft_sess *sess = se_sess->fabric_sess_ptr;
345 return sess->port_id; /* XXX TBD probably not what is needed */
348 u32 ft_sess_get_port_name(struct se_session *se_sess,
349 unsigned char *buf, u32 len)
351 struct ft_sess *sess = se_sess->fabric_sess_ptr;
353 return ft_format_wwn(buf, len, sess->port_name);
357 * libfc ops involving sessions.
360 static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
361 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
363 struct ft_tport *tport;
364 struct ft_sess *sess;
365 struct ft_node_acl *acl;
366 u32 fcp_parm;
368 tport = ft_tport_create(rdata->local_port);
369 if (!tport)
370 return 0; /* not a target for this local port */
372 acl = ft_acl_get(tport->tpg, rdata);
373 if (!acl)
374 return 0;
376 if (!rspp)
377 goto fill;
379 if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
380 return FC_SPP_RESP_NO_PA;
383 * If both target and initiator bits are off, the SPP is invalid.
385 fcp_parm = ntohl(rspp->spp_params);
386 if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
387 return FC_SPP_RESP_INVL;
390 * Create session (image pair) only if requested by
391 * EST_IMG_PAIR flag and if the requestor is an initiator.
393 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
394 spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
395 if (!(fcp_parm & FCP_SPPF_INIT_FCN))
396 return FC_SPP_RESP_CONF;
397 sess = ft_sess_create(tport, rdata->ids.port_id, acl);
398 if (!sess)
399 return FC_SPP_RESP_RES;
400 if (!sess->params)
401 rdata->prli_count++;
402 sess->params = fcp_parm;
403 sess->port_name = rdata->ids.port_name;
404 sess->max_frame = rdata->maxframe_size;
406 /* XXX TBD - clearing actions. unit attn, see 4.10 */
410 * OR in our service parameters with other provider (initiator), if any.
411 * TBD XXX - indicate RETRY capability?
413 fill:
414 fcp_parm = ntohl(spp->spp_params);
415 spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
416 return FC_SPP_RESP_ACK;
420 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
421 * @rdata: remote port private
422 * @spp_len: service parameter page length
423 * @rspp: received service parameter page (NULL for outgoing PRLI)
424 * @spp: response service parameter page
426 * Returns spp response code.
428 static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
429 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
431 int ret;
433 mutex_lock(&ft_lport_lock);
434 ret = ft_prli_locked(rdata, spp_len, rspp, spp);
435 mutex_unlock(&ft_lport_lock);
436 pr_debug("port_id %x flags %x ret %x\n",
437 rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
438 return ret;
441 static void ft_sess_rcu_free(struct rcu_head *rcu)
443 struct ft_sess *sess = container_of(rcu, struct ft_sess, rcu);
445 transport_deregister_session(sess->se_sess);
446 kfree(sess);
449 static void ft_sess_free(struct kref *kref)
451 struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
453 call_rcu(&sess->rcu, ft_sess_rcu_free);
456 void ft_sess_put(struct ft_sess *sess)
458 int sess_held = atomic_read(&sess->kref.refcount);
460 BUG_ON(!sess_held);
461 kref_put(&sess->kref, ft_sess_free);
464 static void ft_prlo(struct fc_rport_priv *rdata)
466 struct ft_sess *sess;
467 struct ft_tport *tport;
469 mutex_lock(&ft_lport_lock);
470 tport = rcu_dereference(rdata->local_port->prov[FC_TYPE_FCP]);
471 if (!tport) {
472 mutex_unlock(&ft_lport_lock);
473 return;
475 sess = ft_sess_delete(tport, rdata->ids.port_id);
476 if (!sess) {
477 mutex_unlock(&ft_lport_lock);
478 return;
480 mutex_unlock(&ft_lport_lock);
481 transport_deregister_session_configfs(sess->se_sess);
482 ft_sess_put(sess); /* release from table */
483 rdata->prli_count--;
484 /* XXX TBD - clearing actions. unit attn, see 4.10 */
488 * Handle incoming FCP request.
489 * Caller has verified that the frame is type FCP.
491 static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
493 struct ft_sess *sess;
494 u32 sid = fc_frame_sid(fp);
496 pr_debug("sid %x\n", sid);
498 sess = ft_sess_get(lport, sid);
499 if (!sess) {
500 pr_debug("sid %x sess lookup failed\n", sid);
501 /* TBD XXX - if FCP_CMND, send PRLO */
502 fc_frame_free(fp);
503 return;
505 ft_recv_req(sess, fp); /* must do ft_sess_put() */
509 * Provider ops for libfc.
511 struct fc4_prov ft_prov = {
512 .prli = ft_prli,
513 .prlo = ft_prlo,
514 .recv = ft_recv,
515 .module = THIS_MODULE,