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 <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>
41 
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>
46 
47 #include "tcm_fc.h"
48 
49 static void ft_sess_delete_all(struct ft_tport *);
50 
51 /*
52  * Lookup or allocate target local port.
53  * Caller holds ft_lport_lock.
54  */
ft_tport_create(struct fc_lport * lport)55 static struct ft_tport *ft_tport_create(struct fc_lport *lport)
56 {
57 	struct ft_tpg *tpg;
58 	struct ft_tport *tport;
59 	int i;
60 
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;
65 
66 	tpg = ft_lport_find_tpg(lport);
67 	if (!tpg)
68 		return NULL;
69 
70 	if (tport) {
71 		tport->tpg = tpg;
72 		tpg->tport = tport;
73 		return tport;
74 	}
75 
76 	tport = kzalloc(sizeof(*tport), GFP_KERNEL);
77 	if (!tport)
78 		return NULL;
79 
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]);
85 
86 	rcu_assign_pointer(lport->prov[FC_TYPE_FCP], tport);
87 	return tport;
88 }
89 
90 /*
91  * Delete a target local port.
92  * Caller holds ft_lport_lock.
93  */
ft_tport_delete(struct ft_tport * tport)94 static void ft_tport_delete(struct ft_tport *tport)
95 {
96 	struct fc_lport *lport;
97 	struct ft_tpg *tpg;
98 
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);
103 
104 	tpg = tport->tpg;
105 	if (tpg) {
106 		tpg->tport = NULL;
107 		tport->tpg = NULL;
108 	}
109 	kfree_rcu(tport, rcu);
110 }
111 
112 /*
113  * Add local port.
114  * Called thru fc_lport_iterate().
115  */
ft_lport_add(struct fc_lport * lport,void * arg)116 void ft_lport_add(struct fc_lport *lport, void *arg)
117 {
118 	mutex_lock(&ft_lport_lock);
119 	ft_tport_create(lport);
120 	mutex_unlock(&ft_lport_lock);
121 }
122 
123 /*
124  * Delete local port.
125  * Called thru fc_lport_iterate().
126  */
ft_lport_del(struct fc_lport * lport,void * arg)127 void ft_lport_del(struct fc_lport *lport, void *arg)
128 {
129 	struct ft_tport *tport;
130 
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);
136 }
137 
138 /*
139  * Notification of local port change from libfc.
140  * Create or delete local port and associated tport.
141  */
ft_lport_notify(struct notifier_block * nb,unsigned long event,void * arg)142 int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg)
143 {
144 	struct fc_lport *lport = arg;
145 
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;
153 	}
154 	return NOTIFY_DONE;
155 }
156 
157 /*
158  * Hash function for FC_IDs.
159  */
ft_sess_hash(u32 port_id)160 static u32 ft_sess_hash(u32 port_id)
161 {
162 	return hash_32(port_id, FT_SESS_HASH_BITS);
163 }
164 
165 /*
166  * Find session in local port.
167  * Sessions and hash lists are RCU-protected.
168  * A reference is taken which must be eventually freed.
169  */
ft_sess_get(struct fc_lport * lport,u32 port_id)170 static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
171 {
172 	struct ft_tport *tport;
173 	struct hlist_head *head;
174 	struct hlist_node *pos;
175 	struct ft_sess *sess;
176 
177 	rcu_read_lock();
178 	tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
179 	if (!tport)
180 		goto out;
181 
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;
189 		}
190 	}
191 out:
192 	rcu_read_unlock();
193 	pr_debug("port_id %x not found\n", port_id);
194 	return NULL;
195 }
196 
197 /*
198  * Allocate session and enter it in the hash for the local port.
199  * Caller holds ft_lport_lock.
200  */
ft_sess_create(struct ft_tport * tport,u32 port_id,struct ft_node_acl * acl)201 static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
202 				      struct ft_node_acl *acl)
203 {
204 	struct ft_sess *sess;
205 	struct hlist_head *head;
206 	struct hlist_node *pos;
207 
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;
212 
213 	sess = kzalloc(sizeof(*sess), GFP_KERNEL);
214 	if (!sess)
215 		return NULL;
216 
217 	sess->se_sess = transport_init_session();
218 	if (IS_ERR(sess->se_sess)) {
219 		kfree(sess);
220 		return NULL;
221 	}
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++;
228 
229 	pr_debug("port_id %x sess %p\n", port_id, sess);
230 
231 	transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl,
232 				   sess->se_sess, sess);
233 	return sess;
234 }
235 
236 /*
237  * Unhash the session.
238  * Caller holds ft_lport_lock.
239  */
ft_sess_unhash(struct ft_sess * sess)240 static void ft_sess_unhash(struct ft_sess *sess)
241 {
242 	struct ft_tport *tport = sess->tport;
243 
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;
249 }
250 
251 /*
252  * Delete session from hash.
253  * Caller holds ft_lport_lock.
254  */
ft_sess_delete(struct ft_tport * tport,u32 port_id)255 static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
256 {
257 	struct hlist_head *head;
258 	struct hlist_node *pos;
259 	struct ft_sess *sess;
260 
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;
266 		}
267 	}
268 	return NULL;
269 }
270 
271 /*
272  * Delete all sessions from tport.
273  * Caller holds ft_lport_lock.
274  */
ft_sess_delete_all(struct ft_tport * tport)275 static void ft_sess_delete_all(struct ft_tport *tport)
276 {
277 	struct hlist_head *head;
278 	struct hlist_node *pos;
279 	struct ft_sess *sess;
280 
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 */
287 		}
288 	}
289 }
290 
291 /*
292  * TCM ops for sessions.
293  */
294 
295 /*
296  * Determine whether session is allowed to be shutdown in the current context.
297  * Returns non-zero if the session should be shutdown.
298  */
ft_sess_shutdown(struct se_session * se_sess)299 int ft_sess_shutdown(struct se_session *se_sess)
300 {
301 	struct ft_sess *sess = se_sess->fabric_sess_ptr;
302 
303 	pr_debug("port_id %x\n", sess->port_id);
304 	return 1;
305 }
306 
307 /*
308  * Remove session and send PRLO.
309  * This is called when the ACL is being deleted or queue depth is changing.
310  */
ft_sess_close(struct se_session * se_sess)311 void ft_sess_close(struct se_session *se_sess)
312 {
313 	struct ft_sess *sess = se_sess->fabric_sess_ptr;
314 	u32 port_id;
315 
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;
321 	}
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 */
329 }
330 
ft_sess_get_index(struct se_session * se_sess)331 u32 ft_sess_get_index(struct se_session *se_sess)
332 {
333 	struct ft_sess *sess = se_sess->fabric_sess_ptr;
334 
335 	return sess->port_id;	/* XXX TBD probably not what is needed */
336 }
337 
ft_sess_get_port_name(struct se_session * se_sess,unsigned char * buf,u32 len)338 u32 ft_sess_get_port_name(struct se_session *se_sess,
339 			  unsigned char *buf, u32 len)
340 {
341 	struct ft_sess *sess = se_sess->fabric_sess_ptr;
342 
343 	return ft_format_wwn(buf, len, sess->port_name);
344 }
345 
346 /*
347  * libfc ops involving sessions.
348  */
349 
ft_prli_locked(struct fc_rport_priv * rdata,u32 spp_len,const struct fc_els_spp * rspp,struct fc_els_spp * spp)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)
352 {
353 	struct ft_tport *tport;
354 	struct ft_sess *sess;
355 	struct ft_node_acl *acl;
356 	u32 fcp_parm;
357 
358 	tport = ft_tport_create(rdata->local_port);
359 	if (!tport)
360 		goto not_target;	/* not a target for this local port */
361 
362 	acl = ft_acl_get(tport->tpg, rdata);
363 	if (!acl)
364 		goto not_target;	/* no target for this remote */
365 
366 	if (!rspp)
367 		goto fill;
368 
369 	if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
370 		return FC_SPP_RESP_NO_PA;
371 
372 	/*
373 	 * If both target and initiator bits are off, the SPP is invalid.
374 	 */
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;
378 
379 	/*
380 	 * Create session (image pair) only if requested by
381 	 * EST_IMG_PAIR flag and if the requestor is an initiator.
382 	 */
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;
395 
396 		/* XXX TBD - clearing actions.  unit attn, see 4.10 */
397 	}
398 
399 	/*
400 	 * OR in our service parameters with other provider (initiator), if any.
401 	 */
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;
407 
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;
413 }
414 
415 /**
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
421  *
422  * Returns spp response code.
423  */
ft_prli(struct fc_rport_priv * rdata,u32 spp_len,const struct fc_els_spp * rspp,struct fc_els_spp * spp)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)
426 {
427 	int ret;
428 
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;
435 }
436 
ft_sess_rcu_free(struct rcu_head * rcu)437 static void ft_sess_rcu_free(struct rcu_head *rcu)
438 {
439 	struct ft_sess *sess = container_of(rcu, struct ft_sess, rcu);
440 
441 	kfree(sess);
442 }
443 
ft_sess_free(struct kref * kref)444 static void ft_sess_free(struct kref *kref)
445 {
446 	struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
447 
448 	transport_deregister_session(sess->se_sess);
449 	call_rcu(&sess->rcu, ft_sess_rcu_free);
450 }
451 
ft_sess_put(struct ft_sess * sess)452 void ft_sess_put(struct ft_sess *sess)
453 {
454 	int sess_held = atomic_read(&sess->kref.refcount);
455 
456 	BUG_ON(!sess_held);
457 	kref_put(&sess->kref, ft_sess_free);
458 }
459 
ft_prlo(struct fc_rport_priv * rdata)460 static void ft_prlo(struct fc_rport_priv *rdata)
461 {
462 	struct ft_sess *sess;
463 	struct ft_tport *tport;
464 
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;
470 	}
471 	sess = ft_sess_delete(tport, rdata->ids.port_id);
472 	if (!sess) {
473 		mutex_unlock(&ft_lport_lock);
474 		return;
475 	}
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 */
481 }
482 
483 /*
484  * Handle incoming FCP request.
485  * Caller has verified that the frame is type FCP.
486  */
ft_recv(struct fc_lport * lport,struct fc_frame * fp)487 static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
488 {
489 	struct ft_sess *sess;
490 	u32 sid = fc_frame_sid(fp);
491 
492 	pr_debug("sid %x\n", sid);
493 
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;
500 	}
501 	ft_recv_req(sess, fp);	/* must do ft_sess_put() */
502 }
503 
504 /*
505  * Provider ops for libfc.
506  */
507 struct fc4_prov ft_prov = {
508 	.prli = ft_prli,
509 	.prlo = ft_prlo,
510 	.recv = ft_recv,
511 	.module = THIS_MODULE,
512 };
513