]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/target/tcm_fc/tfc_sess.c
Merge tag 'arc-4.4-rc1-part2' of git://git.kernel.org/pub/scm/linux/kernel/git/vgupta/arc
[mirror_ubuntu-artful-kernel.git] / drivers / target / tcm_fc / tfc_sess.c
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 <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>
35 #include <scsi/libfc.h>
36
37 #include <target/target_core_base.h>
38 #include <target/target_core_fabric.h>
39
40 #include "tcm_fc.h"
41
42 static void ft_sess_delete_all(struct ft_tport *);
43
44 /*
45 * Lookup or allocate target local port.
46 * Caller holds ft_lport_lock.
47 */
48 static struct ft_tport *ft_tport_get(struct fc_lport *lport)
49 {
50 struct ft_tpg *tpg;
51 struct ft_tport *tport;
52 int i;
53
54 tport = rcu_dereference_protected(lport->prov[FC_TYPE_FCP],
55 lockdep_is_held(&ft_lport_lock));
56 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;
65 tpg->tport = tport;
66 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 /*
84 * Delete a target local port.
85 * Caller holds ft_lport_lock.
86 */
87 static 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]);
95 RCU_INIT_POINTER(lport->prov[FC_TYPE_FCP], NULL);
96
97 tpg = tport->tpg;
98 if (tpg) {
99 tpg->tport = NULL;
100 tport->tpg = NULL;
101 }
102 kfree_rcu(tport, rcu);
103 }
104
105 /*
106 * Add local port.
107 * Called thru fc_lport_iterate().
108 */
109 void ft_lport_add(struct fc_lport *lport, void *arg)
110 {
111 mutex_lock(&ft_lport_lock);
112 ft_tport_get(lport);
113 mutex_unlock(&ft_lport_lock);
114 }
115
116 /*
117 * Delete local port.
118 * Called thru fc_lport_iterate().
119 */
120 void 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 */
135 int 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 */
153 static 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 */
163 static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
164 {
165 struct ft_tport *tport;
166 struct hlist_head *head;
167 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)];
175 hlist_for_each_entry_rcu(sess, head, hash) {
176 if (sess->port_id == port_id) {
177 kref_get(&sess->kref);
178 rcu_read_unlock();
179 pr_debug("port_id %x found %p\n", port_id, sess);
180 return sess;
181 }
182 }
183 out:
184 rcu_read_unlock();
185 pr_debug("port_id %x not found\n", port_id);
186 return NULL;
187 }
188
189 /*
190 * Allocate session and enter it in the hash for the local port.
191 * Caller holds ft_lport_lock.
192 */
193 static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
194 struct ft_node_acl *acl)
195 {
196 struct ft_sess *sess;
197 struct hlist_head *head;
198
199 head = &tport->hash[ft_sess_hash(port_id)];
200 hlist_for_each_entry_rcu(sess, head, hash)
201 if (sess->port_id == port_id)
202 return sess;
203
204 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
205 if (!sess)
206 return NULL;
207
208 sess->se_sess = transport_init_session_tags(TCM_FC_DEFAULT_TAGS,
209 sizeof(struct ft_cmd),
210 TARGET_PROT_NORMAL);
211 if (IS_ERR(sess->se_sess)) {
212 kfree(sess);
213 return NULL;
214 }
215 sess->se_sess->se_node_acl = &acl->se_node_acl;
216 sess->tport = tport;
217 sess->port_id = port_id;
218 kref_init(&sess->kref); /* ref for table entry */
219 hlist_add_head_rcu(&sess->hash, head);
220 tport->sess_count++;
221
222 pr_debug("port_id %x sess %p\n", port_id, sess);
223
224 transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl,
225 sess->se_sess, sess);
226 return sess;
227 }
228
229 /*
230 * Unhash the session.
231 * Caller holds ft_lport_lock.
232 */
233 static void ft_sess_unhash(struct ft_sess *sess)
234 {
235 struct ft_tport *tport = sess->tport;
236
237 hlist_del_rcu(&sess->hash);
238 BUG_ON(!tport->sess_count);
239 tport->sess_count--;
240 sess->port_id = -1;
241 sess->params = 0;
242 }
243
244 /*
245 * Delete session from hash.
246 * Caller holds ft_lport_lock.
247 */
248 static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
249 {
250 struct hlist_head *head;
251 struct ft_sess *sess;
252
253 head = &tport->hash[ft_sess_hash(port_id)];
254 hlist_for_each_entry_rcu(sess, head, hash) {
255 if (sess->port_id == port_id) {
256 ft_sess_unhash(sess);
257 return sess;
258 }
259 }
260 return NULL;
261 }
262
263 /*
264 * Delete all sessions from tport.
265 * Caller holds ft_lport_lock.
266 */
267 static void ft_sess_delete_all(struct ft_tport *tport)
268 {
269 struct hlist_head *head;
270 struct ft_sess *sess;
271
272 for (head = tport->hash;
273 head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
274 hlist_for_each_entry_rcu(sess, head, hash) {
275 ft_sess_unhash(sess);
276 transport_deregister_session_configfs(sess->se_sess);
277 ft_sess_put(sess); /* release from table */
278 }
279 }
280 }
281
282 /*
283 * TCM ops for sessions.
284 */
285
286 /*
287 * Determine whether session is allowed to be shutdown in the current context.
288 * Returns non-zero if the session should be shutdown.
289 */
290 int ft_sess_shutdown(struct se_session *se_sess)
291 {
292 struct ft_sess *sess = se_sess->fabric_sess_ptr;
293
294 pr_debug("port_id %x\n", sess->port_id);
295 return 1;
296 }
297
298 /*
299 * Remove session and send PRLO.
300 * This is called when the ACL is being deleted or queue depth is changing.
301 */
302 void ft_sess_close(struct se_session *se_sess)
303 {
304 struct ft_sess *sess = se_sess->fabric_sess_ptr;
305 u32 port_id;
306
307 mutex_lock(&ft_lport_lock);
308 port_id = sess->port_id;
309 if (port_id == -1) {
310 mutex_unlock(&ft_lport_lock);
311 return;
312 }
313 pr_debug("port_id %x\n", port_id);
314 ft_sess_unhash(sess);
315 mutex_unlock(&ft_lport_lock);
316 transport_deregister_session_configfs(se_sess);
317 ft_sess_put(sess);
318 /* XXX Send LOGO or PRLO */
319 synchronize_rcu(); /* let transport deregister happen */
320 }
321
322 u32 ft_sess_get_index(struct se_session *se_sess)
323 {
324 struct ft_sess *sess = se_sess->fabric_sess_ptr;
325
326 return sess->port_id; /* XXX TBD probably not what is needed */
327 }
328
329 u32 ft_sess_get_port_name(struct se_session *se_sess,
330 unsigned char *buf, u32 len)
331 {
332 struct ft_sess *sess = se_sess->fabric_sess_ptr;
333
334 return ft_format_wwn(buf, len, sess->port_name);
335 }
336
337 /*
338 * libfc ops involving sessions.
339 */
340
341 static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
342 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
343 {
344 struct ft_tport *tport;
345 struct ft_sess *sess;
346 struct ft_node_acl *acl;
347 u32 fcp_parm;
348
349 tport = ft_tport_get(rdata->local_port);
350 if (!tport)
351 goto not_target; /* not a target for this local port */
352
353 acl = ft_acl_get(tport->tpg, rdata);
354 if (!acl)
355 goto not_target; /* no target for this remote */
356
357 if (!rspp)
358 goto fill;
359
360 if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
361 return FC_SPP_RESP_NO_PA;
362
363 /*
364 * If both target and initiator bits are off, the SPP is invalid.
365 */
366 fcp_parm = ntohl(rspp->spp_params);
367 if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
368 return FC_SPP_RESP_INVL;
369
370 /*
371 * Create session (image pair) only if requested by
372 * EST_IMG_PAIR flag and if the requestor is an initiator.
373 */
374 if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
375 spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
376 if (!(fcp_parm & FCP_SPPF_INIT_FCN))
377 return FC_SPP_RESP_CONF;
378 sess = ft_sess_create(tport, rdata->ids.port_id, acl);
379 if (!sess)
380 return FC_SPP_RESP_RES;
381 if (!sess->params)
382 rdata->prli_count++;
383 sess->params = fcp_parm;
384 sess->port_name = rdata->ids.port_name;
385 sess->max_frame = rdata->maxframe_size;
386
387 /* XXX TBD - clearing actions. unit attn, see 4.10 */
388 }
389
390 /*
391 * OR in our service parameters with other provider (initiator), if any.
392 */
393 fill:
394 fcp_parm = ntohl(spp->spp_params);
395 fcp_parm &= ~FCP_SPPF_RETRY;
396 spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
397 return FC_SPP_RESP_ACK;
398
399 not_target:
400 fcp_parm = ntohl(spp->spp_params);
401 fcp_parm &= ~FCP_SPPF_TARG_FCN;
402 spp->spp_params = htonl(fcp_parm);
403 return 0;
404 }
405
406 /**
407 * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
408 * @rdata: remote port private
409 * @spp_len: service parameter page length
410 * @rspp: received service parameter page (NULL for outgoing PRLI)
411 * @spp: response service parameter page
412 *
413 * Returns spp response code.
414 */
415 static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
416 const struct fc_els_spp *rspp, struct fc_els_spp *spp)
417 {
418 int ret;
419
420 mutex_lock(&ft_lport_lock);
421 ret = ft_prli_locked(rdata, spp_len, rspp, spp);
422 mutex_unlock(&ft_lport_lock);
423 pr_debug("port_id %x flags %x ret %x\n",
424 rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
425 return ret;
426 }
427
428 static void ft_sess_free(struct kref *kref)
429 {
430 struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
431
432 transport_deregister_session(sess->se_sess);
433 kfree_rcu(sess, rcu);
434 }
435
436 void ft_sess_put(struct ft_sess *sess)
437 {
438 int sess_held = atomic_read(&sess->kref.refcount);
439
440 BUG_ON(!sess_held);
441 kref_put(&sess->kref, ft_sess_free);
442 }
443
444 static void ft_prlo(struct fc_rport_priv *rdata)
445 {
446 struct ft_sess *sess;
447 struct ft_tport *tport;
448
449 mutex_lock(&ft_lport_lock);
450 tport = rcu_dereference_protected(rdata->local_port->prov[FC_TYPE_FCP],
451 lockdep_is_held(&ft_lport_lock));
452
453 if (!tport) {
454 mutex_unlock(&ft_lport_lock);
455 return;
456 }
457 sess = ft_sess_delete(tport, rdata->ids.port_id);
458 if (!sess) {
459 mutex_unlock(&ft_lport_lock);
460 return;
461 }
462 mutex_unlock(&ft_lport_lock);
463 transport_deregister_session_configfs(sess->se_sess);
464 ft_sess_put(sess); /* release from table */
465 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 */
473 static 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
478 pr_debug("sid %x\n", sid);
479
480 sess = ft_sess_get(lport, sid);
481 if (!sess) {
482 pr_debug("sid %x sess lookup failed\n", sid);
483 /* 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 */
493 struct fc4_prov ft_prov = {
494 .prli = ft_prli,
495 .prlo = ft_prlo,
496 .recv = ft_recv,
497 .module = THIS_MODULE,
498 };