staging:iio:adc:adt7310: allocate chip state with iio_dev and use iio_priv for access.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / target / tcm_fc / tfc_sess.c
bloba3bd57f2ea3204d5c079f38aba247f40aae47e25
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 <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/target_core_base.h>
50 #include <target/configfs_macros.h>
52 #include <scsi/libfc.h>
53 #include "tcm_fc.h"
55 static void ft_sess_delete_all(struct ft_tport *);
58 * Lookup or allocate target local port.
59 * Caller holds ft_lport_lock.
61 static struct ft_tport *ft_tport_create(struct fc_lport *lport)
63 struct ft_tpg *tpg;
64 struct ft_tport *tport;
65 int i;
67 tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
68 if (tport && tport->tpg)
69 return tport;
71 tpg = ft_lport_find_tpg(lport);
72 if (!tpg)
73 return NULL;
75 if (tport) {
76 tport->tpg = tpg;
77 return tport;
80 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
81 if (!tport)
82 return NULL;
84 tport->lport = lport;
85 tport->tpg = tpg;
86 tpg->tport = tport;
87 for (i = 0; i < FT_SESS_HASH_SIZE; i++)
88 INIT_HLIST_HEAD(&tport->hash[i]);
90 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], tport);
91 return tport;
95 * Free tport via RCU.
97 static void ft_tport_rcu_free(struct rcu_head *rcu)
99 struct ft_tport *tport = container_of(rcu, struct ft_tport, rcu);
101 kfree(tport);
105 * Delete a target local port.
106 * Caller holds ft_lport_lock.
108 static void ft_tport_delete(struct ft_tport *tport)
110 struct fc_lport *lport;
111 struct ft_tpg *tpg;
113 ft_sess_delete_all(tport);
114 lport = tport->lport;
115 BUG_ON(tport != lport->prov[FC_TYPE_FCP]);
116 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], NULL);
118 tpg = tport->tpg;
119 if (tpg) {
120 tpg->tport = NULL;
121 tport->tpg = NULL;
123 call_rcu(&tport->rcu, ft_tport_rcu_free);
127 * Add local port.
128 * Called thru fc_lport_iterate().
130 void ft_lport_add(struct fc_lport *lport, void *arg)
132 mutex_lock(&ft_lport_lock);
133 ft_tport_create(lport);
134 mutex_unlock(&ft_lport_lock);
138 * Delete local port.
139 * Called thru fc_lport_iterate().
141 void ft_lport_del(struct fc_lport *lport, void *arg)
143 struct ft_tport *tport;
145 mutex_lock(&ft_lport_lock);
146 tport = lport->prov[FC_TYPE_FCP];
147 if (tport)
148 ft_tport_delete(tport);
149 mutex_unlock(&ft_lport_lock);
153 * Notification of local port change from libfc.
154 * Create or delete local port and associated tport.
156 int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg)
158 struct fc_lport *lport = arg;
160 switch (event) {
161 case FC_LPORT_EV_ADD:
162 ft_lport_add(lport, NULL);
163 break;
164 case FC_LPORT_EV_DEL:
165 ft_lport_del(lport, NULL);
166 break;
168 return NOTIFY_DONE;
172 * Hash function for FC_IDs.
174 static u32 ft_sess_hash(u32 port_id)
176 return hash_32(port_id, FT_SESS_HASH_BITS);
180 * Find session in local port.
181 * Sessions and hash lists are RCU-protected.
182 * A reference is taken which must be eventually freed.
184 static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
186 struct ft_tport *tport;
187 struct hlist_head *head;
188 struct hlist_node *pos;
189 struct ft_sess *sess;
191 rcu_read_lock();
192 tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
193 if (!tport)
194 goto out;
196 head = &tport->hash[ft_sess_hash(port_id)];
197 hlist_for_each_entry_rcu(sess, pos, head, hash) {
198 if (sess->port_id == port_id) {
199 kref_get(&sess->kref);
200 rcu_read_unlock();
201 FT_SESS_DBG("port_id %x found %p\n", port_id, sess);
202 return sess;
205 out:
206 rcu_read_unlock();
207 FT_SESS_DBG("port_id %x not found\n", port_id);
208 return NULL;
212 * Allocate session and enter it in the hash for the local port.
213 * Caller holds ft_lport_lock.
215 static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
216 struct ft_node_acl *acl)
218 struct ft_sess *sess;
219 struct hlist_head *head;
220 struct hlist_node *pos;
222 head = &tport->hash[ft_sess_hash(port_id)];
223 hlist_for_each_entry_rcu(sess, pos, head, hash)
224 if (sess->port_id == port_id)
225 return sess;
227 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
228 if (!sess)
229 return NULL;
231 sess->se_sess = transport_init_session();
232 if (!sess->se_sess) {
233 kfree(sess);
234 return NULL;
236 sess->se_sess->se_node_acl = &acl->se_node_acl;
237 sess->tport = tport;
238 sess->port_id = port_id;
239 kref_init(&sess->kref); /* ref for table entry */
240 hlist_add_head_rcu(&sess->hash, head);
241 tport->sess_count++;
243 FT_SESS_DBG("port_id %x sess %p\n", port_id, sess);
245 transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl,
246 sess->se_sess, sess);
247 return sess;
251 * Unhash the session.
252 * Caller holds ft_lport_lock.
254 static void ft_sess_unhash(struct ft_sess *sess)
256 struct ft_tport *tport = sess->tport;
258 hlist_del_rcu(&sess->hash);
259 BUG_ON(!tport->sess_count);
260 tport->sess_count--;
261 sess->port_id = -1;
262 sess->params = 0;
266 * Delete session from hash.
267 * Caller holds ft_lport_lock.
269 static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
271 struct hlist_head *head;
272 struct hlist_node *pos;
273 struct ft_sess *sess;
275 head = &tport->hash[ft_sess_hash(port_id)];
276 hlist_for_each_entry_rcu(sess, pos, head, hash) {
277 if (sess->port_id == port_id) {
278 ft_sess_unhash(sess);
279 return sess;
282 return NULL;
286 * Delete all sessions from tport.
287 * Caller holds ft_lport_lock.
289 static void ft_sess_delete_all(struct ft_tport *tport)
291 struct hlist_head *head;
292 struct hlist_node *pos;
293 struct ft_sess *sess;
295 for (head = tport->hash;
296 head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
297 hlist_for_each_entry_rcu(sess, pos, head, hash) {
298 ft_sess_unhash(sess);
299 transport_deregister_session_configfs(sess->se_sess);
300 ft_sess_put(sess); /* release from table */
306 * TCM ops for sessions.
310 * Determine whether session is allowed to be shutdown in the current context.
311 * Returns non-zero if the session should be shutdown.
313 int ft_sess_shutdown(struct se_session *se_sess)
315 struct ft_sess *sess = se_sess->fabric_sess_ptr;
317 FT_SESS_DBG("port_id %x\n", sess->port_id);
318 return 1;
322 * Remove session and send PRLO.
323 * This is called when the ACL is being deleted or queue depth is changing.
325 void ft_sess_close(struct se_session *se_sess)
327 struct ft_sess *sess = se_sess->fabric_sess_ptr;
328 struct fc_lport *lport;
329 u32 port_id;
331 mutex_lock(&ft_lport_lock);
332 lport = sess->tport->lport;
333 port_id = sess->port_id;
334 if (port_id == -1) {
335 mutex_lock(&ft_lport_lock);
336 return;
338 FT_SESS_DBG("port_id %x\n", port_id);
339 ft_sess_unhash(sess);
340 mutex_unlock(&ft_lport_lock);
341 transport_deregister_session_configfs(se_sess);
342 ft_sess_put(sess);
343 /* XXX Send LOGO or PRLO */
344 synchronize_rcu(); /* let transport deregister happen */
347 void ft_sess_stop(struct se_session *se_sess, int sess_sleep, int conn_sleep)
349 struct ft_sess *sess = se_sess->fabric_sess_ptr;
351 FT_SESS_DBG("port_id %x\n", sess->port_id);
354 int ft_sess_logged_in(struct se_session *se_sess)
356 struct ft_sess *sess = se_sess->fabric_sess_ptr;
358 return sess->port_id != -1;
361 u32 ft_sess_get_index(struct se_session *se_sess)
363 struct ft_sess *sess = se_sess->fabric_sess_ptr;
365 return sess->port_id; /* XXX TBD probably not what is needed */
368 u32 ft_sess_get_port_name(struct se_session *se_sess,
369 unsigned char *buf, u32 len)
371 struct ft_sess *sess = se_sess->fabric_sess_ptr;
373 return ft_format_wwn(buf, len, sess->port_name);
376 void ft_sess_set_erl0(struct se_session *se_sess)
378 /* XXX TBD called when out of memory */
382 * libfc ops involving sessions.
385 static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
386 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
388 struct ft_tport *tport;
389 struct ft_sess *sess;
390 struct ft_node_acl *acl;
391 u32 fcp_parm;
393 tport = ft_tport_create(rdata->local_port);
394 if (!tport)
395 return 0; /* not a target for this local port */
397 acl = ft_acl_get(tport->tpg, rdata);
398 if (!acl)
399 return 0;
401 if (!rspp)
402 goto fill;
404 if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
405 return FC_SPP_RESP_NO_PA;
408 * If both target and initiator bits are off, the SPP is invalid.
410 fcp_parm = ntohl(rspp->spp_params);
411 if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
412 return FC_SPP_RESP_INVL;
415 * Create session (image pair) only if requested by
416 * EST_IMG_PAIR flag and if the requestor is an initiator.
418 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
419 spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
420 if (!(fcp_parm & FCP_SPPF_INIT_FCN))
421 return FC_SPP_RESP_CONF;
422 sess = ft_sess_create(tport, rdata->ids.port_id, acl);
423 if (!sess)
424 return FC_SPP_RESP_RES;
425 if (!sess->params)
426 rdata->prli_count++;
427 sess->params = fcp_parm;
428 sess->port_name = rdata->ids.port_name;
429 sess->max_frame = rdata->maxframe_size;
431 /* XXX TBD - clearing actions. unit attn, see 4.10 */
435 * OR in our service parameters with other provider (initiator), if any.
436 * TBD XXX - indicate RETRY capability?
438 fill:
439 fcp_parm = ntohl(spp->spp_params);
440 spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
441 return FC_SPP_RESP_ACK;
445 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
446 * @rdata: remote port private
447 * @spp_len: service parameter page length
448 * @rspp: received service parameter page (NULL for outgoing PRLI)
449 * @spp: response service parameter page
451 * Returns spp response code.
453 static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
454 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
456 int ret;
458 mutex_lock(&ft_lport_lock);
459 ret = ft_prli_locked(rdata, spp_len, rspp, spp);
460 mutex_unlock(&ft_lport_lock);
461 FT_SESS_DBG("port_id %x flags %x ret %x\n",
462 rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
463 return ret;
466 static void ft_sess_rcu_free(struct rcu_head *rcu)
468 struct ft_sess *sess = container_of(rcu, struct ft_sess, rcu);
470 transport_deregister_session(sess->se_sess);
471 kfree(sess);
474 static void ft_sess_free(struct kref *kref)
476 struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
478 call_rcu(&sess->rcu, ft_sess_rcu_free);
481 void ft_sess_put(struct ft_sess *sess)
483 int sess_held = atomic_read(&sess->kref.refcount);
485 BUG_ON(!sess_held);
486 kref_put(&sess->kref, ft_sess_free);
489 static void ft_prlo(struct fc_rport_priv *rdata)
491 struct ft_sess *sess;
492 struct ft_tport *tport;
494 mutex_lock(&ft_lport_lock);
495 tport = rcu_dereference(rdata->local_port->prov[FC_TYPE_FCP]);
496 if (!tport) {
497 mutex_unlock(&ft_lport_lock);
498 return;
500 sess = ft_sess_delete(tport, rdata->ids.port_id);
501 if (!sess) {
502 mutex_unlock(&ft_lport_lock);
503 return;
505 mutex_unlock(&ft_lport_lock);
506 transport_deregister_session_configfs(sess->se_sess);
507 ft_sess_put(sess); /* release from table */
508 rdata->prli_count--;
509 /* XXX TBD - clearing actions. unit attn, see 4.10 */
513 * Handle incoming FCP request.
514 * Caller has verified that the frame is type FCP.
516 static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
518 struct ft_sess *sess;
519 u32 sid = fc_frame_sid(fp);
521 FT_SESS_DBG("sid %x\n", sid);
523 sess = ft_sess_get(lport, sid);
524 if (!sess) {
525 FT_SESS_DBG("sid %x sess lookup failed\n", sid);
526 /* TBD XXX - if FCP_CMND, send PRLO */
527 fc_frame_free(fp);
528 return;
530 ft_recv_req(sess, fp); /* must do ft_sess_put() */
534 * Provider ops for libfc.
536 struct fc4_prov ft_prov = {
537 .prli = ft_prli,
538 .prlo = ft_prlo,
539 .recv = ft_recv,
540 .module = THIS_MODULE,