mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / target / tcm_fc / tfc_sess.c
blob639fdb395fb7c7fafbf08f13da167f7ea424b94c
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/utsname.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/kthread.h>
26 #include <linux/types.h>
27 #include <linux/string.h>
28 #include <linux/configfs.h>
29 #include <linux/ctype.h>
30 #include <linux/hash.h>
31 #include <linux/rcupdate.h>
32 #include <linux/rculist.h>
33 #include <linux/kref.h>
34 #include <asm/unaligned.h>
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_host.h>
37 #include <scsi/scsi_device.h>
38 #include <scsi/scsi_cmnd.h>
39 #include <scsi/libfc.h>
41 #include <target/target_core_base.h>
42 #include <target/target_core_fabric.h>
43 #include <target/target_core_configfs.h>
44 #include <target/configfs_macros.h>
46 #include "tcm_fc.h"
48 static void ft_sess_delete_all(struct ft_tport *);
51 * Lookup or allocate target local port.
52 * Caller holds ft_lport_lock.
54 static struct ft_tport *ft_tport_create(struct fc_lport *lport)
56 struct ft_tpg *tpg;
57 struct ft_tport *tport;
58 int i;
60 tport = rcu_dereference_protected(lport->prov[FC_TYPE_FCP],
61 lockdep_is_held(&ft_lport_lock));
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 tpg->tport = tport;
72 return tport;
75 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
76 if (!tport)
77 return NULL;
79 tport->lport = lport;
80 tport->tpg = tpg;
81 tpg->tport = tport;
82 for (i = 0; i < FT_SESS_HASH_SIZE; i++)
83 INIT_HLIST_HEAD(&tport->hash[i]);
85 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], tport);
86 return tport;
90 * Delete a target local port.
91 * Caller holds ft_lport_lock.
93 static void ft_tport_delete(struct ft_tport *tport)
95 struct fc_lport *lport;
96 struct ft_tpg *tpg;
98 ft_sess_delete_all(tport);
99 lport = tport->lport;
100 BUG_ON(tport != lport->prov[FC_TYPE_FCP]);
101 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], NULL);
103 tpg = tport->tpg;
104 if (tpg) {
105 tpg->tport = NULL;
106 tport->tpg = NULL;
108 kfree_rcu(tport, rcu);
112 * Add local port.
113 * Called thru fc_lport_iterate().
115 void ft_lport_add(struct fc_lport *lport, void *arg)
117 mutex_lock(&ft_lport_lock);
118 ft_tport_create(lport);
119 mutex_unlock(&ft_lport_lock);
123 * Delete local port.
124 * Called thru fc_lport_iterate().
126 void ft_lport_del(struct fc_lport *lport, void *arg)
128 struct ft_tport *tport;
130 mutex_lock(&ft_lport_lock);
131 tport = lport->prov[FC_TYPE_FCP];
132 if (tport)
133 ft_tport_delete(tport);
134 mutex_unlock(&ft_lport_lock);
138 * Notification of local port change from libfc.
139 * Create or delete local port and associated tport.
141 int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg)
143 struct fc_lport *lport = arg;
145 switch (event) {
146 case FC_LPORT_EV_ADD:
147 ft_lport_add(lport, NULL);
148 break;
149 case FC_LPORT_EV_DEL:
150 ft_lport_del(lport, NULL);
151 break;
153 return NOTIFY_DONE;
157 * Hash function for FC_IDs.
159 static u32 ft_sess_hash(u32 port_id)
161 return hash_32(port_id, FT_SESS_HASH_BITS);
165 * Find session in local port.
166 * Sessions and hash lists are RCU-protected.
167 * A reference is taken which must be eventually freed.
169 static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
171 struct ft_tport *tport;
172 struct hlist_head *head;
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, 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;
205 head = &tport->hash[ft_sess_hash(port_id)];
206 hlist_for_each_entry_rcu(sess, head, hash)
207 if (sess->port_id == port_id)
208 return sess;
210 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
211 if (!sess)
212 return NULL;
214 sess->se_sess = transport_init_session();
215 if (IS_ERR(sess->se_sess)) {
216 kfree(sess);
217 return NULL;
219 sess->se_sess->se_node_acl = &acl->se_node_acl;
220 sess->tport = tport;
221 sess->port_id = port_id;
222 kref_init(&sess->kref); /* ref for table entry */
223 hlist_add_head_rcu(&sess->hash, head);
224 tport->sess_count++;
226 pr_debug("port_id %x sess %p\n", port_id, sess);
228 transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl,
229 sess->se_sess, sess);
230 return sess;
234 * Unhash the session.
235 * Caller holds ft_lport_lock.
237 static void ft_sess_unhash(struct ft_sess *sess)
239 struct ft_tport *tport = sess->tport;
241 hlist_del_rcu(&sess->hash);
242 BUG_ON(!tport->sess_count);
243 tport->sess_count--;
244 sess->port_id = -1;
245 sess->params = 0;
249 * Delete session from hash.
250 * Caller holds ft_lport_lock.
252 static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
254 struct hlist_head *head;
255 struct ft_sess *sess;
257 head = &tport->hash[ft_sess_hash(port_id)];
258 hlist_for_each_entry_rcu(sess, head, hash) {
259 if (sess->port_id == port_id) {
260 ft_sess_unhash(sess);
261 return sess;
264 return NULL;
268 * Delete all sessions from tport.
269 * Caller holds ft_lport_lock.
271 static void ft_sess_delete_all(struct ft_tport *tport)
273 struct hlist_head *head;
274 struct ft_sess *sess;
276 for (head = tport->hash;
277 head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
278 hlist_for_each_entry_rcu(sess, head, hash) {
279 ft_sess_unhash(sess);
280 transport_deregister_session_configfs(sess->se_sess);
281 ft_sess_put(sess); /* release from table */
287 * TCM ops for sessions.
291 * Determine whether session is allowed to be shutdown in the current context.
292 * Returns non-zero if the session should be shutdown.
294 int ft_sess_shutdown(struct se_session *se_sess)
296 struct ft_sess *sess = se_sess->fabric_sess_ptr;
298 pr_debug("port_id %x\n", sess->port_id);
299 return 1;
303 * Remove session and send PRLO.
304 * This is called when the ACL is being deleted or queue depth is changing.
306 void ft_sess_close(struct se_session *se_sess)
308 struct ft_sess *sess = se_sess->fabric_sess_ptr;
309 u32 port_id;
311 mutex_lock(&ft_lport_lock);
312 port_id = sess->port_id;
313 if (port_id == -1) {
314 mutex_unlock(&ft_lport_lock);
315 return;
317 pr_debug("port_id %x\n", port_id);
318 ft_sess_unhash(sess);
319 mutex_unlock(&ft_lport_lock);
320 transport_deregister_session_configfs(se_sess);
321 ft_sess_put(sess);
322 /* XXX Send LOGO or PRLO */
323 synchronize_rcu(); /* let transport deregister happen */
326 u32 ft_sess_get_index(struct se_session *se_sess)
328 struct ft_sess *sess = se_sess->fabric_sess_ptr;
330 return sess->port_id; /* XXX TBD probably not what is needed */
333 u32 ft_sess_get_port_name(struct se_session *se_sess,
334 unsigned char *buf, u32 len)
336 struct ft_sess *sess = se_sess->fabric_sess_ptr;
338 return ft_format_wwn(buf, len, sess->port_name);
342 * libfc ops involving sessions.
345 static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
346 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
348 struct ft_tport *tport;
349 struct ft_sess *sess;
350 struct ft_node_acl *acl;
351 u32 fcp_parm;
353 tport = ft_tport_create(rdata->local_port);
354 if (!tport)
355 goto not_target; /* not a target for this local port */
357 acl = ft_acl_get(tport->tpg, rdata);
358 if (!acl)
359 goto not_target; /* no target for this remote */
361 if (!rspp)
362 goto fill;
364 if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
365 return FC_SPP_RESP_NO_PA;
368 * If both target and initiator bits are off, the SPP is invalid.
370 fcp_parm = ntohl(rspp->spp_params);
371 if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
372 return FC_SPP_RESP_INVL;
375 * Create session (image pair) only if requested by
376 * EST_IMG_PAIR flag and if the requestor is an initiator.
378 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
379 spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
380 if (!(fcp_parm & FCP_SPPF_INIT_FCN))
381 return FC_SPP_RESP_CONF;
382 sess = ft_sess_create(tport, rdata->ids.port_id, acl);
383 if (!sess)
384 return FC_SPP_RESP_RES;
385 if (!sess->params)
386 rdata->prli_count++;
387 sess->params = fcp_parm;
388 sess->port_name = rdata->ids.port_name;
389 sess->max_frame = rdata->maxframe_size;
391 /* XXX TBD - clearing actions. unit attn, see 4.10 */
395 * OR in our service parameters with other provider (initiator), if any.
397 fill:
398 fcp_parm = ntohl(spp->spp_params);
399 fcp_parm &= ~FCP_SPPF_RETRY;
400 spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
401 return FC_SPP_RESP_ACK;
403 not_target:
404 fcp_parm = ntohl(spp->spp_params);
405 fcp_parm &= ~FCP_SPPF_TARG_FCN;
406 spp->spp_params = htonl(fcp_parm);
407 return 0;
411 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
412 * @rdata: remote port private
413 * @spp_len: service parameter page length
414 * @rspp: received service parameter page (NULL for outgoing PRLI)
415 * @spp: response service parameter page
417 * Returns spp response code.
419 static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
420 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
422 int ret;
424 mutex_lock(&ft_lport_lock);
425 ret = ft_prli_locked(rdata, spp_len, rspp, spp);
426 mutex_unlock(&ft_lport_lock);
427 pr_debug("port_id %x flags %x ret %x\n",
428 rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
429 return ret;
432 static void ft_sess_free(struct kref *kref)
434 struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
436 transport_deregister_session(sess->se_sess);
437 kfree_rcu(sess, rcu);
440 void ft_sess_put(struct ft_sess *sess)
442 int sess_held = atomic_read(&sess->kref.refcount);
444 BUG_ON(!sess_held);
445 kref_put(&sess->kref, ft_sess_free);
448 static void ft_prlo(struct fc_rport_priv *rdata)
450 struct ft_sess *sess;
451 struct ft_tport *tport;
453 mutex_lock(&ft_lport_lock);
454 tport = rcu_dereference_protected(rdata->local_port->prov[FC_TYPE_FCP],
455 lockdep_is_held(&ft_lport_lock));
457 if (!tport) {
458 mutex_unlock(&ft_lport_lock);
459 return;
461 sess = ft_sess_delete(tport, rdata->ids.port_id);
462 if (!sess) {
463 mutex_unlock(&ft_lport_lock);
464 return;
466 mutex_unlock(&ft_lport_lock);
467 transport_deregister_session_configfs(sess->se_sess);
468 ft_sess_put(sess); /* release from table */
469 rdata->prli_count--;
470 /* XXX TBD - clearing actions. unit attn, see 4.10 */
474 * Handle incoming FCP request.
475 * Caller has verified that the frame is type FCP.
477 static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
479 struct ft_sess *sess;
480 u32 sid = fc_frame_sid(fp);
482 pr_debug("sid %x\n", sid);
484 sess = ft_sess_get(lport, sid);
485 if (!sess) {
486 pr_debug("sid %x sess lookup failed\n", sid);
487 /* TBD XXX - if FCP_CMND, send PRLO */
488 fc_frame_free(fp);
489 return;
491 ft_recv_req(sess, fp); /* must do ft_sess_put() */
495 * Provider ops for libfc.
497 struct fc4_prov ft_prov = {
498 .prli = ft_prli,
499 .prlo = ft_prlo,
500 .recv = ft_recv,
501 .module = THIS_MODULE,