Linux 3.4.102
[linux/fpc-iii.git] / drivers / target / tcm_fc / tfc_sess.c
blob5ac8d5610a4a83cd40d5da5fd445af1385d752d1
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_protected(lport->prov[FC_TYPE_FCP],
62 lockdep_is_held(&ft_lport_lock));
63 if (tport && tport->tpg)
64 return tport;
66 tpg = ft_lport_find_tpg(lport);
67 if (!tpg)
68 return NULL;
70 if (tport) {
71 tport->tpg = tpg;
72 tpg->tport = tport;
73 return tport;
76 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
77 if (!tport)
78 return NULL;
80 tport->lport = lport;
81 tport->tpg = tpg;
82 tpg->tport = tport;
83 for (i = 0; i < FT_SESS_HASH_SIZE; i++)
84 INIT_HLIST_HEAD(&tport->hash[i]);
86 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], tport);
87 return tport;
91 * Delete a target local port.
92 * Caller holds ft_lport_lock.
94 static void ft_tport_delete(struct ft_tport *tport)
96 struct fc_lport *lport;
97 struct ft_tpg *tpg;
99 ft_sess_delete_all(tport);
100 lport = tport->lport;
101 BUG_ON(tport != lport->prov[FC_TYPE_FCP]);
102 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], NULL);
104 tpg = tport->tpg;
105 if (tpg) {
106 tpg->tport = NULL;
107 tport->tpg = NULL;
109 kfree_rcu(tport, rcu);
113 * Add local port.
114 * Called thru fc_lport_iterate().
116 void ft_lport_add(struct fc_lport *lport, void *arg)
118 mutex_lock(&ft_lport_lock);
119 ft_tport_create(lport);
120 mutex_unlock(&ft_lport_lock);
124 * Delete local port.
125 * Called thru fc_lport_iterate().
127 void ft_lport_del(struct fc_lport *lport, void *arg)
129 struct ft_tport *tport;
131 mutex_lock(&ft_lport_lock);
132 tport = lport->prov[FC_TYPE_FCP];
133 if (tport)
134 ft_tport_delete(tport);
135 mutex_unlock(&ft_lport_lock);
139 * Notification of local port change from libfc.
140 * Create or delete local port and associated tport.
142 int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg)
144 struct fc_lport *lport = arg;
146 switch (event) {
147 case FC_LPORT_EV_ADD:
148 ft_lport_add(lport, NULL);
149 break;
150 case FC_LPORT_EV_DEL:
151 ft_lport_del(lport, NULL);
152 break;
154 return NOTIFY_DONE;
158 * Hash function for FC_IDs.
160 static u32 ft_sess_hash(u32 port_id)
162 return hash_32(port_id, FT_SESS_HASH_BITS);
166 * Find session in local port.
167 * Sessions and hash lists are RCU-protected.
168 * A reference is taken which must be eventually freed.
170 static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
172 struct ft_tport *tport;
173 struct hlist_head *head;
174 struct hlist_node *pos;
175 struct ft_sess *sess;
177 rcu_read_lock();
178 tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
179 if (!tport)
180 goto out;
182 head = &tport->hash[ft_sess_hash(port_id)];
183 hlist_for_each_entry_rcu(sess, pos, head, hash) {
184 if (sess->port_id == port_id) {
185 kref_get(&sess->kref);
186 rcu_read_unlock();
187 pr_debug("port_id %x found %p\n", port_id, sess);
188 return sess;
191 out:
192 rcu_read_unlock();
193 pr_debug("port_id %x not found\n", port_id);
194 return NULL;
198 * Allocate session and enter it in the hash for the local port.
199 * Caller holds ft_lport_lock.
201 static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
202 struct ft_node_acl *acl)
204 struct ft_sess *sess;
205 struct hlist_head *head;
206 struct hlist_node *pos;
208 head = &tport->hash[ft_sess_hash(port_id)];
209 hlist_for_each_entry_rcu(sess, pos, head, hash)
210 if (sess->port_id == port_id)
211 return sess;
213 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
214 if (!sess)
215 return NULL;
217 sess->se_sess = transport_init_session();
218 if (IS_ERR(sess->se_sess)) {
219 kfree(sess);
220 return NULL;
222 sess->se_sess->se_node_acl = &acl->se_node_acl;
223 sess->tport = tport;
224 sess->port_id = port_id;
225 kref_init(&sess->kref); /* ref for table entry */
226 hlist_add_head_rcu(&sess->hash, head);
227 tport->sess_count++;
229 pr_debug("port_id %x sess %p\n", port_id, sess);
231 transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl,
232 sess->se_sess, sess);
233 return sess;
237 * Unhash the session.
238 * Caller holds ft_lport_lock.
240 static void ft_sess_unhash(struct ft_sess *sess)
242 struct ft_tport *tport = sess->tport;
244 hlist_del_rcu(&sess->hash);
245 BUG_ON(!tport->sess_count);
246 tport->sess_count--;
247 sess->port_id = -1;
248 sess->params = 0;
252 * Delete session from hash.
253 * Caller holds ft_lport_lock.
255 static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
257 struct hlist_head *head;
258 struct hlist_node *pos;
259 struct ft_sess *sess;
261 head = &tport->hash[ft_sess_hash(port_id)];
262 hlist_for_each_entry_rcu(sess, pos, head, hash) {
263 if (sess->port_id == port_id) {
264 ft_sess_unhash(sess);
265 return sess;
268 return NULL;
272 * Delete all sessions from tport.
273 * Caller holds ft_lport_lock.
275 static void ft_sess_delete_all(struct ft_tport *tport)
277 struct hlist_head *head;
278 struct hlist_node *pos;
279 struct ft_sess *sess;
281 for (head = tport->hash;
282 head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
283 hlist_for_each_entry_rcu(sess, pos, head, hash) {
284 ft_sess_unhash(sess);
285 transport_deregister_session_configfs(sess->se_sess);
286 ft_sess_put(sess); /* release from table */
292 * TCM ops for sessions.
296 * Determine whether session is allowed to be shutdown in the current context.
297 * Returns non-zero if the session should be shutdown.
299 int ft_sess_shutdown(struct se_session *se_sess)
301 struct ft_sess *sess = se_sess->fabric_sess_ptr;
303 pr_debug("port_id %x\n", sess->port_id);
304 return 1;
308 * Remove session and send PRLO.
309 * This is called when the ACL is being deleted or queue depth is changing.
311 void ft_sess_close(struct se_session *se_sess)
313 struct ft_sess *sess = se_sess->fabric_sess_ptr;
314 u32 port_id;
316 mutex_lock(&ft_lport_lock);
317 port_id = sess->port_id;
318 if (port_id == -1) {
319 mutex_unlock(&ft_lport_lock);
320 return;
322 pr_debug("port_id %x\n", port_id);
323 ft_sess_unhash(sess);
324 mutex_unlock(&ft_lport_lock);
325 transport_deregister_session_configfs(se_sess);
326 ft_sess_put(sess);
327 /* XXX Send LOGO or PRLO */
328 synchronize_rcu(); /* let transport deregister happen */
331 u32 ft_sess_get_index(struct se_session *se_sess)
333 struct ft_sess *sess = se_sess->fabric_sess_ptr;
335 return sess->port_id; /* XXX TBD probably not what is needed */
338 u32 ft_sess_get_port_name(struct se_session *se_sess,
339 unsigned char *buf, u32 len)
341 struct ft_sess *sess = se_sess->fabric_sess_ptr;
343 return ft_format_wwn(buf, len, sess->port_name);
347 * libfc ops involving sessions.
350 static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
351 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
353 struct ft_tport *tport;
354 struct ft_sess *sess;
355 struct ft_node_acl *acl;
356 u32 fcp_parm;
358 tport = ft_tport_create(rdata->local_port);
359 if (!tport)
360 goto not_target; /* not a target for this local port */
362 acl = ft_acl_get(tport->tpg, rdata);
363 if (!acl)
364 goto not_target; /* no target for this remote */
366 if (!rspp)
367 goto fill;
369 if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
370 return FC_SPP_RESP_NO_PA;
373 * If both target and initiator bits are off, the SPP is invalid.
375 fcp_parm = ntohl(rspp->spp_params);
376 if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
377 return FC_SPP_RESP_INVL;
380 * Create session (image pair) only if requested by
381 * EST_IMG_PAIR flag and if the requestor is an initiator.
383 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
384 spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
385 if (!(fcp_parm & FCP_SPPF_INIT_FCN))
386 return FC_SPP_RESP_CONF;
387 sess = ft_sess_create(tport, rdata->ids.port_id, acl);
388 if (!sess)
389 return FC_SPP_RESP_RES;
390 if (!sess->params)
391 rdata->prli_count++;
392 sess->params = fcp_parm;
393 sess->port_name = rdata->ids.port_name;
394 sess->max_frame = rdata->maxframe_size;
396 /* XXX TBD - clearing actions. unit attn, see 4.10 */
400 * OR in our service parameters with other provider (initiator), if any.
402 fill:
403 fcp_parm = ntohl(spp->spp_params);
404 fcp_parm &= ~FCP_SPPF_RETRY;
405 spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
406 return FC_SPP_RESP_ACK;
408 not_target:
409 fcp_parm = ntohl(spp->spp_params);
410 fcp_parm &= ~FCP_SPPF_TARG_FCN;
411 spp->spp_params = htonl(fcp_parm);
412 return 0;
416 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
417 * @rdata: remote port private
418 * @spp_len: service parameter page length
419 * @rspp: received service parameter page (NULL for outgoing PRLI)
420 * @spp: response service parameter page
422 * Returns spp response code.
424 static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
425 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
427 int ret;
429 mutex_lock(&ft_lport_lock);
430 ret = ft_prli_locked(rdata, spp_len, rspp, spp);
431 mutex_unlock(&ft_lport_lock);
432 pr_debug("port_id %x flags %x ret %x\n",
433 rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
434 return ret;
437 static void ft_sess_rcu_free(struct rcu_head *rcu)
439 struct ft_sess *sess = container_of(rcu, struct ft_sess, rcu);
441 kfree(sess);
444 static void ft_sess_free(struct kref *kref)
446 struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
448 transport_deregister_session(sess->se_sess);
449 call_rcu(&sess->rcu, ft_sess_rcu_free);
452 void ft_sess_put(struct ft_sess *sess)
454 int sess_held = atomic_read(&sess->kref.refcount);
456 BUG_ON(!sess_held);
457 kref_put(&sess->kref, ft_sess_free);
460 static void ft_prlo(struct fc_rport_priv *rdata)
462 struct ft_sess *sess;
463 struct ft_tport *tport;
465 mutex_lock(&ft_lport_lock);
466 tport = rcu_dereference(rdata->local_port->prov[FC_TYPE_FCP]);
467 if (!tport) {
468 mutex_unlock(&ft_lport_lock);
469 return;
471 sess = ft_sess_delete(tport, rdata->ids.port_id);
472 if (!sess) {
473 mutex_unlock(&ft_lport_lock);
474 return;
476 mutex_unlock(&ft_lport_lock);
477 transport_deregister_session_configfs(sess->se_sess);
478 ft_sess_put(sess); /* release from table */
479 rdata->prli_count--;
480 /* XXX TBD - clearing actions. unit attn, see 4.10 */
484 * Handle incoming FCP request.
485 * Caller has verified that the frame is type FCP.
487 static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
489 struct ft_sess *sess;
490 u32 sid = fc_frame_sid(fp);
492 pr_debug("sid %x\n", sid);
494 sess = ft_sess_get(lport, sid);
495 if (!sess) {
496 pr_debug("sid %x sess lookup failed\n", sid);
497 /* TBD XXX - if FCP_CMND, send PRLO */
498 fc_frame_free(fp);
499 return;
501 ft_recv_req(sess, fp); /* must do ft_sess_put() */
505 * Provider ops for libfc.
507 struct fc4_prov ft_prov = {
508 .prli = ft_prli,
509 .prlo = ft_prlo,
510 .recv = ft_recv,
511 .module = THIS_MODULE,