blob: 3c9e5b57caabea23567192e2d2f85843ca6bc70f [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
Mark Rustad863555b2012-06-26 15:57:30 -070061 tport = rcu_dereference_protected(lport->prov[FC_TYPE_FCP],
62 lockdep_is_held(&ft_lport_lock));
Kiran Patil3699d922011-04-18 16:24:14 -070063 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 return tport;
73 }
74
75 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
76 if (!tport)
77 return NULL;
78
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]);
84
85 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], tport);
86 return tport;
87}
88
89/*
Kiran Patil3699d922011-04-18 16:24:14 -070090 * Delete a target local port.
91 * Caller holds ft_lport_lock.
92 */
93static void ft_tport_delete(struct ft_tport *tport)
94{
95 struct fc_lport *lport;
96 struct ft_tpg *tpg;
97
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);
102
103 tpg = tport->tpg;
104 if (tpg) {
105 tpg->tport = NULL;
106 tport->tpg = NULL;
107 }
Paul E. McKenneya6c76da2012-01-06 17:02:13 -0800108 kfree_rcu(tport, rcu);
Kiran Patil3699d922011-04-18 16:24:14 -0700109}
110
111/*
112 * Add local port.
113 * Called thru fc_lport_iterate().
114 */
115void ft_lport_add(struct fc_lport *lport, void *arg)
116{
117 mutex_lock(&ft_lport_lock);
118 ft_tport_create(lport);
119 mutex_unlock(&ft_lport_lock);
120}
121
122/*
123 * Delete local port.
124 * Called thru fc_lport_iterate().
125 */
126void ft_lport_del(struct fc_lport *lport, void *arg)
127{
128 struct ft_tport *tport;
129
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);
135}
136
137/*
138 * Notification of local port change from libfc.
139 * Create or delete local port and associated tport.
140 */
141int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg)
142{
143 struct fc_lport *lport = arg;
144
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;
152 }
153 return NOTIFY_DONE;
154}
155
156/*
157 * Hash function for FC_IDs.
158 */
159static u32 ft_sess_hash(u32 port_id)
160{
161 return hash_32(port_id, FT_SESS_HASH_BITS);
162}
163
164/*
165 * Find session in local port.
166 * Sessions and hash lists are RCU-protected.
167 * A reference is taken which must be eventually freed.
168 */
169static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
170{
171 struct ft_tport *tport;
172 struct hlist_head *head;
173 struct hlist_node *pos;
174 struct ft_sess *sess;
175
176 rcu_read_lock();
177 tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
178 if (!tport)
179 goto out;
180
181 head = &tport->hash[ft_sess_hash(port_id)];
182 hlist_for_each_entry_rcu(sess, pos, head, hash) {
183 if (sess->port_id == port_id) {
184 kref_get(&sess->kref);
185 rcu_read_unlock();
Andy Grover6708bb22011-06-08 10:36:43 -0700186 pr_debug("port_id %x found %p\n", port_id, sess);
Kiran Patil3699d922011-04-18 16:24:14 -0700187 return sess;
188 }
189 }
190out:
191 rcu_read_unlock();
Andy Grover6708bb22011-06-08 10:36:43 -0700192 pr_debug("port_id %x not found\n", port_id);
Kiran Patil3699d922011-04-18 16:24:14 -0700193 return NULL;
194}
195
196/*
197 * Allocate session and enter it in the hash for the local port.
198 * Caller holds ft_lport_lock.
199 */
200static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
201 struct ft_node_acl *acl)
202{
203 struct ft_sess *sess;
204 struct hlist_head *head;
205 struct hlist_node *pos;
206
207 head = &tport->hash[ft_sess_hash(port_id)];
208 hlist_for_each_entry_rcu(sess, pos, head, hash)
209 if (sess->port_id == port_id)
210 return sess;
211
212 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
213 if (!sess)
214 return NULL;
215
216 sess->se_sess = transport_init_session();
Dan Carpenter552523d2011-06-15 09:41:33 -0700217 if (IS_ERR(sess->se_sess)) {
Kiran Patil3699d922011-04-18 16:24:14 -0700218 kfree(sess);
219 return NULL;
220 }
221 sess->se_sess->se_node_acl = &acl->se_node_acl;
222 sess->tport = tport;
223 sess->port_id = port_id;
224 kref_init(&sess->kref); /* ref for table entry */
225 hlist_add_head_rcu(&sess->hash, head);
226 tport->sess_count++;
227
Andy Grover6708bb22011-06-08 10:36:43 -0700228 pr_debug("port_id %x sess %p\n", port_id, sess);
Kiran Patil3699d922011-04-18 16:24:14 -0700229
230 transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl,
231 sess->se_sess, sess);
232 return sess;
233}
234
235/*
236 * Unhash the session.
237 * Caller holds ft_lport_lock.
238 */
239static void ft_sess_unhash(struct ft_sess *sess)
240{
241 struct ft_tport *tport = sess->tport;
242
243 hlist_del_rcu(&sess->hash);
244 BUG_ON(!tport->sess_count);
245 tport->sess_count--;
246 sess->port_id = -1;
247 sess->params = 0;
248}
249
250/*
251 * Delete session from hash.
252 * Caller holds ft_lport_lock.
253 */
254static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
255{
256 struct hlist_head *head;
257 struct hlist_node *pos;
258 struct ft_sess *sess;
259
260 head = &tport->hash[ft_sess_hash(port_id)];
261 hlist_for_each_entry_rcu(sess, pos, head, hash) {
262 if (sess->port_id == port_id) {
263 ft_sess_unhash(sess);
264 return sess;
265 }
266 }
267 return NULL;
268}
269
270/*
271 * Delete all sessions from tport.
272 * Caller holds ft_lport_lock.
273 */
274static void ft_sess_delete_all(struct ft_tport *tport)
275{
276 struct hlist_head *head;
277 struct hlist_node *pos;
278 struct ft_sess *sess;
279
280 for (head = tport->hash;
281 head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
282 hlist_for_each_entry_rcu(sess, pos, head, hash) {
283 ft_sess_unhash(sess);
284 transport_deregister_session_configfs(sess->se_sess);
285 ft_sess_put(sess); /* release from table */
286 }
287 }
288}
289
290/*
291 * TCM ops for sessions.
292 */
293
294/*
295 * Determine whether session is allowed to be shutdown in the current context.
296 * Returns non-zero if the session should be shutdown.
297 */
298int ft_sess_shutdown(struct se_session *se_sess)
299{
300 struct ft_sess *sess = se_sess->fabric_sess_ptr;
301
Andy Grover6708bb22011-06-08 10:36:43 -0700302 pr_debug("port_id %x\n", sess->port_id);
Kiran Patil3699d922011-04-18 16:24:14 -0700303 return 1;
304}
305
306/*
307 * Remove session and send PRLO.
308 * This is called when the ACL is being deleted or queue depth is changing.
309 */
310void ft_sess_close(struct se_session *se_sess)
311{
312 struct ft_sess *sess = se_sess->fabric_sess_ptr;
Kiran Patil3699d922011-04-18 16:24:14 -0700313 u32 port_id;
314
315 mutex_lock(&ft_lport_lock);
Kiran Patil3699d922011-04-18 16:24:14 -0700316 port_id = sess->port_id;
317 if (port_id == -1) {
Dan Carpenter7c7cf3b2011-06-13 23:08:46 +0300318 mutex_unlock(&ft_lport_lock);
Kiran Patil3699d922011-04-18 16:24:14 -0700319 return;
320 }
Andy Grover6708bb22011-06-08 10:36:43 -0700321 pr_debug("port_id %x\n", port_id);
Kiran Patil3699d922011-04-18 16:24:14 -0700322 ft_sess_unhash(sess);
323 mutex_unlock(&ft_lport_lock);
324 transport_deregister_session_configfs(se_sess);
325 ft_sess_put(sess);
326 /* XXX Send LOGO or PRLO */
327 synchronize_rcu(); /* let transport deregister happen */
328}
329
Kiran Patil3699d922011-04-18 16:24:14 -0700330u32 ft_sess_get_index(struct se_session *se_sess)
331{
332 struct ft_sess *sess = se_sess->fabric_sess_ptr;
333
334 return sess->port_id; /* XXX TBD probably not what is needed */
335}
336
337u32 ft_sess_get_port_name(struct se_session *se_sess,
338 unsigned char *buf, u32 len)
339{
340 struct ft_sess *sess = se_sess->fabric_sess_ptr;
341
342 return ft_format_wwn(buf, len, sess->port_name);
343}
344
Kiran Patil3699d922011-04-18 16:24:14 -0700345/*
346 * libfc ops involving sessions.
347 */
348
349static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
350 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
351{
352 struct ft_tport *tport;
353 struct ft_sess *sess;
354 struct ft_node_acl *acl;
355 u32 fcp_parm;
356
357 tport = ft_tport_create(rdata->local_port);
358 if (!tport)
359 return 0; /* not a target for this local port */
360
361 acl = ft_acl_get(tport->tpg, rdata);
362 if (!acl)
363 return 0;
364
365 if (!rspp)
366 goto fill;
367
368 if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
369 return FC_SPP_RESP_NO_PA;
370
371 /*
372 * If both target and initiator bits are off, the SPP is invalid.
373 */
374 fcp_parm = ntohl(rspp->spp_params);
375 if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
376 return FC_SPP_RESP_INVL;
377
378 /*
379 * Create session (image pair) only if requested by
380 * EST_IMG_PAIR flag and if the requestor is an initiator.
381 */
382 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
383 spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
384 if (!(fcp_parm & FCP_SPPF_INIT_FCN))
385 return FC_SPP_RESP_CONF;
386 sess = ft_sess_create(tport, rdata->ids.port_id, acl);
387 if (!sess)
388 return FC_SPP_RESP_RES;
389 if (!sess->params)
390 rdata->prli_count++;
391 sess->params = fcp_parm;
392 sess->port_name = rdata->ids.port_name;
393 sess->max_frame = rdata->maxframe_size;
394
395 /* XXX TBD - clearing actions. unit attn, see 4.10 */
396 }
397
398 /*
399 * OR in our service parameters with other provider (initiator), if any.
400 * TBD XXX - indicate RETRY capability?
401 */
402fill:
403 fcp_parm = ntohl(spp->spp_params);
404 spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
405 return FC_SPP_RESP_ACK;
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 */
417static 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);
Andy Grover6708bb22011-06-08 10:36:43 -0700425 pr_debug("port_id %x flags %x ret %x\n",
Kiran Patil3699d922011-04-18 16:24:14 -0700426 rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
427 return ret;
428}
429
430static void ft_sess_rcu_free(struct rcu_head *rcu)
431{
432 struct ft_sess *sess = container_of(rcu, struct ft_sess, rcu);
433
434 transport_deregister_session(sess->se_sess);
435 kfree(sess);
436}
437
438static void ft_sess_free(struct kref *kref)
439{
440 struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
441
442 call_rcu(&sess->rcu, ft_sess_rcu_free);
443}
444
445void ft_sess_put(struct ft_sess *sess)
446{
447 int sess_held = atomic_read(&sess->kref.refcount);
448
449 BUG_ON(!sess_held);
450 kref_put(&sess->kref, ft_sess_free);
451}
452
453static void ft_prlo(struct fc_rport_priv *rdata)
454{
455 struct ft_sess *sess;
456 struct ft_tport *tport;
457
458 mutex_lock(&ft_lport_lock);
Denis Efremov08a162082012-08-18 16:10:31 +0400459 tport = rcu_dereference_protected(rdata->local_port->prov[FC_TYPE_FCP],
460 lockdep_is_held(&ft_lport_lock));
461
Kiran Patil3699d922011-04-18 16:24:14 -0700462 if (!tport) {
463 mutex_unlock(&ft_lport_lock);
464 return;
465 }
466 sess = ft_sess_delete(tport, rdata->ids.port_id);
467 if (!sess) {
468 mutex_unlock(&ft_lport_lock);
469 return;
470 }
471 mutex_unlock(&ft_lport_lock);
472 transport_deregister_session_configfs(sess->se_sess);
473 ft_sess_put(sess); /* release from table */
474 rdata->prli_count--;
475 /* XXX TBD - clearing actions. unit attn, see 4.10 */
476}
477
478/*
479 * Handle incoming FCP request.
480 * Caller has verified that the frame is type FCP.
481 */
482static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
483{
484 struct ft_sess *sess;
485 u32 sid = fc_frame_sid(fp);
486
Andy Grover6708bb22011-06-08 10:36:43 -0700487 pr_debug("sid %x\n", sid);
Kiran Patil3699d922011-04-18 16:24:14 -0700488
489 sess = ft_sess_get(lport, sid);
490 if (!sess) {
Andy Grover6708bb22011-06-08 10:36:43 -0700491 pr_debug("sid %x sess lookup failed\n", sid);
Kiran Patil3699d922011-04-18 16:24:14 -0700492 /* TBD XXX - if FCP_CMND, send PRLO */
493 fc_frame_free(fp);
494 return;
495 }
496 ft_recv_req(sess, fp); /* must do ft_sess_put() */
497}
498
499/*
500 * Provider ops for libfc.
501 */
502struct fc4_prov ft_prov = {
503 .prli = ft_prli,
504 .prlo = ft_prlo,
505 .recv = ft_recv,
506 .module = THIS_MODULE,
507};