a86df0b41ae363cbced7719907e5b99b5064f82d
[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                 if (trans_state == FC_PORTSTATE_ROGUE)
280                         put_device(&rport->dev);
281                 else {
282                         port_id = rport->port_id;
283                         fc_remote_port_delete(rport);
284                         lport->tt.exch_mgr_reset(lport, 0, port_id);
285                         lport->tt.exch_mgr_reset(lport, port_id, 0);
286                 }
287         } else
288                 mutex_unlock(&rdata->rp_mutex);
289 }
290
291 /**
292  * fc_rport_login() - Start the remote port login state machine
293  * @rport: Fibre Channel remote port
294  *
295  * Locking Note: Called without the rport lock held. This
296  * function will hold the rport lock, call an _enter_*
297  * function and then unlock the rport.
298  */
299 int fc_rport_login(struct fc_rport *rport)
300 {
301         struct fc_rport_libfc_priv *rdata = rport->dd_data;
302
303         mutex_lock(&rdata->rp_mutex);
304
305         FC_RPORT_DBG(rport, "Login to port\n");
306
307         fc_rport_enter_plogi(rport);
308
309         mutex_unlock(&rdata->rp_mutex);
310
311         return 0;
312 }
313
314 /**
315  * fc_rport_enter_delete() - schedule a remote port to be deleted.
316  * @rport: Fibre Channel remote port
317  * @event: event to report as the reason for deletion
318  *
319  * Locking Note: Called with the rport lock held.
320  *
321  * Allow state change into DELETE only once.
322  *
323  * Call queue_work only if there's no event already pending.
324  * Set the new event so that the old pending event will not occur.
325  * Since we have the mutex, even if fc_rport_work() is already started,
326  * it'll see the new event.
327  */
328 static void fc_rport_enter_delete(struct fc_rport *rport,
329                                   enum fc_rport_event event)
330 {
331         struct fc_rport_libfc_priv *rdata = rport->dd_data;
332
333         if (rdata->rp_state == RPORT_ST_DELETE)
334                 return;
335
336         FC_RPORT_DBG(rport, "Delete port\n");
337
338         fc_rport_state_enter(rport, RPORT_ST_DELETE);
339
340         if (rdata->event == RPORT_EV_NONE)
341                 queue_work(rport_event_queue, &rdata->event_work);
342         rdata->event = event;
343 }
344
345 /**
346  * fc_rport_logoff() - Logoff and remove an rport
347  * @rport: Fibre Channel remote port to be removed
348  *
349  * Locking Note: Called without the rport lock held. This
350  * function will hold the rport lock, call an _enter_*
351  * function and then unlock the rport.
352  */
353 int fc_rport_logoff(struct fc_rport *rport)
354 {
355         struct fc_rport_libfc_priv *rdata = rport->dd_data;
356
357         mutex_lock(&rdata->rp_mutex);
358
359         FC_RPORT_DBG(rport, "Remove port\n");
360
361         if (rdata->rp_state == RPORT_ST_DELETE) {
362                 FC_RPORT_DBG(rport, "Port in Delete state, not removing\n");
363                 mutex_unlock(&rdata->rp_mutex);
364                 goto out;
365         }
366
367         fc_rport_enter_logo(rport);
368
369         /*
370          * Change the state to Delete so that we discard
371          * the response.
372          */
373         fc_rport_enter_delete(rport, RPORT_EV_STOP);
374         mutex_unlock(&rdata->rp_mutex);
375
376 out:
377         return 0;
378 }
379
380 /**
381  * fc_rport_enter_ready() - The rport is ready
382  * @rport: Fibre Channel remote port that is ready
383  *
384  * Locking Note: The rport lock is expected to be held before calling
385  * this routine.
386  */
387 static void fc_rport_enter_ready(struct fc_rport *rport)
388 {
389         struct fc_rport_libfc_priv *rdata = rport->dd_data;
390
391         fc_rport_state_enter(rport, RPORT_ST_READY);
392
393         FC_RPORT_DBG(rport, "Port is Ready\n");
394
395         if (rdata->event == RPORT_EV_NONE)
396                 queue_work(rport_event_queue, &rdata->event_work);
397         rdata->event = RPORT_EV_CREATED;
398 }
399
400 /**
401  * fc_rport_timeout() - Handler for the retry_work timer.
402  * @work: The work struct of the fc_rport_libfc_priv
403  *
404  * Locking Note: Called without the rport lock held. This
405  * function will hold the rport lock, call an _enter_*
406  * function and then unlock the rport.
407  */
408 static void fc_rport_timeout(struct work_struct *work)
409 {
410         struct fc_rport_libfc_priv *rdata =
411                 container_of(work, struct fc_rport_libfc_priv, retry_work.work);
412         struct fc_rport *rport = PRIV_TO_RPORT(rdata);
413
414         mutex_lock(&rdata->rp_mutex);
415
416         switch (rdata->rp_state) {
417         case RPORT_ST_PLOGI:
418                 fc_rport_enter_plogi(rport);
419                 break;
420         case RPORT_ST_PRLI:
421                 fc_rport_enter_prli(rport);
422                 break;
423         case RPORT_ST_RTV:
424                 fc_rport_enter_rtv(rport);
425                 break;
426         case RPORT_ST_LOGO:
427                 fc_rport_enter_logo(rport);
428                 break;
429         case RPORT_ST_READY:
430         case RPORT_ST_INIT:
431         case RPORT_ST_DELETE:
432                 break;
433         }
434
435         mutex_unlock(&rdata->rp_mutex);
436         put_device(&rport->dev);
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                 get_device(&rport->dev);
498                 schedule_delayed_work(&rdata->retry_work, delay);
499                 return;
500         }
501
502         return fc_rport_error(rport, fp);
503 }
504
505 /**
506  * fc_rport_plogi_recv_resp() - Handle incoming ELS PLOGI response
507  * @sp: current sequence in the PLOGI exchange
508  * @fp: response frame
509  * @rp_arg: Fibre Channel remote port
510  *
511  * Locking Note: This function will be called without the rport lock
512  * held, but it will lock, call an _enter_* function or fc_rport_error
513  * and then unlock the rport.
514  */
515 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
516                                 void *rp_arg)
517 {
518         struct fc_rport *rport = rp_arg;
519         struct fc_rport_libfc_priv *rdata = rport->dd_data;
520         struct fc_lport *lport = rdata->local_port;
521         struct fc_els_flogi *plp = NULL;
522         unsigned int tov;
523         u16 csp_seq;
524         u16 cssp_seq;
525         u8 op;
526
527         mutex_lock(&rdata->rp_mutex);
528
529         FC_RPORT_DBG(rport, "Received a PLOGI response\n");
530
531         if (rdata->rp_state != RPORT_ST_PLOGI) {
532                 FC_RPORT_DBG(rport, "Received a PLOGI response, but in state "
533                              "%s\n", fc_rport_state(rport));
534                 if (IS_ERR(fp))
535                         goto err;
536                 goto out;
537         }
538
539         if (IS_ERR(fp)) {
540                 fc_rport_error_retry(rport, fp);
541                 goto err;
542         }
543
544         op = fc_frame_payload_op(fp);
545         if (op == ELS_LS_ACC &&
546             (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
547                 rport->port_name = get_unaligned_be64(&plp->fl_wwpn);
548                 rport->node_name = get_unaligned_be64(&plp->fl_wwnn);
549
550                 tov = ntohl(plp->fl_csp.sp_e_d_tov);
551                 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
552                         tov /= 1000;
553                 if (tov > rdata->e_d_tov)
554                         rdata->e_d_tov = tov;
555                 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
556                 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
557                 if (cssp_seq < csp_seq)
558                         csp_seq = cssp_seq;
559                 rdata->max_seq = csp_seq;
560                 rport->maxframe_size =
561                         fc_plogi_get_maxframe(plp, lport->mfs);
562
563                 /*
564                  * If the rport is one of the well known addresses
565                  * we skip PRLI and RTV and go straight to READY.
566                  */
567                 if (rport->port_id >= FC_FID_DOM_MGR)
568                         fc_rport_enter_ready(rport);
569                 else
570                         fc_rport_enter_prli(rport);
571         } else
572                 fc_rport_error_retry(rport, fp);
573
574 out:
575         fc_frame_free(fp);
576 err:
577         mutex_unlock(&rdata->rp_mutex);
578         put_device(&rport->dev);
579 }
580
581 /**
582  * fc_rport_enter_plogi() - Send Port Login (PLOGI) request to peer
583  * @rport: Fibre Channel remote port to send PLOGI to
584  *
585  * Locking Note: The rport lock is expected to be held before calling
586  * this routine.
587  */
588 static void fc_rport_enter_plogi(struct fc_rport *rport)
589 {
590         struct fc_rport_libfc_priv *rdata = rport->dd_data;
591         struct fc_lport *lport = rdata->local_port;
592         struct fc_frame *fp;
593
594         FC_RPORT_DBG(rport, "Port entered PLOGI state from %s state\n",
595                      fc_rport_state(rport));
596
597         fc_rport_state_enter(rport, RPORT_ST_PLOGI);
598
599         rport->maxframe_size = FC_MIN_MAX_PAYLOAD;
600         fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
601         if (!fp) {
602                 fc_rport_error_retry(rport, fp);
603                 return;
604         }
605         rdata->e_d_tov = lport->e_d_tov;
606
607         if (!lport->tt.elsct_send(lport, rport, fp, ELS_PLOGI,
608                                   fc_rport_plogi_resp, rport, lport->e_d_tov))
609                 fc_rport_error_retry(rport, fp);
610         else
611                 get_device(&rport->dev);
612 }
613
614 /**
615  * fc_rport_prli_resp() - Process Login (PRLI) response handler
616  * @sp: current sequence in the PRLI exchange
617  * @fp: response frame
618  * @rp_arg: Fibre Channel remote port
619  *
620  * Locking Note: This function will be called without the rport lock
621  * held, but it will lock, call an _enter_* function or fc_rport_error
622  * and then unlock the rport.
623  */
624 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
625                                void *rp_arg)
626 {
627         struct fc_rport *rport = rp_arg;
628         struct fc_rport_libfc_priv *rdata = rport->dd_data;
629         struct {
630                 struct fc_els_prli prli;
631                 struct fc_els_spp spp;
632         } *pp;
633         u32 roles = FC_RPORT_ROLE_UNKNOWN;
634         u32 fcp_parm = 0;
635         u8 op;
636
637         mutex_lock(&rdata->rp_mutex);
638
639         FC_RPORT_DBG(rport, "Received a PRLI response\n");
640
641         if (rdata->rp_state != RPORT_ST_PRLI) {
642                 FC_RPORT_DBG(rport, "Received a PRLI response, but in state "
643                              "%s\n", fc_rport_state(rport));
644                 if (IS_ERR(fp))
645                         goto err;
646                 goto out;
647         }
648
649         if (IS_ERR(fp)) {
650                 fc_rport_error_retry(rport, fp);
651                 goto err;
652         }
653
654         op = fc_frame_payload_op(fp);
655         if (op == ELS_LS_ACC) {
656                 pp = fc_frame_payload_get(fp, sizeof(*pp));
657                 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
658                         fcp_parm = ntohl(pp->spp.spp_params);
659                         if (fcp_parm & FCP_SPPF_RETRY)
660                                 rdata->flags |= FC_RP_FLAGS_RETRY;
661                 }
662
663                 rport->supported_classes = FC_COS_CLASS3;
664                 if (fcp_parm & FCP_SPPF_INIT_FCN)
665                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
666                 if (fcp_parm & FCP_SPPF_TARG_FCN)
667                         roles |= FC_RPORT_ROLE_FCP_TARGET;
668
669                 rport->roles = roles;
670                 fc_rport_enter_rtv(rport);
671
672         } else {
673                 FC_RPORT_DBG(rport, "Bad ELS response for PRLI command\n");
674                 fc_rport_enter_delete(rport, RPORT_EV_FAILED);
675         }
676
677 out:
678         fc_frame_free(fp);
679 err:
680         mutex_unlock(&rdata->rp_mutex);
681         put_device(&rport->dev);
682 }
683
684 /**
685  * fc_rport_logo_resp() - Logout (LOGO) response handler
686  * @sp: current sequence in the LOGO exchange
687  * @fp: response frame
688  * @rp_arg: Fibre Channel remote port
689  *
690  * Locking Note: This function will be called without the rport lock
691  * held, but it will lock, call an _enter_* function or fc_rport_error
692  * and then unlock the rport.
693  */
694 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
695                                void *rp_arg)
696 {
697         struct fc_rport *rport = rp_arg;
698         struct fc_rport_libfc_priv *rdata = rport->dd_data;
699         u8 op;
700
701         mutex_lock(&rdata->rp_mutex);
702
703         FC_RPORT_DBG(rport, "Received a LOGO response\n");
704
705         if (rdata->rp_state != RPORT_ST_LOGO) {
706                 FC_RPORT_DBG(rport, "Received a LOGO response, but in state "
707                              "%s\n", fc_rport_state(rport));
708                 if (IS_ERR(fp))
709                         goto err;
710                 goto out;
711         }
712
713         if (IS_ERR(fp)) {
714                 fc_rport_error_retry(rport, fp);
715                 goto err;
716         }
717
718         op = fc_frame_payload_op(fp);
719         if (op == ELS_LS_ACC) {
720                 fc_rport_enter_rtv(rport);
721         } else {
722                 FC_RPORT_DBG(rport, "Bad ELS response for LOGO command\n");
723                 fc_rport_enter_delete(rport, RPORT_EV_LOGO);
724         }
725
726 out:
727         fc_frame_free(fp);
728 err:
729         mutex_unlock(&rdata->rp_mutex);
730         put_device(&rport->dev);
731 }
732
733 /**
734  * fc_rport_enter_prli() - Send Process Login (PRLI) request to peer
735  * @rport: Fibre Channel remote port to send PRLI to
736  *
737  * Locking Note: The rport lock is expected to be held before calling
738  * this routine.
739  */
740 static void fc_rport_enter_prli(struct fc_rport *rport)
741 {
742         struct fc_rport_libfc_priv *rdata = rport->dd_data;
743         struct fc_lport *lport = rdata->local_port;
744         struct {
745                 struct fc_els_prli prli;
746                 struct fc_els_spp spp;
747         } *pp;
748         struct fc_frame *fp;
749
750         FC_RPORT_DBG(rport, "Port entered PRLI state from %s state\n",
751                      fc_rport_state(rport));
752
753         fc_rport_state_enter(rport, RPORT_ST_PRLI);
754
755         fp = fc_frame_alloc(lport, sizeof(*pp));
756         if (!fp) {
757                 fc_rport_error_retry(rport, fp);
758                 return;
759         }
760
761         if (!lport->tt.elsct_send(lport, rport, fp, ELS_PRLI,
762                                   fc_rport_prli_resp, rport, lport->e_d_tov))
763                 fc_rport_error_retry(rport, fp);
764         else
765                 get_device(&rport->dev);
766 }
767
768 /**
769  * fc_rport_els_rtv_resp() - Request Timeout Value response handler
770  * @sp: current sequence in the RTV exchange
771  * @fp: response frame
772  * @rp_arg: Fibre Channel remote port
773  *
774  * Many targets don't seem to support this.
775  *
776  * Locking Note: This function will be called without the rport lock
777  * held, but it will lock, call an _enter_* function or fc_rport_error
778  * and then unlock the rport.
779  */
780 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
781                               void *rp_arg)
782 {
783         struct fc_rport *rport = rp_arg;
784         struct fc_rport_libfc_priv *rdata = rport->dd_data;
785         u8 op;
786
787         mutex_lock(&rdata->rp_mutex);
788
789         FC_RPORT_DBG(rport, "Received a RTV response\n");
790
791         if (rdata->rp_state != RPORT_ST_RTV) {
792                 FC_RPORT_DBG(rport, "Received a RTV response, but in state "
793                              "%s\n", fc_rport_state(rport));
794                 if (IS_ERR(fp))
795                         goto err;
796                 goto out;
797         }
798
799         if (IS_ERR(fp)) {
800                 fc_rport_error(rport, fp);
801                 goto err;
802         }
803
804         op = fc_frame_payload_op(fp);
805         if (op == ELS_LS_ACC) {
806                 struct fc_els_rtv_acc *rtv;
807                 u32 toq;
808                 u32 tov;
809
810                 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
811                 if (rtv) {
812                         toq = ntohl(rtv->rtv_toq);
813                         tov = ntohl(rtv->rtv_r_a_tov);
814                         if (tov == 0)
815                                 tov = 1;
816                         rdata->r_a_tov = tov;
817                         tov = ntohl(rtv->rtv_e_d_tov);
818                         if (toq & FC_ELS_RTV_EDRES)
819                                 tov /= 1000000;
820                         if (tov == 0)
821                                 tov = 1;
822                         rdata->e_d_tov = tov;
823                 }
824         }
825
826         fc_rport_enter_ready(rport);
827
828 out:
829         fc_frame_free(fp);
830 err:
831         mutex_unlock(&rdata->rp_mutex);
832         put_device(&rport->dev);
833 }
834
835 /**
836  * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request to peer
837  * @rport: Fibre Channel remote port to send RTV to
838  *
839  * Locking Note: The rport lock is expected to be held before calling
840  * this routine.
841  */
842 static void fc_rport_enter_rtv(struct fc_rport *rport)
843 {
844         struct fc_frame *fp;
845         struct fc_rport_libfc_priv *rdata = rport->dd_data;
846         struct fc_lport *lport = rdata->local_port;
847
848         FC_RPORT_DBG(rport, "Port entered RTV state from %s state\n",
849                      fc_rport_state(rport));
850
851         fc_rport_state_enter(rport, RPORT_ST_RTV);
852
853         fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
854         if (!fp) {
855                 fc_rport_error_retry(rport, fp);
856                 return;
857         }
858
859         if (!lport->tt.elsct_send(lport, rport, fp, ELS_RTV,
860                                      fc_rport_rtv_resp, rport, lport->e_d_tov))
861                 fc_rport_error_retry(rport, fp);
862         else
863                 get_device(&rport->dev);
864 }
865
866 /**
867  * fc_rport_enter_logo() - Send Logout (LOGO) request to peer
868  * @rport: Fibre Channel remote port to send LOGO to
869  *
870  * Locking Note: The rport lock is expected to be held before calling
871  * this routine.
872  */
873 static void fc_rport_enter_logo(struct fc_rport *rport)
874 {
875         struct fc_rport_libfc_priv *rdata = rport->dd_data;
876         struct fc_lport *lport = rdata->local_port;
877         struct fc_frame *fp;
878
879         FC_RPORT_DBG(rport, "Port entered LOGO state from %s state\n",
880                      fc_rport_state(rport));
881
882         fc_rport_state_enter(rport, RPORT_ST_LOGO);
883
884         fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
885         if (!fp) {
886                 fc_rport_error_retry(rport, fp);
887                 return;
888         }
889
890         if (!lport->tt.elsct_send(lport, rport, fp, ELS_LOGO,
891                                   fc_rport_logo_resp, rport, lport->e_d_tov))
892                 fc_rport_error_retry(rport, fp);
893         else
894                 get_device(&rport->dev);
895 }
896
897
898 /**
899  * fc_rport_recv_req() - Receive a request from a rport
900  * @sp: current sequence in the PLOGI exchange
901  * @fp: response frame
902  * @rp_arg: Fibre Channel remote port
903  *
904  * Locking Note: Called without the rport lock held. This
905  * function will hold the rport lock, call an _enter_*
906  * function and then unlock the rport.
907  */
908 void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
909                        struct fc_rport *rport)
910 {
911         struct fc_rport_libfc_priv *rdata = rport->dd_data;
912         struct fc_lport *lport = rdata->local_port;
913
914         struct fc_frame_header *fh;
915         struct fc_seq_els_data els_data;
916         u8 op;
917
918         mutex_lock(&rdata->rp_mutex);
919
920         els_data.fp = NULL;
921         els_data.explan = ELS_EXPL_NONE;
922         els_data.reason = ELS_RJT_NONE;
923
924         fh = fc_frame_header_get(fp);
925
926         if (fh->fh_r_ctl == FC_RCTL_ELS_REQ && fh->fh_type == FC_TYPE_ELS) {
927                 op = fc_frame_payload_op(fp);
928                 switch (op) {
929                 case ELS_PLOGI:
930                         fc_rport_recv_plogi_req(rport, sp, fp);
931                         break;
932                 case ELS_PRLI:
933                         fc_rport_recv_prli_req(rport, sp, fp);
934                         break;
935                 case ELS_PRLO:
936                         fc_rport_recv_prlo_req(rport, sp, fp);
937                         break;
938                 case ELS_LOGO:
939                         fc_rport_recv_logo_req(rport, sp, fp);
940                         break;
941                 case ELS_RRQ:
942                         els_data.fp = fp;
943                         lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
944                         break;
945                 case ELS_REC:
946                         els_data.fp = fp;
947                         lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
948                         break;
949                 default:
950                         els_data.reason = ELS_RJT_UNSUP;
951                         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
952                         break;
953                 }
954         }
955
956         mutex_unlock(&rdata->rp_mutex);
957 }
958
959 /**
960  * fc_rport_recv_plogi_req() - Handle incoming Port Login (PLOGI) request
961  * @rport: Fibre Channel remote port that initiated PLOGI
962  * @sp: current sequence in the PLOGI exchange
963  * @fp: PLOGI request frame
964  *
965  * Locking Note: The rport lock is exected to be held before calling
966  * this function.
967  */
968 static void fc_rport_recv_plogi_req(struct fc_rport *rport,
969                                     struct fc_seq *sp, struct fc_frame *rx_fp)
970 {
971         struct fc_rport_libfc_priv *rdata = rport->dd_data;
972         struct fc_lport *lport = rdata->local_port;
973         struct fc_frame *fp = rx_fp;
974         struct fc_exch *ep;
975         struct fc_frame_header *fh;
976         struct fc_els_flogi *pl;
977         struct fc_seq_els_data rjt_data;
978         u32 sid;
979         u64 wwpn;
980         u64 wwnn;
981         enum fc_els_rjt_reason reject = 0;
982         u32 f_ctl;
983         rjt_data.fp = NULL;
984
985         fh = fc_frame_header_get(fp);
986
987         FC_RPORT_DBG(rport, "Received PLOGI request while in state %s\n",
988                      fc_rport_state(rport));
989
990         sid = ntoh24(fh->fh_s_id);
991         pl = fc_frame_payload_get(fp, sizeof(*pl));
992         if (!pl) {
993                 FC_RPORT_DBG(rport, "Received PLOGI too short\n");
994                 WARN_ON(1);
995                 /* XXX TBD: send reject? */
996                 fc_frame_free(fp);
997                 return;
998         }
999         wwpn = get_unaligned_be64(&pl->fl_wwpn);
1000         wwnn = get_unaligned_be64(&pl->fl_wwnn);
1001
1002         /*
1003          * If the session was just created, possibly due to the incoming PLOGI,
1004          * set the state appropriately and accept the PLOGI.
1005          *
1006          * If we had also sent a PLOGI, and if the received PLOGI is from a
1007          * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1008          * "command already in progress".
1009          *
1010          * XXX TBD: If the session was ready before, the PLOGI should result in
1011          * all outstanding exchanges being reset.
1012          */
1013         switch (rdata->rp_state) {
1014         case RPORT_ST_INIT:
1015                 FC_RPORT_DBG(rport, "Received PLOGI, wwpn %llx state INIT "
1016                              "- reject\n", (unsigned long long)wwpn);
1017                 reject = ELS_RJT_UNSUP;
1018                 break;
1019         case RPORT_ST_PLOGI:
1020                 FC_RPORT_DBG(rport, "Received PLOGI in PLOGI state %d\n",
1021                              rdata->rp_state);
1022                 if (wwpn < lport->wwpn)
1023                         reject = ELS_RJT_INPROG;
1024                 break;
1025         case RPORT_ST_PRLI:
1026         case RPORT_ST_READY:
1027                 FC_RPORT_DBG(rport, "Received PLOGI in logged-in state %d "
1028                              "- ignored for now\n", rdata->rp_state);
1029                 /* XXX TBD - should reset */
1030                 break;
1031         case RPORT_ST_DELETE:
1032         default:
1033                 FC_RPORT_DBG(rport, "Received PLOGI in unexpected "
1034                              "state %d\n", rdata->rp_state);
1035                 fc_frame_free(fp);
1036                 return;
1037                 break;
1038         }
1039
1040         if (reject) {
1041                 rjt_data.reason = reject;
1042                 rjt_data.explan = ELS_EXPL_NONE;
1043                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1044                 fc_frame_free(fp);
1045         } else {
1046                 fp = fc_frame_alloc(lport, sizeof(*pl));
1047                 if (fp == NULL) {
1048                         fp = rx_fp;
1049                         rjt_data.reason = ELS_RJT_UNAB;
1050                         rjt_data.explan = ELS_EXPL_NONE;
1051                         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1052                         fc_frame_free(fp);
1053                 } else {
1054                         sp = lport->tt.seq_start_next(sp);
1055                         WARN_ON(!sp);
1056                         fc_rport_set_name(rport, wwpn, wwnn);
1057
1058                         /*
1059                          * Get session payload size from incoming PLOGI.
1060                          */
1061                         rport->maxframe_size =
1062                                 fc_plogi_get_maxframe(pl, lport->mfs);
1063                         fc_frame_free(rx_fp);
1064                         fc_plogi_fill(lport, fp, ELS_LS_ACC);
1065
1066                         /*
1067                          * Send LS_ACC.  If this fails,
1068                          * the originator should retry.
1069                          */
1070                         f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1071                         f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1072                         ep = fc_seq_exch(sp);
1073                         fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1074                                        FC_TYPE_ELS, f_ctl, 0);
1075                         lport->tt.seq_send(lport, sp, fp);
1076                         if (rdata->rp_state == RPORT_ST_PLOGI)
1077                                 fc_rport_enter_prli(rport);
1078                 }
1079         }
1080 }
1081
1082 /**
1083  * fc_rport_recv_prli_req() - Handle incoming Process Login (PRLI) request
1084  * @rport: Fibre Channel remote port that initiated PRLI
1085  * @sp: current sequence in the PRLI exchange
1086  * @fp: PRLI request frame
1087  *
1088  * Locking Note: The rport lock is exected to be held before calling
1089  * this function.
1090  */
1091 static void fc_rport_recv_prli_req(struct fc_rport *rport,
1092                                    struct fc_seq *sp, struct fc_frame *rx_fp)
1093 {
1094         struct fc_rport_libfc_priv *rdata = rport->dd_data;
1095         struct fc_lport *lport = rdata->local_port;
1096         struct fc_exch *ep;
1097         struct fc_frame *fp;
1098         struct fc_frame_header *fh;
1099         struct {
1100                 struct fc_els_prli prli;
1101                 struct fc_els_spp spp;
1102         } *pp;
1103         struct fc_els_spp *rspp;        /* request service param page */
1104         struct fc_els_spp *spp; /* response spp */
1105         unsigned int len;
1106         unsigned int plen;
1107         enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1108         enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1109         enum fc_els_spp_resp resp;
1110         struct fc_seq_els_data rjt_data;
1111         u32 f_ctl;
1112         u32 fcp_parm;
1113         u32 roles = FC_RPORT_ROLE_UNKNOWN;
1114         rjt_data.fp = NULL;
1115
1116         fh = fc_frame_header_get(rx_fp);
1117
1118         FC_RPORT_DBG(rport, "Received PRLI request while in state %s\n",
1119                      fc_rport_state(rport));
1120
1121         switch (rdata->rp_state) {
1122         case RPORT_ST_PRLI:
1123         case RPORT_ST_READY:
1124                 reason = ELS_RJT_NONE;
1125                 break;
1126         default:
1127                 fc_frame_free(rx_fp);
1128                 return;
1129                 break;
1130         }
1131         len = fr_len(rx_fp) - sizeof(*fh);
1132         pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1133         if (pp == NULL) {
1134                 reason = ELS_RJT_PROT;
1135                 explan = ELS_EXPL_INV_LEN;
1136         } else {
1137                 plen = ntohs(pp->prli.prli_len);
1138                 if ((plen % 4) != 0 || plen > len) {
1139                         reason = ELS_RJT_PROT;
1140                         explan = ELS_EXPL_INV_LEN;
1141                 } else if (plen < len) {
1142                         len = plen;
1143                 }
1144                 plen = pp->prli.prli_spp_len;
1145                 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1146                     plen > len || len < sizeof(*pp)) {
1147                         reason = ELS_RJT_PROT;
1148                         explan = ELS_EXPL_INV_LEN;
1149                 }
1150                 rspp = &pp->spp;
1151         }
1152         if (reason != ELS_RJT_NONE ||
1153             (fp = fc_frame_alloc(lport, len)) == NULL) {
1154                 rjt_data.reason = reason;
1155                 rjt_data.explan = explan;
1156                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1157         } else {
1158                 sp = lport->tt.seq_start_next(sp);
1159                 WARN_ON(!sp);
1160                 pp = fc_frame_payload_get(fp, len);
1161                 WARN_ON(!pp);
1162                 memset(pp, 0, len);
1163                 pp->prli.prli_cmd = ELS_LS_ACC;
1164                 pp->prli.prli_spp_len = plen;
1165                 pp->prli.prli_len = htons(len);
1166                 len -= sizeof(struct fc_els_prli);
1167
1168                 /*
1169                  * Go through all the service parameter pages and build
1170                  * response.  If plen indicates longer SPP than standard,
1171                  * use that.  The entire response has been pre-cleared above.
1172                  */
1173                 spp = &pp->spp;
1174                 while (len >= plen) {
1175                         spp->spp_type = rspp->spp_type;
1176                         spp->spp_type_ext = rspp->spp_type_ext;
1177                         spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1178                         resp = FC_SPP_RESP_ACK;
1179                         if (rspp->spp_flags & FC_SPP_RPA_VAL)
1180                                 resp = FC_SPP_RESP_NO_PA;
1181                         switch (rspp->spp_type) {
1182                         case 0: /* common to all FC-4 types */
1183                                 break;
1184                         case FC_TYPE_FCP:
1185                                 fcp_parm = ntohl(rspp->spp_params);
1186                                 if (fcp_parm * FCP_SPPF_RETRY)
1187                                         rdata->flags |= FC_RP_FLAGS_RETRY;
1188                                 rport->supported_classes = FC_COS_CLASS3;
1189                                 if (fcp_parm & FCP_SPPF_INIT_FCN)
1190                                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1191                                 if (fcp_parm & FCP_SPPF_TARG_FCN)
1192                                         roles |= FC_RPORT_ROLE_FCP_TARGET;
1193                                 rport->roles = roles;
1194
1195                                 spp->spp_params =
1196                                         htonl(lport->service_params);
1197                                 break;
1198                         default:
1199                                 resp = FC_SPP_RESP_INVL;
1200                                 break;
1201                         }
1202                         spp->spp_flags |= resp;
1203                         len -= plen;
1204                         rspp = (struct fc_els_spp *)((char *)rspp + plen);
1205                         spp = (struct fc_els_spp *)((char *)spp + plen);
1206                 }
1207
1208                 /*
1209                  * Send LS_ACC.  If this fails, the originator should retry.
1210                  */
1211                 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1212                 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1213                 ep = fc_seq_exch(sp);
1214                 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1215                                FC_TYPE_ELS, f_ctl, 0);
1216                 lport->tt.seq_send(lport, sp, fp);
1217
1218                 /*
1219                  * Get lock and re-check state.
1220                  */
1221                 switch (rdata->rp_state) {
1222                 case RPORT_ST_PRLI:
1223                         fc_rport_enter_ready(rport);
1224                         break;
1225                 case RPORT_ST_READY:
1226                         break;
1227                 default:
1228                         break;
1229                 }
1230         }
1231         fc_frame_free(rx_fp);
1232 }
1233
1234 /**
1235  * fc_rport_recv_prlo_req() - Handle incoming Process Logout (PRLO) request
1236  * @rport: Fibre Channel remote port that initiated PRLO
1237  * @sp: current sequence in the PRLO exchange
1238  * @fp: PRLO request frame
1239  *
1240  * Locking Note: The rport lock is exected to be held before calling
1241  * this function.
1242  */
1243 static void fc_rport_recv_prlo_req(struct fc_rport *rport, struct fc_seq *sp,
1244                                    struct fc_frame *fp)
1245 {
1246         struct fc_rport_libfc_priv *rdata = rport->dd_data;
1247         struct fc_lport *lport = rdata->local_port;
1248
1249         struct fc_frame_header *fh;
1250         struct fc_seq_els_data rjt_data;
1251
1252         fh = fc_frame_header_get(fp);
1253
1254         FC_RPORT_DBG(rport, "Received PRLO request while in state %s\n",
1255                      fc_rport_state(rport));
1256
1257         if (rdata->rp_state == RPORT_ST_DELETE) {
1258                 fc_frame_free(fp);
1259                 return;
1260         }
1261
1262         rjt_data.fp = NULL;
1263         rjt_data.reason = ELS_RJT_UNAB;
1264         rjt_data.explan = ELS_EXPL_NONE;
1265         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1266         fc_frame_free(fp);
1267 }
1268
1269 /**
1270  * fc_rport_recv_logo_req() - Handle incoming Logout (LOGO) request
1271  * @rport: Fibre Channel remote port that initiated LOGO
1272  * @sp: current sequence in the LOGO exchange
1273  * @fp: LOGO request frame
1274  *
1275  * Locking Note: The rport lock is exected to be held before calling
1276  * this function.
1277  */
1278 static void fc_rport_recv_logo_req(struct fc_rport *rport, struct fc_seq *sp,
1279                                    struct fc_frame *fp)
1280 {
1281         struct fc_frame_header *fh;
1282         struct fc_rport_libfc_priv *rdata = rport->dd_data;
1283         struct fc_lport *lport = rdata->local_port;
1284
1285         fh = fc_frame_header_get(fp);
1286
1287         FC_RPORT_DBG(rport, "Received LOGO request while in state %s\n",
1288                      fc_rport_state(rport));
1289
1290         if (rdata->rp_state == RPORT_ST_DELETE) {
1291                 fc_frame_free(fp);
1292                 return;
1293         }
1294
1295         rdata->event = RPORT_EV_LOGO;
1296         fc_rport_state_enter(rport, RPORT_ST_DELETE);
1297         queue_work(rport_event_queue, &rdata->event_work);
1298
1299         lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1300         fc_frame_free(fp);
1301 }
1302
1303 static void fc_rport_flush_queue(void)
1304 {
1305         flush_workqueue(rport_event_queue);
1306 }
1307
1308 int fc_rport_init(struct fc_lport *lport)
1309 {
1310         if (!lport->tt.rport_create)
1311                 lport->tt.rport_create = fc_rport_rogue_create;
1312
1313         if (!lport->tt.rport_login)
1314                 lport->tt.rport_login = fc_rport_login;
1315
1316         if (!lport->tt.rport_logoff)
1317                 lport->tt.rport_logoff = fc_rport_logoff;
1318
1319         if (!lport->tt.rport_recv_req)
1320                 lport->tt.rport_recv_req = fc_rport_recv_req;
1321
1322         if (!lport->tt.rport_flush_queue)
1323                 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1324
1325         return 0;
1326 }
1327 EXPORT_SYMBOL(fc_rport_init);
1328
1329 int fc_setup_rport(void)
1330 {
1331         rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1332         if (!rport_event_queue)
1333                 return -ENOMEM;
1334         return 0;
1335 }
1336 EXPORT_SYMBOL(fc_setup_rport);
1337
1338 void fc_destroy_rport(void)
1339 {
1340         destroy_workqueue(rport_event_queue);
1341 }
1342 EXPORT_SYMBOL(fc_destroy_rport);
1343
1344 void fc_rport_terminate_io(struct fc_rport *rport)
1345 {
1346         struct fc_rport_libfc_priv *rdata = rport->dd_data;
1347         struct fc_lport *lport = rdata->local_port;
1348
1349         lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1350         lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
1351 }
1352 EXPORT_SYMBOL(fc_rport_terminate_io);