target/tcm_fc: return detailed error in ft_sess_create()
[cascardo/linux.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         lport->service_params &= ~FCP_SPPF_TARG_FCN;
95         BUG_ON(tport != lport->prov[FC_TYPE_FCP]);
96         RCU_INIT_POINTER(lport->prov[FC_TYPE_FCP], NULL);
97
98         tpg = tport->tpg;
99         if (tpg) {
100                 tpg->tport = NULL;
101                 tport->tpg = NULL;
102         }
103         kfree_rcu(tport, rcu);
104 }
105
106 /*
107  * Add local port.
108  * Called thru fc_lport_iterate().
109  */
110 void ft_lport_add(struct fc_lport *lport, void *arg)
111 {
112         mutex_lock(&ft_lport_lock);
113         ft_tport_get(lport);
114         lport->service_params |= FCP_SPPF_TARG_FCN;
115         mutex_unlock(&ft_lport_lock);
116 }
117
118 /*
119  * Delete local port.
120  * Called thru fc_lport_iterate().
121  */
122 void 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  */
137 int 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  */
155 static 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  */
165 static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
166 {
167         struct ft_tport *tport;
168         struct hlist_head *head;
169         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)];
177         hlist_for_each_entry_rcu(sess, head, hash) {
178                 if (sess->port_id == port_id) {
179                         kref_get(&sess->kref);
180                         rcu_read_unlock();
181                         pr_debug("port_id %x found %p\n", port_id, sess);
182                         return sess;
183                 }
184         }
185 out:
186         rcu_read_unlock();
187         pr_debug("port_id %x not found\n", port_id);
188         return NULL;
189 }
190
191 static 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
205 /*
206  * Allocate session and enter it in the hash for the local port.
207  * Caller holds ft_lport_lock.
208  */
209 static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
210                                       struct fc_rport_priv *rdata)
211 {
212         struct se_portal_group *se_tpg = &tport->tpg->se_tpg;
213         struct ft_sess *sess;
214         struct hlist_head *head;
215         unsigned char initiatorname[TRANSPORT_IQN_LEN];
216
217         ft_format_wwn(&initiatorname[0], TRANSPORT_IQN_LEN, rdata->ids.port_name);
218
219         head = &tport->hash[ft_sess_hash(port_id)];
220         hlist_for_each_entry_rcu(sess, head, hash)
221                 if (sess->port_id == port_id)
222                         return sess;
223
224         sess = kzalloc(sizeof(*sess), GFP_KERNEL);
225         if (!sess)
226                 return ERR_PTR(-ENOMEM);
227
228         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);
236         if (IS_ERR(sess->se_sess)) {
237                 int rc = PTR_ERR(sess->se_sess);
238                 kfree(sess);
239                 sess = ERR_PTR(rc);
240         }
241         return sess;
242 }
243
244 /*
245  * Unhash the session.
246  * Caller holds ft_lport_lock.
247  */
248 static void ft_sess_unhash(struct ft_sess *sess)
249 {
250         struct ft_tport *tport = sess->tport;
251
252         hlist_del_rcu(&sess->hash);
253         BUG_ON(!tport->sess_count);
254         tport->sess_count--;
255         sess->port_id = -1;
256         sess->params = 0;
257 }
258
259 /*
260  * Delete session from hash.
261  * Caller holds ft_lport_lock.
262  */
263 static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
264 {
265         struct hlist_head *head;
266         struct ft_sess *sess;
267
268         head = &tport->hash[ft_sess_hash(port_id)];
269         hlist_for_each_entry_rcu(sess, head, hash) {
270                 if (sess->port_id == port_id) {
271                         ft_sess_unhash(sess);
272                         return sess;
273                 }
274         }
275         return NULL;
276 }
277
278 static void ft_close_sess(struct ft_sess *sess)
279 {
280         transport_deregister_session_configfs(sess->se_sess);
281         target_sess_cmd_list_set_waiting(sess->se_sess);
282         target_wait_for_sess_cmds(sess->se_sess);
283         ft_sess_put(sess);
284 }
285
286 /*
287  * Delete all sessions from tport.
288  * Caller holds ft_lport_lock.
289  */
290 static void ft_sess_delete_all(struct ft_tport *tport)
291 {
292         struct hlist_head *head;
293         struct ft_sess *sess;
294
295         for (head = tport->hash;
296              head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
297                 hlist_for_each_entry_rcu(sess, head, hash) {
298                         ft_sess_unhash(sess);
299                         ft_close_sess(sess);    /* release from table */
300                 }
301         }
302 }
303
304 /*
305  * TCM ops for sessions.
306  */
307
308 /*
309  * Remove session and send PRLO.
310  * This is called when the ACL is being deleted or queue depth is changing.
311  */
312 void ft_sess_close(struct se_session *se_sess)
313 {
314         struct ft_sess *sess = se_sess->fabric_sess_ptr;
315         u32 port_id;
316
317         mutex_lock(&ft_lport_lock);
318         port_id = sess->port_id;
319         if (port_id == -1) {
320                 mutex_unlock(&ft_lport_lock);
321                 return;
322         }
323         pr_debug("port_id %x\n", port_id);
324         ft_sess_unhash(sess);
325         mutex_unlock(&ft_lport_lock);
326         ft_close_sess(sess);
327         /* XXX Send LOGO or PRLO */
328         synchronize_rcu();              /* let transport deregister happen */
329 }
330
331 u32 ft_sess_get_index(struct se_session *se_sess)
332 {
333         struct ft_sess *sess = se_sess->fabric_sess_ptr;
334
335         return sess->port_id;   /* XXX TBD probably not what is needed */
336 }
337
338 u32 ft_sess_get_port_name(struct se_session *se_sess,
339                           unsigned char *buf, u32 len)
340 {
341         struct ft_sess *sess = se_sess->fabric_sess_ptr;
342
343         return ft_format_wwn(buf, len, sess->port_name);
344 }
345
346 /*
347  * libfc ops involving sessions.
348  */
349
350 static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
351                           const struct fc_els_spp *rspp, struct fc_els_spp *spp)
352 {
353         struct ft_tport *tport;
354         struct ft_sess *sess;
355         u32 fcp_parm;
356
357         tport = ft_tport_get(rdata->local_port);
358         if (!tport)
359                 goto not_target;        /* not a target for this local port */
360
361         if (!rspp)
362                 goto fill;
363
364         if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
365                 return FC_SPP_RESP_NO_PA;
366
367         /*
368          * If both target and initiator bits are off, the SPP is invalid.
369          */
370         fcp_parm = ntohl(rspp->spp_params);
371         if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
372                 return FC_SPP_RESP_INVL;
373
374         /*
375          * Create session (image pair) only if requested by
376          * EST_IMG_PAIR flag and if the requestor is an initiator.
377          */
378         if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
379                 spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
380                 if (!(fcp_parm & FCP_SPPF_INIT_FCN))
381                         return FC_SPP_RESP_CONF;
382                 sess = ft_sess_create(tport, rdata->ids.port_id, rdata);
383                 if (!sess)
384                         return FC_SPP_RESP_RES;
385                 if (!sess->params)
386                         rdata->prli_count++;
387                 sess->params = fcp_parm;
388                 sess->port_name = rdata->ids.port_name;
389                 sess->max_frame = rdata->maxframe_size;
390
391                 /* XXX TBD - clearing actions.  unit attn, see 4.10 */
392         }
393
394         /*
395          * OR in our service parameters with other provider (initiator), if any.
396          */
397 fill:
398         fcp_parm = ntohl(spp->spp_params);
399         fcp_parm &= ~FCP_SPPF_RETRY;
400         spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
401         return FC_SPP_RESP_ACK;
402
403 not_target:
404         fcp_parm = ntohl(spp->spp_params);
405         fcp_parm &= ~FCP_SPPF_TARG_FCN;
406         spp->spp_params = htonl(fcp_parm);
407         return 0;
408 }
409
410 /**
411  * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
412  * @rdata: remote port private
413  * @spp_len: service parameter page length
414  * @rspp: received service parameter page (NULL for outgoing PRLI)
415  * @spp: response service parameter page
416  *
417  * Returns spp response code.
418  */
419 static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
420                    const struct fc_els_spp *rspp, struct fc_els_spp *spp)
421 {
422         int ret;
423
424         mutex_lock(&ft_lport_lock);
425         ret = ft_prli_locked(rdata, spp_len, rspp, spp);
426         mutex_unlock(&ft_lport_lock);
427         pr_debug("port_id %x flags %x ret %x\n",
428                rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
429         return ret;
430 }
431
432 static void ft_sess_free(struct kref *kref)
433 {
434         struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
435
436         transport_deregister_session(sess->se_sess);
437         kfree_rcu(sess, rcu);
438 }
439
440 void ft_sess_put(struct ft_sess *sess)
441 {
442         int sess_held = atomic_read(&sess->kref.refcount);
443
444         BUG_ON(!sess_held);
445         kref_put(&sess->kref, ft_sess_free);
446 }
447
448 static void ft_prlo(struct fc_rport_priv *rdata)
449 {
450         struct ft_sess *sess;
451         struct ft_tport *tport;
452
453         mutex_lock(&ft_lport_lock);
454         tport = rcu_dereference_protected(rdata->local_port->prov[FC_TYPE_FCP],
455                                           lockdep_is_held(&ft_lport_lock));
456
457         if (!tport) {
458                 mutex_unlock(&ft_lport_lock);
459                 return;
460         }
461         sess = ft_sess_delete(tport, rdata->ids.port_id);
462         if (!sess) {
463                 mutex_unlock(&ft_lport_lock);
464                 return;
465         }
466         mutex_unlock(&ft_lport_lock);
467         ft_close_sess(sess);            /* release from table */
468         rdata->prli_count--;
469         /* XXX TBD - clearing actions.  unit attn, see 4.10 */
470 }
471
472 /*
473  * Handle incoming FCP request.
474  * Caller has verified that the frame is type FCP.
475  */
476 static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
477 {
478         struct ft_sess *sess;
479         u32 sid = fc_frame_sid(fp);
480
481         pr_debug("sid %x\n", sid);
482
483         sess = ft_sess_get(lport, sid);
484         if (!sess) {
485                 pr_debug("sid %x sess lookup failed\n", sid);
486                 /* TBD XXX - if FCP_CMND, send PRLO */
487                 fc_frame_free(fp);
488                 return;
489         }
490         ft_recv_req(sess, fp);  /* must do ft_sess_put() */
491 }
492
493 /*
494  * Provider ops for libfc.
495  */
496 struct fc4_prov ft_prov = {
497         .prli = ft_prli,
498         .prlo = ft_prlo,
499         .recv = ft_recv,
500         .module = THIS_MODULE,
501 };