GNU Linux-libre 4.9.284-gnu1
[releases.git] / 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 hierarchy.
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/slab.h>
51 #include <linux/rcupdate.h>
52 #include <linux/timer.h>
53 #include <linux/workqueue.h>
54 #include <linux/export.h>
55 #include <asm/unaligned.h>
56
57 #include <scsi/libfc.h>
58 #include <scsi/fc_encode.h>
59
60 #include "fc_libfc.h"
61
62 static struct workqueue_struct *rport_event_queue;
63
64 static void fc_rport_enter_flogi(struct fc_rport_priv *);
65 static void fc_rport_enter_plogi(struct fc_rport_priv *);
66 static void fc_rport_enter_prli(struct fc_rport_priv *);
67 static void fc_rport_enter_rtv(struct fc_rport_priv *);
68 static void fc_rport_enter_ready(struct fc_rport_priv *);
69 static void fc_rport_enter_logo(struct fc_rport_priv *);
70 static void fc_rport_enter_adisc(struct fc_rport_priv *);
71
72 static void fc_rport_recv_plogi_req(struct fc_lport *, struct fc_frame *);
73 static void fc_rport_recv_prli_req(struct fc_rport_priv *, struct fc_frame *);
74 static void fc_rport_recv_prlo_req(struct fc_rport_priv *, struct fc_frame *);
75 static void fc_rport_recv_logo_req(struct fc_lport *, struct fc_frame *);
76 static void fc_rport_timeout(struct work_struct *);
77 static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
78 static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
79 static void fc_rport_work(struct work_struct *);
80
81 static const char *fc_rport_state_names[] = {
82         [RPORT_ST_INIT] = "Init",
83         [RPORT_ST_FLOGI] = "FLOGI",
84         [RPORT_ST_PLOGI_WAIT] = "PLOGI_WAIT",
85         [RPORT_ST_PLOGI] = "PLOGI",
86         [RPORT_ST_PRLI] = "PRLI",
87         [RPORT_ST_RTV] = "RTV",
88         [RPORT_ST_READY] = "Ready",
89         [RPORT_ST_ADISC] = "ADISC",
90         [RPORT_ST_DELETE] = "Delete",
91 };
92
93 /**
94  * fc_rport_lookup() - Lookup a remote port by port_id
95  * @lport:   The local port to lookup the remote port on
96  * @port_id: The remote port ID to look up
97  *
98  * The reference count of the fc_rport_priv structure is
99  * increased by one.
100  */
101 static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
102                                              u32 port_id)
103 {
104         struct fc_rport_priv *rdata = NULL, *tmp_rdata;
105
106         rcu_read_lock();
107         list_for_each_entry_rcu(tmp_rdata, &lport->disc.rports, peers)
108                 if (tmp_rdata->ids.port_id == port_id &&
109                     kref_get_unless_zero(&tmp_rdata->kref)) {
110                         rdata = tmp_rdata;
111                         break;
112                 }
113         rcu_read_unlock();
114         return rdata;
115 }
116
117 /**
118  * fc_rport_create() - Create a new remote port
119  * @lport: The local port this remote port will be associated with
120  * @ids:   The identifiers for the new remote port
121  *
122  * The remote port will start in the INIT state.
123  *
124  * Locking note:  must be called with the disc_mutex held.
125  */
126 static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
127                                              u32 port_id)
128 {
129         struct fc_rport_priv *rdata;
130         size_t rport_priv_size = sizeof(*rdata);
131
132         rdata = lport->tt.rport_lookup(lport, port_id);
133         if (rdata)
134                 return rdata;
135
136         if (lport->rport_priv_size > 0)
137                 rport_priv_size = lport->rport_priv_size;
138         rdata = kzalloc(rport_priv_size, GFP_KERNEL);
139         if (!rdata)
140                 return NULL;
141
142         rdata->ids.node_name = -1;
143         rdata->ids.port_name = -1;
144         rdata->ids.port_id = port_id;
145         rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
146
147         kref_init(&rdata->kref);
148         mutex_init(&rdata->rp_mutex);
149         rdata->local_port = lport;
150         rdata->rp_state = RPORT_ST_INIT;
151         rdata->event = RPORT_EV_NONE;
152         rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
153         rdata->e_d_tov = lport->e_d_tov;
154         rdata->r_a_tov = lport->r_a_tov;
155         rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
156         INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
157         INIT_WORK(&rdata->event_work, fc_rport_work);
158         if (port_id != FC_FID_DIR_SERV) {
159                 rdata->lld_event_callback = lport->tt.rport_event_callback;
160                 list_add_rcu(&rdata->peers, &lport->disc.rports);
161         }
162         return rdata;
163 }
164
165 /**
166  * fc_rport_destroy() - Free a remote port after last reference is released
167  * @kref: The remote port's kref
168  */
169 static void fc_rport_destroy(struct kref *kref)
170 {
171         struct fc_rport_priv *rdata;
172
173         rdata = container_of(kref, struct fc_rport_priv, kref);
174         kfree_rcu(rdata, rcu);
175 }
176
177 /**
178  * fc_rport_state() - Return a string identifying the remote port's state
179  * @rdata: The remote port
180  */
181 static const char *fc_rport_state(struct fc_rport_priv *rdata)
182 {
183         const char *cp;
184
185         cp = fc_rport_state_names[rdata->rp_state];
186         if (!cp)
187                 cp = "Unknown";
188         return cp;
189 }
190
191 /**
192  * fc_set_rport_loss_tmo() - Set the remote port loss timeout
193  * @rport:   The remote port that gets a new timeout value
194  * @timeout: The new timeout value (in seconds)
195  */
196 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
197 {
198         if (timeout)
199                 rport->dev_loss_tmo = timeout;
200         else
201                 rport->dev_loss_tmo = 1;
202 }
203 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
204
205 /**
206  * fc_plogi_get_maxframe() - Get the maximum payload from the common service
207  *                           parameters in a FLOGI frame
208  * @flp:    The FLOGI or PLOGI payload
209  * @maxval: The maximum frame size upper limit; this may be less than what
210  *          is in the service parameters
211  */
212 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
213                                           unsigned int maxval)
214 {
215         unsigned int mfs;
216
217         /*
218          * Get max payload from the common service parameters and the
219          * class 3 receive data field size.
220          */
221         mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
222         if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
223                 maxval = mfs;
224         mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
225         if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
226                 maxval = mfs;
227         return maxval;
228 }
229
230 /**
231  * fc_rport_state_enter() - Change the state of a remote port
232  * @rdata: The remote port whose state should change
233  * @new:   The new state
234  *
235  * Locking Note: Called with the rport lock held
236  */
237 static void fc_rport_state_enter(struct fc_rport_priv *rdata,
238                                  enum fc_rport_state new)
239 {
240         if (rdata->rp_state != new)
241                 rdata->retries = 0;
242         rdata->rp_state = new;
243 }
244
245 /**
246  * fc_rport_work() - Handler for remote port events in the rport_event_queue
247  * @work: Handle to the remote port being dequeued
248  */
249 static void fc_rport_work(struct work_struct *work)
250 {
251         u32 port_id;
252         struct fc_rport_priv *rdata =
253                 container_of(work, struct fc_rport_priv, event_work);
254         struct fc_rport_libfc_priv *rpriv;
255         enum fc_rport_event event;
256         struct fc_lport *lport = rdata->local_port;
257         struct fc_rport_operations *rport_ops;
258         struct fc_rport_identifiers ids;
259         struct fc_rport *rport;
260         struct fc4_prov *prov;
261         u8 type;
262
263         mutex_lock(&rdata->rp_mutex);
264         event = rdata->event;
265         rport_ops = rdata->ops;
266         rport = rdata->rport;
267
268         FC_RPORT_DBG(rdata, "work event %u\n", event);
269
270         switch (event) {
271         case RPORT_EV_READY:
272                 ids = rdata->ids;
273                 rdata->event = RPORT_EV_NONE;
274                 rdata->major_retries = 0;
275                 kref_get(&rdata->kref);
276                 mutex_unlock(&rdata->rp_mutex);
277
278                 if (!rport)
279                         rport = fc_remote_port_add(lport->host, 0, &ids);
280                 if (!rport) {
281                         FC_RPORT_DBG(rdata, "Failed to add the rport\n");
282                         lport->tt.rport_logoff(rdata);
283                         kref_put(&rdata->kref, lport->tt.rport_destroy);
284                         return;
285                 }
286                 mutex_lock(&rdata->rp_mutex);
287                 if (rdata->rport)
288                         FC_RPORT_DBG(rdata, "rport already allocated\n");
289                 rdata->rport = rport;
290                 rport->maxframe_size = rdata->maxframe_size;
291                 rport->supported_classes = rdata->supported_classes;
292
293                 rpriv = rport->dd_data;
294                 rpriv->local_port = lport;
295                 rpriv->rp_state = rdata->rp_state;
296                 rpriv->flags = rdata->flags;
297                 rpriv->e_d_tov = rdata->e_d_tov;
298                 rpriv->r_a_tov = rdata->r_a_tov;
299                 mutex_unlock(&rdata->rp_mutex);
300
301                 if (rport_ops && rport_ops->event_callback) {
302                         FC_RPORT_DBG(rdata, "callback ev %d\n", event);
303                         rport_ops->event_callback(lport, rdata, event);
304                 }
305                 if (rdata->lld_event_callback) {
306                         FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
307                         rdata->lld_event_callback(lport, rdata, event);
308                 }
309                 kref_put(&rdata->kref, lport->tt.rport_destroy);
310                 break;
311
312         case RPORT_EV_FAILED:
313         case RPORT_EV_LOGO:
314         case RPORT_EV_STOP:
315                 if (rdata->prli_count) {
316                         mutex_lock(&fc_prov_mutex);
317                         for (type = 1; type < FC_FC4_PROV_SIZE; type++) {
318                                 prov = fc_passive_prov[type];
319                                 if (prov && prov->prlo)
320                                         prov->prlo(rdata);
321                         }
322                         mutex_unlock(&fc_prov_mutex);
323                 }
324                 port_id = rdata->ids.port_id;
325                 mutex_unlock(&rdata->rp_mutex);
326
327                 if (rport_ops && rport_ops->event_callback) {
328                         FC_RPORT_DBG(rdata, "callback ev %d\n", event);
329                         rport_ops->event_callback(lport, rdata, event);
330                 }
331                 if (rdata->lld_event_callback) {
332                         FC_RPORT_DBG(rdata, "lld callback ev %d\n", event);
333                         rdata->lld_event_callback(lport, rdata, event);
334                 }
335                 cancel_delayed_work_sync(&rdata->retry_work);
336
337                 /*
338                  * Reset any outstanding exchanges before freeing rport.
339                  */
340                 lport->tt.exch_mgr_reset(lport, 0, port_id);
341                 lport->tt.exch_mgr_reset(lport, port_id, 0);
342
343                 if (rport) {
344                         rpriv = rport->dd_data;
345                         rpriv->rp_state = RPORT_ST_DELETE;
346                         mutex_lock(&rdata->rp_mutex);
347                         rdata->rport = NULL;
348                         mutex_unlock(&rdata->rp_mutex);
349                         fc_remote_port_delete(rport);
350                 }
351
352                 mutex_lock(&rdata->rp_mutex);
353                 if (rdata->rp_state == RPORT_ST_DELETE) {
354                         if (port_id == FC_FID_DIR_SERV) {
355                                 rdata->event = RPORT_EV_NONE;
356                                 mutex_unlock(&rdata->rp_mutex);
357                                 kref_put(&rdata->kref, lport->tt.rport_destroy);
358                         } else if ((rdata->flags & FC_RP_STARTED) &&
359                                    rdata->major_retries <
360                                    lport->max_rport_retry_count) {
361                                 rdata->major_retries++;
362                                 rdata->event = RPORT_EV_NONE;
363                                 FC_RPORT_DBG(rdata, "work restart\n");
364                                 fc_rport_enter_flogi(rdata);
365                                 mutex_unlock(&rdata->rp_mutex);
366                         } else {
367                                 FC_RPORT_DBG(rdata, "work delete\n");
368                                 list_del_rcu(&rdata->peers);
369                                 mutex_unlock(&rdata->rp_mutex);
370                                 kref_put(&rdata->kref, lport->tt.rport_destroy);
371                         }
372                 } else {
373                         /*
374                          * Re-open for events.  Reissue READY event if ready.
375                          */
376                         rdata->event = RPORT_EV_NONE;
377                         if (rdata->rp_state == RPORT_ST_READY)
378                                 fc_rport_enter_ready(rdata);
379                         mutex_unlock(&rdata->rp_mutex);
380                 }
381                 break;
382
383         default:
384                 mutex_unlock(&rdata->rp_mutex);
385                 break;
386         }
387 }
388
389 /**
390  * fc_rport_login() - Start the remote port login state machine
391  * @rdata: The remote port to be logged in to
392  *
393  * Locking Note: Called without the rport lock held. This
394  * function will hold the rport lock, call an _enter_*
395  * function and then unlock the rport.
396  *
397  * This indicates the intent to be logged into the remote port.
398  * If it appears we are already logged in, ADISC is used to verify
399  * the setup.
400  */
401 static int fc_rport_login(struct fc_rport_priv *rdata)
402 {
403         mutex_lock(&rdata->rp_mutex);
404
405         rdata->flags |= FC_RP_STARTED;
406         switch (rdata->rp_state) {
407         case RPORT_ST_READY:
408                 FC_RPORT_DBG(rdata, "ADISC port\n");
409                 fc_rport_enter_adisc(rdata);
410                 break;
411         case RPORT_ST_DELETE:
412                 FC_RPORT_DBG(rdata, "Restart deleted port\n");
413                 break;
414         default:
415                 FC_RPORT_DBG(rdata, "Login to port\n");
416                 fc_rport_enter_flogi(rdata);
417                 break;
418         }
419         mutex_unlock(&rdata->rp_mutex);
420
421         return 0;
422 }
423
424 /**
425  * fc_rport_enter_delete() - Schedule a remote port to be deleted
426  * @rdata: The remote port to be deleted
427  * @event: The event to report as the reason for deletion
428  *
429  * Locking Note: Called with the rport lock held.
430  *
431  * Allow state change into DELETE only once.
432  *
433  * Call queue_work only if there's no event already pending.
434  * Set the new event so that the old pending event will not occur.
435  * Since we have the mutex, even if fc_rport_work() is already started,
436  * it'll see the new event.
437  */
438 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
439                                   enum fc_rport_event event)
440 {
441         if (rdata->rp_state == RPORT_ST_DELETE)
442                 return;
443
444         FC_RPORT_DBG(rdata, "Delete port\n");
445
446         fc_rport_state_enter(rdata, RPORT_ST_DELETE);
447
448         if (rdata->event == RPORT_EV_NONE)
449                 queue_work(rport_event_queue, &rdata->event_work);
450         rdata->event = event;
451 }
452
453 /**
454  * fc_rport_logoff() - Logoff and remove a remote port
455  * @rdata: The remote port to be logged off of
456  *
457  * Locking Note: Called without the rport lock held. This
458  * function will hold the rport lock, call an _enter_*
459  * function and then unlock the rport.
460  */
461 static int fc_rport_logoff(struct fc_rport_priv *rdata)
462 {
463         struct fc_lport *lport = rdata->local_port;
464         u32 port_id = rdata->ids.port_id;
465
466         mutex_lock(&rdata->rp_mutex);
467
468         FC_RPORT_DBG(rdata, "Remove port\n");
469
470         rdata->flags &= ~FC_RP_STARTED;
471         if (rdata->rp_state == RPORT_ST_DELETE) {
472                 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
473                 goto out;
474         }
475         /*
476          * FC-LS states:
477          * To explicitly Logout, the initiating Nx_Port shall terminate
478          * other open Sequences that it initiated with the destination
479          * Nx_Port prior to performing Logout.
480          */
481         lport->tt.exch_mgr_reset(lport, 0, port_id);
482         lport->tt.exch_mgr_reset(lport, port_id, 0);
483
484         fc_rport_enter_logo(rdata);
485
486         /*
487          * Change the state to Delete so that we discard
488          * the response.
489          */
490         fc_rport_enter_delete(rdata, RPORT_EV_STOP);
491 out:
492         mutex_unlock(&rdata->rp_mutex);
493         return 0;
494 }
495
496 /**
497  * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
498  * @rdata: The remote port that is ready
499  *
500  * Locking Note: The rport lock is expected to be held before calling
501  * this routine.
502  */
503 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
504 {
505         fc_rport_state_enter(rdata, RPORT_ST_READY);
506
507         FC_RPORT_DBG(rdata, "Port is Ready\n");
508
509         if (rdata->event == RPORT_EV_NONE)
510                 queue_work(rport_event_queue, &rdata->event_work);
511         rdata->event = RPORT_EV_READY;
512 }
513
514 /**
515  * fc_rport_timeout() - Handler for the retry_work timer
516  * @work: Handle to the remote port that has timed out
517  *
518  * Locking Note: Called without the rport lock held. This
519  * function will hold the rport lock, call an _enter_*
520  * function and then unlock the rport.
521  */
522 static void fc_rport_timeout(struct work_struct *work)
523 {
524         struct fc_rport_priv *rdata =
525                 container_of(work, struct fc_rport_priv, retry_work.work);
526
527         mutex_lock(&rdata->rp_mutex);
528
529         switch (rdata->rp_state) {
530         case RPORT_ST_FLOGI:
531                 fc_rport_enter_flogi(rdata);
532                 break;
533         case RPORT_ST_PLOGI:
534                 fc_rport_enter_plogi(rdata);
535                 break;
536         case RPORT_ST_PRLI:
537                 fc_rport_enter_prli(rdata);
538                 break;
539         case RPORT_ST_RTV:
540                 fc_rport_enter_rtv(rdata);
541                 break;
542         case RPORT_ST_ADISC:
543                 fc_rport_enter_adisc(rdata);
544                 break;
545         case RPORT_ST_PLOGI_WAIT:
546         case RPORT_ST_READY:
547         case RPORT_ST_INIT:
548         case RPORT_ST_DELETE:
549                 break;
550         }
551
552         mutex_unlock(&rdata->rp_mutex);
553 }
554
555 /**
556  * fc_rport_error() - Error handler, called once retries have been exhausted
557  * @rdata: The remote port the error is happened on
558  * @fp:    The error code encapsulated in a frame pointer
559  *
560  * Locking Note: The rport lock is expected to be held before
561  * calling this routine
562  */
563 static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
564 {
565         struct fc_lport *lport = rdata->local_port;
566
567         FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
568                      IS_ERR(fp) ? -PTR_ERR(fp) : 0,
569                      fc_rport_state(rdata), rdata->retries);
570
571         switch (rdata->rp_state) {
572         case RPORT_ST_FLOGI:
573                 rdata->flags &= ~FC_RP_STARTED;
574                 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
575                 break;
576         case RPORT_ST_PLOGI:
577                 if (lport->point_to_multipoint) {
578                         rdata->flags &= ~FC_RP_STARTED;
579                         fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
580                 } else
581                         fc_rport_enter_logo(rdata);
582                 break;
583         case RPORT_ST_RTV:
584                 fc_rport_enter_ready(rdata);
585                 break;
586         case RPORT_ST_PRLI:
587         case RPORT_ST_ADISC:
588                 fc_rport_enter_logo(rdata);
589                 break;
590         case RPORT_ST_PLOGI_WAIT:
591         case RPORT_ST_DELETE:
592         case RPORT_ST_READY:
593         case RPORT_ST_INIT:
594                 break;
595         }
596 }
597
598 /**
599  * fc_rport_error_retry() - Handler for remote port state retries
600  * @rdata: The remote port whose state is to be retried
601  * @fp:    The error code encapsulated in a frame pointer
602  *
603  * If the error was an exchange timeout retry immediately,
604  * otherwise wait for E_D_TOV.
605  *
606  * Locking Note: The rport lock is expected to be held before
607  * calling this routine
608  */
609 static void fc_rport_error_retry(struct fc_rport_priv *rdata,
610                                  struct fc_frame *fp)
611 {
612         unsigned long delay = msecs_to_jiffies(FC_DEF_E_D_TOV);
613
614         /* make sure this isn't an FC_EX_CLOSED error, never retry those */
615         if (PTR_ERR(fp) == -FC_EX_CLOSED)
616                 goto out;
617
618         if (rdata->retries < rdata->local_port->max_rport_retry_count) {
619                 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
620                              PTR_ERR(fp), fc_rport_state(rdata));
621                 rdata->retries++;
622                 /* no additional delay on exchange timeouts */
623                 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
624                         delay = 0;
625                 schedule_delayed_work(&rdata->retry_work, delay);
626                 return;
627         }
628
629 out:
630         fc_rport_error(rdata, fp);
631 }
632
633 /**
634  * fc_rport_login_complete() - Handle parameters and completion of p-mp login.
635  * @rdata:  The remote port which we logged into or which logged into us.
636  * @fp:     The FLOGI or PLOGI request or response frame
637  *
638  * Returns non-zero error if a problem is detected with the frame.
639  * Does not free the frame.
640  *
641  * This is only used in point-to-multipoint mode for FIP currently.
642  */
643 static int fc_rport_login_complete(struct fc_rport_priv *rdata,
644                                    struct fc_frame *fp)
645 {
646         struct fc_lport *lport = rdata->local_port;
647         struct fc_els_flogi *flogi;
648         unsigned int e_d_tov;
649         u16 csp_flags;
650
651         flogi = fc_frame_payload_get(fp, sizeof(*flogi));
652         if (!flogi)
653                 return -EINVAL;
654
655         csp_flags = ntohs(flogi->fl_csp.sp_features);
656
657         if (fc_frame_payload_op(fp) == ELS_FLOGI) {
658                 if (csp_flags & FC_SP_FT_FPORT) {
659                         FC_RPORT_DBG(rdata, "Fabric bit set in FLOGI\n");
660                         return -EINVAL;
661                 }
662         } else {
663
664                 /*
665                  * E_D_TOV is not valid on an incoming FLOGI request.
666                  */
667                 e_d_tov = ntohl(flogi->fl_csp.sp_e_d_tov);
668                 if (csp_flags & FC_SP_FT_EDTR)
669                         e_d_tov /= 1000000;
670                 if (e_d_tov > rdata->e_d_tov)
671                         rdata->e_d_tov = e_d_tov;
672         }
673         rdata->maxframe_size = fc_plogi_get_maxframe(flogi, lport->mfs);
674         return 0;
675 }
676
677 /**
678  * fc_rport_flogi_resp() - Handle response to FLOGI request for p-mp mode
679  * @sp:     The sequence that the FLOGI was on
680  * @fp:     The FLOGI response frame
681  * @rp_arg: The remote port that received the FLOGI response
682  */
683 static void fc_rport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
684                                 void *rp_arg)
685 {
686         struct fc_rport_priv *rdata = rp_arg;
687         struct fc_lport *lport = rdata->local_port;
688         struct fc_els_flogi *flogi;
689         unsigned int r_a_tov;
690
691         FC_RPORT_DBG(rdata, "Received a FLOGI %s\n", fc_els_resp_type(fp));
692
693         if (fp == ERR_PTR(-FC_EX_CLOSED))
694                 goto put;
695
696         mutex_lock(&rdata->rp_mutex);
697
698         if (rdata->rp_state != RPORT_ST_FLOGI) {
699                 FC_RPORT_DBG(rdata, "Received a FLOGI response, but in state "
700                              "%s\n", fc_rport_state(rdata));
701                 if (IS_ERR(fp))
702                         goto err;
703                 goto out;
704         }
705
706         if (IS_ERR(fp)) {
707                 fc_rport_error(rdata, fp);
708                 goto err;
709         }
710
711         if (fc_frame_payload_op(fp) != ELS_LS_ACC)
712                 goto bad;
713         if (fc_rport_login_complete(rdata, fp))
714                 goto bad;
715
716         flogi = fc_frame_payload_get(fp, sizeof(*flogi));
717         if (!flogi)
718                 goto bad;
719         r_a_tov = ntohl(flogi->fl_csp.sp_r_a_tov);
720         if (r_a_tov > rdata->r_a_tov)
721                 rdata->r_a_tov = r_a_tov;
722
723         if (rdata->ids.port_name < lport->wwpn)
724                 fc_rport_enter_plogi(rdata);
725         else
726                 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
727 out:
728         fc_frame_free(fp);
729 err:
730         mutex_unlock(&rdata->rp_mutex);
731 put:
732         kref_put(&rdata->kref, lport->tt.rport_destroy);
733         return;
734 bad:
735         FC_RPORT_DBG(rdata, "Bad FLOGI response\n");
736         fc_rport_error_retry(rdata, fp);
737         goto out;
738 }
739
740 /**
741  * fc_rport_enter_flogi() - Send a FLOGI request to the remote port for p-mp
742  * @rdata: The remote port to send a FLOGI to
743  *
744  * Locking Note: The rport lock is expected to be held before calling
745  * this routine.
746  */
747 static void fc_rport_enter_flogi(struct fc_rport_priv *rdata)
748 {
749         struct fc_lport *lport = rdata->local_port;
750         struct fc_frame *fp;
751
752         if (!lport->point_to_multipoint)
753                 return fc_rport_enter_plogi(rdata);
754
755         FC_RPORT_DBG(rdata, "Entered FLOGI state from %s state\n",
756                      fc_rport_state(rdata));
757
758         fc_rport_state_enter(rdata, RPORT_ST_FLOGI);
759
760         fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
761         if (!fp)
762                 return fc_rport_error_retry(rdata, fp);
763
764         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_FLOGI,
765                                   fc_rport_flogi_resp, rdata,
766                                   2 * lport->r_a_tov))
767                 fc_rport_error_retry(rdata, NULL);
768         else
769                 kref_get(&rdata->kref);
770 }
771
772 /**
773  * fc_rport_recv_flogi_req() - Handle Fabric Login (FLOGI) request in p-mp mode
774  * @lport: The local port that received the PLOGI request
775  * @rx_fp: The PLOGI request frame
776  */
777 static void fc_rport_recv_flogi_req(struct fc_lport *lport,
778                                     struct fc_frame *rx_fp)
779 {
780         struct fc_disc *disc;
781         struct fc_els_flogi *flp;
782         struct fc_rport_priv *rdata;
783         struct fc_frame *fp = rx_fp;
784         struct fc_seq_els_data rjt_data;
785         u32 sid;
786
787         sid = fc_frame_sid(fp);
788
789         FC_RPORT_ID_DBG(lport, sid, "Received FLOGI request\n");
790
791         disc = &lport->disc;
792         if (!lport->point_to_multipoint) {
793                 rjt_data.reason = ELS_RJT_UNSUP;
794                 rjt_data.explan = ELS_EXPL_NONE;
795                 goto reject;
796         }
797
798         flp = fc_frame_payload_get(fp, sizeof(*flp));
799         if (!flp) {
800                 rjt_data.reason = ELS_RJT_LOGIC;
801                 rjt_data.explan = ELS_EXPL_INV_LEN;
802                 goto reject;
803         }
804
805         rdata = lport->tt.rport_lookup(lport, sid);
806         if (!rdata) {
807                 rjt_data.reason = ELS_RJT_FIP;
808                 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
809                 goto reject;
810         }
811         mutex_lock(&rdata->rp_mutex);
812
813         FC_RPORT_DBG(rdata, "Received FLOGI in %s state\n",
814                      fc_rport_state(rdata));
815
816         switch (rdata->rp_state) {
817         case RPORT_ST_INIT:
818                 /*
819                  * If received the FLOGI request on RPORT which is INIT state
820                  * (means not transition to FLOGI either fc_rport timeout
821                  * function didn;t trigger or this end hasn;t received
822                  * beacon yet from other end. In that case only, allow RPORT
823                  * state machine to continue, otherwise fall through which
824                  * causes the code to send reject response.
825                  * NOTE; Not checking for FIP->state such as VNMP_UP or
826                  * VNMP_CLAIM because if FIP state is not one of those,
827                  * RPORT wouldn;t have created and 'rport_lookup' would have
828                  * failed anyway in that case.
829                  */
830                 if (lport->point_to_multipoint)
831                         break;
832         case RPORT_ST_DELETE:
833                 mutex_unlock(&rdata->rp_mutex);
834                 rjt_data.reason = ELS_RJT_FIP;
835                 rjt_data.explan = ELS_EXPL_NOT_NEIGHBOR;
836                 goto reject_put;
837         case RPORT_ST_FLOGI:
838         case RPORT_ST_PLOGI_WAIT:
839         case RPORT_ST_PLOGI:
840                 break;
841         case RPORT_ST_PRLI:
842         case RPORT_ST_RTV:
843         case RPORT_ST_READY:
844         case RPORT_ST_ADISC:
845                 /*
846                  * Set the remote port to be deleted and to then restart.
847                  * This queues work to be sure exchanges are reset.
848                  */
849                 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
850                 mutex_unlock(&rdata->rp_mutex);
851                 rjt_data.reason = ELS_RJT_BUSY;
852                 rjt_data.explan = ELS_EXPL_NONE;
853                 goto reject_put;
854         }
855         if (fc_rport_login_complete(rdata, fp)) {
856                 mutex_unlock(&rdata->rp_mutex);
857                 rjt_data.reason = ELS_RJT_LOGIC;
858                 rjt_data.explan = ELS_EXPL_NONE;
859                 goto reject_put;
860         }
861
862         fp = fc_frame_alloc(lport, sizeof(*flp));
863         if (!fp)
864                 goto out;
865
866         fc_flogi_fill(lport, fp);
867         flp = fc_frame_payload_get(fp, sizeof(*flp));
868         flp->fl_cmd = ELS_LS_ACC;
869
870         fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
871         lport->tt.frame_send(lport, fp);
872
873         if (rdata->ids.port_name < lport->wwpn)
874                 fc_rport_enter_plogi(rdata);
875         else
876                 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
877 out:
878         mutex_unlock(&rdata->rp_mutex);
879         kref_put(&rdata->kref, lport->tt.rport_destroy);
880         fc_frame_free(rx_fp);
881         return;
882
883 reject_put:
884         kref_put(&rdata->kref, lport->tt.rport_destroy);
885 reject:
886         lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
887         fc_frame_free(rx_fp);
888 }
889
890 /**
891  * fc_rport_plogi_resp() - Handler for ELS PLOGI responses
892  * @sp:        The sequence the PLOGI is on
893  * @fp:        The PLOGI response frame
894  * @rdata_arg: The remote port that sent the PLOGI response
895  *
896  * Locking Note: This function will be called without the rport lock
897  * held, but it will lock, call an _enter_* function or fc_rport_error
898  * and then unlock the rport.
899  */
900 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
901                                 void *rdata_arg)
902 {
903         struct fc_rport_priv *rdata = rdata_arg;
904         struct fc_lport *lport = rdata->local_port;
905         struct fc_els_flogi *plp = NULL;
906         u16 csp_seq;
907         u16 cssp_seq;
908         u8 op;
909
910         mutex_lock(&rdata->rp_mutex);
911
912         FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
913
914         if (rdata->rp_state != RPORT_ST_PLOGI) {
915                 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
916                              "%s\n", fc_rport_state(rdata));
917                 if (IS_ERR(fp))
918                         goto err;
919                 goto out;
920         }
921
922         if (IS_ERR(fp)) {
923                 fc_rport_error_retry(rdata, fp);
924                 goto err;
925         }
926
927         op = fc_frame_payload_op(fp);
928         if (op == ELS_LS_ACC &&
929             (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
930                 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
931                 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
932
933                 /* save plogi response sp_features for further reference */
934                 rdata->sp_features = ntohs(plp->fl_csp.sp_features);
935
936                 if (lport->point_to_multipoint)
937                         fc_rport_login_complete(rdata, fp);
938                 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
939                 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
940                 if (cssp_seq < csp_seq)
941                         csp_seq = cssp_seq;
942                 rdata->max_seq = csp_seq;
943                 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
944                 fc_rport_enter_prli(rdata);
945         } else
946                 fc_rport_error_retry(rdata, fp);
947
948 out:
949         fc_frame_free(fp);
950 err:
951         mutex_unlock(&rdata->rp_mutex);
952         kref_put(&rdata->kref, lport->tt.rport_destroy);
953 }
954
955 static bool
956 fc_rport_compatible_roles(struct fc_lport *lport, struct fc_rport_priv *rdata)
957 {
958         if (rdata->ids.roles == FC_PORT_ROLE_UNKNOWN)
959                 return true;
960         if ((rdata->ids.roles & FC_PORT_ROLE_FCP_TARGET) &&
961             (lport->service_params & FCP_SPPF_INIT_FCN))
962                 return true;
963         if ((rdata->ids.roles & FC_PORT_ROLE_FCP_INITIATOR) &&
964             (lport->service_params & FCP_SPPF_TARG_FCN))
965                 return true;
966         return false;
967 }
968
969 /**
970  * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
971  * @rdata: The remote port to send a PLOGI to
972  *
973  * Locking Note: The rport lock is expected to be held before calling
974  * this routine.
975  */
976 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
977 {
978         struct fc_lport *lport = rdata->local_port;
979         struct fc_frame *fp;
980
981         if (!fc_rport_compatible_roles(lport, rdata)) {
982                 FC_RPORT_DBG(rdata, "PLOGI suppressed for incompatible role\n");
983                 fc_rport_state_enter(rdata, RPORT_ST_PLOGI_WAIT);
984                 return;
985         }
986
987         FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
988                      fc_rport_state(rdata));
989
990         fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
991
992         rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
993         fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
994         if (!fp) {
995                 FC_RPORT_DBG(rdata, "%s frame alloc failed\n", __func__);
996                 fc_rport_error_retry(rdata, fp);
997                 return;
998         }
999         rdata->e_d_tov = lport->e_d_tov;
1000
1001         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
1002                                   fc_rport_plogi_resp, rdata,
1003                                   2 * lport->r_a_tov))
1004                 fc_rport_error_retry(rdata, NULL);
1005         else
1006                 kref_get(&rdata->kref);
1007 }
1008
1009 /**
1010  * fc_rport_prli_resp() - Process Login (PRLI) response handler
1011  * @sp:        The sequence the PRLI response was on
1012  * @fp:        The PRLI response frame
1013  * @rdata_arg: The remote port that sent the PRLI response
1014  *
1015  * Locking Note: This function will be called without the rport lock
1016  * held, but it will lock, call an _enter_* function or fc_rport_error
1017  * and then unlock the rport.
1018  */
1019 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
1020                                void *rdata_arg)
1021 {
1022         struct fc_rport_priv *rdata = rdata_arg;
1023         struct {
1024                 struct fc_els_prli prli;
1025                 struct fc_els_spp spp;
1026         } *pp;
1027         struct fc_els_spp temp_spp;
1028         struct fc4_prov *prov;
1029         u32 roles = FC_RPORT_ROLE_UNKNOWN;
1030         u32 fcp_parm = 0;
1031         u8 op;
1032         u8 resp_code = 0;
1033
1034         mutex_lock(&rdata->rp_mutex);
1035
1036         FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
1037
1038         if (rdata->rp_state != RPORT_ST_PRLI) {
1039                 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
1040                              "%s\n", fc_rport_state(rdata));
1041                 if (IS_ERR(fp))
1042                         goto err;
1043                 goto out;
1044         }
1045
1046         if (IS_ERR(fp)) {
1047                 fc_rport_error_retry(rdata, fp);
1048                 goto err;
1049         }
1050
1051         /* reinitialize remote port roles */
1052         rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1053
1054         op = fc_frame_payload_op(fp);
1055         if (op == ELS_LS_ACC) {
1056                 pp = fc_frame_payload_get(fp, sizeof(*pp));
1057                 if (!pp)
1058                         goto out;
1059
1060                 resp_code = (pp->spp.spp_flags & FC_SPP_RESP_MASK);
1061                 FC_RPORT_DBG(rdata, "PRLI spp_flags = 0x%x\n",
1062                              pp->spp.spp_flags);
1063                 rdata->spp_type = pp->spp.spp_type;
1064                 if (resp_code != FC_SPP_RESP_ACK) {
1065                         if (resp_code == FC_SPP_RESP_CONF)
1066                                 fc_rport_error(rdata, fp);
1067                         else
1068                                 fc_rport_error_retry(rdata, fp);
1069                         goto out;
1070                 }
1071                 if (pp->prli.prli_spp_len < sizeof(pp->spp))
1072                         goto out;
1073
1074                 fcp_parm = ntohl(pp->spp.spp_params);
1075                 if (fcp_parm & FCP_SPPF_RETRY)
1076                         rdata->flags |= FC_RP_FLAGS_RETRY;
1077                 if (fcp_parm & FCP_SPPF_CONF_COMPL)
1078                         rdata->flags |= FC_RP_FLAGS_CONF_REQ;
1079
1080                 prov = fc_passive_prov[FC_TYPE_FCP];
1081                 if (prov) {
1082                         memset(&temp_spp, 0, sizeof(temp_spp));
1083                         prov->prli(rdata, pp->prli.prli_spp_len,
1084                                    &pp->spp, &temp_spp);
1085                 }
1086
1087                 rdata->supported_classes = FC_COS_CLASS3;
1088                 if (fcp_parm & FCP_SPPF_INIT_FCN)
1089                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1090                 if (fcp_parm & FCP_SPPF_TARG_FCN)
1091                         roles |= FC_RPORT_ROLE_FCP_TARGET;
1092
1093                 rdata->ids.roles = roles;
1094                 fc_rport_enter_rtv(rdata);
1095
1096         } else {
1097                 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
1098                 fc_rport_error_retry(rdata, fp);
1099         }
1100
1101 out:
1102         fc_frame_free(fp);
1103 err:
1104         mutex_unlock(&rdata->rp_mutex);
1105         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1106 }
1107
1108 /**
1109  * fc_rport_enter_prli() - Send Process Login (PRLI) request
1110  * @rdata: The remote port to send the PRLI request to
1111  *
1112  * Locking Note: The rport lock is expected to be held before calling
1113  * this routine.
1114  */
1115 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
1116 {
1117         struct fc_lport *lport = rdata->local_port;
1118         struct {
1119                 struct fc_els_prli prli;
1120                 struct fc_els_spp spp;
1121         } *pp;
1122         struct fc_frame *fp;
1123         struct fc4_prov *prov;
1124
1125         /*
1126          * If the rport is one of the well known addresses
1127          * we skip PRLI and RTV and go straight to READY.
1128          */
1129         if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
1130                 fc_rport_enter_ready(rdata);
1131                 return;
1132         }
1133
1134         FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
1135                      fc_rport_state(rdata));
1136
1137         fc_rport_state_enter(rdata, RPORT_ST_PRLI);
1138
1139         fp = fc_frame_alloc(lport, sizeof(*pp));
1140         if (!fp) {
1141                 fc_rport_error_retry(rdata, fp);
1142                 return;
1143         }
1144
1145         fc_prli_fill(lport, fp);
1146
1147         prov = fc_passive_prov[FC_TYPE_FCP];
1148         if (prov) {
1149                 pp = fc_frame_payload_get(fp, sizeof(*pp));
1150                 prov->prli(rdata, sizeof(pp->spp), NULL, &pp->spp);
1151         }
1152
1153         fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rdata->ids.port_id,
1154                        fc_host_port_id(lport->host), FC_TYPE_ELS,
1155                        FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
1156
1157         if (!lport->tt.exch_seq_send(lport, fp, fc_rport_prli_resp,
1158                                     NULL, rdata, 2 * lport->r_a_tov))
1159                 fc_rport_error_retry(rdata, NULL);
1160         else
1161                 kref_get(&rdata->kref);
1162 }
1163
1164 /**
1165  * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
1166  * @sp:        The sequence the RTV was on
1167  * @fp:        The RTV response frame
1168  * @rdata_arg: The remote port that sent the RTV response
1169  *
1170  * Many targets don't seem to support this.
1171  *
1172  * Locking Note: This function will be called without the rport lock
1173  * held, but it will lock, call an _enter_* function or fc_rport_error
1174  * and then unlock the rport.
1175  */
1176 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
1177                               void *rdata_arg)
1178 {
1179         struct fc_rport_priv *rdata = rdata_arg;
1180         u8 op;
1181
1182         mutex_lock(&rdata->rp_mutex);
1183
1184         FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
1185
1186         if (rdata->rp_state != RPORT_ST_RTV) {
1187                 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
1188                              "%s\n", fc_rport_state(rdata));
1189                 if (IS_ERR(fp))
1190                         goto err;
1191                 goto out;
1192         }
1193
1194         if (IS_ERR(fp)) {
1195                 fc_rport_error(rdata, fp);
1196                 goto err;
1197         }
1198
1199         op = fc_frame_payload_op(fp);
1200         if (op == ELS_LS_ACC) {
1201                 struct fc_els_rtv_acc *rtv;
1202                 u32 toq;
1203                 u32 tov;
1204
1205                 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
1206                 if (rtv) {
1207                         toq = ntohl(rtv->rtv_toq);
1208                         tov = ntohl(rtv->rtv_r_a_tov);
1209                         if (tov == 0)
1210                                 tov = 1;
1211                         rdata->r_a_tov = tov;
1212                         tov = ntohl(rtv->rtv_e_d_tov);
1213                         if (toq & FC_ELS_RTV_EDRES)
1214                                 tov /= 1000000;
1215                         if (tov == 0)
1216                                 tov = 1;
1217                         rdata->e_d_tov = tov;
1218                 }
1219         }
1220
1221         fc_rport_enter_ready(rdata);
1222
1223 out:
1224         fc_frame_free(fp);
1225 err:
1226         mutex_unlock(&rdata->rp_mutex);
1227         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1228 }
1229
1230 /**
1231  * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
1232  * @rdata: The remote port to send the RTV request to
1233  *
1234  * Locking Note: The rport lock is expected to be held before calling
1235  * this routine.
1236  */
1237 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
1238 {
1239         struct fc_frame *fp;
1240         struct fc_lport *lport = rdata->local_port;
1241
1242         FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
1243                      fc_rport_state(rdata));
1244
1245         fc_rport_state_enter(rdata, RPORT_ST_RTV);
1246
1247         fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
1248         if (!fp) {
1249                 fc_rport_error_retry(rdata, fp);
1250                 return;
1251         }
1252
1253         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
1254                                   fc_rport_rtv_resp, rdata,
1255                                   2 * lport->r_a_tov))
1256                 fc_rport_error_retry(rdata, NULL);
1257         else
1258                 kref_get(&rdata->kref);
1259 }
1260
1261 /**
1262  * fc_rport_logo_resp() - Handler for logout (LOGO) responses
1263  * @sp:        The sequence the LOGO was on
1264  * @fp:        The LOGO response frame
1265  * @lport_arg: The local port
1266  */
1267 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1268                                void *lport_arg)
1269 {
1270         struct fc_lport *lport = lport_arg;
1271
1272         FC_RPORT_ID_DBG(lport, fc_seq_exch(sp)->did,
1273                         "Received a LOGO %s\n", fc_els_resp_type(fp));
1274         if (IS_ERR(fp))
1275                 return;
1276         fc_frame_free(fp);
1277 }
1278
1279 /**
1280  * fc_rport_enter_logo() - Send a logout (LOGO) request
1281  * @rdata: The remote port to send the LOGO request to
1282  *
1283  * Locking Note: The rport lock is expected to be held before calling
1284  * this routine.
1285  */
1286 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
1287 {
1288         struct fc_lport *lport = rdata->local_port;
1289         struct fc_frame *fp;
1290
1291         FC_RPORT_DBG(rdata, "Port sending LOGO from %s state\n",
1292                      fc_rport_state(rdata));
1293
1294         fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
1295         if (!fp)
1296                 return;
1297         (void)lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
1298                                    fc_rport_logo_resp, lport, 0);
1299 }
1300
1301 /**
1302  * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
1303  * @sp:        The sequence the ADISC response was on
1304  * @fp:        The ADISC response frame
1305  * @rdata_arg: The remote port that sent the ADISC response
1306  *
1307  * Locking Note: This function will be called without the rport lock
1308  * held, but it will lock, call an _enter_* function or fc_rport_error
1309  * and then unlock the rport.
1310  */
1311 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
1312                                 void *rdata_arg)
1313 {
1314         struct fc_rport_priv *rdata = rdata_arg;
1315         struct fc_els_adisc *adisc;
1316         u8 op;
1317
1318         mutex_lock(&rdata->rp_mutex);
1319
1320         FC_RPORT_DBG(rdata, "Received a ADISC response\n");
1321
1322         if (rdata->rp_state != RPORT_ST_ADISC) {
1323                 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
1324                              fc_rport_state(rdata));
1325                 if (IS_ERR(fp))
1326                         goto err;
1327                 goto out;
1328         }
1329
1330         if (IS_ERR(fp)) {
1331                 fc_rport_error(rdata, fp);
1332                 goto err;
1333         }
1334
1335         /*
1336          * If address verification failed.  Consider us logged out of the rport.
1337          * Since the rport is still in discovery, we want to be
1338          * logged in, so go to PLOGI state.  Otherwise, go back to READY.
1339          */
1340         op = fc_frame_payload_op(fp);
1341         adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1342         if (op != ELS_LS_ACC || !adisc ||
1343             ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1344             get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1345             get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1346                 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1347                 fc_rport_enter_flogi(rdata);
1348         } else {
1349                 FC_RPORT_DBG(rdata, "ADISC OK\n");
1350                 fc_rport_enter_ready(rdata);
1351         }
1352 out:
1353         fc_frame_free(fp);
1354 err:
1355         mutex_unlock(&rdata->rp_mutex);
1356         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1357 }
1358
1359 /**
1360  * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1361  * @rdata: The remote port to send the ADISC request to
1362  *
1363  * Locking Note: The rport lock is expected to be held before calling
1364  * this routine.
1365  */
1366 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1367 {
1368         struct fc_lport *lport = rdata->local_port;
1369         struct fc_frame *fp;
1370
1371         FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1372                      fc_rport_state(rdata));
1373
1374         fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1375
1376         fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1377         if (!fp) {
1378                 fc_rport_error_retry(rdata, fp);
1379                 return;
1380         }
1381         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1382                                   fc_rport_adisc_resp, rdata,
1383                                   2 * lport->r_a_tov))
1384                 fc_rport_error_retry(rdata, NULL);
1385         else
1386                 kref_get(&rdata->kref);
1387 }
1388
1389 /**
1390  * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1391  * @rdata: The remote port that sent the ADISC request
1392  * @in_fp: The ADISC request frame
1393  *
1394  * Locking Note:  Called with the lport and rport locks held.
1395  */
1396 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1397                                     struct fc_frame *in_fp)
1398 {
1399         struct fc_lport *lport = rdata->local_port;
1400         struct fc_frame *fp;
1401         struct fc_els_adisc *adisc;
1402         struct fc_seq_els_data rjt_data;
1403
1404         FC_RPORT_DBG(rdata, "Received ADISC request\n");
1405
1406         adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1407         if (!adisc) {
1408                 rjt_data.reason = ELS_RJT_PROT;
1409                 rjt_data.explan = ELS_EXPL_INV_LEN;
1410                 lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
1411                 goto drop;
1412         }
1413
1414         fp = fc_frame_alloc(lport, sizeof(*adisc));
1415         if (!fp)
1416                 goto drop;
1417         fc_adisc_fill(lport, fp);
1418         adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1419         adisc->adisc_cmd = ELS_LS_ACC;
1420         fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
1421         lport->tt.frame_send(lport, fp);
1422 drop:
1423         fc_frame_free(in_fp);
1424 }
1425
1426 /**
1427  * fc_rport_recv_rls_req() - Handle received Read Link Status request
1428  * @rdata: The remote port that sent the RLS request
1429  * @rx_fp: The PRLI request frame
1430  *
1431  * Locking Note: The rport lock is expected to be held before calling
1432  * this function.
1433  */
1434 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1435                                   struct fc_frame *rx_fp)
1436
1437 {
1438         struct fc_lport *lport = rdata->local_port;
1439         struct fc_frame *fp;
1440         struct fc_els_rls *rls;
1441         struct fc_els_rls_resp *rsp;
1442         struct fc_els_lesb *lesb;
1443         struct fc_seq_els_data rjt_data;
1444         struct fc_host_statistics *hst;
1445
1446         FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1447                      fc_rport_state(rdata));
1448
1449         rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1450         if (!rls) {
1451                 rjt_data.reason = ELS_RJT_PROT;
1452                 rjt_data.explan = ELS_EXPL_INV_LEN;
1453                 goto out_rjt;
1454         }
1455
1456         fp = fc_frame_alloc(lport, sizeof(*rsp));
1457         if (!fp) {
1458                 rjt_data.reason = ELS_RJT_UNAB;
1459                 rjt_data.explan = ELS_EXPL_INSUF_RES;
1460                 goto out_rjt;
1461         }
1462
1463         rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1464         memset(rsp, 0, sizeof(*rsp));
1465         rsp->rls_cmd = ELS_LS_ACC;
1466         lesb = &rsp->rls_lesb;
1467         if (lport->tt.get_lesb) {
1468                 /* get LESB from LLD if it supports it */
1469                 lport->tt.get_lesb(lport, lesb);
1470         } else {
1471                 fc_get_host_stats(lport->host);
1472                 hst = &lport->host_stats;
1473                 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1474                 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1475                 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1476                 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1477                 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1478                 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1479         }
1480
1481         fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1482         lport->tt.frame_send(lport, fp);
1483         goto out;
1484
1485 out_rjt:
1486         lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1487 out:
1488         fc_frame_free(rx_fp);
1489 }
1490
1491 /**
1492  * fc_rport_recv_els_req() - Handler for validated ELS requests
1493  * @lport: The local port that received the ELS request
1494  * @fp:    The ELS request frame
1495  *
1496  * Handle incoming ELS requests that require port login.
1497  * The ELS opcode has already been validated by the caller.
1498  *
1499  * Locking Note: Called with the lport lock held.
1500  */
1501 static void fc_rport_recv_els_req(struct fc_lport *lport, struct fc_frame *fp)
1502 {
1503         struct fc_rport_priv *rdata;
1504         struct fc_seq_els_data els_data;
1505
1506         rdata = lport->tt.rport_lookup(lport, fc_frame_sid(fp));
1507         if (!rdata)
1508                 goto reject;
1509
1510         mutex_lock(&rdata->rp_mutex);
1511
1512         switch (rdata->rp_state) {
1513         case RPORT_ST_PRLI:
1514         case RPORT_ST_RTV:
1515         case RPORT_ST_READY:
1516         case RPORT_ST_ADISC:
1517                 break;
1518         default:
1519                 mutex_unlock(&rdata->rp_mutex);
1520                 kref_put(&rdata->kref, lport->tt.rport_destroy);
1521                 goto reject;
1522         }
1523
1524         switch (fc_frame_payload_op(fp)) {
1525         case ELS_PRLI:
1526                 fc_rport_recv_prli_req(rdata, fp);
1527                 break;
1528         case ELS_PRLO:
1529                 fc_rport_recv_prlo_req(rdata, fp);
1530                 break;
1531         case ELS_ADISC:
1532                 fc_rport_recv_adisc_req(rdata, fp);
1533                 break;
1534         case ELS_RRQ:
1535                 lport->tt.seq_els_rsp_send(fp, ELS_RRQ, NULL);
1536                 fc_frame_free(fp);
1537                 break;
1538         case ELS_REC:
1539                 lport->tt.seq_els_rsp_send(fp, ELS_REC, NULL);
1540                 fc_frame_free(fp);
1541                 break;
1542         case ELS_RLS:
1543                 fc_rport_recv_rls_req(rdata, fp);
1544                 break;
1545         default:
1546                 fc_frame_free(fp);      /* can't happen */
1547                 break;
1548         }
1549
1550         mutex_unlock(&rdata->rp_mutex);
1551         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1552         return;
1553
1554 reject:
1555         els_data.reason = ELS_RJT_UNAB;
1556         els_data.explan = ELS_EXPL_PLOGI_REQD;
1557         lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1558         fc_frame_free(fp);
1559 }
1560
1561 /**
1562  * fc_rport_recv_req() - Handler for requests
1563  * @lport: The local port that received the request
1564  * @fp:    The request frame
1565  *
1566  * Locking Note: Called with the lport lock held.
1567  */
1568 static void fc_rport_recv_req(struct fc_lport *lport, struct fc_frame *fp)
1569 {
1570         struct fc_seq_els_data els_data;
1571
1572         /*
1573          * Handle FLOGI, PLOGI and LOGO requests separately, since they
1574          * don't require prior login.
1575          * Check for unsupported opcodes first and reject them.
1576          * For some ops, it would be incorrect to reject with "PLOGI required".
1577          */
1578         switch (fc_frame_payload_op(fp)) {
1579         case ELS_FLOGI:
1580                 fc_rport_recv_flogi_req(lport, fp);
1581                 break;
1582         case ELS_PLOGI:
1583                 fc_rport_recv_plogi_req(lport, fp);
1584                 break;
1585         case ELS_LOGO:
1586                 fc_rport_recv_logo_req(lport, fp);
1587                 break;
1588         case ELS_PRLI:
1589         case ELS_PRLO:
1590         case ELS_ADISC:
1591         case ELS_RRQ:
1592         case ELS_REC:
1593         case ELS_RLS:
1594                 fc_rport_recv_els_req(lport, fp);
1595                 break;
1596         default:
1597                 els_data.reason = ELS_RJT_UNSUP;
1598                 els_data.explan = ELS_EXPL_NONE;
1599                 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &els_data);
1600                 fc_frame_free(fp);
1601                 break;
1602         }
1603 }
1604
1605 /**
1606  * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1607  * @lport: The local port that received the PLOGI request
1608  * @rx_fp: The PLOGI request frame
1609  *
1610  * Locking Note: The rport lock is held before calling this function.
1611  */
1612 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1613                                     struct fc_frame *rx_fp)
1614 {
1615         struct fc_disc *disc;
1616         struct fc_rport_priv *rdata;
1617         struct fc_frame *fp = rx_fp;
1618         struct fc_els_flogi *pl;
1619         struct fc_seq_els_data rjt_data;
1620         u32 sid;
1621
1622         sid = fc_frame_sid(fp);
1623
1624         FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1625
1626         pl = fc_frame_payload_get(fp, sizeof(*pl));
1627         if (!pl) {
1628                 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1629                 rjt_data.reason = ELS_RJT_PROT;
1630                 rjt_data.explan = ELS_EXPL_INV_LEN;
1631                 goto reject;
1632         }
1633
1634         disc = &lport->disc;
1635         mutex_lock(&disc->disc_mutex);
1636         rdata = lport->tt.rport_create(lport, sid);
1637         if (!rdata) {
1638                 mutex_unlock(&disc->disc_mutex);
1639                 rjt_data.reason = ELS_RJT_UNAB;
1640                 rjt_data.explan = ELS_EXPL_INSUF_RES;
1641                 goto reject;
1642         }
1643
1644         mutex_lock(&rdata->rp_mutex);
1645         mutex_unlock(&disc->disc_mutex);
1646
1647         rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1648         rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1649
1650         /*
1651          * If the rport was just created, possibly due to the incoming PLOGI,
1652          * set the state appropriately and accept the PLOGI.
1653          *
1654          * If we had also sent a PLOGI, and if the received PLOGI is from a
1655          * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1656          * "command already in progress".
1657          *
1658          * XXX TBD: If the session was ready before, the PLOGI should result in
1659          * all outstanding exchanges being reset.
1660          */
1661         switch (rdata->rp_state) {
1662         case RPORT_ST_INIT:
1663                 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1664                 break;
1665         case RPORT_ST_PLOGI_WAIT:
1666                 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI_WAIT state\n");
1667                 break;
1668         case RPORT_ST_PLOGI:
1669                 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1670                 if (rdata->ids.port_name < lport->wwpn) {
1671                         mutex_unlock(&rdata->rp_mutex);
1672                         rjt_data.reason = ELS_RJT_INPROG;
1673                         rjt_data.explan = ELS_EXPL_NONE;
1674                         goto reject;
1675                 }
1676                 break;
1677         case RPORT_ST_PRLI:
1678         case RPORT_ST_RTV:
1679         case RPORT_ST_READY:
1680         case RPORT_ST_ADISC:
1681                 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1682                              "- ignored for now\n", rdata->rp_state);
1683                 /* XXX TBD - should reset */
1684                 break;
1685         case RPORT_ST_FLOGI:
1686         case RPORT_ST_DELETE:
1687                 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1688                              fc_rport_state(rdata));
1689                 mutex_unlock(&rdata->rp_mutex);
1690                 rjt_data.reason = ELS_RJT_BUSY;
1691                 rjt_data.explan = ELS_EXPL_NONE;
1692                 goto reject;
1693         }
1694         if (!fc_rport_compatible_roles(lport, rdata)) {
1695                 FC_RPORT_DBG(rdata, "Received PLOGI for incompatible role\n");
1696                 mutex_unlock(&rdata->rp_mutex);
1697                 rjt_data.reason = ELS_RJT_LOGIC;
1698                 rjt_data.explan = ELS_EXPL_NONE;
1699                 goto reject;
1700         }
1701
1702         /*
1703          * Get session payload size from incoming PLOGI.
1704          */
1705         rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1706
1707         /*
1708          * Send LS_ACC.  If this fails, the originator should retry.
1709          */
1710         fp = fc_frame_alloc(lport, sizeof(*pl));
1711         if (!fp)
1712                 goto out;
1713
1714         fc_plogi_fill(lport, fp, ELS_LS_ACC);
1715         fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1716         lport->tt.frame_send(lport, fp);
1717         fc_rport_enter_prli(rdata);
1718 out:
1719         mutex_unlock(&rdata->rp_mutex);
1720         fc_frame_free(rx_fp);
1721         return;
1722
1723 reject:
1724         lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
1725         fc_frame_free(fp);
1726 }
1727
1728 /**
1729  * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1730  * @rdata: The remote port that sent the PRLI request
1731  * @rx_fp: The PRLI request frame
1732  *
1733  * Locking Note: The rport lock is expected to be held before calling
1734  * this function.
1735  */
1736 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1737                                    struct fc_frame *rx_fp)
1738 {
1739         struct fc_lport *lport = rdata->local_port;
1740         struct fc_frame *fp;
1741         struct {
1742                 struct fc_els_prli prli;
1743                 struct fc_els_spp spp;
1744         } *pp;
1745         struct fc_els_spp *rspp;        /* request service param page */
1746         struct fc_els_spp *spp; /* response spp */
1747         unsigned int len;
1748         unsigned int plen;
1749         enum fc_els_spp_resp resp;
1750         enum fc_els_spp_resp passive;
1751         struct fc_seq_els_data rjt_data;
1752         struct fc4_prov *prov;
1753
1754         FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1755                      fc_rport_state(rdata));
1756
1757         len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1758         pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1759         if (!pp)
1760                 goto reject_len;
1761         plen = ntohs(pp->prli.prli_len);
1762         if ((plen % 4) != 0 || plen > len || plen < 16)
1763                 goto reject_len;
1764         if (plen < len)
1765                 len = plen;
1766         plen = pp->prli.prli_spp_len;
1767         if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1768             plen > len || len < sizeof(*pp) || plen < 12)
1769                 goto reject_len;
1770         rspp = &pp->spp;
1771
1772         fp = fc_frame_alloc(lport, len);
1773         if (!fp) {
1774                 rjt_data.reason = ELS_RJT_UNAB;
1775                 rjt_data.explan = ELS_EXPL_INSUF_RES;
1776                 goto reject;
1777         }
1778         pp = fc_frame_payload_get(fp, len);
1779         WARN_ON(!pp);
1780         memset(pp, 0, len);
1781         pp->prli.prli_cmd = ELS_LS_ACC;
1782         pp->prli.prli_spp_len = plen;
1783         pp->prli.prli_len = htons(len);
1784         len -= sizeof(struct fc_els_prli);
1785
1786         /*
1787          * Go through all the service parameter pages and build
1788          * response.  If plen indicates longer SPP than standard,
1789          * use that.  The entire response has been pre-cleared above.
1790          */
1791         spp = &pp->spp;
1792         mutex_lock(&fc_prov_mutex);
1793         while (len >= plen) {
1794                 rdata->spp_type = rspp->spp_type;
1795                 spp->spp_type = rspp->spp_type;
1796                 spp->spp_type_ext = rspp->spp_type_ext;
1797                 resp = 0;
1798
1799                 if (rspp->spp_type < FC_FC4_PROV_SIZE) {
1800                         prov = fc_active_prov[rspp->spp_type];
1801                         if (prov)
1802                                 resp = prov->prli(rdata, plen, rspp, spp);
1803                         prov = fc_passive_prov[rspp->spp_type];
1804                         if (prov) {
1805                                 passive = prov->prli(rdata, plen, rspp, spp);
1806                                 if (!resp || passive == FC_SPP_RESP_ACK)
1807                                         resp = passive;
1808                         }
1809                 }
1810                 if (!resp) {
1811                         if (spp->spp_flags & FC_SPP_EST_IMG_PAIR)
1812                                 resp |= FC_SPP_RESP_CONF;
1813                         else
1814                                 resp |= FC_SPP_RESP_INVL;
1815                 }
1816                 spp->spp_flags |= resp;
1817                 len -= plen;
1818                 rspp = (struct fc_els_spp *)((char *)rspp + plen);
1819                 spp = (struct fc_els_spp *)((char *)spp + plen);
1820         }
1821         mutex_unlock(&fc_prov_mutex);
1822
1823         /*
1824          * Send LS_ACC.  If this fails, the originator should retry.
1825          */
1826         fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1827         lport->tt.frame_send(lport, fp);
1828
1829         switch (rdata->rp_state) {
1830         case RPORT_ST_PRLI:
1831                 fc_rport_enter_ready(rdata);
1832                 break;
1833         default:
1834                 break;
1835         }
1836         goto drop;
1837
1838 reject_len:
1839         rjt_data.reason = ELS_RJT_PROT;
1840         rjt_data.explan = ELS_EXPL_INV_LEN;
1841 reject:
1842         lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1843 drop:
1844         fc_frame_free(rx_fp);
1845 }
1846
1847 /**
1848  * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1849  * @rdata: The remote port that sent the PRLO request
1850  * @rx_fp: The PRLO request frame
1851  *
1852  * Locking Note: The rport lock is expected to be held before calling
1853  * this function.
1854  */
1855 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1856                                    struct fc_frame *rx_fp)
1857 {
1858         struct fc_lport *lport = rdata->local_port;
1859         struct fc_frame *fp;
1860         struct {
1861                 struct fc_els_prlo prlo;
1862                 struct fc_els_spp spp;
1863         } *pp;
1864         struct fc_els_spp *rspp;        /* request service param page */
1865         struct fc_els_spp *spp;         /* response spp */
1866         unsigned int len;
1867         unsigned int plen;
1868         struct fc_seq_els_data rjt_data;
1869
1870         FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1871                      fc_rport_state(rdata));
1872
1873         len = fr_len(rx_fp) - sizeof(struct fc_frame_header);
1874         pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1875         if (!pp)
1876                 goto reject_len;
1877         plen = ntohs(pp->prlo.prlo_len);
1878         if (plen != 20)
1879                 goto reject_len;
1880         if (plen < len)
1881                 len = plen;
1882
1883         rspp = &pp->spp;
1884
1885         fp = fc_frame_alloc(lport, len);
1886         if (!fp) {
1887                 rjt_data.reason = ELS_RJT_UNAB;
1888                 rjt_data.explan = ELS_EXPL_INSUF_RES;
1889                 goto reject;
1890         }
1891
1892         pp = fc_frame_payload_get(fp, len);
1893         WARN_ON(!pp);
1894         memset(pp, 0, len);
1895         pp->prlo.prlo_cmd = ELS_LS_ACC;
1896         pp->prlo.prlo_obs = 0x10;
1897         pp->prlo.prlo_len = htons(len);
1898         spp = &pp->spp;
1899         spp->spp_type = rspp->spp_type;
1900         spp->spp_type_ext = rspp->spp_type_ext;
1901         spp->spp_flags = FC_SPP_RESP_ACK;
1902
1903         fc_rport_enter_prli(rdata);
1904
1905         fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
1906         lport->tt.frame_send(lport, fp);
1907         goto drop;
1908
1909 reject_len:
1910         rjt_data.reason = ELS_RJT_PROT;
1911         rjt_data.explan = ELS_EXPL_INV_LEN;
1912 reject:
1913         lport->tt.seq_els_rsp_send(rx_fp, ELS_LS_RJT, &rjt_data);
1914 drop:
1915         fc_frame_free(rx_fp);
1916 }
1917
1918 /**
1919  * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1920  * @lport: The local port that received the LOGO request
1921  * @fp:    The LOGO request frame
1922  *
1923  * Locking Note: The rport lock is expected to be held before calling
1924  * this function.
1925  */
1926 static void fc_rport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
1927 {
1928         struct fc_rport_priv *rdata;
1929         u32 sid;
1930
1931         lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
1932
1933         sid = fc_frame_sid(fp);
1934
1935         rdata = lport->tt.rport_lookup(lport, sid);
1936         if (rdata) {
1937                 mutex_lock(&rdata->rp_mutex);
1938                 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1939                              fc_rport_state(rdata));
1940
1941                 fc_rport_enter_delete(rdata, RPORT_EV_STOP);
1942                 mutex_unlock(&rdata->rp_mutex);
1943                 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1944         } else
1945                 FC_RPORT_ID_DBG(lport, sid,
1946                                 "Received LOGO from non-logged-in port\n");
1947         fc_frame_free(fp);
1948 }
1949
1950 /**
1951  * fc_rport_flush_queue() - Flush the rport_event_queue
1952  */
1953 static void fc_rport_flush_queue(void)
1954 {
1955         flush_workqueue(rport_event_queue);
1956 }
1957
1958 /**
1959  * fc_rport_init() - Initialize the remote port layer for a local port
1960  * @lport: The local port to initialize the remote port layer for
1961  */
1962 int fc_rport_init(struct fc_lport *lport)
1963 {
1964         if (!lport->tt.rport_lookup)
1965                 lport->tt.rport_lookup = fc_rport_lookup;
1966
1967         if (!lport->tt.rport_create)
1968                 lport->tt.rport_create = fc_rport_create;
1969
1970         if (!lport->tt.rport_login)
1971                 lport->tt.rport_login = fc_rport_login;
1972
1973         if (!lport->tt.rport_logoff)
1974                 lport->tt.rport_logoff = fc_rport_logoff;
1975
1976         if (!lport->tt.rport_recv_req)
1977                 lport->tt.rport_recv_req = fc_rport_recv_req;
1978
1979         if (!lport->tt.rport_flush_queue)
1980                 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1981
1982         if (!lport->tt.rport_destroy)
1983                 lport->tt.rport_destroy = fc_rport_destroy;
1984
1985         return 0;
1986 }
1987 EXPORT_SYMBOL(fc_rport_init);
1988
1989 /**
1990  * fc_rport_fcp_prli() - Handle incoming PRLI for the FCP initiator.
1991  * @rdata: remote port private
1992  * @spp_len: service parameter page length
1993  * @rspp: received service parameter page
1994  * @spp: response service parameter page
1995  *
1996  * Returns the value for the response code to be placed in spp_flags;
1997  * Returns 0 if not an initiator.
1998  */
1999 static int fc_rport_fcp_prli(struct fc_rport_priv *rdata, u32 spp_len,
2000                              const struct fc_els_spp *rspp,
2001                              struct fc_els_spp *spp)
2002 {
2003         struct fc_lport *lport = rdata->local_port;
2004         u32 fcp_parm;
2005
2006         fcp_parm = ntohl(rspp->spp_params);
2007         rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
2008         if (fcp_parm & FCP_SPPF_INIT_FCN)
2009                 rdata->ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2010         if (fcp_parm & FCP_SPPF_TARG_FCN)
2011                 rdata->ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2012         if (fcp_parm & FCP_SPPF_RETRY)
2013                 rdata->flags |= FC_RP_FLAGS_RETRY;
2014         rdata->supported_classes = FC_COS_CLASS3;
2015
2016         if (!(lport->service_params & FCP_SPPF_INIT_FCN))
2017                 return 0;
2018
2019         spp->spp_flags |= rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
2020
2021         /*
2022          * OR in our service parameters with other providers (target), if any.
2023          */
2024         fcp_parm = ntohl(spp->spp_params);
2025         spp->spp_params = htonl(fcp_parm | lport->service_params);
2026         return FC_SPP_RESP_ACK;
2027 }
2028
2029 /*
2030  * FC-4 provider ops for FCP initiator.
2031  */
2032 struct fc4_prov fc_rport_fcp_init = {
2033         .prli = fc_rport_fcp_prli,
2034 };
2035
2036 /**
2037  * fc_rport_t0_prli() - Handle incoming PRLI parameters for type 0
2038  * @rdata: remote port private
2039  * @spp_len: service parameter page length
2040  * @rspp: received service parameter page
2041  * @spp: response service parameter page
2042  */
2043 static int fc_rport_t0_prli(struct fc_rport_priv *rdata, u32 spp_len,
2044                             const struct fc_els_spp *rspp,
2045                             struct fc_els_spp *spp)
2046 {
2047         if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR)
2048                 return FC_SPP_RESP_INVL;
2049         return FC_SPP_RESP_ACK;
2050 }
2051
2052 /*
2053  * FC-4 provider ops for type 0 service parameters.
2054  *
2055  * This handles the special case of type 0 which is always successful
2056  * but doesn't do anything otherwise.
2057  */
2058 struct fc4_prov fc_rport_t0_prov = {
2059         .prli = fc_rport_t0_prli,
2060 };
2061
2062 /**
2063  * fc_setup_rport() - Initialize the rport_event_queue
2064  */
2065 int fc_setup_rport(void)
2066 {
2067         rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
2068         if (!rport_event_queue)
2069                 return -ENOMEM;
2070         return 0;
2071 }
2072
2073 /**
2074  * fc_destroy_rport() - Destroy the rport_event_queue
2075  */
2076 void fc_destroy_rport(void)
2077 {
2078         destroy_workqueue(rport_event_queue);
2079 }
2080
2081 /**
2082  * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
2083  * @rport: The remote port whose I/O should be terminated
2084  */
2085 void fc_rport_terminate_io(struct fc_rport *rport)
2086 {
2087         struct fc_rport_libfc_priv *rpriv = rport->dd_data;
2088         struct fc_lport *lport = rpriv->local_port;
2089
2090         lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
2091         lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
2092 }
2093 EXPORT_SYMBOL(fc_rport_terminate_io);