blob: 6ffbb603d9122a0259daa69db5bcca03ba891aa5 [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;
Hannes Reinecke107818e2016-07-19 15:01:55 +020094 lport->service_params &= ~FCP_SPPF_TARG_FCN;
Kiran Patil3699d922011-04-18 16:24:14 -070095 BUG_ON(tport != lport->prov[FC_TYPE_FCP]);
Andreea-Cristina Bernatc04047e2014-08-18 15:05:37 +030096 RCU_INIT_POINTER(lport->prov[FC_TYPE_FCP], NULL);
Kiran Patil3699d922011-04-18 16:24:14 -070097
98 tpg = tport->tpg;
99 if (tpg) {
100 tpg->tport = NULL;
101 tport->tpg = NULL;
102 }
Paul E. McKenneya6c76da2012-01-06 17:02:13 -0800103 kfree_rcu(tport, rcu);
Kiran Patil3699d922011-04-18 16:24:14 -0700104}
105
106/*
107 * Add local port.
108 * Called thru fc_lport_iterate().
109 */
110void ft_lport_add(struct fc_lport *lport, void *arg)
111{
112 mutex_lock(&ft_lport_lock);
Andy Grovere3d44402014-04-04 16:54:15 -0700113 ft_tport_get(lport);
Hannes Reinecke107818e2016-07-19 15:01:55 +0200114 lport->service_params |= FCP_SPPF_TARG_FCN;
Kiran Patil3699d922011-04-18 16:24:14 -0700115 mutex_unlock(&ft_lport_lock);
116}
117
118/*
119 * Delete local port.
120 * Called thru fc_lport_iterate().
121 */
122void 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 */
137int 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 */
155static 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 */
165static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
166{
167 struct ft_tport *tport;
168 struct hlist_head *head;
Kiran Patil3699d922011-04-18 16:24:14 -0700169 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)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800177 hlist_for_each_entry_rcu(sess, head, hash) {
Kiran Patil3699d922011-04-18 16:24:14 -0700178 if (sess->port_id == port_id) {
179 kref_get(&sess->kref);
180 rcu_read_unlock();
Andy Grover6708bb22011-06-08 10:36:43 -0700181 pr_debug("port_id %x found %p\n", port_id, sess);
Kiran Patil3699d922011-04-18 16:24:14 -0700182 return sess;
183 }
184 }
185out:
186 rcu_read_unlock();
Andy Grover6708bb22011-06-08 10:36:43 -0700187 pr_debug("port_id %x not found\n", port_id);
Kiran Patil3699d922011-04-18 16:24:14 -0700188 return NULL;
189}
190
Nicholas Bellinger9ed59652016-01-09 20:12:40 -0800191static int ft_sess_alloc_cb(struct se_portal_group *se_tpg,
192 struct se_session *se_sess, void *p)
193{
194 struct ft_sess *sess = p;
195 struct ft_tport *tport = sess->tport;
196 struct hlist_head *head = &tport->hash[ft_sess_hash(sess->port_id)];
197
198 pr_debug("port_id %x sess %p\n", sess->port_id, sess);
199 hlist_add_head_rcu(&sess->hash, head);
200 tport->sess_count++;
201
202 return 0;
203}
204
Kiran Patil3699d922011-04-18 16:24:14 -0700205/*
206 * Allocate session and enter it in the hash for the local port.
207 * Caller holds ft_lport_lock.
208 */
209static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
Nicholas Bellinger7fbef3d2016-01-07 22:17:16 -0800210 struct fc_rport_priv *rdata)
Kiran Patil3699d922011-04-18 16:24:14 -0700211{
Nicholas Bellinger7fbef3d2016-01-07 22:17:16 -0800212 struct se_portal_group *se_tpg = &tport->tpg->se_tpg;
Kiran Patil3699d922011-04-18 16:24:14 -0700213 struct ft_sess *sess;
214 struct hlist_head *head;
Nicholas Bellinger7fbef3d2016-01-07 22:17:16 -0800215 unsigned char initiatorname[TRANSPORT_IQN_LEN];
216
217 ft_format_wwn(&initiatorname[0], TRANSPORT_IQN_LEN, rdata->ids.port_name);
Kiran Patil3699d922011-04-18 16:24:14 -0700218
219 head = &tport->hash[ft_sess_hash(port_id)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800220 hlist_for_each_entry_rcu(sess, head, hash)
Kiran Patil3699d922011-04-18 16:24:14 -0700221 if (sess->port_id == port_id)
222 return sess;
223
224 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
225 if (!sess)
226 return NULL;
227
Nicholas Bellinger9ed59652016-01-09 20:12:40 -0800228 kref_init(&sess->kref); /* ref for table entry */
229 sess->tport = tport;
230 sess->port_id = port_id;
231
232 sess->se_sess = target_alloc_session(se_tpg, TCM_FC_DEFAULT_TAGS,
233 sizeof(struct ft_cmd),
234 TARGET_PROT_NORMAL, &initiatorname[0],
235 sess, ft_sess_alloc_cb);
Dan Carpenter552523d2011-06-15 09:41:33 -0700236 if (IS_ERR(sess->se_sess)) {
Kiran Patil3699d922011-04-18 16:24:14 -0700237 kfree(sess);
238 return NULL;
239 }
Kiran Patil3699d922011-04-18 16:24:14 -0700240 return sess;
241}
242
243/*
244 * Unhash the session.
245 * Caller holds ft_lport_lock.
246 */
247static void ft_sess_unhash(struct ft_sess *sess)
248{
249 struct ft_tport *tport = sess->tport;
250
251 hlist_del_rcu(&sess->hash);
252 BUG_ON(!tport->sess_count);
253 tport->sess_count--;
254 sess->port_id = -1;
255 sess->params = 0;
256}
257
258/*
259 * Delete session from hash.
260 * Caller holds ft_lport_lock.
261 */
262static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
263{
264 struct hlist_head *head;
Kiran Patil3699d922011-04-18 16:24:14 -0700265 struct ft_sess *sess;
266
267 head = &tport->hash[ft_sess_hash(port_id)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800268 hlist_for_each_entry_rcu(sess, head, hash) {
Kiran Patil3699d922011-04-18 16:24:14 -0700269 if (sess->port_id == port_id) {
270 ft_sess_unhash(sess);
271 return sess;
272 }
273 }
274 return NULL;
275}
276
Bart Van Asschede7ee9a2016-01-05 14:47:58 +0100277static void ft_close_sess(struct ft_sess *sess)
278{
279 transport_deregister_session_configfs(sess->se_sess);
280 target_sess_cmd_list_set_waiting(sess->se_sess);
281 target_wait_for_sess_cmds(sess->se_sess);
282 ft_sess_put(sess);
283}
284
Kiran Patil3699d922011-04-18 16:24:14 -0700285/*
286 * Delete all sessions from tport.
287 * Caller holds ft_lport_lock.
288 */
289static void ft_sess_delete_all(struct ft_tport *tport)
290{
291 struct hlist_head *head;
Kiran Patil3699d922011-04-18 16:24:14 -0700292 struct ft_sess *sess;
293
294 for (head = tport->hash;
295 head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800296 hlist_for_each_entry_rcu(sess, head, hash) {
Kiran Patil3699d922011-04-18 16:24:14 -0700297 ft_sess_unhash(sess);
Bart Van Asschede7ee9a2016-01-05 14:47:58 +0100298 ft_close_sess(sess); /* release from table */
Kiran Patil3699d922011-04-18 16:24:14 -0700299 }
300 }
301}
302
303/*
304 * TCM ops for sessions.
305 */
306
307/*
Kiran Patil3699d922011-04-18 16:24:14 -0700308 * Remove session and send PRLO.
309 * This is called when the ACL is being deleted or queue depth is changing.
310 */
311void ft_sess_close(struct se_session *se_sess)
312{
313 struct ft_sess *sess = se_sess->fabric_sess_ptr;
Kiran Patil3699d922011-04-18 16:24:14 -0700314 u32 port_id;
315
316 mutex_lock(&ft_lport_lock);
Kiran Patil3699d922011-04-18 16:24:14 -0700317 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);
Bart Van Asschede7ee9a2016-01-05 14:47:58 +0100325 ft_close_sess(sess);
Kiran Patil3699d922011-04-18 16:24:14 -0700326 /* 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;
Kiran Patil3699d922011-04-18 16:24:14 -0700354 u32 fcp_parm;
355
Andy Grovere3d44402014-04-04 16:54:15 -0700356 tport = ft_tport_get(rdata->local_port);
Kiran Patil3699d922011-04-18 16:24:14 -0700357 if (!tport)
Mark Rustadedec8df2012-12-21 10:58:19 -0800358 goto not_target; /* not a target for this local port */
Kiran Patil3699d922011-04-18 16:24:14 -0700359
Kiran Patil3699d922011-04-18 16:24:14 -0700360 if (!rspp)
361 goto fill;
362
363 if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
364 return FC_SPP_RESP_NO_PA;
365
366 /*
367 * If both target and initiator bits are off, the SPP is invalid.
368 */
369 fcp_parm = ntohl(rspp->spp_params);
370 if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
371 return FC_SPP_RESP_INVL;
372
373 /*
374 * Create session (image pair) only if requested by
375 * EST_IMG_PAIR flag and if the requestor is an initiator.
376 */
377 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
378 spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
379 if (!(fcp_parm & FCP_SPPF_INIT_FCN))
380 return FC_SPP_RESP_CONF;
Nicholas Bellinger7fbef3d2016-01-07 22:17:16 -0800381 sess = ft_sess_create(tport, rdata->ids.port_id, rdata);
Kiran Patil3699d922011-04-18 16:24:14 -0700382 if (!sess)
383 return FC_SPP_RESP_RES;
384 if (!sess->params)
385 rdata->prli_count++;
386 sess->params = fcp_parm;
387 sess->port_name = rdata->ids.port_name;
388 sess->max_frame = rdata->maxframe_size;
389
390 /* XXX TBD - clearing actions. unit attn, see 4.10 */
391 }
392
393 /*
394 * OR in our service parameters with other provider (initiator), if any.
Kiran Patil3699d922011-04-18 16:24:14 -0700395 */
396fill:
397 fcp_parm = ntohl(spp->spp_params);
Mark Rustadf2eeba22012-12-21 10:58:14 -0800398 fcp_parm &= ~FCP_SPPF_RETRY;
Kiran Patil3699d922011-04-18 16:24:14 -0700399 spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
400 return FC_SPP_RESP_ACK;
Mark Rustadedec8df2012-12-21 10:58:19 -0800401
402not_target:
403 fcp_parm = ntohl(spp->spp_params);
404 fcp_parm &= ~FCP_SPPF_TARG_FCN;
405 spp->spp_params = htonl(fcp_parm);
406 return 0;
Kiran Patil3699d922011-04-18 16:24:14 -0700407}
408
409/**
410 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
411 * @rdata: remote port private
412 * @spp_len: service parameter page length
413 * @rspp: received service parameter page (NULL for outgoing PRLI)
414 * @spp: response service parameter page
415 *
416 * Returns spp response code.
417 */
418static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
419 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
420{
421 int ret;
422
423 mutex_lock(&ft_lport_lock);
424 ret = ft_prli_locked(rdata, spp_len, rspp, spp);
425 mutex_unlock(&ft_lport_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700426 pr_debug("port_id %x flags %x ret %x\n",
Kiran Patil3699d922011-04-18 16:24:14 -0700427 rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
428 return ret;
429}
430
Kiran Patil3699d922011-04-18 16:24:14 -0700431static void ft_sess_free(struct kref *kref)
432{
433 struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
434
Yi Zou9f4ad442012-12-10 17:04:00 -0800435 transport_deregister_session(sess->se_sess);
Wei Yongjuna6ad57e2013-03-11 21:48:28 +0800436 kfree_rcu(sess, rcu);
Kiran Patil3699d922011-04-18 16:24:14 -0700437}
438
439void ft_sess_put(struct ft_sess *sess)
440{
441 int sess_held = atomic_read(&sess->kref.refcount);
442
443 BUG_ON(!sess_held);
444 kref_put(&sess->kref, ft_sess_free);
445}
446
447static void ft_prlo(struct fc_rport_priv *rdata)
448{
449 struct ft_sess *sess;
450 struct ft_tport *tport;
451
452 mutex_lock(&ft_lport_lock);
Denis Efremov08a162082012-08-18 16:10:31 +0400453 tport = rcu_dereference_protected(rdata->local_port->prov[FC_TYPE_FCP],
454 lockdep_is_held(&ft_lport_lock));
455
Kiran Patil3699d922011-04-18 16:24:14 -0700456 if (!tport) {
457 mutex_unlock(&ft_lport_lock);
458 return;
459 }
460 sess = ft_sess_delete(tport, rdata->ids.port_id);
461 if (!sess) {
462 mutex_unlock(&ft_lport_lock);
463 return;
464 }
465 mutex_unlock(&ft_lport_lock);
Bart Van Asschede7ee9a2016-01-05 14:47:58 +0100466 ft_close_sess(sess); /* release from table */
Kiran Patil3699d922011-04-18 16:24:14 -0700467 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 */
475static 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
Andy Grover6708bb22011-06-08 10:36:43 -0700480 pr_debug("sid %x\n", sid);
Kiran Patil3699d922011-04-18 16:24:14 -0700481
482 sess = ft_sess_get(lport, sid);
483 if (!sess) {
Andy Grover6708bb22011-06-08 10:36:43 -0700484 pr_debug("sid %x sess lookup failed\n", sid);
Kiran Patil3699d922011-04-18 16:24:14 -0700485 /* 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 */
495struct fc4_prov ft_prov = {
496 .prli = ft_prli,
497 .prlo = ft_prlo,
498 .recv = ft_recv,
499 .module = THIS_MODULE,
500};