xref: /openbmc/linux/drivers/target/tcm_fc/tfc_sess.c (revision ba929992)
1 /*
2  * Copyright (c) 2010 Cisco Systems, Inc.
3  *
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.
7  *
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.
12  *
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.
16  */
17 
18 /* XXX TBD some includes may be extraneous */
19 
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/libfc.h>
36 
37 #include <target/target_core_base.h>
38 #include <target/target_core_fabric.h>
39 #include <target/target_core_configfs.h>
40 #include <target/configfs_macros.h>
41 
42 #include "tcm_fc.h"
43 
44 static void ft_sess_delete_all(struct ft_tport *);
45 
46 /*
47  * Lookup or allocate target local port.
48  * Caller holds ft_lport_lock.
49  */
50 static struct ft_tport *ft_tport_get(struct fc_lport *lport)
51 {
52 	struct ft_tpg *tpg;
53 	struct ft_tport *tport;
54 	int i;
55 
56 	tport = rcu_dereference_protected(lport->prov[FC_TYPE_FCP],
57 					  lockdep_is_held(&ft_lport_lock));
58 	if (tport && tport->tpg)
59 		return tport;
60 
61 	tpg = ft_lport_find_tpg(lport);
62 	if (!tpg)
63 		return NULL;
64 
65 	if (tport) {
66 		tport->tpg = tpg;
67 		tpg->tport = tport;
68 		return tport;
69 	}
70 
71 	tport = kzalloc(sizeof(*tport), GFP_KERNEL);
72 	if (!tport)
73 		return NULL;
74 
75 	tport->lport = lport;
76 	tport->tpg = tpg;
77 	tpg->tport = tport;
78 	for (i = 0; i < FT_SESS_HASH_SIZE; i++)
79 		INIT_HLIST_HEAD(&tport->hash[i]);
80 
81 	rcu_assign_pointer(lport->prov[FC_TYPE_FCP], tport);
82 	return tport;
83 }
84 
85 /*
86  * Delete a target local port.
87  * Caller holds ft_lport_lock.
88  */
89 static void ft_tport_delete(struct ft_tport *tport)
90 {
91 	struct fc_lport *lport;
92 	struct ft_tpg *tpg;
93 
94 	ft_sess_delete_all(tport);
95 	lport = tport->lport;
96 	BUG_ON(tport != lport->prov[FC_TYPE_FCP]);
97 	RCU_INIT_POINTER(lport->prov[FC_TYPE_FCP], NULL);
98 
99 	tpg = tport->tpg;
100 	if (tpg) {
101 		tpg->tport = NULL;
102 		tport->tpg = NULL;
103 	}
104 	kfree_rcu(tport, rcu);
105 }
106 
107 /*
108  * Add local port.
109  * Called thru fc_lport_iterate().
110  */
111 void ft_lport_add(struct fc_lport *lport, void *arg)
112 {
113 	mutex_lock(&ft_lport_lock);
114 	ft_tport_get(lport);
115 	mutex_unlock(&ft_lport_lock);
116 }
117 
118 /*
119  * Delete local port.
120  * Called thru fc_lport_iterate().
121  */
122 void ft_lport_del(struct fc_lport *lport, void *arg)
123 {
124 	struct ft_tport *tport;
125 
126 	mutex_lock(&ft_lport_lock);
127 	tport = lport->prov[FC_TYPE_FCP];
128 	if (tport)
129 		ft_tport_delete(tport);
130 	mutex_unlock(&ft_lport_lock);
131 }
132 
133 /*
134  * Notification of local port change from libfc.
135  * Create or delete local port and associated tport.
136  */
137 int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg)
138 {
139 	struct fc_lport *lport = arg;
140 
141 	switch (event) {
142 	case FC_LPORT_EV_ADD:
143 		ft_lport_add(lport, NULL);
144 		break;
145 	case FC_LPORT_EV_DEL:
146 		ft_lport_del(lport, NULL);
147 		break;
148 	}
149 	return NOTIFY_DONE;
150 }
151 
152 /*
153  * Hash function for FC_IDs.
154  */
155 static u32 ft_sess_hash(u32 port_id)
156 {
157 	return hash_32(port_id, FT_SESS_HASH_BITS);
158 }
159 
160 /*
161  * Find session in local port.
162  * Sessions and hash lists are RCU-protected.
163  * A reference is taken which must be eventually freed.
164  */
165 static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
166 {
167 	struct ft_tport *tport;
168 	struct hlist_head *head;
169 	struct ft_sess *sess;
170 
171 	rcu_read_lock();
172 	tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
173 	if (!tport)
174 		goto out;
175 
176 	head = &tport->hash[ft_sess_hash(port_id)];
177 	hlist_for_each_entry_rcu(sess, head, hash) {
178 		if (sess->port_id == port_id) {
179 			kref_get(&sess->kref);
180 			rcu_read_unlock();
181 			pr_debug("port_id %x found %p\n", port_id, sess);
182 			return sess;
183 		}
184 	}
185 out:
186 	rcu_read_unlock();
187 	pr_debug("port_id %x not found\n", port_id);
188 	return NULL;
189 }
190 
191 /*
192  * Allocate session and enter it in the hash for the local port.
193  * Caller holds ft_lport_lock.
194  */
195 static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
196 				      struct ft_node_acl *acl)
197 {
198 	struct ft_sess *sess;
199 	struct hlist_head *head;
200 
201 	head = &tport->hash[ft_sess_hash(port_id)];
202 	hlist_for_each_entry_rcu(sess, head, hash)
203 		if (sess->port_id == port_id)
204 			return sess;
205 
206 	sess = kzalloc(sizeof(*sess), GFP_KERNEL);
207 	if (!sess)
208 		return NULL;
209 
210 	sess->se_sess = transport_init_session_tags(TCM_FC_DEFAULT_TAGS,
211 						    sizeof(struct ft_cmd),
212 						    TARGET_PROT_NORMAL);
213 	if (IS_ERR(sess->se_sess)) {
214 		kfree(sess);
215 		return NULL;
216 	}
217 	sess->se_sess->se_node_acl = &acl->se_node_acl;
218 	sess->tport = tport;
219 	sess->port_id = port_id;
220 	kref_init(&sess->kref);	/* ref for table entry */
221 	hlist_add_head_rcu(&sess->hash, head);
222 	tport->sess_count++;
223 
224 	pr_debug("port_id %x sess %p\n", port_id, sess);
225 
226 	transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl,
227 				   sess->se_sess, sess);
228 	return sess;
229 }
230 
231 /*
232  * Unhash the session.
233  * Caller holds ft_lport_lock.
234  */
235 static void ft_sess_unhash(struct ft_sess *sess)
236 {
237 	struct ft_tport *tport = sess->tport;
238 
239 	hlist_del_rcu(&sess->hash);
240 	BUG_ON(!tport->sess_count);
241 	tport->sess_count--;
242 	sess->port_id = -1;
243 	sess->params = 0;
244 }
245 
246 /*
247  * Delete session from hash.
248  * Caller holds ft_lport_lock.
249  */
250 static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
251 {
252 	struct hlist_head *head;
253 	struct ft_sess *sess;
254 
255 	head = &tport->hash[ft_sess_hash(port_id)];
256 	hlist_for_each_entry_rcu(sess, head, hash) {
257 		if (sess->port_id == port_id) {
258 			ft_sess_unhash(sess);
259 			return sess;
260 		}
261 	}
262 	return NULL;
263 }
264 
265 /*
266  * Delete all sessions from tport.
267  * Caller holds ft_lport_lock.
268  */
269 static void ft_sess_delete_all(struct ft_tport *tport)
270 {
271 	struct hlist_head *head;
272 	struct ft_sess *sess;
273 
274 	for (head = tport->hash;
275 	     head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
276 		hlist_for_each_entry_rcu(sess, head, hash) {
277 			ft_sess_unhash(sess);
278 			transport_deregister_session_configfs(sess->se_sess);
279 			ft_sess_put(sess);	/* release from table */
280 		}
281 	}
282 }
283 
284 /*
285  * TCM ops for sessions.
286  */
287 
288 /*
289  * Determine whether session is allowed to be shutdown in the current context.
290  * Returns non-zero if the session should be shutdown.
291  */
292 int ft_sess_shutdown(struct se_session *se_sess)
293 {
294 	struct ft_sess *sess = se_sess->fabric_sess_ptr;
295 
296 	pr_debug("port_id %x\n", sess->port_id);
297 	return 1;
298 }
299 
300 /*
301  * Remove session and send PRLO.
302  * This is called when the ACL is being deleted or queue depth is changing.
303  */
304 void ft_sess_close(struct se_session *se_sess)
305 {
306 	struct ft_sess *sess = se_sess->fabric_sess_ptr;
307 	u32 port_id;
308 
309 	mutex_lock(&ft_lport_lock);
310 	port_id = sess->port_id;
311 	if (port_id == -1) {
312 		mutex_unlock(&ft_lport_lock);
313 		return;
314 	}
315 	pr_debug("port_id %x\n", port_id);
316 	ft_sess_unhash(sess);
317 	mutex_unlock(&ft_lport_lock);
318 	transport_deregister_session_configfs(se_sess);
319 	ft_sess_put(sess);
320 	/* XXX Send LOGO or PRLO */
321 	synchronize_rcu();		/* let transport deregister happen */
322 }
323 
324 u32 ft_sess_get_index(struct se_session *se_sess)
325 {
326 	struct ft_sess *sess = se_sess->fabric_sess_ptr;
327 
328 	return sess->port_id;	/* XXX TBD probably not what is needed */
329 }
330 
331 u32 ft_sess_get_port_name(struct se_session *se_sess,
332 			  unsigned char *buf, u32 len)
333 {
334 	struct ft_sess *sess = se_sess->fabric_sess_ptr;
335 
336 	return ft_format_wwn(buf, len, sess->port_name);
337 }
338 
339 /*
340  * libfc ops involving sessions.
341  */
342 
343 static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
344 			  const struct fc_els_spp *rspp, struct fc_els_spp *spp)
345 {
346 	struct ft_tport *tport;
347 	struct ft_sess *sess;
348 	struct ft_node_acl *acl;
349 	u32 fcp_parm;
350 
351 	tport = ft_tport_get(rdata->local_port);
352 	if (!tport)
353 		goto not_target;	/* not a target for this local port */
354 
355 	acl = ft_acl_get(tport->tpg, rdata);
356 	if (!acl)
357 		goto not_target;	/* no target for this remote */
358 
359 	if (!rspp)
360 		goto fill;
361 
362 	if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
363 		return FC_SPP_RESP_NO_PA;
364 
365 	/*
366 	 * If both target and initiator bits are off, the SPP is invalid.
367 	 */
368 	fcp_parm = ntohl(rspp->spp_params);
369 	if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
370 		return FC_SPP_RESP_INVL;
371 
372 	/*
373 	 * Create session (image pair) only if requested by
374 	 * EST_IMG_PAIR flag and if the requestor is an initiator.
375 	 */
376 	if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
377 		spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
378 		if (!(fcp_parm & FCP_SPPF_INIT_FCN))
379 			return FC_SPP_RESP_CONF;
380 		sess = ft_sess_create(tport, rdata->ids.port_id, acl);
381 		if (!sess)
382 			return FC_SPP_RESP_RES;
383 		if (!sess->params)
384 			rdata->prli_count++;
385 		sess->params = fcp_parm;
386 		sess->port_name = rdata->ids.port_name;
387 		sess->max_frame = rdata->maxframe_size;
388 
389 		/* XXX TBD - clearing actions.  unit attn, see 4.10 */
390 	}
391 
392 	/*
393 	 * OR in our service parameters with other provider (initiator), if any.
394 	 */
395 fill:
396 	fcp_parm = ntohl(spp->spp_params);
397 	fcp_parm &= ~FCP_SPPF_RETRY;
398 	spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
399 	return FC_SPP_RESP_ACK;
400 
401 not_target:
402 	fcp_parm = ntohl(spp->spp_params);
403 	fcp_parm &= ~FCP_SPPF_TARG_FCN;
404 	spp->spp_params = htonl(fcp_parm);
405 	return 0;
406 }
407 
408 /**
409  * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
410  * @rdata: remote port private
411  * @spp_len: service parameter page length
412  * @rspp: received service parameter page (NULL for outgoing PRLI)
413  * @spp: response service parameter page
414  *
415  * Returns spp response code.
416  */
417 static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
418 		   const struct fc_els_spp *rspp, struct fc_els_spp *spp)
419 {
420 	int ret;
421 
422 	mutex_lock(&ft_lport_lock);
423 	ret = ft_prli_locked(rdata, spp_len, rspp, spp);
424 	mutex_unlock(&ft_lport_lock);
425 	pr_debug("port_id %x flags %x ret %x\n",
426 	       rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
427 	return ret;
428 }
429 
430 static void ft_sess_free(struct kref *kref)
431 {
432 	struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
433 
434 	transport_deregister_session(sess->se_sess);
435 	kfree_rcu(sess, rcu);
436 }
437 
438 void ft_sess_put(struct ft_sess *sess)
439 {
440 	int sess_held = atomic_read(&sess->kref.refcount);
441 
442 	BUG_ON(!sess_held);
443 	kref_put(&sess->kref, ft_sess_free);
444 }
445 
446 static void ft_prlo(struct fc_rport_priv *rdata)
447 {
448 	struct ft_sess *sess;
449 	struct ft_tport *tport;
450 
451 	mutex_lock(&ft_lport_lock);
452 	tport = rcu_dereference_protected(rdata->local_port->prov[FC_TYPE_FCP],
453 					  lockdep_is_held(&ft_lport_lock));
454 
455 	if (!tport) {
456 		mutex_unlock(&ft_lport_lock);
457 		return;
458 	}
459 	sess = ft_sess_delete(tport, rdata->ids.port_id);
460 	if (!sess) {
461 		mutex_unlock(&ft_lport_lock);
462 		return;
463 	}
464 	mutex_unlock(&ft_lport_lock);
465 	transport_deregister_session_configfs(sess->se_sess);
466 	ft_sess_put(sess);		/* release from table */
467 	rdata->prli_count--;
468 	/* XXX TBD - clearing actions.  unit attn, see 4.10 */
469 }
470 
471 /*
472  * Handle incoming FCP request.
473  * Caller has verified that the frame is type FCP.
474  */
475 static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
476 {
477 	struct ft_sess *sess;
478 	u32 sid = fc_frame_sid(fp);
479 
480 	pr_debug("sid %x\n", sid);
481 
482 	sess = ft_sess_get(lport, sid);
483 	if (!sess) {
484 		pr_debug("sid %x sess lookup failed\n", sid);
485 		/* TBD XXX - if FCP_CMND, send PRLO */
486 		fc_frame_free(fp);
487 		return;
488 	}
489 	ft_recv_req(sess, fp);	/* must do ft_sess_put() */
490 }
491 
492 /*
493  * Provider ops for libfc.
494  */
495 struct fc4_prov ft_prov = {
496 	.prli = ft_prli,
497 	.prlo = ft_prlo,
498 	.recv = ft_recv,
499 	.module = THIS_MODULE,
500 };
501