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