blob: eff512b5a2a0c980191d1b37cf423debbe87524c [file] [log] [blame]
Kiran Patil3699d922011-04-18 16:24:14 -07001/*
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>
Kiran Patil3699d922011-04-18 16:24:14 -070022#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>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050043#include <target/target_core_fabric.h>
Kiran Patil3699d922011-04-18 16:24:14 -070044#include <target/target_core_configfs.h>
Kiran Patil3699d922011-04-18 16:24:14 -070045#include <target/configfs_macros.h>
46
Kiran Patil3699d922011-04-18 16:24:14 -070047#include "tcm_fc.h"
48
49static void ft_sess_delete_all(struct ft_tport *);
50
51/*
52 * Lookup or allocate target local port.
53 * Caller holds ft_lport_lock.
54 */
55static 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(lport->prov[FC_TYPE_FCP]);
62 if (tport && tport->tpg)
63 return tport;
64
65 tpg = ft_lport_find_tpg(lport);
66 if (!tpg)
67 return NULL;
68
69 if (tport) {
70 tport->tpg = tpg;
71 return tport;
72 }
73
74 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
75 if (!tport)
76 return NULL;
77
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]);
83
84 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], tport);
85 return tport;
86}
87
88/*
Kiran Patil3699d922011-04-18 16:24:14 -070089 * Delete a target local port.
90 * Caller holds ft_lport_lock.
91 */
92static void ft_tport_delete(struct ft_tport *tport)
93{
94 struct fc_lport *lport;
95 struct ft_tpg *tpg;
96
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);
101
102 tpg = tport->tpg;
103 if (tpg) {
104 tpg->tport = NULL;
105 tport->tpg = NULL;
106 }
Paul E. McKenneya6c76da2012-01-06 17:02:13 -0800107 kfree_rcu(tport, rcu);
Kiran Patil3699d922011-04-18 16:24:14 -0700108}
109
110/*
111 * Add local port.
112 * Called thru fc_lport_iterate().
113 */
114void ft_lport_add(struct fc_lport *lport, void *arg)
115{
116 mutex_lock(&ft_lport_lock);
117 ft_tport_create(lport);
118 mutex_unlock(&ft_lport_lock);
119}
120
121/*
122 * Delete local port.
123 * Called thru fc_lport_iterate().
124 */
125void ft_lport_del(struct fc_lport *lport, void *arg)
126{
127 struct ft_tport *tport;
128
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);
134}
135
136/*
137 * Notification of local port change from libfc.
138 * Create or delete local port and associated tport.
139 */
140int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg)
141{
142 struct fc_lport *lport = arg;
143
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;
151 }
152 return NOTIFY_DONE;
153}
154
155/*
156 * Hash function for FC_IDs.
157 */
158static u32 ft_sess_hash(u32 port_id)
159{
160 return hash_32(port_id, FT_SESS_HASH_BITS);
161}
162
163/*
164 * Find session in local port.
165 * Sessions and hash lists are RCU-protected.
166 * A reference is taken which must be eventually freed.
167 */
168static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
169{
170 struct ft_tport *tport;
171 struct hlist_head *head;
172 struct hlist_node *pos;
173 struct ft_sess *sess;
174
175 rcu_read_lock();
176 tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
177 if (!tport)
178 goto out;
179
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();
Andy Grover6708bb22011-06-08 10:36:43 -0700185 pr_debug("port_id %x found %p\n", port_id, sess);
Kiran Patil3699d922011-04-18 16:24:14 -0700186 return sess;
187 }
188 }
189out:
190 rcu_read_unlock();
Andy Grover6708bb22011-06-08 10:36:43 -0700191 pr_debug("port_id %x not found\n", port_id);
Kiran Patil3699d922011-04-18 16:24:14 -0700192 return NULL;
193}
194
195/*
196 * Allocate session and enter it in the hash for the local port.
197 * Caller holds ft_lport_lock.
198 */
199static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
200 struct ft_node_acl *acl)
201{
202 struct ft_sess *sess;
203 struct hlist_head *head;
204 struct hlist_node *pos;
205
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;
210
211 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
212 if (!sess)
213 return NULL;
214
215 sess->se_sess = transport_init_session();
Dan Carpenter552523d2011-06-15 09:41:33 -0700216 if (IS_ERR(sess->se_sess)) {
Kiran Patil3699d922011-04-18 16:24:14 -0700217 kfree(sess);
218 return NULL;
219 }
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++;
226
Andy Grover6708bb22011-06-08 10:36:43 -0700227 pr_debug("port_id %x sess %p\n", port_id, sess);
Kiran Patil3699d922011-04-18 16:24:14 -0700228
229 transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl,
230 sess->se_sess, sess);
231 return sess;
232}
233
234/*
235 * Unhash the session.
236 * Caller holds ft_lport_lock.
237 */
238static void ft_sess_unhash(struct ft_sess *sess)
239{
240 struct ft_tport *tport = sess->tport;
241
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;
247}
248
249/*
250 * Delete session from hash.
251 * Caller holds ft_lport_lock.
252 */
253static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
254{
255 struct hlist_head *head;
256 struct hlist_node *pos;
257 struct ft_sess *sess;
258
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;
264 }
265 }
266 return NULL;
267}
268
269/*
270 * Delete all sessions from tport.
271 * Caller holds ft_lport_lock.
272 */
273static void ft_sess_delete_all(struct ft_tport *tport)
274{
275 struct hlist_head *head;
276 struct hlist_node *pos;
277 struct ft_sess *sess;
278
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 */
285 }
286 }
287}
288
289/*
290 * TCM ops for sessions.
291 */
292
293/*
294 * Determine whether session is allowed to be shutdown in the current context.
295 * Returns non-zero if the session should be shutdown.
296 */
297int ft_sess_shutdown(struct se_session *se_sess)
298{
299 struct ft_sess *sess = se_sess->fabric_sess_ptr;
300
Andy Grover6708bb22011-06-08 10:36:43 -0700301 pr_debug("port_id %x\n", sess->port_id);
Kiran Patil3699d922011-04-18 16:24:14 -0700302 return 1;
303}
304
305/*
306 * Remove session and send PRLO.
307 * This is called when the ACL is being deleted or queue depth is changing.
308 */
309void ft_sess_close(struct se_session *se_sess)
310{
311 struct ft_sess *sess = se_sess->fabric_sess_ptr;
312 struct fc_lport *lport;
313 u32 port_id;
314
315 mutex_lock(&ft_lport_lock);
316 lport = sess->tport->lport;
317 port_id = sess->port_id;
318 if (port_id == -1) {
Dan Carpenter7c7cf3b2011-06-13 23:08:46 +0300319 mutex_unlock(&ft_lport_lock);
Kiran Patil3699d922011-04-18 16:24:14 -0700320 return;
321 }
Andy Grover6708bb22011-06-08 10:36:43 -0700322 pr_debug("port_id %x\n", port_id);
Kiran Patil3699d922011-04-18 16:24:14 -0700323 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
331void ft_sess_stop(struct se_session *se_sess, int sess_sleep, int conn_sleep)
332{
333 struct ft_sess *sess = se_sess->fabric_sess_ptr;
334
Andy Grover6708bb22011-06-08 10:36:43 -0700335 pr_debug("port_id %x\n", sess->port_id);
Kiran Patil3699d922011-04-18 16:24:14 -0700336}
337
338int ft_sess_logged_in(struct se_session *se_sess)
339{
340 struct ft_sess *sess = se_sess->fabric_sess_ptr;
341
342 return sess->port_id != -1;
343}
344
345u32 ft_sess_get_index(struct se_session *se_sess)
346{
347 struct ft_sess *sess = se_sess->fabric_sess_ptr;
348
349 return sess->port_id; /* XXX TBD probably not what is needed */
350}
351
352u32 ft_sess_get_port_name(struct se_session *se_sess,
353 unsigned char *buf, u32 len)
354{
355 struct ft_sess *sess = se_sess->fabric_sess_ptr;
356
357 return ft_format_wwn(buf, len, sess->port_name);
358}
359
360void ft_sess_set_erl0(struct se_session *se_sess)
361{
362 /* XXX TBD called when out of memory */
363}
364
365/*
366 * libfc ops involving sessions.
367 */
368
369static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
370 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
371{
372 struct ft_tport *tport;
373 struct ft_sess *sess;
374 struct ft_node_acl *acl;
375 u32 fcp_parm;
376
377 tport = ft_tport_create(rdata->local_port);
378 if (!tport)
379 return 0; /* not a target for this local port */
380
381 acl = ft_acl_get(tport->tpg, rdata);
382 if (!acl)
383 return 0;
384
385 if (!rspp)
386 goto fill;
387
388 if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
389 return FC_SPP_RESP_NO_PA;
390
391 /*
392 * If both target and initiator bits are off, the SPP is invalid.
393 */
394 fcp_parm = ntohl(rspp->spp_params);
395 if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
396 return FC_SPP_RESP_INVL;
397
398 /*
399 * Create session (image pair) only if requested by
400 * EST_IMG_PAIR flag and if the requestor is an initiator.
401 */
402 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
403 spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
404 if (!(fcp_parm & FCP_SPPF_INIT_FCN))
405 return FC_SPP_RESP_CONF;
406 sess = ft_sess_create(tport, rdata->ids.port_id, acl);
407 if (!sess)
408 return FC_SPP_RESP_RES;
409 if (!sess->params)
410 rdata->prli_count++;
411 sess->params = fcp_parm;
412 sess->port_name = rdata->ids.port_name;
413 sess->max_frame = rdata->maxframe_size;
414
415 /* XXX TBD - clearing actions. unit attn, see 4.10 */
416 }
417
418 /*
419 * OR in our service parameters with other provider (initiator), if any.
420 * TBD XXX - indicate RETRY capability?
421 */
422fill:
423 fcp_parm = ntohl(spp->spp_params);
424 spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
425 return FC_SPP_RESP_ACK;
426}
427
428/**
429 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
430 * @rdata: remote port private
431 * @spp_len: service parameter page length
432 * @rspp: received service parameter page (NULL for outgoing PRLI)
433 * @spp: response service parameter page
434 *
435 * Returns spp response code.
436 */
437static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
438 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
439{
440 int ret;
441
442 mutex_lock(&ft_lport_lock);
443 ret = ft_prli_locked(rdata, spp_len, rspp, spp);
444 mutex_unlock(&ft_lport_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700445 pr_debug("port_id %x flags %x ret %x\n",
Kiran Patil3699d922011-04-18 16:24:14 -0700446 rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
447 return ret;
448}
449
450static void ft_sess_rcu_free(struct rcu_head *rcu)
451{
452 struct ft_sess *sess = container_of(rcu, struct ft_sess, rcu);
453
454 transport_deregister_session(sess->se_sess);
455 kfree(sess);
456}
457
458static void ft_sess_free(struct kref *kref)
459{
460 struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
461
462 call_rcu(&sess->rcu, ft_sess_rcu_free);
463}
464
465void ft_sess_put(struct ft_sess *sess)
466{
467 int sess_held = atomic_read(&sess->kref.refcount);
468
469 BUG_ON(!sess_held);
470 kref_put(&sess->kref, ft_sess_free);
471}
472
473static void ft_prlo(struct fc_rport_priv *rdata)
474{
475 struct ft_sess *sess;
476 struct ft_tport *tport;
477
478 mutex_lock(&ft_lport_lock);
479 tport = rcu_dereference(rdata->local_port->prov[FC_TYPE_FCP]);
480 if (!tport) {
481 mutex_unlock(&ft_lport_lock);
482 return;
483 }
484 sess = ft_sess_delete(tport, rdata->ids.port_id);
485 if (!sess) {
486 mutex_unlock(&ft_lport_lock);
487 return;
488 }
489 mutex_unlock(&ft_lport_lock);
490 transport_deregister_session_configfs(sess->se_sess);
491 ft_sess_put(sess); /* release from table */
492 rdata->prli_count--;
493 /* XXX TBD - clearing actions. unit attn, see 4.10 */
494}
495
496/*
497 * Handle incoming FCP request.
498 * Caller has verified that the frame is type FCP.
499 */
500static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
501{
502 struct ft_sess *sess;
503 u32 sid = fc_frame_sid(fp);
504
Andy Grover6708bb22011-06-08 10:36:43 -0700505 pr_debug("sid %x\n", sid);
Kiran Patil3699d922011-04-18 16:24:14 -0700506
507 sess = ft_sess_get(lport, sid);
508 if (!sess) {
Andy Grover6708bb22011-06-08 10:36:43 -0700509 pr_debug("sid %x sess lookup failed\n", sid);
Kiran Patil3699d922011-04-18 16:24:14 -0700510 /* TBD XXX - if FCP_CMND, send PRLO */
511 fc_frame_free(fp);
512 return;
513 }
514 ft_recv_req(sess, fp); /* must do ft_sess_put() */
515}
516
517/*
518 * Provider ops for libfc.
519 */
520struct fc4_prov ft_prov = {
521 .prli = ft_prli,
522 .prlo = ft_prlo,
523 .recv = ft_recv,
524 .module = THIS_MODULE,
525};