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