[SCSI] libfc: whenever queueing delete ev for rport, set state to NONE
[safe/jmp/linux-2.6] / drivers / scsi / libfc / fc_rport.c
1 /*
2  * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
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  * Maintained at www.Open-FCoE.org
18  */
19
20 /*
21  * RPORT GENERAL INFO
22  *
23  * This file contains all processing regarding fc_rports. It contains the
24  * rport state machine and does all rport interaction with the transport class.
25  * There should be no other places in libfc that interact directly with the
26  * transport class in regards to adding and deleting rports.
27  *
28  * fc_rport's represent N_Port's within the fabric.
29  */
30
31 /*
32  * RPORT LOCKING
33  *
34  * The rport should never hold the rport mutex and then attempt to acquire
35  * either the lport or disc mutexes. The rport's mutex is considered lesser
36  * than both the lport's mutex and the disc mutex. Refer to fc_lport.c for
37  * more comments on the heirarchy.
38  *
39  * The locking strategy is similar to the lport's strategy. The lock protects
40  * the rport's states and is held and released by the entry points to the rport
41  * block. All _enter_* functions correspond to rport states and expect the rport
42  * mutex to be locked before calling them. This means that rports only handle
43  * one request or response at a time, since they're not critical for the I/O
44  * path this potential over-use of the mutex is acceptable.
45  */
46
47 #include <linux/kernel.h>
48 #include <linux/spinlock.h>
49 #include <linux/interrupt.h>
50 #include <linux/rcupdate.h>
51 #include <linux/timer.h>
52 #include <linux/workqueue.h>
53 #include <asm/unaligned.h>
54
55 #include <scsi/libfc.h>
56 #include <scsi/fc_encode.h>
57
58 static int fc_rport_debug;
59
60 #define FC_DEBUG_RPORT(fmt...)                  \
61         do {                                    \
62                 if (fc_rport_debug)             \
63                         FC_DBG(fmt);            \
64         } while (0)
65
66 struct workqueue_struct *rport_event_queue;
67
68 static void fc_rport_enter_plogi(struct fc_rport *);
69 static void fc_rport_enter_prli(struct fc_rport *);
70 static void fc_rport_enter_rtv(struct fc_rport *);
71 static void fc_rport_enter_ready(struct fc_rport *);
72 static void fc_rport_enter_logo(struct fc_rport *);
73
74 static void fc_rport_recv_plogi_req(struct fc_rport *,
75                                     struct fc_seq *, struct fc_frame *);
76 static void fc_rport_recv_prli_req(struct fc_rport *,
77                                    struct fc_seq *, struct fc_frame *);
78 static void fc_rport_recv_prlo_req(struct fc_rport *,
79                                    struct fc_seq *, struct fc_frame *);
80 static void fc_rport_recv_logo_req(struct fc_rport *,
81                                    struct fc_seq *, struct fc_frame *);
82 static void fc_rport_timeout(struct work_struct *);
83 static void fc_rport_error(struct fc_rport *, struct fc_frame *);
84 static void fc_rport_error_retry(struct fc_rport *, struct fc_frame *);
85 static void fc_rport_work(struct work_struct *);
86
87 static const char *fc_rport_state_names[] = {
88         [RPORT_ST_NONE] = "None",
89         [RPORT_ST_INIT] = "Init",
90         [RPORT_ST_PLOGI] = "PLOGI",
91         [RPORT_ST_PRLI] = "PRLI",
92         [RPORT_ST_RTV] = "RTV",
93         [RPORT_ST_READY] = "Ready",
94         [RPORT_ST_LOGO] = "LOGO",
95 };
96
97 static void fc_rport_rogue_destroy(struct device *dev)
98 {
99         struct fc_rport *rport = dev_to_rport(dev);
100         FC_DEBUG_RPORT("Destroying rogue rport (%6x)\n", rport->port_id);
101         kfree(rport);
102 }
103
104 struct fc_rport *fc_rport_rogue_create(struct fc_disc_port *dp)
105 {
106         struct fc_rport *rport;
107         struct fc_rport_libfc_priv *rdata;
108         rport = kzalloc(sizeof(*rport) + sizeof(*rdata), GFP_KERNEL);
109
110         if (!rport)
111                 return NULL;
112
113         rdata = RPORT_TO_PRIV(rport);
114
115         rport->dd_data = rdata;
116         rport->port_id = dp->ids.port_id;
117         rport->port_name = dp->ids.port_name;
118         rport->node_name = dp->ids.node_name;
119         rport->roles = dp->ids.roles;
120         rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
121         /*
122          * Note: all this libfc rogue rport code will be removed for
123          * upstream so it fine that this is really ugly and hacky right now.
124          */
125         device_initialize(&rport->dev);
126         rport->dev.release = fc_rport_rogue_destroy;
127
128         mutex_init(&rdata->rp_mutex);
129         rdata->local_port = dp->lp;
130         rdata->trans_state = FC_PORTSTATE_ROGUE;
131         rdata->rp_state = RPORT_ST_INIT;
132         rdata->event = RPORT_EV_NONE;
133         rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
134         rdata->ops = NULL;
135         rdata->e_d_tov = dp->lp->e_d_tov;
136         rdata->r_a_tov = dp->lp->r_a_tov;
137         INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
138         INIT_WORK(&rdata->event_work, fc_rport_work);
139         /*
140          * For good measure, but not necessary as we should only
141          * add REAL rport to the lport list.
142          */
143         INIT_LIST_HEAD(&rdata->peers);
144
145         return rport;
146 }
147
148 /**
149  * fc_rport_state() - return a string for the state the rport is in
150  * @rport: The rport whose state we want to get a string for
151  */
152 static const char *fc_rport_state(struct fc_rport *rport)
153 {
154         const char *cp;
155         struct fc_rport_libfc_priv *rdata = rport->dd_data;
156
157         cp = fc_rport_state_names[rdata->rp_state];
158         if (!cp)
159                 cp = "Unknown";
160         return cp;
161 }
162
163 /**
164  * fc_set_rport_loss_tmo() - Set the remote port loss timeout in seconds.
165  * @rport: Pointer to Fibre Channel remote port structure
166  * @timeout: timeout in seconds
167  */
168 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
169 {
170         if (timeout)
171                 rport->dev_loss_tmo = timeout + 5;
172         else
173                 rport->dev_loss_tmo = 30;
174 }
175 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
176
177 /**
178  * fc_plogi_get_maxframe() - Get max payload from the common service parameters
179  * @flp: FLOGI payload structure
180  * @maxval: upper limit, may be less than what is in the service parameters
181  */
182 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
183                                           unsigned int maxval)
184 {
185         unsigned int mfs;
186
187         /*
188          * Get max payload from the common service parameters and the
189          * class 3 receive data field size.
190          */
191         mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
192         if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
193                 maxval = mfs;
194         mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
195         if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
196                 maxval = mfs;
197         return maxval;
198 }
199
200 /**
201  * fc_rport_state_enter() - Change the rport's state
202  * @rport: The rport whose state should change
203  * @new: The new state of the rport
204  *
205  * Locking Note: Called with the rport lock held
206  */
207 static void fc_rport_state_enter(struct fc_rport *rport,
208                                  enum fc_rport_state new)
209 {
210         struct fc_rport_libfc_priv *rdata = rport->dd_data;
211         if (rdata->rp_state != new)
212                 rdata->retries = 0;
213         rdata->rp_state = new;
214 }
215
216 static void fc_rport_work(struct work_struct *work)
217 {
218         u32 port_id;
219         struct fc_rport_libfc_priv *rdata =
220                 container_of(work, struct fc_rport_libfc_priv, event_work);
221         enum fc_rport_event event;
222         enum fc_rport_trans_state trans_state;
223         struct fc_lport *lport = rdata->local_port;
224         struct fc_rport_operations *rport_ops;
225         struct fc_rport *rport = PRIV_TO_RPORT(rdata);
226
227         mutex_lock(&rdata->rp_mutex);
228         event = rdata->event;
229         rport_ops = rdata->ops;
230
231         if (event == RPORT_EV_CREATED) {
232                 struct fc_rport *new_rport;
233                 struct fc_rport_libfc_priv *new_rdata;
234                 struct fc_rport_identifiers ids;
235
236                 ids.port_id = rport->port_id;
237                 ids.roles = rport->roles;
238                 ids.port_name = rport->port_name;
239                 ids.node_name = rport->node_name;
240
241                 mutex_unlock(&rdata->rp_mutex);
242
243                 new_rport = fc_remote_port_add(lport->host, 0, &ids);
244                 if (new_rport) {
245                         /*
246                          * Switch from the rogue rport to the rport
247                          * returned by the FC class.
248                          */
249                         new_rport->maxframe_size = rport->maxframe_size;
250
251                         new_rdata = new_rport->dd_data;
252                         new_rdata->e_d_tov = rdata->e_d_tov;
253                         new_rdata->r_a_tov = rdata->r_a_tov;
254                         new_rdata->ops = rdata->ops;
255                         new_rdata->local_port = rdata->local_port;
256                         new_rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
257                         new_rdata->trans_state = FC_PORTSTATE_REAL;
258                         mutex_init(&new_rdata->rp_mutex);
259                         INIT_DELAYED_WORK(&new_rdata->retry_work,
260                                           fc_rport_timeout);
261                         INIT_LIST_HEAD(&new_rdata->peers);
262                         INIT_WORK(&new_rdata->event_work, fc_rport_work);
263
264                         fc_rport_state_enter(new_rport, RPORT_ST_READY);
265                 } else {
266                         FC_DBG("Failed to create the rport for port "
267                                "(%6x).\n", ids.port_id);
268                         event = RPORT_EV_FAILED;
269                 }
270                 if (rport->port_id != FC_FID_DIR_SERV)
271                         if (rport_ops->event_callback)
272                                 rport_ops->event_callback(lport, rport,
273                                                           RPORT_EV_FAILED);
274                 put_device(&rport->dev);
275                 rport = new_rport;
276                 rdata = new_rport->dd_data;
277                 if (rport_ops->event_callback)
278                         rport_ops->event_callback(lport, rport, event);
279         } else if ((event == RPORT_EV_FAILED) ||
280                    (event == RPORT_EV_LOGO) ||
281                    (event == RPORT_EV_STOP)) {
282                 trans_state = rdata->trans_state;
283                 mutex_unlock(&rdata->rp_mutex);
284                 if (rport_ops->event_callback)
285                         rport_ops->event_callback(lport, rport, event);
286                 if (trans_state == FC_PORTSTATE_ROGUE)
287                         put_device(&rport->dev);
288                 else {
289                         port_id = rport->port_id;
290                         fc_remote_port_delete(rport);
291                         lport->tt.exch_mgr_reset(lport, 0, port_id);
292                         lport->tt.exch_mgr_reset(lport, port_id, 0);
293                 }
294         } else
295                 mutex_unlock(&rdata->rp_mutex);
296 }
297
298 /**
299  * fc_rport_login() - Start the remote port login state machine
300  * @rport: Fibre Channel remote port
301  *
302  * Locking Note: Called without the rport lock held. This
303  * function will hold the rport lock, call an _enter_*
304  * function and then unlock the rport.
305  */
306 int fc_rport_login(struct fc_rport *rport)
307 {
308         struct fc_rport_libfc_priv *rdata = rport->dd_data;
309
310         mutex_lock(&rdata->rp_mutex);
311
312         FC_DEBUG_RPORT("Login to port (%6x)\n", rport->port_id);
313
314         fc_rport_enter_plogi(rport);
315
316         mutex_unlock(&rdata->rp_mutex);
317
318         return 0;
319 }
320
321 /**
322  * fc_rport_logoff() - Logoff and remove an rport
323  * @rport: Fibre Channel remote port to be removed
324  *
325  * Locking Note: Called without the rport lock held. This
326  * function will hold the rport lock, call an _enter_*
327  * function and then unlock the rport.
328  */
329 int fc_rport_logoff(struct fc_rport *rport)
330 {
331         struct fc_rport_libfc_priv *rdata = rport->dd_data;
332         struct fc_lport *lport = rdata->local_port;
333
334         mutex_lock(&rdata->rp_mutex);
335
336         FC_DEBUG_RPORT("Remove port (%6x)\n", rport->port_id);
337
338         if (rdata->rp_state == RPORT_ST_NONE) {
339                 FC_DEBUG_RPORT("(%6x): Port (%6x) in NONE state,"
340                                " not removing", fc_host_port_id(lport->host),
341                                rport->port_id);
342                 mutex_unlock(&rdata->rp_mutex);
343                 goto out;
344         }
345
346         fc_rport_enter_logo(rport);
347
348         /*
349          * Change the state to NONE so that we discard
350          * the response.
351          */
352         fc_rport_state_enter(rport, RPORT_ST_NONE);
353
354         mutex_unlock(&rdata->rp_mutex);
355
356         cancel_delayed_work_sync(&rdata->retry_work);
357
358         mutex_lock(&rdata->rp_mutex);
359
360         rdata->event = RPORT_EV_STOP;
361         queue_work(rport_event_queue, &rdata->event_work);
362
363         mutex_unlock(&rdata->rp_mutex);
364
365 out:
366         return 0;
367 }
368
369 /**
370  * fc_rport_enter_ready() - The rport is ready
371  * @rport: Fibre Channel remote port that is ready
372  *
373  * Locking Note: The rport lock is expected to be held before calling
374  * this routine.
375  */
376 static void fc_rport_enter_ready(struct fc_rport *rport)
377 {
378         struct fc_rport_libfc_priv *rdata = rport->dd_data;
379
380         fc_rport_state_enter(rport, RPORT_ST_READY);
381
382         FC_DEBUG_RPORT("Port (%6x) is Ready\n", rport->port_id);
383
384         rdata->event = RPORT_EV_CREATED;
385         queue_work(rport_event_queue, &rdata->event_work);
386 }
387
388 /**
389  * fc_rport_timeout() - Handler for the retry_work timer.
390  * @work: The work struct of the fc_rport_libfc_priv
391  *
392  * Locking Note: Called without the rport lock held. This
393  * function will hold the rport lock, call an _enter_*
394  * function and then unlock the rport.
395  */
396 static void fc_rport_timeout(struct work_struct *work)
397 {
398         struct fc_rport_libfc_priv *rdata =
399                 container_of(work, struct fc_rport_libfc_priv, retry_work.work);
400         struct fc_rport *rport = PRIV_TO_RPORT(rdata);
401
402         mutex_lock(&rdata->rp_mutex);
403
404         switch (rdata->rp_state) {
405         case RPORT_ST_PLOGI:
406                 fc_rport_enter_plogi(rport);
407                 break;
408         case RPORT_ST_PRLI:
409                 fc_rport_enter_prli(rport);
410                 break;
411         case RPORT_ST_RTV:
412                 fc_rport_enter_rtv(rport);
413                 break;
414         case RPORT_ST_LOGO:
415                 fc_rport_enter_logo(rport);
416                 break;
417         case RPORT_ST_READY:
418         case RPORT_ST_INIT:
419         case RPORT_ST_NONE:
420                 break;
421         }
422
423         mutex_unlock(&rdata->rp_mutex);
424         put_device(&rport->dev);
425 }
426
427 /**
428  * fc_rport_error() - Error handler, called once retries have been exhausted
429  * @rport: The fc_rport object
430  * @fp: The frame pointer
431  *
432  * Locking Note: The rport lock is expected to be held before
433  * calling this routine
434  */
435 static void fc_rport_error(struct fc_rport *rport, struct fc_frame *fp)
436 {
437         struct fc_rport_libfc_priv *rdata = rport->dd_data;
438
439         FC_DEBUG_RPORT("Error %ld in state %s, retries %d\n",
440                        PTR_ERR(fp), fc_rport_state(rport), rdata->retries);
441
442         switch (rdata->rp_state) {
443         case RPORT_ST_PLOGI:
444         case RPORT_ST_PRLI:
445         case RPORT_ST_LOGO:
446                 rdata->event = RPORT_EV_FAILED;
447                 fc_rport_state_enter(rport, RPORT_ST_NONE);
448                 queue_work(rport_event_queue,
449                            &rdata->event_work);
450                 break;
451         case RPORT_ST_RTV:
452                 fc_rport_enter_ready(rport);
453                 break;
454         case RPORT_ST_NONE:
455         case RPORT_ST_READY:
456         case RPORT_ST_INIT:
457                 break;
458         }
459 }
460
461 /**
462  * fc_rport_error_retry() - Error handler when retries are desired
463  * @rport: The fc_rport object
464  * @fp: The frame pointer
465  *
466  * If the error was an exchange timeout retry immediately,
467  * otherwise wait for E_D_TOV.
468  *
469  * Locking Note: The rport lock is expected to be held before
470  * calling this routine
471  */
472 static void fc_rport_error_retry(struct fc_rport *rport, struct fc_frame *fp)
473 {
474         struct fc_rport_libfc_priv *rdata = rport->dd_data;
475         unsigned long delay = FC_DEF_E_D_TOV;
476
477         /* make sure this isn't an FC_EX_CLOSED error, never retry those */
478         if (PTR_ERR(fp) == -FC_EX_CLOSED)
479                 return fc_rport_error(rport, fp);
480
481         if (rdata->retries < rdata->local_port->max_retry_count) {
482                 FC_DEBUG_RPORT("Error %ld in state %s, retrying\n",
483                                PTR_ERR(fp), fc_rport_state(rport));
484                 rdata->retries++;
485                 /* no additional delay on exchange timeouts */
486                 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
487                         delay = 0;
488                 get_device(&rport->dev);
489                 schedule_delayed_work(&rdata->retry_work, delay);
490                 return;
491         }
492
493         return fc_rport_error(rport, fp);
494 }
495
496 /**
497  * fc_rport_plogi_recv_resp() - Handle incoming ELS PLOGI response
498  * @sp: current sequence in the PLOGI exchange
499  * @fp: response frame
500  * @rp_arg: Fibre Channel remote port
501  *
502  * Locking Note: This function will be called without the rport lock
503  * held, but it will lock, call an _enter_* function or fc_rport_error
504  * and then unlock the rport.
505  */
506 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
507                                 void *rp_arg)
508 {
509         struct fc_rport *rport = rp_arg;
510         struct fc_rport_libfc_priv *rdata = rport->dd_data;
511         struct fc_lport *lport = rdata->local_port;
512         struct fc_els_flogi *plp;
513         unsigned int tov;
514         u16 csp_seq;
515         u16 cssp_seq;
516         u8 op;
517
518         mutex_lock(&rdata->rp_mutex);
519
520         FC_DEBUG_RPORT("Received a PLOGI response from port (%6x)\n",
521                        rport->port_id);
522
523         if (rdata->rp_state != RPORT_ST_PLOGI) {
524                 FC_DBG("Received a PLOGI response, but in state %s\n",
525                        fc_rport_state(rport));
526                 if (IS_ERR(fp))
527                         goto err;
528                 goto out;
529         }
530
531         if (IS_ERR(fp)) {
532                 fc_rport_error_retry(rport, fp);
533                 goto err;
534         }
535
536         op = fc_frame_payload_op(fp);
537         if (op == ELS_LS_ACC &&
538             (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
539                 rport->port_name = get_unaligned_be64(&plp->fl_wwpn);
540                 rport->node_name = get_unaligned_be64(&plp->fl_wwnn);
541
542                 tov = ntohl(plp->fl_csp.sp_e_d_tov);
543                 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
544                         tov /= 1000;
545                 if (tov > rdata->e_d_tov)
546                         rdata->e_d_tov = tov;
547                 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
548                 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
549                 if (cssp_seq < csp_seq)
550                         csp_seq = cssp_seq;
551                 rdata->max_seq = csp_seq;
552                 rport->maxframe_size =
553                         fc_plogi_get_maxframe(plp, lport->mfs);
554
555                 /*
556                  * If the rport is one of the well known addresses
557                  * we skip PRLI and RTV and go straight to READY.
558                  */
559                 if (rport->port_id >= FC_FID_DOM_MGR)
560                         fc_rport_enter_ready(rport);
561                 else
562                         fc_rport_enter_prli(rport);
563         } else
564                 fc_rport_error_retry(rport, fp);
565
566 out:
567         fc_frame_free(fp);
568 err:
569         mutex_unlock(&rdata->rp_mutex);
570         put_device(&rport->dev);
571 }
572
573 /**
574  * fc_rport_enter_plogi() - Send Port Login (PLOGI) request to peer
575  * @rport: Fibre Channel remote port to send PLOGI to
576  *
577  * Locking Note: The rport lock is expected to be held before calling
578  * this routine.
579  */
580 static void fc_rport_enter_plogi(struct fc_rport *rport)
581 {
582         struct fc_rport_libfc_priv *rdata = rport->dd_data;
583         struct fc_lport *lport = rdata->local_port;
584         struct fc_frame *fp;
585
586         FC_DEBUG_RPORT("Port (%6x) entered PLOGI state from %s state\n",
587                        rport->port_id, fc_rport_state(rport));
588
589         fc_rport_state_enter(rport, RPORT_ST_PLOGI);
590
591         rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
592         fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
593         if (!fp) {
594                 fc_rport_error_retry(rport, fp);
595                 return;
596         }
597         rdata->e_d_tov = lport->e_d_tov;
598
599         if (!lport->tt.elsct_send(lport, rport, fp, ELS_PLOGI,
600                                   fc_rport_plogi_resp, rport, lport->e_d_tov))
601                 fc_rport_error_retry(rport, fp);
602         else
603                 get_device(&rport->dev);
604 }
605
606 /**
607  * fc_rport_prli_resp() - Process Login (PRLI) response handler
608  * @sp: current sequence in the PRLI exchange
609  * @fp: response frame
610  * @rp_arg: Fibre Channel remote port
611  *
612  * Locking Note: This function will be called without the rport lock
613  * held, but it will lock, call an _enter_* function or fc_rport_error
614  * and then unlock the rport.
615  */
616 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
617                                void *rp_arg)
618 {
619         struct fc_rport *rport = rp_arg;
620         struct fc_rport_libfc_priv *rdata = rport->dd_data;
621         struct {
622                 struct fc_els_prli prli;
623                 struct fc_els_spp spp;
624         } *pp;
625         u32 roles = FC_RPORT_ROLE_UNKNOWN;
626         u32 fcp_parm = 0;
627         u8 op;
628
629         mutex_lock(&rdata->rp_mutex);
630
631         FC_DEBUG_RPORT("Received a PRLI response from port (%6x)\n",
632                        rport->port_id);
633
634         if (rdata->rp_state != RPORT_ST_PRLI) {
635                 FC_DBG("Received a PRLI response, but in state %s\n",
636                        fc_rport_state(rport));
637                 if (IS_ERR(fp))
638                         goto err;
639                 goto out;
640         }
641
642         if (IS_ERR(fp)) {
643                 fc_rport_error_retry(rport, fp);
644                 goto err;
645         }
646
647         op = fc_frame_payload_op(fp);
648         if (op == ELS_LS_ACC) {
649                 pp = fc_frame_payload_get(fp, sizeof(*pp));
650                 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
651                         fcp_parm = ntohl(pp->spp.spp_params);
652                         if (fcp_parm & FCP_SPPF_RETRY)
653                                 rdata->flags |= FC_RP_FLAGS_RETRY;
654                 }
655
656                 rport->supported_classes = FC_COS_CLASS3;
657                 if (fcp_parm & FCP_SPPF_INIT_FCN)
658                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
659                 if (fcp_parm & FCP_SPPF_TARG_FCN)
660                         roles |= FC_RPORT_ROLE_FCP_TARGET;
661
662                 rport->roles = roles;
663                 fc_rport_enter_rtv(rport);
664
665         } else {
666                 FC_DBG("Bad ELS response\n");
667                 rdata->event = RPORT_EV_FAILED;
668                 fc_rport_state_enter(rport, RPORT_ST_NONE);
669                 queue_work(rport_event_queue, &rdata->event_work);
670         }
671
672 out:
673         fc_frame_free(fp);
674 err:
675         mutex_unlock(&rdata->rp_mutex);
676         put_device(&rport->dev);
677 }
678
679 /**
680  * fc_rport_logo_resp() - Logout (LOGO) response handler
681  * @sp: current sequence in the LOGO exchange
682  * @fp: response frame
683  * @rp_arg: Fibre Channel remote port
684  *
685  * Locking Note: This function will be called without the rport lock
686  * held, but it will lock, call an _enter_* function or fc_rport_error
687  * and then unlock the rport.
688  */
689 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
690                                void *rp_arg)
691 {
692         struct fc_rport *rport = rp_arg;
693         struct fc_rport_libfc_priv *rdata = rport->dd_data;
694         u8 op;
695
696         mutex_lock(&rdata->rp_mutex);
697
698         FC_DEBUG_RPORT("Received a LOGO response from port (%6x)\n",
699                        rport->port_id);
700
701         if (rdata->rp_state != RPORT_ST_LOGO) {
702                 FC_DEBUG_RPORT("Received a LOGO response, but in state %s\n",
703                                fc_rport_state(rport));
704                 if (IS_ERR(fp))
705                         goto err;
706                 goto out;
707         }
708
709         if (IS_ERR(fp)) {
710                 fc_rport_error_retry(rport, fp);
711                 goto err;
712         }
713
714         op = fc_frame_payload_op(fp);
715         if (op == ELS_LS_ACC) {
716                 fc_rport_enter_rtv(rport);
717         } else {
718                 FC_DBG("Bad ELS response\n");
719                 rdata->event = RPORT_EV_LOGO;
720                 fc_rport_state_enter(rport, RPORT_ST_NONE);
721                 queue_work(rport_event_queue, &rdata->event_work);
722         }
723
724 out:
725         fc_frame_free(fp);
726 err:
727         mutex_unlock(&rdata->rp_mutex);
728         put_device(&rport->dev);
729 }
730
731 /**
732  * fc_rport_enter_prli() - Send Process Login (PRLI) request to peer
733  * @rport: Fibre Channel remote port to send PRLI to
734  *
735  * Locking Note: The rport lock is expected to be held before calling
736  * this routine.
737  */
738 static void fc_rport_enter_prli(struct fc_rport *rport)
739 {
740         struct fc_rport_libfc_priv *rdata = rport->dd_data;
741         struct fc_lport *lport = rdata->local_port;
742         struct {
743                 struct fc_els_prli prli;
744                 struct fc_els_spp spp;
745         } *pp;
746         struct fc_frame *fp;
747
748         FC_DEBUG_RPORT("Port (%6x) entered PRLI state from %s state\n",
749                        rport->port_id, fc_rport_state(rport));
750
751         fc_rport_state_enter(rport, RPORT_ST_PRLI);
752
753         fp = fc_frame_alloc(lport, sizeof(*pp));
754         if (!fp) {
755                 fc_rport_error_retry(rport, fp);
756                 return;
757         }
758
759         if (!lport->tt.elsct_send(lport, rport, fp, ELS_PRLI,
760                                   fc_rport_prli_resp, rport, lport->e_d_tov))
761                 fc_rport_error_retry(rport, fp);
762         else
763                 get_device(&rport->dev);
764 }
765
766 /**
767  * fc_rport_els_rtv_resp() - Request Timeout Value response handler
768  * @sp: current sequence in the RTV exchange
769  * @fp: response frame
770  * @rp_arg: Fibre Channel remote port
771  *
772  * Many targets don't seem to support this.
773  *
774  * Locking Note: This function will be called without the rport lock
775  * held, but it will lock, call an _enter_* function or fc_rport_error
776  * and then unlock the rport.
777  */
778 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
779                               void *rp_arg)
780 {
781         struct fc_rport *rport = rp_arg;
782         struct fc_rport_libfc_priv *rdata = rport->dd_data;
783         u8 op;
784
785         mutex_lock(&rdata->rp_mutex);
786
787         FC_DEBUG_RPORT("Received a RTV response from port (%6x)\n",
788                        rport->port_id);
789
790         if (rdata->rp_state != RPORT_ST_RTV) {
791                 FC_DBG("Received a RTV response, but in state %s\n",
792                        fc_rport_state(rport));
793                 if (IS_ERR(fp))
794                         goto err;
795                 goto out;
796         }
797
798         if (IS_ERR(fp)) {
799                 fc_rport_error(rport, fp);
800                 goto err;
801         }
802
803         op = fc_frame_payload_op(fp);
804         if (op == ELS_LS_ACC) {
805                 struct fc_els_rtv_acc *rtv;
806                 u32 toq;
807                 u32 tov;
808
809                 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
810                 if (rtv) {
811                         toq = ntohl(rtv->rtv_toq);
812                         tov = ntohl(rtv->rtv_r_a_tov);
813                         if (tov == 0)
814                                 tov = 1;
815                         rdata->r_a_tov = tov;
816                         tov = ntohl(rtv->rtv_e_d_tov);
817                         if (toq & FC_ELS_RTV_EDRES)
818                                 tov /= 1000000;
819                         if (tov == 0)
820                                 tov = 1;
821                         rdata->e_d_tov = tov;
822                 }
823         }
824
825         fc_rport_enter_ready(rport);
826
827 out:
828         fc_frame_free(fp);
829 err:
830         mutex_unlock(&rdata->rp_mutex);
831         put_device(&rport->dev);
832 }
833
834 /**
835  * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request to peer
836  * @rport: Fibre Channel remote port to send RTV to
837  *
838  * Locking Note: The rport lock is expected to be held before calling
839  * this routine.
840  */
841 static void fc_rport_enter_rtv(struct fc_rport *rport)
842 {
843         struct fc_frame *fp;
844         struct fc_rport_libfc_priv *rdata = rport->dd_data;
845         struct fc_lport *lport = rdata->local_port;
846
847         FC_DEBUG_RPORT("Port (%6x) entered RTV state from %s state\n",
848                        rport->port_id, fc_rport_state(rport));
849
850         fc_rport_state_enter(rport, RPORT_ST_RTV);
851
852         fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
853         if (!fp) {
854                 fc_rport_error_retry(rport, fp);
855                 return;
856         }
857
858         if (!lport->tt.elsct_send(lport, rport, fp, ELS_RTV,
859                                      fc_rport_rtv_resp, rport, lport->e_d_tov))
860                 fc_rport_error_retry(rport, fp);
861         else
862                 get_device(&rport->dev);
863 }
864
865 /**
866  * fc_rport_enter_logo() - Send Logout (LOGO) request to peer
867  * @rport: Fibre Channel remote port to send LOGO to
868  *
869  * Locking Note: The rport lock is expected to be held before calling
870  * this routine.
871  */
872 static void fc_rport_enter_logo(struct fc_rport *rport)
873 {
874         struct fc_rport_libfc_priv *rdata = rport->dd_data;
875         struct fc_lport *lport = rdata->local_port;
876         struct fc_frame *fp;
877
878         FC_DEBUG_RPORT("Port (%6x) entered LOGO state from %s state\n",
879                        rport->port_id, fc_rport_state(rport));
880
881         fc_rport_state_enter(rport, RPORT_ST_LOGO);
882
883         fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
884         if (!fp) {
885                 fc_rport_error_retry(rport, fp);
886                 return;
887         }
888
889         if (!lport->tt.elsct_send(lport, rport, fp, ELS_LOGO,
890                                   fc_rport_logo_resp, rport, lport->e_d_tov))
891                 fc_rport_error_retry(rport, fp);
892         else
893                 get_device(&rport->dev);
894 }
895
896
897 /**
898  * fc_rport_recv_req() - Receive a request from a rport
899  * @sp: current sequence in the PLOGI exchange
900  * @fp: response frame
901  * @rp_arg: Fibre Channel remote port
902  *
903  * Locking Note: Called without the rport lock held. This
904  * function will hold the rport lock, call an _enter_*
905  * function and then unlock the rport.
906  */
907 void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
908                        struct fc_rport *rport)
909 {
910         struct fc_rport_libfc_priv *rdata = rport->dd_data;
911         struct fc_lport *lport = rdata->local_port;
912
913         struct fc_frame_header *fh;
914         struct fc_seq_els_data els_data;
915         u8 op;
916
917         mutex_lock(&rdata->rp_mutex);
918
919         els_data.fp = NULL;
920         els_data.explan = ELS_EXPL_NONE;
921         els_data.reason = ELS_RJT_NONE;
922
923         fh = fc_frame_header_get(fp);
924
925         if (fh->fh_r_ctl == FC_RCTL_ELS_REQ && fh->fh_type == FC_TYPE_ELS) {
926                 op = fc_frame_payload_op(fp);
927                 switch (op) {
928                 case ELS_PLOGI:
929                         fc_rport_recv_plogi_req(rport, sp, fp);
930                         break;
931                 case ELS_PRLI:
932                         fc_rport_recv_prli_req(rport, sp, fp);
933                         break;
934                 case ELS_PRLO:
935                         fc_rport_recv_prlo_req(rport, sp, fp);
936                         break;
937                 case ELS_LOGO:
938                         fc_rport_recv_logo_req(rport, sp, fp);
939                         break;
940                 case ELS_RRQ:
941                         els_data.fp = fp;
942                         lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
943                         break;
944                 case ELS_REC:
945                         els_data.fp = fp;
946                         lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
947                         break;
948                 default:
949                         els_data.reason = ELS_RJT_UNSUP;
950                         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
951                         break;
952                 }
953         }
954
955         mutex_unlock(&rdata->rp_mutex);
956 }
957
958 /**
959  * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request
960  * @rport: Fibre Channel remote port that initiated PLOGI
961  * @sp: current sequence in the PLOGI exchange
962  * @fp: PLOGI request frame
963  *
964  * Locking Note: The rport lock is exected to be held before calling
965  * this function.
966  */
967 static void fc_rport_recv_plogi_req(struct fc_rport *rport,
968                                     struct fc_seq *sp, struct fc_frame *rx_fp)
969 {
970         struct fc_rport_libfc_priv *rdata = rport->dd_data;
971         struct fc_lport *lport = rdata->local_port;
972         struct fc_frame *fp = rx_fp;
973         struct fc_exch *ep;
974         struct fc_frame_header *fh;
975         struct fc_els_flogi *pl;
976         struct fc_seq_els_data rjt_data;
977         u32 sid;
978         u64 wwpn;
979         u64 wwnn;
980         enum fc_els_rjt_reason reject = 0;
981         u32 f_ctl;
982         rjt_data.fp = NULL;
983
984         fh = fc_frame_header_get(fp);
985
986         FC_DEBUG_RPORT("Received PLOGI request from port (%6x) "
987                        "while in state %s\n", ntoh24(fh->fh_s_id),
988                        fc_rport_state(rport));
989
990         sid = ntoh24(fh->fh_s_id);
991         pl = fc_frame_payload_get(fp, sizeof(*pl));
992         if (!pl) {
993                 FC_DBG("incoming PLOGI from %x too short\n", sid);
994                 WARN_ON(1);
995                 /* XXX TBD: send reject? */
996                 fc_frame_free(fp);
997                 return;
998         }
999         wwpn = get_unaligned_be64(&pl->fl_wwpn);
1000         wwnn = get_unaligned_be64(&pl->fl_wwnn);
1001
1002         /*
1003          * If the session was just created, possibly due to the incoming PLOGI,
1004          * set the state appropriately and accept the PLOGI.
1005          *
1006          * If we had also sent a PLOGI, and if the received PLOGI is from a
1007          * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1008          * "command already in progress".
1009          *
1010          * XXX TBD: If the session was ready before, the PLOGI should result in
1011          * all outstanding exchanges being reset.
1012          */
1013         switch (rdata->rp_state) {
1014         case RPORT_ST_INIT:
1015                 FC_DEBUG_RPORT("incoming PLOGI from %6x wwpn %llx state INIT "
1016                                "- reject\n", sid, (unsigned long long)wwpn);
1017                 reject = ELS_RJT_UNSUP;
1018                 break;
1019         case RPORT_ST_PLOGI:
1020                 FC_DEBUG_RPORT("incoming PLOGI from %x in PLOGI state %d\n",
1021                                sid, rdata->rp_state);
1022                 if (wwpn < lport->wwpn)
1023                         reject = ELS_RJT_INPROG;
1024                 break;
1025         case RPORT_ST_PRLI:
1026         case RPORT_ST_READY:
1027                 FC_DEBUG_RPORT("incoming PLOGI from %x in logged-in state %d "
1028                                "- ignored for now\n", sid, rdata->rp_state);
1029                 /* XXX TBD - should reset */
1030                 break;
1031         case RPORT_ST_NONE:
1032         default:
1033                 FC_DEBUG_RPORT("incoming PLOGI from %x in unexpected "
1034                                "state %d\n", sid, rdata->rp_state);
1035                 fc_frame_free(fp);
1036                 return;
1037                 break;
1038         }
1039
1040         if (reject) {
1041                 rjt_data.reason = reject;
1042                 rjt_data.explan = ELS_EXPL_NONE;
1043                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1044                 fc_frame_free(fp);
1045         } else {
1046                 fp = fc_frame_alloc(lport, sizeof(*pl));
1047                 if (fp == NULL) {
1048                         fp = rx_fp;
1049                         rjt_data.reason = ELS_RJT_UNAB;
1050                         rjt_data.explan = ELS_EXPL_NONE;
1051                         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1052                         fc_frame_free(fp);
1053                 } else {
1054                         sp = lport->tt.seq_start_next(sp);
1055                         WARN_ON(!sp);
1056                         fc_rport_set_name(rport, wwpn, wwnn);
1057
1058                         /*
1059                          * Get session payload size from incoming PLOGI.
1060                          */
1061                         rport->maxframe_size =
1062                                 fc_plogi_get_maxframe(pl, lport->mfs);
1063                         fc_frame_free(rx_fp);
1064                         fc_plogi_fill(lport, fp, ELS_LS_ACC);
1065
1066                         /*
1067                          * Send LS_ACC.  If this fails,
1068                          * the originator should retry.
1069                          */
1070                         f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1071                         f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1072                         ep = fc_seq_exch(sp);
1073                         fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1074                                        FC_TYPE_ELS, f_ctl, 0);
1075                         lport->tt.seq_send(lport, sp, fp);
1076                         if (rdata->rp_state == RPORT_ST_PLOGI)
1077                                 fc_rport_enter_prli(rport);
1078                 }
1079         }
1080 }
1081
1082 /**
1083  * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request
1084  * @rport: Fibre Channel remote port that initiated PRLI
1085  * @sp: current sequence in the PRLI exchange
1086  * @fp: PRLI request frame
1087  *
1088  * Locking Note: The rport lock is exected to be held before calling
1089  * this function.
1090  */
1091 static void fc_rport_recv_prli_req(struct fc_rport *rport,
1092                                    struct fc_seq *sp, struct fc_frame *rx_fp)
1093 {
1094         struct fc_rport_libfc_priv *rdata = rport->dd_data;
1095         struct fc_lport *lport = rdata->local_port;
1096         struct fc_exch *ep;
1097         struct fc_frame *fp;
1098         struct fc_frame_header *fh;
1099         struct {
1100                 struct fc_els_prli prli;
1101                 struct fc_els_spp spp;
1102         } *pp;
1103         struct fc_els_spp *rspp;        /* request service param page */
1104         struct fc_els_spp *spp; /* response spp */
1105         unsigned int len;
1106         unsigned int plen;
1107         enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1108         enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1109         enum fc_els_spp_resp resp;
1110         struct fc_seq_els_data rjt_data;
1111         u32 f_ctl;
1112         u32 fcp_parm;
1113         u32 roles = FC_RPORT_ROLE_UNKNOWN;
1114         rjt_data.fp = NULL;
1115
1116         fh = fc_frame_header_get(rx_fp);
1117
1118         FC_DEBUG_RPORT("Received PRLI request from port (%6x) "
1119                        "while in state %s\n", ntoh24(fh->fh_s_id),
1120                        fc_rport_state(rport));
1121
1122         switch (rdata->rp_state) {
1123         case RPORT_ST_PRLI:
1124         case RPORT_ST_READY:
1125                 reason = ELS_RJT_NONE;
1126                 break;
1127         default:
1128                 fc_frame_free(rx_fp);
1129                 return;
1130                 break;
1131         }
1132         len = fr_len(rx_fp) - sizeof(*fh);
1133         pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1134         if (pp == NULL) {
1135                 reason = ELS_RJT_PROT;
1136                 explan = ELS_EXPL_INV_LEN;
1137         } else {
1138                 plen = ntohs(pp->prli.prli_len);
1139                 if ((plen % 4) != 0 || plen > len) {
1140                         reason = ELS_RJT_PROT;
1141                         explan = ELS_EXPL_INV_LEN;
1142                 } else if (plen < len) {
1143                         len = plen;
1144                 }
1145                 plen = pp->prli.prli_spp_len;
1146                 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1147                     plen > len || len < sizeof(*pp)) {
1148                         reason = ELS_RJT_PROT;
1149                         explan = ELS_EXPL_INV_LEN;
1150                 }
1151                 rspp = &pp->spp;
1152         }
1153         if (reason != ELS_RJT_NONE ||
1154             (fp = fc_frame_alloc(lport, len)) == NULL) {
1155                 rjt_data.reason = reason;
1156                 rjt_data.explan = explan;
1157                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1158         } else {
1159                 sp = lport->tt.seq_start_next(sp);
1160                 WARN_ON(!sp);
1161                 pp = fc_frame_payload_get(fp, len);
1162                 WARN_ON(!pp);
1163                 memset(pp, 0, len);
1164                 pp->prli.prli_cmd = ELS_LS_ACC;
1165                 pp->prli.prli_spp_len = plen;
1166                 pp->prli.prli_len = htons(len);
1167                 len -= sizeof(struct fc_els_prli);
1168
1169                 /*
1170                  * Go through all the service parameter pages and build
1171                  * response.  If plen indicates longer SPP than standard,
1172                  * use that.  The entire response has been pre-cleared above.
1173                  */
1174                 spp = &pp->spp;
1175                 while (len >= plen) {
1176                         spp->spp_type = rspp->spp_type;
1177                         spp->spp_type_ext = rspp->spp_type_ext;
1178                         spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1179                         resp = FC_SPP_RESP_ACK;
1180                         if (rspp->spp_flags & FC_SPP_RPA_VAL)
1181                                 resp = FC_SPP_RESP_NO_PA;
1182                         switch (rspp->spp_type) {
1183                         case 0: /* common to all FC-4 types */
1184                                 break;
1185                         case FC_TYPE_FCP:
1186                                 fcp_parm = ntohl(rspp->spp_params);
1187                                 if (fcp_parm * FCP_SPPF_RETRY)
1188                                         rdata->flags |= FC_RP_FLAGS_RETRY;
1189                                 rport->supported_classes = FC_COS_CLASS3;
1190                                 if (fcp_parm & FCP_SPPF_INIT_FCN)
1191                                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1192                                 if (fcp_parm & FCP_SPPF_TARG_FCN)
1193                                         roles |= FC_RPORT_ROLE_FCP_TARGET;
1194                                 rport->roles = roles;
1195
1196                                 spp->spp_params =
1197                                         htonl(lport->service_params);
1198                                 break;
1199                         default:
1200                                 resp = FC_SPP_RESP_INVL;
1201                                 break;
1202                         }
1203                         spp->spp_flags |= resp;
1204                         len -= plen;
1205                         rspp = (struct fc_els_spp *)((char *)rspp + plen);
1206                         spp = (struct fc_els_spp *)((char *)spp + plen);
1207                 }
1208
1209                 /*
1210                  * Send LS_ACC.  If this fails, the originator should retry.
1211                  */
1212                 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1213                 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1214                 ep = fc_seq_exch(sp);
1215                 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1216                                FC_TYPE_ELS, f_ctl, 0);
1217                 lport->tt.seq_send(lport, sp, fp);
1218
1219                 /*
1220                  * Get lock and re-check state.
1221                  */
1222                 switch (rdata->rp_state) {
1223                 case RPORT_ST_PRLI:
1224                         fc_rport_enter_ready(rport);
1225                         break;
1226                 case RPORT_ST_READY:
1227                         break;
1228                 default:
1229                         break;
1230                 }
1231         }
1232         fc_frame_free(rx_fp);
1233 }
1234
1235 /**
1236  * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request
1237  * @rport: Fibre Channel remote port that initiated PRLO
1238  * @sp: current sequence in the PRLO exchange
1239  * @fp: PRLO request frame
1240  *
1241  * Locking Note: The rport lock is exected to be held before calling
1242  * this function.
1243  */
1244 static void fc_rport_recv_prlo_req(struct fc_rport *rport, struct fc_seq *sp,
1245                                    struct fc_frame *fp)
1246 {
1247         struct fc_rport_libfc_priv *rdata = rport->dd_data;
1248         struct fc_lport *lport = rdata->local_port;
1249
1250         struct fc_frame_header *fh;
1251         struct fc_seq_els_data rjt_data;
1252
1253         fh = fc_frame_header_get(fp);
1254
1255         FC_DEBUG_RPORT("Received PRLO request from port (%6x) "
1256                        "while in state %s\n", ntoh24(fh->fh_s_id),
1257                        fc_rport_state(rport));
1258
1259         if (rdata->rp_state == RPORT_ST_NONE) {
1260                 fc_frame_free(fp);
1261                 return;
1262         }
1263
1264         rjt_data.fp = NULL;
1265         rjt_data.reason = ELS_RJT_UNAB;
1266         rjt_data.explan = ELS_EXPL_NONE;
1267         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1268         fc_frame_free(fp);
1269 }
1270
1271 /**
1272  * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request
1273  * @rport: Fibre Channel remote port that initiated LOGO
1274  * @sp: current sequence in the LOGO exchange
1275  * @fp: LOGO request frame
1276  *
1277  * Locking Note: The rport lock is exected to be held before calling
1278  * this function.
1279  */
1280 static void fc_rport_recv_logo_req(struct fc_rport *rport, struct fc_seq *sp,
1281                                    struct fc_frame *fp)
1282 {
1283         struct fc_frame_header *fh;
1284         struct fc_rport_libfc_priv *rdata = rport->dd_data;
1285         struct fc_lport *lport = rdata->local_port;
1286
1287         fh = fc_frame_header_get(fp);
1288
1289         FC_DEBUG_RPORT("Received LOGO request from port (%6x) "
1290                        "while in state %s\n", ntoh24(fh->fh_s_id),
1291                        fc_rport_state(rport));
1292
1293         if (rdata->rp_state == RPORT_ST_NONE) {
1294                 fc_frame_free(fp);
1295                 return;
1296         }
1297
1298         rdata->event = RPORT_EV_LOGO;
1299         fc_rport_state_enter(rport, RPORT_ST_NONE);
1300         queue_work(rport_event_queue, &rdata->event_work);
1301
1302         lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1303         fc_frame_free(fp);
1304 }
1305
1306 static void fc_rport_flush_queue(void)
1307 {
1308         flush_workqueue(rport_event_queue);
1309 }
1310
1311
1312 int fc_rport_init(struct fc_lport *lport)
1313 {
1314         if (!lport->tt.rport_create)
1315                 lport->tt.rport_create = fc_rport_rogue_create;
1316
1317         if (!lport->tt.rport_login)
1318                 lport->tt.rport_login = fc_rport_login;
1319
1320         if (!lport->tt.rport_logoff)
1321                 lport->tt.rport_logoff = fc_rport_logoff;
1322
1323         if (!lport->tt.rport_recv_req)
1324                 lport->tt.rport_recv_req = fc_rport_recv_req;
1325
1326         if (!lport->tt.rport_flush_queue)
1327                 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1328
1329         return 0;
1330 }
1331 EXPORT_SYMBOL(fc_rport_init);
1332
1333 int fc_setup_rport()
1334 {
1335         rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1336         if (!rport_event_queue)
1337                 return -ENOMEM;
1338         return 0;
1339 }
1340 EXPORT_SYMBOL(fc_setup_rport);
1341
1342 void fc_destroy_rport()
1343 {
1344         destroy_workqueue(rport_event_queue);
1345 }
1346 EXPORT_SYMBOL(fc_destroy_rport);
1347
1348 void fc_rport_terminate_io(struct fc_rport *rport)
1349 {
1350         struct fc_rport_libfc_priv *rdata = rport->dd_data;
1351         struct fc_lport *lport = rdata->local_port;
1352
1353         lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1354         lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
1355 }
1356 EXPORT_SYMBOL(fc_rport_terminate_io);