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