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