connector: use nlmsg_put() instead of NLMSG_PUT() macro.
[linux-2.6.git] / drivers / target / tcm_fc / tfc_sess.c
blobcb99da920068986d2c2153f6af507781561074d5
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 * Delete a target local port.
90 * Caller holds ft_lport_lock.
92 static void ft_tport_delete(struct ft_tport *tport)
94 struct fc_lport *lport;
95 struct ft_tpg *tpg;
97 ft_sess_delete_all(tport);
98 lport = tport->lport;
99 BUG_ON(tport != lport->prov[FC_TYPE_FCP]);
100 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], NULL);
102 tpg = tport->tpg;
103 if (tpg) {
104 tpg->tport = NULL;
105 tport->tpg = NULL;
107 kfree_rcu(tport, rcu);
111 * Add local port.
112 * Called thru fc_lport_iterate().
114 void ft_lport_add(struct fc_lport *lport, void *arg)
116 mutex_lock(&ft_lport_lock);
117 ft_tport_create(lport);
118 mutex_unlock(&ft_lport_lock);
122 * Delete local port.
123 * Called thru fc_lport_iterate().
125 void ft_lport_del(struct fc_lport *lport, void *arg)
127 struct ft_tport *tport;
129 mutex_lock(&ft_lport_lock);
130 tport = lport->prov[FC_TYPE_FCP];
131 if (tport)
132 ft_tport_delete(tport);
133 mutex_unlock(&ft_lport_lock);
137 * Notification of local port change from libfc.
138 * Create or delete local port and associated tport.
140 int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg)
142 struct fc_lport *lport = arg;
144 switch (event) {
145 case FC_LPORT_EV_ADD:
146 ft_lport_add(lport, NULL);
147 break;
148 case FC_LPORT_EV_DEL:
149 ft_lport_del(lport, NULL);
150 break;
152 return NOTIFY_DONE;
156 * Hash function for FC_IDs.
158 static u32 ft_sess_hash(u32 port_id)
160 return hash_32(port_id, FT_SESS_HASH_BITS);
164 * Find session in local port.
165 * Sessions and hash lists are RCU-protected.
166 * A reference is taken which must be eventually freed.
168 static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
170 struct ft_tport *tport;
171 struct hlist_head *head;
172 struct hlist_node *pos;
173 struct ft_sess *sess;
175 rcu_read_lock();
176 tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
177 if (!tport)
178 goto out;
180 head = &tport->hash[ft_sess_hash(port_id)];
181 hlist_for_each_entry_rcu(sess, pos, head, hash) {
182 if (sess->port_id == port_id) {
183 kref_get(&sess->kref);
184 rcu_read_unlock();
185 pr_debug("port_id %x found %p\n", port_id, sess);
186 return sess;
189 out:
190 rcu_read_unlock();
191 pr_debug("port_id %x not found\n", port_id);
192 return NULL;
196 * Allocate session and enter it in the hash for the local port.
197 * Caller holds ft_lport_lock.
199 static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
200 struct ft_node_acl *acl)
202 struct ft_sess *sess;
203 struct hlist_head *head;
204 struct hlist_node *pos;
206 head = &tport->hash[ft_sess_hash(port_id)];
207 hlist_for_each_entry_rcu(sess, pos, head, hash)
208 if (sess->port_id == port_id)
209 return sess;
211 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
212 if (!sess)
213 return NULL;
215 sess->se_sess = transport_init_session();
216 if (IS_ERR(sess->se_sess)) {
217 kfree(sess);
218 return NULL;
220 sess->se_sess->se_node_acl = &acl->se_node_acl;
221 sess->tport = tport;
222 sess->port_id = port_id;
223 kref_init(&sess->kref); /* ref for table entry */
224 hlist_add_head_rcu(&sess->hash, head);
225 tport->sess_count++;
227 pr_debug("port_id %x sess %p\n", port_id, sess);
229 transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl,
230 sess->se_sess, sess);
231 return sess;
235 * Unhash the session.
236 * Caller holds ft_lport_lock.
238 static void ft_sess_unhash(struct ft_sess *sess)
240 struct ft_tport *tport = sess->tport;
242 hlist_del_rcu(&sess->hash);
243 BUG_ON(!tport->sess_count);
244 tport->sess_count--;
245 sess->port_id = -1;
246 sess->params = 0;
250 * Delete session from hash.
251 * Caller holds ft_lport_lock.
253 static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
255 struct hlist_head *head;
256 struct hlist_node *pos;
257 struct ft_sess *sess;
259 head = &tport->hash[ft_sess_hash(port_id)];
260 hlist_for_each_entry_rcu(sess, pos, head, hash) {
261 if (sess->port_id == port_id) {
262 ft_sess_unhash(sess);
263 return sess;
266 return NULL;
270 * Delete all sessions from tport.
271 * Caller holds ft_lport_lock.
273 static void ft_sess_delete_all(struct ft_tport *tport)
275 struct hlist_head *head;
276 struct hlist_node *pos;
277 struct ft_sess *sess;
279 for (head = tport->hash;
280 head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
281 hlist_for_each_entry_rcu(sess, pos, head, hash) {
282 ft_sess_unhash(sess);
283 transport_deregister_session_configfs(sess->se_sess);
284 ft_sess_put(sess); /* release from table */
290 * TCM ops for sessions.
294 * Determine whether session is allowed to be shutdown in the current context.
295 * Returns non-zero if the session should be shutdown.
297 int ft_sess_shutdown(struct se_session *se_sess)
299 struct ft_sess *sess = se_sess->fabric_sess_ptr;
301 pr_debug("port_id %x\n", sess->port_id);
302 return 1;
306 * Remove session and send PRLO.
307 * This is called when the ACL is being deleted or queue depth is changing.
309 void ft_sess_close(struct se_session *se_sess)
311 struct ft_sess *sess = se_sess->fabric_sess_ptr;
312 u32 port_id;
314 mutex_lock(&ft_lport_lock);
315 port_id = sess->port_id;
316 if (port_id == -1) {
317 mutex_unlock(&ft_lport_lock);
318 return;
320 pr_debug("port_id %x\n", port_id);
321 ft_sess_unhash(sess);
322 mutex_unlock(&ft_lport_lock);
323 transport_deregister_session_configfs(se_sess);
324 ft_sess_put(sess);
325 /* XXX Send LOGO or PRLO */
326 synchronize_rcu(); /* let transport deregister happen */
329 u32 ft_sess_get_index(struct se_session *se_sess)
331 struct ft_sess *sess = se_sess->fabric_sess_ptr;
333 return sess->port_id; /* XXX TBD probably not what is needed */
336 u32 ft_sess_get_port_name(struct se_session *se_sess,
337 unsigned char *buf, u32 len)
339 struct ft_sess *sess = se_sess->fabric_sess_ptr;
341 return ft_format_wwn(buf, len, sess->port_name);
345 * libfc ops involving sessions.
348 static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
349 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
351 struct ft_tport *tport;
352 struct ft_sess *sess;
353 struct ft_node_acl *acl;
354 u32 fcp_parm;
356 tport = ft_tport_create(rdata->local_port);
357 if (!tport)
358 return 0; /* not a target for this local port */
360 acl = ft_acl_get(tport->tpg, rdata);
361 if (!acl)
362 return 0;
364 if (!rspp)
365 goto fill;
367 if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
368 return FC_SPP_RESP_NO_PA;
371 * If both target and initiator bits are off, the SPP is invalid.
373 fcp_parm = ntohl(rspp->spp_params);
374 if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
375 return FC_SPP_RESP_INVL;
378 * Create session (image pair) only if requested by
379 * EST_IMG_PAIR flag and if the requestor is an initiator.
381 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
382 spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
383 if (!(fcp_parm & FCP_SPPF_INIT_FCN))
384 return FC_SPP_RESP_CONF;
385 sess = ft_sess_create(tport, rdata->ids.port_id, acl);
386 if (!sess)
387 return FC_SPP_RESP_RES;
388 if (!sess->params)
389 rdata->prli_count++;
390 sess->params = fcp_parm;
391 sess->port_name = rdata->ids.port_name;
392 sess->max_frame = rdata->maxframe_size;
394 /* XXX TBD - clearing actions. unit attn, see 4.10 */
398 * OR in our service parameters with other provider (initiator), if any.
399 * TBD XXX - indicate RETRY capability?
401 fill:
402 fcp_parm = ntohl(spp->spp_params);
403 spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
404 return FC_SPP_RESP_ACK;
408 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
409 * @rdata: remote port private
410 * @spp_len: service parameter page length
411 * @rspp: received service parameter page (NULL for outgoing PRLI)
412 * @spp: response service parameter page
414 * Returns spp response code.
416 static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
417 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
419 int ret;
421 mutex_lock(&ft_lport_lock);
422 ret = ft_prli_locked(rdata, spp_len, rspp, spp);
423 mutex_unlock(&ft_lport_lock);
424 pr_debug("port_id %x flags %x ret %x\n",
425 rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
426 return ret;
429 static void ft_sess_rcu_free(struct rcu_head *rcu)
431 struct ft_sess *sess = container_of(rcu, struct ft_sess, rcu);
433 transport_deregister_session(sess->se_sess);
434 kfree(sess);
437 static void ft_sess_free(struct kref *kref)
439 struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
441 call_rcu(&sess->rcu, ft_sess_rcu_free);
444 void ft_sess_put(struct ft_sess *sess)
446 int sess_held = atomic_read(&sess->kref.refcount);
448 BUG_ON(!sess_held);
449 kref_put(&sess->kref, ft_sess_free);
452 static void ft_prlo(struct fc_rport_priv *rdata)
454 struct ft_sess *sess;
455 struct ft_tport *tport;
457 mutex_lock(&ft_lport_lock);
458 tport = rcu_dereference(rdata->local_port->prov[FC_TYPE_FCP]);
459 if (!tport) {
460 mutex_unlock(&ft_lport_lock);
461 return;
463 sess = ft_sess_delete(tport, rdata->ids.port_id);
464 if (!sess) {
465 mutex_unlock(&ft_lport_lock);
466 return;
468 mutex_unlock(&ft_lport_lock);
469 transport_deregister_session_configfs(sess->se_sess);
470 ft_sess_put(sess); /* release from table */
471 rdata->prli_count--;
472 /* XXX TBD - clearing actions. unit attn, see 4.10 */
476 * Handle incoming FCP request.
477 * Caller has verified that the frame is type FCP.
479 static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
481 struct ft_sess *sess;
482 u32 sid = fc_frame_sid(fp);
484 pr_debug("sid %x\n", sid);
486 sess = ft_sess_get(lport, sid);
487 if (!sess) {
488 pr_debug("sid %x sess lookup failed\n", sid);
489 /* TBD XXX - if FCP_CMND, send PRLO */
490 fc_frame_free(fp);
491 return;
493 ft_recv_req(sess, fp); /* must do ft_sess_put() */
497 * Provider ops for libfc.
499 struct fc4_prov ft_prov = {
500 .prli = ft_prli,
501 .prlo = ft_prlo,
502 .recv = ft_recv,
503 .module = THIS_MODULE,