blob: f5186a74439950631c48f9735e3d9fbed1699c30 [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 <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>
Kiran Patil3699d922011-04-18 16:24:14 -070035#include <scsi/libfc.h>
36
37#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050038#include <target/target_core_fabric.h>
Kiran Patil3699d922011-04-18 16:24:14 -070039
Kiran Patil3699d922011-04-18 16:24:14 -070040#include "tcm_fc.h"
41
42static void ft_sess_delete_all(struct ft_tport *);
43
44/*
45 * Lookup or allocate target local port.
46 * Caller holds ft_lport_lock.
47 */
Andy Grovere3d44402014-04-04 16:54:15 -070048static struct ft_tport *ft_tport_get(struct fc_lport *lport)
Kiran Patil3699d922011-04-18 16:24:14 -070049{
50 struct ft_tpg *tpg;
51 struct ft_tport *tport;
52 int i;
53
Mark Rustad863555b2012-06-26 15:57:30 -070054 tport = rcu_dereference_protected(lport->prov[FC_TYPE_FCP],
55 lockdep_is_held(&ft_lport_lock));
Kiran Patil3699d922011-04-18 16:24:14 -070056 if (tport && tport->tpg)
57 return tport;
58
59 tpg = ft_lport_find_tpg(lport);
60 if (!tpg)
61 return NULL;
62
63 if (tport) {
64 tport->tpg = tpg;
Andy Grover2c42be22014-04-04 16:44:37 -070065 tpg->tport = tport;
Kiran Patil3699d922011-04-18 16:24:14 -070066 return tport;
67 }
68
69 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
70 if (!tport)
71 return NULL;
72
73 tport->lport = lport;
74 tport->tpg = tpg;
75 tpg->tport = tport;
76 for (i = 0; i < FT_SESS_HASH_SIZE; i++)
77 INIT_HLIST_HEAD(&tport->hash[i]);
78
79 rcu_assign_pointer(lport->prov[FC_TYPE_FCP], tport);
80 return tport;
81}
82
83/*
Kiran Patil3699d922011-04-18 16:24:14 -070084 * Delete a target local port.
85 * Caller holds ft_lport_lock.
86 */
87static void ft_tport_delete(struct ft_tport *tport)
88{
89 struct fc_lport *lport;
90 struct ft_tpg *tpg;
91
92 ft_sess_delete_all(tport);
93 lport = tport->lport;
94 BUG_ON(tport != lport->prov[FC_TYPE_FCP]);
Andreea-Cristina Bernatc04047e2014-08-18 15:05:37 +030095 RCU_INIT_POINTER(lport->prov[FC_TYPE_FCP], NULL);
Kiran Patil3699d922011-04-18 16:24:14 -070096
97 tpg = tport->tpg;
98 if (tpg) {
99 tpg->tport = NULL;
100 tport->tpg = NULL;
101 }
Paul E. McKenneya6c76da2012-01-06 17:02:13 -0800102 kfree_rcu(tport, rcu);
Kiran Patil3699d922011-04-18 16:24:14 -0700103}
104
105/*
106 * Add local port.
107 * Called thru fc_lport_iterate().
108 */
109void ft_lport_add(struct fc_lport *lport, void *arg)
110{
111 mutex_lock(&ft_lport_lock);
Andy Grovere3d44402014-04-04 16:54:15 -0700112 ft_tport_get(lport);
Kiran Patil3699d922011-04-18 16:24:14 -0700113 mutex_unlock(&ft_lport_lock);
114}
115
116/*
117 * Delete local port.
118 * Called thru fc_lport_iterate().
119 */
120void ft_lport_del(struct fc_lport *lport, void *arg)
121{
122 struct ft_tport *tport;
123
124 mutex_lock(&ft_lport_lock);
125 tport = lport->prov[FC_TYPE_FCP];
126 if (tport)
127 ft_tport_delete(tport);
128 mutex_unlock(&ft_lport_lock);
129}
130
131/*
132 * Notification of local port change from libfc.
133 * Create or delete local port and associated tport.
134 */
135int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg)
136{
137 struct fc_lport *lport = arg;
138
139 switch (event) {
140 case FC_LPORT_EV_ADD:
141 ft_lport_add(lport, NULL);
142 break;
143 case FC_LPORT_EV_DEL:
144 ft_lport_del(lport, NULL);
145 break;
146 }
147 return NOTIFY_DONE;
148}
149
150/*
151 * Hash function for FC_IDs.
152 */
153static u32 ft_sess_hash(u32 port_id)
154{
155 return hash_32(port_id, FT_SESS_HASH_BITS);
156}
157
158/*
159 * Find session in local port.
160 * Sessions and hash lists are RCU-protected.
161 * A reference is taken which must be eventually freed.
162 */
163static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
164{
165 struct ft_tport *tport;
166 struct hlist_head *head;
Kiran Patil3699d922011-04-18 16:24:14 -0700167 struct ft_sess *sess;
168
169 rcu_read_lock();
170 tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
171 if (!tport)
172 goto out;
173
174 head = &tport->hash[ft_sess_hash(port_id)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800175 hlist_for_each_entry_rcu(sess, head, hash) {
Kiran Patil3699d922011-04-18 16:24:14 -0700176 if (sess->port_id == port_id) {
177 kref_get(&sess->kref);
178 rcu_read_unlock();
Andy Grover6708bb22011-06-08 10:36:43 -0700179 pr_debug("port_id %x found %p\n", port_id, sess);
Kiran Patil3699d922011-04-18 16:24:14 -0700180 return sess;
181 }
182 }
183out:
184 rcu_read_unlock();
Andy Grover6708bb22011-06-08 10:36:43 -0700185 pr_debug("port_id %x not found\n", port_id);
Kiran Patil3699d922011-04-18 16:24:14 -0700186 return NULL;
187}
188
Nicholas Bellinger9ed59652016-01-09 20:12:40 -0800189static int ft_sess_alloc_cb(struct se_portal_group *se_tpg,
190 struct se_session *se_sess, void *p)
191{
192 struct ft_sess *sess = p;
193 struct ft_tport *tport = sess->tport;
194 struct hlist_head *head = &tport->hash[ft_sess_hash(sess->port_id)];
195
196 pr_debug("port_id %x sess %p\n", sess->port_id, sess);
197 hlist_add_head_rcu(&sess->hash, head);
198 tport->sess_count++;
199
200 return 0;
201}
202
Kiran Patil3699d922011-04-18 16:24:14 -0700203/*
204 * Allocate session and enter it in the hash for the local port.
205 * Caller holds ft_lport_lock.
206 */
207static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
Nicholas Bellinger7fbef3d2016-01-07 22:17:16 -0800208 struct fc_rport_priv *rdata)
Kiran Patil3699d922011-04-18 16:24:14 -0700209{
Nicholas Bellinger7fbef3d2016-01-07 22:17:16 -0800210 struct se_portal_group *se_tpg = &tport->tpg->se_tpg;
Kiran Patil3699d922011-04-18 16:24:14 -0700211 struct ft_sess *sess;
212 struct hlist_head *head;
Nicholas Bellinger7fbef3d2016-01-07 22:17:16 -0800213 unsigned char initiatorname[TRANSPORT_IQN_LEN];
214
215 ft_format_wwn(&initiatorname[0], TRANSPORT_IQN_LEN, rdata->ids.port_name);
Kiran Patil3699d922011-04-18 16:24:14 -0700216
217 head = &tport->hash[ft_sess_hash(port_id)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800218 hlist_for_each_entry_rcu(sess, head, hash)
Kiran Patil3699d922011-04-18 16:24:14 -0700219 if (sess->port_id == port_id)
220 return sess;
221
222 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
223 if (!sess)
224 return NULL;
225
Nicholas Bellinger9ed59652016-01-09 20:12:40 -0800226 kref_init(&sess->kref); /* ref for table entry */
227 sess->tport = tport;
228 sess->port_id = port_id;
229
230 sess->se_sess = target_alloc_session(se_tpg, TCM_FC_DEFAULT_TAGS,
231 sizeof(struct ft_cmd),
232 TARGET_PROT_NORMAL, &initiatorname[0],
233 sess, ft_sess_alloc_cb);
Dan Carpenter552523d2011-06-15 09:41:33 -0700234 if (IS_ERR(sess->se_sess)) {
Kiran Patil3699d922011-04-18 16:24:14 -0700235 kfree(sess);
236 return NULL;
237 }
Kiran Patil3699d922011-04-18 16:24:14 -0700238 return sess;
239}
240
241/*
242 * Unhash the session.
243 * Caller holds ft_lport_lock.
244 */
245static void ft_sess_unhash(struct ft_sess *sess)
246{
247 struct ft_tport *tport = sess->tport;
248
249 hlist_del_rcu(&sess->hash);
250 BUG_ON(!tport->sess_count);
251 tport->sess_count--;
252 sess->port_id = -1;
253 sess->params = 0;
254}
255
256/*
257 * Delete session from hash.
258 * Caller holds ft_lport_lock.
259 */
260static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
261{
262 struct hlist_head *head;
Kiran Patil3699d922011-04-18 16:24:14 -0700263 struct ft_sess *sess;
264
265 head = &tport->hash[ft_sess_hash(port_id)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800266 hlist_for_each_entry_rcu(sess, head, hash) {
Kiran Patil3699d922011-04-18 16:24:14 -0700267 if (sess->port_id == port_id) {
268 ft_sess_unhash(sess);
269 return sess;
270 }
271 }
272 return NULL;
273}
274
Bart Van Asschede7ee9a2016-01-05 14:47:58 +0100275static void ft_close_sess(struct ft_sess *sess)
276{
277 transport_deregister_session_configfs(sess->se_sess);
278 target_sess_cmd_list_set_waiting(sess->se_sess);
279 target_wait_for_sess_cmds(sess->se_sess);
280 ft_sess_put(sess);
281}
282
Kiran Patil3699d922011-04-18 16:24:14 -0700283/*
284 * Delete all sessions from tport.
285 * Caller holds ft_lport_lock.
286 */
287static void ft_sess_delete_all(struct ft_tport *tport)
288{
289 struct hlist_head *head;
Kiran Patil3699d922011-04-18 16:24:14 -0700290 struct ft_sess *sess;
291
292 for (head = tport->hash;
293 head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800294 hlist_for_each_entry_rcu(sess, head, hash) {
Kiran Patil3699d922011-04-18 16:24:14 -0700295 ft_sess_unhash(sess);
Bart Van Asschede7ee9a2016-01-05 14:47:58 +0100296 ft_close_sess(sess); /* release from table */
Kiran Patil3699d922011-04-18 16:24:14 -0700297 }
298 }
299}
300
301/*
302 * TCM ops for sessions.
303 */
304
305/*
Kiran Patil3699d922011-04-18 16:24:14 -0700306 * 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;
Kiran Patil3699d922011-04-18 16:24:14 -0700312 u32 port_id;
313
314 mutex_lock(&ft_lport_lock);
Kiran Patil3699d922011-04-18 16:24:14 -0700315 port_id = sess->port_id;
316 if (port_id == -1) {
Dan Carpenter7c7cf3b2011-06-13 23:08:46 +0300317 mutex_unlock(&ft_lport_lock);
Kiran Patil3699d922011-04-18 16:24:14 -0700318 return;
319 }
Andy Grover6708bb22011-06-08 10:36:43 -0700320 pr_debug("port_id %x\n", port_id);
Kiran Patil3699d922011-04-18 16:24:14 -0700321 ft_sess_unhash(sess);
322 mutex_unlock(&ft_lport_lock);
Bart Van Asschede7ee9a2016-01-05 14:47:58 +0100323 ft_close_sess(sess);
Kiran Patil3699d922011-04-18 16:24:14 -0700324 /* XXX Send LOGO or PRLO */
325 synchronize_rcu(); /* let transport deregister happen */
326}
327
Kiran Patil3699d922011-04-18 16:24:14 -0700328u32 ft_sess_get_index(struct se_session *se_sess)
329{
330 struct ft_sess *sess = se_sess->fabric_sess_ptr;
331
332 return sess->port_id; /* XXX TBD probably not what is needed */
333}
334
335u32 ft_sess_get_port_name(struct se_session *se_sess,
336 unsigned char *buf, u32 len)
337{
338 struct ft_sess *sess = se_sess->fabric_sess_ptr;
339
340 return ft_format_wwn(buf, len, sess->port_name);
341}
342
Kiran Patil3699d922011-04-18 16:24:14 -0700343/*
344 * libfc ops involving sessions.
345 */
346
347static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
348 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
349{
350 struct ft_tport *tport;
351 struct ft_sess *sess;
Kiran Patil3699d922011-04-18 16:24:14 -0700352 u32 fcp_parm;
353
Andy Grovere3d44402014-04-04 16:54:15 -0700354 tport = ft_tport_get(rdata->local_port);
Kiran Patil3699d922011-04-18 16:24:14 -0700355 if (!tport)
Mark Rustadedec8df2012-12-21 10:58:19 -0800356 goto not_target; /* not a target for this local port */
Kiran Patil3699d922011-04-18 16:24:14 -0700357
Kiran Patil3699d922011-04-18 16:24:14 -0700358 if (!rspp)
359 goto fill;
360
361 if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
362 return FC_SPP_RESP_NO_PA;
363
364 /*
365 * If both target and initiator bits are off, the SPP is invalid.
366 */
367 fcp_parm = ntohl(rspp->spp_params);
368 if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
369 return FC_SPP_RESP_INVL;
370
371 /*
372 * Create session (image pair) only if requested by
373 * EST_IMG_PAIR flag and if the requestor is an initiator.
374 */
375 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
376 spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
377 if (!(fcp_parm & FCP_SPPF_INIT_FCN))
378 return FC_SPP_RESP_CONF;
Nicholas Bellinger7fbef3d2016-01-07 22:17:16 -0800379 sess = ft_sess_create(tport, rdata->ids.port_id, rdata);
Kiran Patil3699d922011-04-18 16:24:14 -0700380 if (!sess)
381 return FC_SPP_RESP_RES;
382 if (!sess->params)
383 rdata->prli_count++;
384 sess->params = fcp_parm;
385 sess->port_name = rdata->ids.port_name;
386 sess->max_frame = rdata->maxframe_size;
387
388 /* XXX TBD - clearing actions. unit attn, see 4.10 */
389 }
390
391 /*
392 * OR in our service parameters with other provider (initiator), if any.
Kiran Patil3699d922011-04-18 16:24:14 -0700393 */
394fill:
395 fcp_parm = ntohl(spp->spp_params);
Mark Rustadf2eeba22012-12-21 10:58:14 -0800396 fcp_parm &= ~FCP_SPPF_RETRY;
Kiran Patil3699d922011-04-18 16:24:14 -0700397 spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
398 return FC_SPP_RESP_ACK;
Mark Rustadedec8df2012-12-21 10:58:19 -0800399
400not_target:
401 fcp_parm = ntohl(spp->spp_params);
402 fcp_parm &= ~FCP_SPPF_TARG_FCN;
403 spp->spp_params = htonl(fcp_parm);
404 return 0;
Kiran Patil3699d922011-04-18 16:24:14 -0700405}
406
407/**
408 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
409 * @rdata: remote port private
410 * @spp_len: service parameter page length
411 * @rspp: received service parameter page (NULL for outgoing PRLI)
412 * @spp: response service parameter page
413 *
414 * Returns spp response code.
415 */
416static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
417 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
418{
419 int ret;
420
421 mutex_lock(&ft_lport_lock);
422 ret = ft_prli_locked(rdata, spp_len, rspp, spp);
423 mutex_unlock(&ft_lport_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700424 pr_debug("port_id %x flags %x ret %x\n",
Kiran Patil3699d922011-04-18 16:24:14 -0700425 rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
426 return ret;
427}
428
Kiran Patil3699d922011-04-18 16:24:14 -0700429static void ft_sess_free(struct kref *kref)
430{
431 struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
432
Yi Zou9f4ad442012-12-10 17:04:00 -0800433 transport_deregister_session(sess->se_sess);
Wei Yongjuna6ad57e2013-03-11 21:48:28 +0800434 kfree_rcu(sess, rcu);
Kiran Patil3699d922011-04-18 16:24:14 -0700435}
436
437void ft_sess_put(struct ft_sess *sess)
438{
439 int sess_held = atomic_read(&sess->kref.refcount);
440
441 BUG_ON(!sess_held);
442 kref_put(&sess->kref, ft_sess_free);
443}
444
445static void ft_prlo(struct fc_rport_priv *rdata)
446{
447 struct ft_sess *sess;
448 struct ft_tport *tport;
449
450 mutex_lock(&ft_lport_lock);
Denis Efremov08a162082012-08-18 16:10:31 +0400451 tport = rcu_dereference_protected(rdata->local_port->prov[FC_TYPE_FCP],
452 lockdep_is_held(&ft_lport_lock));
453
Kiran Patil3699d922011-04-18 16:24:14 -0700454 if (!tport) {
455 mutex_unlock(&ft_lport_lock);
456 return;
457 }
458 sess = ft_sess_delete(tport, rdata->ids.port_id);
459 if (!sess) {
460 mutex_unlock(&ft_lport_lock);
461 return;
462 }
463 mutex_unlock(&ft_lport_lock);
Bart Van Asschede7ee9a2016-01-05 14:47:58 +0100464 ft_close_sess(sess); /* release from table */
Kiran Patil3699d922011-04-18 16:24:14 -0700465 rdata->prli_count--;
466 /* XXX TBD - clearing actions. unit attn, see 4.10 */
467}
468
469/*
470 * Handle incoming FCP request.
471 * Caller has verified that the frame is type FCP.
472 */
473static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
474{
475 struct ft_sess *sess;
476 u32 sid = fc_frame_sid(fp);
477
Andy Grover6708bb22011-06-08 10:36:43 -0700478 pr_debug("sid %x\n", sid);
Kiran Patil3699d922011-04-18 16:24:14 -0700479
480 sess = ft_sess_get(lport, sid);
481 if (!sess) {
Andy Grover6708bb22011-06-08 10:36:43 -0700482 pr_debug("sid %x sess lookup failed\n", sid);
Kiran Patil3699d922011-04-18 16:24:14 -0700483 /* TBD XXX - if FCP_CMND, send PRLO */
484 fc_frame_free(fp);
485 return;
486 }
487 ft_recv_req(sess, fp); /* must do ft_sess_put() */
488}
489
490/*
491 * Provider ops for libfc.
492 */
493struct fc4_prov ft_prov = {
494 .prli = ft_prli,
495 .prlo = ft_prlo,
496 .recv = ft_recv,
497 .module = THIS_MODULE,
498};