[SCSI] libfc: remote port gets stuck in restart state without really restarting
[safe/jmp/linux-2.6] / drivers / scsi / libfc / fc_rport.c
1 /*
2  * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16  *
17  * Maintained at www.Open-FCoE.org
18  */
19
20 /*
21  * RPORT GENERAL INFO
22  *
23  * This file contains all processing regarding fc_rports. It contains the
24  * rport state machine and does all rport interaction with the transport class.
25  * There should be no other places in libfc that interact directly with the
26  * transport class in regards to adding and deleting rports.
27  *
28  * fc_rport's represent N_Port's within the fabric.
29  */
30
31 /*
32  * RPORT LOCKING
33  *
34  * The rport should never hold the rport mutex and then attempt to acquire
35  * either the lport or disc mutexes. The rport's mutex is considered lesser
36  * than both the lport's mutex and the disc mutex. Refer to fc_lport.c for
37  * more comments on the heirarchy.
38  *
39  * The locking strategy is similar to the lport's strategy. The lock protects
40  * the rport's states and is held and released by the entry points to the rport
41  * block. All _enter_* functions correspond to rport states and expect the rport
42  * mutex to be locked before calling them. This means that rports only handle
43  * one request or response at a time, since they're not critical for the I/O
44  * path this potential over-use of the mutex is acceptable.
45  */
46
47 #include <linux/kernel.h>
48 #include <linux/spinlock.h>
49 #include <linux/interrupt.h>
50 #include <linux/rcupdate.h>
51 #include <linux/timer.h>
52 #include <linux/workqueue.h>
53 #include <asm/unaligned.h>
54
55 #include <scsi/libfc.h>
56 #include <scsi/fc_encode.h>
57
58 #include "fc_libfc.h"
59
60 struct workqueue_struct *rport_event_queue;
61
62 static void fc_rport_enter_plogi(struct fc_rport_priv *);
63 static void fc_rport_enter_prli(struct fc_rport_priv *);
64 static void fc_rport_enter_rtv(struct fc_rport_priv *);
65 static void fc_rport_enter_ready(struct fc_rport_priv *);
66 static void fc_rport_enter_logo(struct fc_rport_priv *);
67 static void fc_rport_enter_adisc(struct fc_rport_priv *);
68
69 static void fc_rport_recv_plogi_req(struct fc_lport *,
70                                     struct fc_seq *, struct fc_frame *);
71 static void fc_rport_recv_prli_req(struct fc_rport_priv *,
72                                    struct fc_seq *, struct fc_frame *);
73 static void fc_rport_recv_prlo_req(struct fc_rport_priv *,
74                                    struct fc_seq *, struct fc_frame *);
75 static void fc_rport_recv_logo_req(struct fc_lport *,
76                                    struct fc_seq *, struct fc_frame *);
77 static void fc_rport_timeout(struct work_struct *);
78 static void fc_rport_error(struct fc_rport_priv *, struct fc_frame *);
79 static void fc_rport_error_retry(struct fc_rport_priv *, struct fc_frame *);
80 static void fc_rport_work(struct work_struct *);
81
82 static const char *fc_rport_state_names[] = {
83         [RPORT_ST_INIT] = "Init",
84         [RPORT_ST_PLOGI] = "PLOGI",
85         [RPORT_ST_PRLI] = "PRLI",
86         [RPORT_ST_RTV] = "RTV",
87         [RPORT_ST_READY] = "Ready",
88         [RPORT_ST_LOGO] = "LOGO",
89         [RPORT_ST_ADISC] = "ADISC",
90         [RPORT_ST_DELETE] = "Delete",
91         [RPORT_ST_RESTART] = "Restart",
92 };
93
94 /**
95  * fc_rport_lookup() - Lookup a remote port by port_id
96  * @lport:   The local port to lookup the remote port on
97  * @port_id: The remote port ID to look up
98  */
99 static struct fc_rport_priv *fc_rport_lookup(const struct fc_lport *lport,
100                                              u32 port_id)
101 {
102         struct fc_rport_priv *rdata;
103
104         list_for_each_entry(rdata, &lport->disc.rports, peers)
105                 if (rdata->ids.port_id == port_id)
106                         return rdata;
107         return NULL;
108 }
109
110 /**
111  * fc_rport_create() - Create a new remote port
112  * @lport: The local port this remote port will be associated with
113  * @ids:   The identifiers for the new remote port
114  *
115  * The remote port will start in the INIT state.
116  *
117  * Locking note:  must be called with the disc_mutex held.
118  */
119 static struct fc_rport_priv *fc_rport_create(struct fc_lport *lport,
120                                              u32 port_id)
121 {
122         struct fc_rport_priv *rdata;
123
124         rdata = lport->tt.rport_lookup(lport, port_id);
125         if (rdata)
126                 return rdata;
127
128         rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
129         if (!rdata)
130                 return NULL;
131
132         rdata->ids.node_name = -1;
133         rdata->ids.port_name = -1;
134         rdata->ids.port_id = port_id;
135         rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
136
137         kref_init(&rdata->kref);
138         mutex_init(&rdata->rp_mutex);
139         rdata->local_port = lport;
140         rdata->rp_state = RPORT_ST_INIT;
141         rdata->event = RPORT_EV_NONE;
142         rdata->flags = FC_RP_FLAGS_REC_SUPPORTED;
143         rdata->e_d_tov = lport->e_d_tov;
144         rdata->r_a_tov = lport->r_a_tov;
145         rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
146         INIT_DELAYED_WORK(&rdata->retry_work, fc_rport_timeout);
147         INIT_WORK(&rdata->event_work, fc_rport_work);
148         if (port_id != FC_FID_DIR_SERV)
149                 list_add(&rdata->peers, &lport->disc.rports);
150         return rdata;
151 }
152
153 /**
154  * fc_rport_destroy() - Free a remote port after last reference is released
155  * @kref: The remote port's kref
156  */
157 static void fc_rport_destroy(struct kref *kref)
158 {
159         struct fc_rport_priv *rdata;
160
161         rdata = container_of(kref, struct fc_rport_priv, kref);
162         kfree(rdata);
163 }
164
165 /**
166  * fc_rport_state() - Return a string identifying the remote port's state
167  * @rdata: The remote port
168  */
169 static const char *fc_rport_state(struct fc_rport_priv *rdata)
170 {
171         const char *cp;
172
173         cp = fc_rport_state_names[rdata->rp_state];
174         if (!cp)
175                 cp = "Unknown";
176         return cp;
177 }
178
179 /**
180  * fc_set_rport_loss_tmo() - Set the remote port loss timeout
181  * @rport:   The remote port that gets a new timeout value
182  * @timeout: The new timeout value (in seconds)
183  */
184 void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout)
185 {
186         if (timeout)
187                 rport->dev_loss_tmo = timeout + 5;
188         else
189                 rport->dev_loss_tmo = 30;
190 }
191 EXPORT_SYMBOL(fc_set_rport_loss_tmo);
192
193 /**
194  * fc_plogi_get_maxframe() - Get the maximum payload from the common service
195  *                           parameters in a FLOGI frame
196  * @flp:    The FLOGI payload
197  * @maxval: The maximum frame size upper limit; this may be less than what
198  *          is in the service parameters
199  */
200 static unsigned int fc_plogi_get_maxframe(struct fc_els_flogi *flp,
201                                           unsigned int maxval)
202 {
203         unsigned int mfs;
204
205         /*
206          * Get max payload from the common service parameters and the
207          * class 3 receive data field size.
208          */
209         mfs = ntohs(flp->fl_csp.sp_bb_data) & FC_SP_BB_DATA_MASK;
210         if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
211                 maxval = mfs;
212         mfs = ntohs(flp->fl_cssp[3 - 1].cp_rdfs);
213         if (mfs >= FC_SP_MIN_MAX_PAYLOAD && mfs < maxval)
214                 maxval = mfs;
215         return maxval;
216 }
217
218 /**
219  * fc_rport_state_enter() - Change the state of a remote port
220  * @rdata: The remote port whose state should change
221  * @new:   The new state
222  *
223  * Locking Note: Called with the rport lock held
224  */
225 static void fc_rport_state_enter(struct fc_rport_priv *rdata,
226                                  enum fc_rport_state new)
227 {
228         if (rdata->rp_state != new)
229                 rdata->retries = 0;
230         rdata->rp_state = new;
231 }
232
233 /**
234  * fc_rport_work() - Handler for remote port events in the rport_event_queue
235  * @work: Handle to the remote port being dequeued
236  */
237 static void fc_rport_work(struct work_struct *work)
238 {
239         u32 port_id;
240         struct fc_rport_priv *rdata =
241                 container_of(work, struct fc_rport_priv, event_work);
242         struct fc_rport_libfc_priv *rpriv;
243         enum fc_rport_event event;
244         struct fc_lport *lport = rdata->local_port;
245         struct fc_rport_operations *rport_ops;
246         struct fc_rport_identifiers ids;
247         struct fc_rport *rport;
248         int restart = 0;
249
250         mutex_lock(&rdata->rp_mutex);
251         event = rdata->event;
252         rport_ops = rdata->ops;
253         rport = rdata->rport;
254
255         FC_RPORT_DBG(rdata, "work event %u\n", event);
256
257         switch (event) {
258         case RPORT_EV_READY:
259                 ids = rdata->ids;
260                 rdata->event = RPORT_EV_NONE;
261                 kref_get(&rdata->kref);
262                 mutex_unlock(&rdata->rp_mutex);
263
264                 if (!rport)
265                         rport = fc_remote_port_add(lport->host, 0, &ids);
266                 if (!rport) {
267                         FC_RPORT_DBG(rdata, "Failed to add the rport\n");
268                         lport->tt.rport_logoff(rdata);
269                         kref_put(&rdata->kref, lport->tt.rport_destroy);
270                         return;
271                 }
272                 mutex_lock(&rdata->rp_mutex);
273                 if (rdata->rport)
274                         FC_RPORT_DBG(rdata, "rport already allocated\n");
275                 rdata->rport = rport;
276                 rport->maxframe_size = rdata->maxframe_size;
277                 rport->supported_classes = rdata->supported_classes;
278
279                 rpriv = rport->dd_data;
280                 rpriv->local_port = lport;
281                 rpriv->rp_state = rdata->rp_state;
282                 rpriv->flags = rdata->flags;
283                 rpriv->e_d_tov = rdata->e_d_tov;
284                 rpriv->r_a_tov = rdata->r_a_tov;
285                 mutex_unlock(&rdata->rp_mutex);
286
287                 if (rport_ops && rport_ops->event_callback) {
288                         FC_RPORT_DBG(rdata, "callback ev %d\n", event);
289                         rport_ops->event_callback(lport, rdata, event);
290                 }
291                 kref_put(&rdata->kref, lport->tt.rport_destroy);
292                 break;
293
294         case RPORT_EV_FAILED:
295         case RPORT_EV_LOGO:
296         case RPORT_EV_STOP:
297                 port_id = rdata->ids.port_id;
298                 mutex_unlock(&rdata->rp_mutex);
299
300                 if (port_id != FC_FID_DIR_SERV) {
301                         /*
302                          * We must drop rp_mutex before taking disc_mutex.
303                          * Re-evaluate state to allow for restart.
304                          * A transition to RESTART state must only happen
305                          * while disc_mutex is held and rdata is on the list.
306                          */
307                         mutex_lock(&lport->disc.disc_mutex);
308                         mutex_lock(&rdata->rp_mutex);
309                         if (rdata->rp_state == RPORT_ST_RESTART)
310                                 restart = 1;
311                         else
312                                 list_del(&rdata->peers);
313                         rdata->event = RPORT_EV_NONE;
314                         mutex_unlock(&rdata->rp_mutex);
315                         mutex_unlock(&lport->disc.disc_mutex);
316                 }
317
318                 if (rport_ops && rport_ops->event_callback) {
319                         FC_RPORT_DBG(rdata, "callback ev %d\n", event);
320                         rport_ops->event_callback(lport, rdata, event);
321                 }
322                 cancel_delayed_work_sync(&rdata->retry_work);
323
324                 /*
325                  * Reset any outstanding exchanges before freeing rport.
326                  */
327                 lport->tt.exch_mgr_reset(lport, 0, port_id);
328                 lport->tt.exch_mgr_reset(lport, port_id, 0);
329
330                 if (rport) {
331                         rpriv = rport->dd_data;
332                         rpriv->rp_state = RPORT_ST_DELETE;
333                         mutex_lock(&rdata->rp_mutex);
334                         rdata->rport = NULL;
335                         mutex_unlock(&rdata->rp_mutex);
336                         fc_remote_port_delete(rport);
337                 }
338                 if (restart) {
339                         mutex_lock(&rdata->rp_mutex);
340                         FC_RPORT_DBG(rdata, "work restart\n");
341                         fc_rport_enter_plogi(rdata);
342                         mutex_unlock(&rdata->rp_mutex);
343                 } else
344                         kref_put(&rdata->kref, lport->tt.rport_destroy);
345                 break;
346
347         default:
348                 mutex_unlock(&rdata->rp_mutex);
349                 break;
350         }
351 }
352
353 /**
354  * fc_rport_login() - Start the remote port login state machine
355  * @rdata: The remote port to be logged in to
356  *
357  * Locking Note: Called without the rport lock held. This
358  * function will hold the rport lock, call an _enter_*
359  * function and then unlock the rport.
360  *
361  * This indicates the intent to be logged into the remote port.
362  * If it appears we are already logged in, ADISC is used to verify
363  * the setup.
364  */
365 int fc_rport_login(struct fc_rport_priv *rdata)
366 {
367         mutex_lock(&rdata->rp_mutex);
368
369         switch (rdata->rp_state) {
370         case RPORT_ST_READY:
371                 FC_RPORT_DBG(rdata, "ADISC port\n");
372                 fc_rport_enter_adisc(rdata);
373                 break;
374         case RPORT_ST_RESTART:
375                 break;
376         case RPORT_ST_DELETE:
377                 FC_RPORT_DBG(rdata, "Restart deleted port\n");
378                 fc_rport_state_enter(rdata, RPORT_ST_RESTART);
379                 break;
380         default:
381                 FC_RPORT_DBG(rdata, "Login to port\n");
382                 fc_rport_enter_plogi(rdata);
383                 break;
384         }
385         mutex_unlock(&rdata->rp_mutex);
386
387         return 0;
388 }
389
390 /**
391  * fc_rport_enter_delete() - Schedule a remote port to be deleted
392  * @rdata: The remote port to be deleted
393  * @event: The event to report as the reason for deletion
394  *
395  * Locking Note: Called with the rport lock held.
396  *
397  * Allow state change into DELETE only once.
398  *
399  * Call queue_work only if there's no event already pending.
400  * Set the new event so that the old pending event will not occur.
401  * Since we have the mutex, even if fc_rport_work() is already started,
402  * it'll see the new event.
403  */
404 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
405                                   enum fc_rport_event event)
406 {
407         if (rdata->rp_state == RPORT_ST_DELETE)
408                 return;
409
410         FC_RPORT_DBG(rdata, "Delete port\n");
411
412         fc_rport_state_enter(rdata, RPORT_ST_DELETE);
413
414         if (rdata->event == RPORT_EV_NONE)
415                 queue_work(rport_event_queue, &rdata->event_work);
416         rdata->event = event;
417 }
418
419 /**
420  * fc_rport_logoff() - Logoff and remove a remote port
421  * @rdata: The remote port to be logged off of
422  *
423  * Locking Note: Called without the rport lock held. This
424  * function will hold the rport lock, call an _enter_*
425  * function and then unlock the rport.
426  */
427 int fc_rport_logoff(struct fc_rport_priv *rdata)
428 {
429         mutex_lock(&rdata->rp_mutex);
430
431         FC_RPORT_DBG(rdata, "Remove port\n");
432
433         if (rdata->rp_state == RPORT_ST_DELETE) {
434                 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
435                 goto out;
436         }
437
438         if (rdata->rp_state == RPORT_ST_RESTART)
439                 FC_RPORT_DBG(rdata, "Port in Restart state, deleting\n");
440         else
441                 fc_rport_enter_logo(rdata);
442
443         /*
444          * Change the state to Delete so that we discard
445          * the response.
446          */
447         fc_rport_enter_delete(rdata, RPORT_EV_STOP);
448 out:
449         mutex_unlock(&rdata->rp_mutex);
450         return 0;
451 }
452
453 /**
454  * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
455  * @rdata: The remote port that is ready
456  *
457  * Locking Note: The rport lock is expected to be held before calling
458  * this routine.
459  */
460 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
461 {
462         fc_rport_state_enter(rdata, RPORT_ST_READY);
463
464         FC_RPORT_DBG(rdata, "Port is Ready\n");
465
466         if (rdata->event == RPORT_EV_NONE)
467                 queue_work(rport_event_queue, &rdata->event_work);
468         rdata->event = RPORT_EV_READY;
469 }
470
471 /**
472  * fc_rport_timeout() - Handler for the retry_work timer
473  * @work: Handle to the remote port that has timed out
474  *
475  * Locking Note: Called without the rport lock held. This
476  * function will hold the rport lock, call an _enter_*
477  * function and then unlock the rport.
478  */
479 static void fc_rport_timeout(struct work_struct *work)
480 {
481         struct fc_rport_priv *rdata =
482                 container_of(work, struct fc_rport_priv, retry_work.work);
483
484         mutex_lock(&rdata->rp_mutex);
485
486         switch (rdata->rp_state) {
487         case RPORT_ST_PLOGI:
488                 fc_rport_enter_plogi(rdata);
489                 break;
490         case RPORT_ST_PRLI:
491                 fc_rport_enter_prli(rdata);
492                 break;
493         case RPORT_ST_RTV:
494                 fc_rport_enter_rtv(rdata);
495                 break;
496         case RPORT_ST_LOGO:
497                 fc_rport_enter_logo(rdata);
498                 break;
499         case RPORT_ST_ADISC:
500                 fc_rport_enter_adisc(rdata);
501                 break;
502         case RPORT_ST_READY:
503         case RPORT_ST_INIT:
504         case RPORT_ST_DELETE:
505         case RPORT_ST_RESTART:
506                 break;
507         }
508
509         mutex_unlock(&rdata->rp_mutex);
510 }
511
512 /**
513  * fc_rport_error() - Error handler, called once retries have been exhausted
514  * @rdata: The remote port the error is happened on
515  * @fp:    The error code encapsulated in a frame pointer
516  *
517  * Locking Note: The rport lock is expected to be held before
518  * calling this routine
519  */
520 static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
521 {
522         FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
523                      IS_ERR(fp) ? -PTR_ERR(fp) : 0,
524                      fc_rport_state(rdata), rdata->retries);
525
526         switch (rdata->rp_state) {
527         case RPORT_ST_PLOGI:
528         case RPORT_ST_LOGO:
529                 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
530                 break;
531         case RPORT_ST_RTV:
532                 fc_rport_enter_ready(rdata);
533                 break;
534         case RPORT_ST_PRLI:
535         case RPORT_ST_ADISC:
536                 fc_rport_enter_logo(rdata);
537                 break;
538         case RPORT_ST_DELETE:
539         case RPORT_ST_RESTART:
540         case RPORT_ST_READY:
541         case RPORT_ST_INIT:
542                 break;
543         }
544 }
545
546 /**
547  * fc_rport_error_retry() - Handler for remote port state retries
548  * @rdata: The remote port whose state is to be retried
549  * @fp:    The error code encapsulated in a frame pointer
550  *
551  * If the error was an exchange timeout retry immediately,
552  * otherwise wait for E_D_TOV.
553  *
554  * Locking Note: The rport lock is expected to be held before
555  * calling this routine
556  */
557 static void fc_rport_error_retry(struct fc_rport_priv *rdata,
558                                  struct fc_frame *fp)
559 {
560         unsigned long delay = FC_DEF_E_D_TOV;
561
562         /* make sure this isn't an FC_EX_CLOSED error, never retry those */
563         if (PTR_ERR(fp) == -FC_EX_CLOSED)
564                 return fc_rport_error(rdata, fp);
565
566         if (rdata->retries < rdata->local_port->max_rport_retry_count) {
567                 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
568                              PTR_ERR(fp), fc_rport_state(rdata));
569                 rdata->retries++;
570                 /* no additional delay on exchange timeouts */
571                 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
572                         delay = 0;
573                 schedule_delayed_work(&rdata->retry_work, delay);
574                 return;
575         }
576
577         return fc_rport_error(rdata, fp);
578 }
579
580 /**
581  * fc_rport_plogi_recv_resp() - Handler for ELS PLOGI responses
582  * @sp:        The sequence the PLOGI is on
583  * @fp:        The PLOGI response frame
584  * @rdata_arg: The remote port that sent the PLOGI response
585  *
586  * Locking Note: This function will be called without the rport lock
587  * held, but it will lock, call an _enter_* function or fc_rport_error
588  * and then unlock the rport.
589  */
590 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
591                                 void *rdata_arg)
592 {
593         struct fc_rport_priv *rdata = rdata_arg;
594         struct fc_lport *lport = rdata->local_port;
595         struct fc_els_flogi *plp = NULL;
596         unsigned int tov;
597         u16 csp_seq;
598         u16 cssp_seq;
599         u8 op;
600
601         mutex_lock(&rdata->rp_mutex);
602
603         FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
604
605         if (rdata->rp_state != RPORT_ST_PLOGI) {
606                 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
607                              "%s\n", fc_rport_state(rdata));
608                 if (IS_ERR(fp))
609                         goto err;
610                 goto out;
611         }
612
613         if (IS_ERR(fp)) {
614                 fc_rport_error_retry(rdata, fp);
615                 goto err;
616         }
617
618         op = fc_frame_payload_op(fp);
619         if (op == ELS_LS_ACC &&
620             (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
621                 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
622                 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
623
624                 tov = ntohl(plp->fl_csp.sp_e_d_tov);
625                 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
626                         tov /= 1000;
627                 if (tov > rdata->e_d_tov)
628                         rdata->e_d_tov = tov;
629                 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
630                 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
631                 if (cssp_seq < csp_seq)
632                         csp_seq = cssp_seq;
633                 rdata->max_seq = csp_seq;
634                 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
635                 fc_rport_enter_prli(rdata);
636         } else
637                 fc_rport_error_retry(rdata, fp);
638
639 out:
640         fc_frame_free(fp);
641 err:
642         mutex_unlock(&rdata->rp_mutex);
643         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
644 }
645
646 /**
647  * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
648  * @rdata: The remote port to send a PLOGI to
649  *
650  * Locking Note: The rport lock is expected to be held before calling
651  * this routine.
652  */
653 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
654 {
655         struct fc_lport *lport = rdata->local_port;
656         struct fc_frame *fp;
657
658         FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
659                      fc_rport_state(rdata));
660
661         fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
662
663         rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
664         fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
665         if (!fp) {
666                 fc_rport_error_retry(rdata, fp);
667                 return;
668         }
669         rdata->e_d_tov = lport->e_d_tov;
670
671         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
672                                   fc_rport_plogi_resp, rdata,
673                                   2 * lport->r_a_tov))
674                 fc_rport_error_retry(rdata, NULL);
675         else
676                 kref_get(&rdata->kref);
677 }
678
679 /**
680  * fc_rport_prli_resp() - Process Login (PRLI) response handler
681  * @sp:        The sequence the PRLI response was on
682  * @fp:        The PRLI response frame
683  * @rdata_arg: The remote port that sent the PRLI response
684  *
685  * Locking Note: This function will be called without the rport lock
686  * held, but it will lock, call an _enter_* function or fc_rport_error
687  * and then unlock the rport.
688  */
689 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
690                                void *rdata_arg)
691 {
692         struct fc_rport_priv *rdata = rdata_arg;
693         struct {
694                 struct fc_els_prli prli;
695                 struct fc_els_spp spp;
696         } *pp;
697         u32 roles = FC_RPORT_ROLE_UNKNOWN;
698         u32 fcp_parm = 0;
699         u8 op;
700
701         mutex_lock(&rdata->rp_mutex);
702
703         FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
704
705         if (rdata->rp_state != RPORT_ST_PRLI) {
706                 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
707                              "%s\n", fc_rport_state(rdata));
708                 if (IS_ERR(fp))
709                         goto err;
710                 goto out;
711         }
712
713         if (IS_ERR(fp)) {
714                 fc_rport_error_retry(rdata, fp);
715                 goto err;
716         }
717
718         /* reinitialize remote port roles */
719         rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
720
721         op = fc_frame_payload_op(fp);
722         if (op == ELS_LS_ACC) {
723                 pp = fc_frame_payload_get(fp, sizeof(*pp));
724                 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
725                         fcp_parm = ntohl(pp->spp.spp_params);
726                         if (fcp_parm & FCP_SPPF_RETRY)
727                                 rdata->flags |= FC_RP_FLAGS_RETRY;
728                 }
729
730                 rdata->supported_classes = FC_COS_CLASS3;
731                 if (fcp_parm & FCP_SPPF_INIT_FCN)
732                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
733                 if (fcp_parm & FCP_SPPF_TARG_FCN)
734                         roles |= FC_RPORT_ROLE_FCP_TARGET;
735
736                 rdata->ids.roles = roles;
737                 fc_rport_enter_rtv(rdata);
738
739         } else {
740                 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
741                 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
742         }
743
744 out:
745         fc_frame_free(fp);
746 err:
747         mutex_unlock(&rdata->rp_mutex);
748         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
749 }
750
751 /**
752  * fc_rport_logo_resp() - Handler for logout (LOGO) responses
753  * @sp:        The sequence the LOGO was on
754  * @fp:        The LOGO response frame
755  * @rdata_arg: The remote port that sent the LOGO response
756  *
757  * Locking Note: This function will be called without the rport lock
758  * held, but it will lock, call an _enter_* function or fc_rport_error
759  * and then unlock the rport.
760  */
761 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
762                                void *rdata_arg)
763 {
764         struct fc_rport_priv *rdata = rdata_arg;
765         u8 op;
766
767         mutex_lock(&rdata->rp_mutex);
768
769         FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
770
771         if (rdata->rp_state != RPORT_ST_LOGO) {
772                 FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
773                              "%s\n", fc_rport_state(rdata));
774                 if (IS_ERR(fp))
775                         goto err;
776                 goto out;
777         }
778
779         if (IS_ERR(fp)) {
780                 fc_rport_error_retry(rdata, fp);
781                 goto err;
782         }
783
784         op = fc_frame_payload_op(fp);
785         if (op != ELS_LS_ACC)
786                 FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
787                              op);
788         fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
789
790 out:
791         fc_frame_free(fp);
792 err:
793         mutex_unlock(&rdata->rp_mutex);
794         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
795 }
796
797 /**
798  * fc_rport_enter_prli() - Send Process Login (PRLI) request
799  * @rdata: The remote port to send the PRLI request to
800  *
801  * Locking Note: The rport lock is expected to be held before calling
802  * this routine.
803  */
804 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
805 {
806         struct fc_lport *lport = rdata->local_port;
807         struct {
808                 struct fc_els_prli prli;
809                 struct fc_els_spp spp;
810         } *pp;
811         struct fc_frame *fp;
812
813         /*
814          * If the rport is one of the well known addresses
815          * we skip PRLI and RTV and go straight to READY.
816          */
817         if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
818                 fc_rport_enter_ready(rdata);
819                 return;
820         }
821
822         FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
823                      fc_rport_state(rdata));
824
825         fc_rport_state_enter(rdata, RPORT_ST_PRLI);
826
827         fp = fc_frame_alloc(lport, sizeof(*pp));
828         if (!fp) {
829                 fc_rport_error_retry(rdata, fp);
830                 return;
831         }
832
833         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
834                                   fc_rport_prli_resp, rdata,
835                                   2 * lport->r_a_tov))
836                 fc_rport_error_retry(rdata, NULL);
837         else
838                 kref_get(&rdata->kref);
839 }
840
841 /**
842  * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
843  * @sp:        The sequence the RTV was on
844  * @fp:        The RTV response frame
845  * @rdata_arg: The remote port that sent the RTV response
846  *
847  * Many targets don't seem to support this.
848  *
849  * Locking Note: This function will be called without the rport lock
850  * held, but it will lock, call an _enter_* function or fc_rport_error
851  * and then unlock the rport.
852  */
853 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
854                               void *rdata_arg)
855 {
856         struct fc_rport_priv *rdata = rdata_arg;
857         u8 op;
858
859         mutex_lock(&rdata->rp_mutex);
860
861         FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
862
863         if (rdata->rp_state != RPORT_ST_RTV) {
864                 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
865                              "%s\n", fc_rport_state(rdata));
866                 if (IS_ERR(fp))
867                         goto err;
868                 goto out;
869         }
870
871         if (IS_ERR(fp)) {
872                 fc_rport_error(rdata, fp);
873                 goto err;
874         }
875
876         op = fc_frame_payload_op(fp);
877         if (op == ELS_LS_ACC) {
878                 struct fc_els_rtv_acc *rtv;
879                 u32 toq;
880                 u32 tov;
881
882                 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
883                 if (rtv) {
884                         toq = ntohl(rtv->rtv_toq);
885                         tov = ntohl(rtv->rtv_r_a_tov);
886                         if (tov == 0)
887                                 tov = 1;
888                         rdata->r_a_tov = tov;
889                         tov = ntohl(rtv->rtv_e_d_tov);
890                         if (toq & FC_ELS_RTV_EDRES)
891                                 tov /= 1000000;
892                         if (tov == 0)
893                                 tov = 1;
894                         rdata->e_d_tov = tov;
895                 }
896         }
897
898         fc_rport_enter_ready(rdata);
899
900 out:
901         fc_frame_free(fp);
902 err:
903         mutex_unlock(&rdata->rp_mutex);
904         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
905 }
906
907 /**
908  * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
909  * @rdata: The remote port to send the RTV request to
910  *
911  * Locking Note: The rport lock is expected to be held before calling
912  * this routine.
913  */
914 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
915 {
916         struct fc_frame *fp;
917         struct fc_lport *lport = rdata->local_port;
918
919         FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
920                      fc_rport_state(rdata));
921
922         fc_rport_state_enter(rdata, RPORT_ST_RTV);
923
924         fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
925         if (!fp) {
926                 fc_rport_error_retry(rdata, fp);
927                 return;
928         }
929
930         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
931                                   fc_rport_rtv_resp, rdata,
932                                   2 * lport->r_a_tov))
933                 fc_rport_error_retry(rdata, NULL);
934         else
935                 kref_get(&rdata->kref);
936 }
937
938 /**
939  * fc_rport_enter_logo() - Send a logout (LOGO) request
940  * @rdata: The remote port to send the LOGO request to
941  *
942  * Locking Note: The rport lock is expected to be held before calling
943  * this routine.
944  */
945 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
946 {
947         struct fc_lport *lport = rdata->local_port;
948         struct fc_frame *fp;
949
950         FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
951                      fc_rport_state(rdata));
952
953         fc_rport_state_enter(rdata, RPORT_ST_LOGO);
954
955         fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
956         if (!fp) {
957                 fc_rport_error_retry(rdata, fp);
958                 return;
959         }
960
961         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
962                                   fc_rport_logo_resp, rdata,
963                                   2 * lport->r_a_tov))
964                 fc_rport_error_retry(rdata, NULL);
965         else
966                 kref_get(&rdata->kref);
967 }
968
969 /**
970  * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
971  * @sp:        The sequence the ADISC response was on
972  * @fp:        The ADISC response frame
973  * @rdata_arg: The remote port that sent the ADISC response
974  *
975  * Locking Note: This function will be called without the rport lock
976  * held, but it will lock, call an _enter_* function or fc_rport_error
977  * and then unlock the rport.
978  */
979 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
980                                 void *rdata_arg)
981 {
982         struct fc_rport_priv *rdata = rdata_arg;
983         struct fc_els_adisc *adisc;
984         u8 op;
985
986         mutex_lock(&rdata->rp_mutex);
987
988         FC_RPORT_DBG(rdata, "Received a ADISC response\n");
989
990         if (rdata->rp_state != RPORT_ST_ADISC) {
991                 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
992                              fc_rport_state(rdata));
993                 if (IS_ERR(fp))
994                         goto err;
995                 goto out;
996         }
997
998         if (IS_ERR(fp)) {
999                 fc_rport_error(rdata, fp);
1000                 goto err;
1001         }
1002
1003         /*
1004          * If address verification failed.  Consider us logged out of the rport.
1005          * Since the rport is still in discovery, we want to be
1006          * logged in, so go to PLOGI state.  Otherwise, go back to READY.
1007          */
1008         op = fc_frame_payload_op(fp);
1009         adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1010         if (op != ELS_LS_ACC || !adisc ||
1011             ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1012             get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1013             get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1014                 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1015                 fc_rport_enter_plogi(rdata);
1016         } else {
1017                 FC_RPORT_DBG(rdata, "ADISC OK\n");
1018                 fc_rport_enter_ready(rdata);
1019         }
1020 out:
1021         fc_frame_free(fp);
1022 err:
1023         mutex_unlock(&rdata->rp_mutex);
1024         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1025 }
1026
1027 /**
1028  * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1029  * @rdata: The remote port to send the ADISC request to
1030  *
1031  * Locking Note: The rport lock is expected to be held before calling
1032  * this routine.
1033  */
1034 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1035 {
1036         struct fc_lport *lport = rdata->local_port;
1037         struct fc_frame *fp;
1038
1039         FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1040                      fc_rport_state(rdata));
1041
1042         fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1043
1044         fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1045         if (!fp) {
1046                 fc_rport_error_retry(rdata, fp);
1047                 return;
1048         }
1049         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1050                                   fc_rport_adisc_resp, rdata,
1051                                   2 * lport->r_a_tov))
1052                 fc_rport_error_retry(rdata, NULL);
1053         else
1054                 kref_get(&rdata->kref);
1055 }
1056
1057 /**
1058  * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1059  * @rdata: The remote port that sent the ADISC request
1060  * @sp:    The sequence the ADISC request was on
1061  * @in_fp: The ADISC request frame
1062  *
1063  * Locking Note:  Called with the lport and rport locks held.
1064  */
1065 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1066                                     struct fc_seq *sp, struct fc_frame *in_fp)
1067 {
1068         struct fc_lport *lport = rdata->local_port;
1069         struct fc_frame *fp;
1070         struct fc_exch *ep = fc_seq_exch(sp);
1071         struct fc_els_adisc *adisc;
1072         struct fc_seq_els_data rjt_data;
1073         u32 f_ctl;
1074
1075         FC_RPORT_DBG(rdata, "Received ADISC request\n");
1076
1077         adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1078         if (!adisc) {
1079                 rjt_data.fp = NULL;
1080                 rjt_data.reason = ELS_RJT_PROT;
1081                 rjt_data.explan = ELS_EXPL_INV_LEN;
1082                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1083                 goto drop;
1084         }
1085
1086         fp = fc_frame_alloc(lport, sizeof(*adisc));
1087         if (!fp)
1088                 goto drop;
1089         fc_adisc_fill(lport, fp);
1090         adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1091         adisc->adisc_cmd = ELS_LS_ACC;
1092         sp = lport->tt.seq_start_next(sp);
1093         f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1094         fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1095                        FC_TYPE_ELS, f_ctl, 0);
1096         lport->tt.seq_send(lport, sp, fp);
1097 drop:
1098         fc_frame_free(in_fp);
1099 }
1100
1101 /**
1102  * fc_rport_recv_rls_req() - Handle received Read Link Status request
1103  * @rdata: The remote port that sent the RLS request
1104  * @sp: The sequence that the RLS was on
1105  * @rx_fp: The PRLI request frame
1106  *
1107  * Locking Note: The rport lock is expected to be held before calling
1108  * this function.
1109  */
1110 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1111                                   struct fc_seq *sp, struct fc_frame *rx_fp)
1112
1113 {
1114         struct fc_lport *lport = rdata->local_port;
1115         struct fc_frame *fp;
1116         struct fc_exch *ep = fc_seq_exch(sp);
1117         struct fc_els_rls *rls;
1118         struct fc_els_rls_resp *rsp;
1119         struct fc_els_lesb *lesb;
1120         struct fc_seq_els_data rjt_data;
1121         struct fc_host_statistics *hst;
1122         u32 f_ctl;
1123
1124         FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1125                      fc_rport_state(rdata));
1126
1127         rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1128         if (!rls) {
1129                 rjt_data.reason = ELS_RJT_PROT;
1130                 rjt_data.explan = ELS_EXPL_INV_LEN;
1131                 goto out_rjt;
1132         }
1133
1134         fp = fc_frame_alloc(lport, sizeof(*rsp));
1135         if (!fp) {
1136                 rjt_data.reason = ELS_RJT_UNAB;
1137                 rjt_data.explan = ELS_EXPL_INSUF_RES;
1138                 goto out_rjt;
1139         }
1140
1141         rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1142         memset(rsp, 0, sizeof(*rsp));
1143         rsp->rls_cmd = ELS_LS_ACC;
1144         lesb = &rsp->rls_lesb;
1145         if (lport->tt.get_lesb) {
1146                 /* get LESB from LLD if it supports it */
1147                 lport->tt.get_lesb(lport, lesb);
1148         } else {
1149                 fc_get_host_stats(lport->host);
1150                 hst = &lport->host_stats;
1151                 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1152                 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1153                 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1154                 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1155                 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1156                 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1157         }
1158
1159         sp = lport->tt.seq_start_next(sp);
1160         f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1161         fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1162                        FC_TYPE_ELS, f_ctl, 0);
1163         lport->tt.seq_send(lport, sp, fp);
1164         goto out;
1165
1166 out_rjt:
1167         rjt_data.fp = NULL;
1168         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1169 out:
1170         fc_frame_free(rx_fp);
1171 }
1172
1173 /**
1174  * fc_rport_recv_els_req() - Handler for validated ELS requests
1175  * @lport: The local port that received the ELS request
1176  * @sp:    The sequence that the ELS request was on
1177  * @fp:    The ELS request frame
1178  *
1179  * Handle incoming ELS requests that require port login.
1180  * The ELS opcode has already been validated by the caller.
1181  *
1182  * Locking Note: Called with the lport lock held.
1183  */
1184 static void fc_rport_recv_els_req(struct fc_lport *lport,
1185                                   struct fc_seq *sp, struct fc_frame *fp)
1186 {
1187         struct fc_rport_priv *rdata;
1188         struct fc_frame_header *fh;
1189         struct fc_seq_els_data els_data;
1190
1191         els_data.fp = NULL;
1192         els_data.reason = ELS_RJT_UNAB;
1193         els_data.explan = ELS_EXPL_PLOGI_REQD;
1194
1195         fh = fc_frame_header_get(fp);
1196
1197         mutex_lock(&lport->disc.disc_mutex);
1198         rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
1199         if (!rdata) {
1200                 mutex_unlock(&lport->disc.disc_mutex);
1201                 goto reject;
1202         }
1203         mutex_lock(&rdata->rp_mutex);
1204         mutex_unlock(&lport->disc.disc_mutex);
1205
1206         switch (rdata->rp_state) {
1207         case RPORT_ST_PRLI:
1208         case RPORT_ST_RTV:
1209         case RPORT_ST_READY:
1210         case RPORT_ST_ADISC:
1211                 break;
1212         default:
1213                 mutex_unlock(&rdata->rp_mutex);
1214                 goto reject;
1215         }
1216
1217         switch (fc_frame_payload_op(fp)) {
1218         case ELS_PRLI:
1219                 fc_rport_recv_prli_req(rdata, sp, fp);
1220                 break;
1221         case ELS_PRLO:
1222                 fc_rport_recv_prlo_req(rdata, sp, fp);
1223                 break;
1224         case ELS_ADISC:
1225                 fc_rport_recv_adisc_req(rdata, sp, fp);
1226                 break;
1227         case ELS_RRQ:
1228                 els_data.fp = fp;
1229                 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
1230                 break;
1231         case ELS_REC:
1232                 els_data.fp = fp;
1233                 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
1234                 break;
1235         case ELS_RLS:
1236                 fc_rport_recv_rls_req(rdata, sp, fp);
1237                 break;
1238         default:
1239                 fc_frame_free(fp);      /* can't happen */
1240                 break;
1241         }
1242
1243         mutex_unlock(&rdata->rp_mutex);
1244         return;
1245
1246 reject:
1247         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1248         fc_frame_free(fp);
1249 }
1250
1251 /**
1252  * fc_rport_recv_req() - Handler for requests
1253  * @sp:    The sequence the request was on
1254  * @fp:    The request frame
1255  * @lport: The local port that received the request
1256  *
1257  * Locking Note: Called with the lport lock held.
1258  */
1259 void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1260                        struct fc_lport *lport)
1261 {
1262         struct fc_seq_els_data els_data;
1263
1264         /*
1265          * Handle PLOGI and LOGO requests separately, since they
1266          * don't require prior login.
1267          * Check for unsupported opcodes first and reject them.
1268          * For some ops, it would be incorrect to reject with "PLOGI required".
1269          */
1270         switch (fc_frame_payload_op(fp)) {
1271         case ELS_PLOGI:
1272                 fc_rport_recv_plogi_req(lport, sp, fp);
1273                 break;
1274         case ELS_LOGO:
1275                 fc_rport_recv_logo_req(lport, sp, fp);
1276                 break;
1277         case ELS_PRLI:
1278         case ELS_PRLO:
1279         case ELS_ADISC:
1280         case ELS_RRQ:
1281         case ELS_REC:
1282         case ELS_RLS:
1283                 fc_rport_recv_els_req(lport, sp, fp);
1284                 break;
1285         default:
1286                 fc_frame_free(fp);
1287                 els_data.fp = NULL;
1288                 els_data.reason = ELS_RJT_UNSUP;
1289                 els_data.explan = ELS_EXPL_NONE;
1290                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1291                 break;
1292         }
1293 }
1294
1295 /**
1296  * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1297  * @lport: The local port that received the PLOGI request
1298  * @sp:    The sequence that the PLOGI request was on
1299  * @rx_fp: The PLOGI request frame
1300  *
1301  * Locking Note: The rport lock is held before calling this function.
1302  */
1303 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1304                                     struct fc_seq *sp, struct fc_frame *rx_fp)
1305 {
1306         struct fc_disc *disc;
1307         struct fc_rport_priv *rdata;
1308         struct fc_frame *fp = rx_fp;
1309         struct fc_exch *ep;
1310         struct fc_frame_header *fh;
1311         struct fc_els_flogi *pl;
1312         struct fc_seq_els_data rjt_data;
1313         u32 sid, f_ctl;
1314
1315         rjt_data.fp = NULL;
1316         fh = fc_frame_header_get(fp);
1317         sid = ntoh24(fh->fh_s_id);
1318
1319         FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1320
1321         pl = fc_frame_payload_get(fp, sizeof(*pl));
1322         if (!pl) {
1323                 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1324                 rjt_data.reason = ELS_RJT_PROT;
1325                 rjt_data.explan = ELS_EXPL_INV_LEN;
1326                 goto reject;
1327         }
1328
1329         disc = &lport->disc;
1330         mutex_lock(&disc->disc_mutex);
1331         rdata = lport->tt.rport_create(lport, sid);
1332         if (!rdata) {
1333                 mutex_unlock(&disc->disc_mutex);
1334                 rjt_data.reason = ELS_RJT_UNAB;
1335                 rjt_data.explan = ELS_EXPL_INSUF_RES;
1336                 goto reject;
1337         }
1338
1339         mutex_lock(&rdata->rp_mutex);
1340         mutex_unlock(&disc->disc_mutex);
1341
1342         rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1343         rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1344
1345         /*
1346          * If the rport was just created, possibly due to the incoming PLOGI,
1347          * set the state appropriately and accept the PLOGI.
1348          *
1349          * If we had also sent a PLOGI, and if the received PLOGI is from a
1350          * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1351          * "command already in progress".
1352          *
1353          * XXX TBD: If the session was ready before, the PLOGI should result in
1354          * all outstanding exchanges being reset.
1355          */
1356         switch (rdata->rp_state) {
1357         case RPORT_ST_INIT:
1358                 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1359                 break;
1360         case RPORT_ST_PLOGI:
1361                 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1362                 if (rdata->ids.port_name < lport->wwpn) {
1363                         mutex_unlock(&rdata->rp_mutex);
1364                         rjt_data.reason = ELS_RJT_INPROG;
1365                         rjt_data.explan = ELS_EXPL_NONE;
1366                         goto reject;
1367                 }
1368                 break;
1369         case RPORT_ST_PRLI:
1370         case RPORT_ST_RTV:
1371         case RPORT_ST_READY:
1372         case RPORT_ST_ADISC:
1373                 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1374                              "- ignored for now\n", rdata->rp_state);
1375                 /* XXX TBD - should reset */
1376                 break;
1377         case RPORT_ST_DELETE:
1378         case RPORT_ST_LOGO:
1379         case RPORT_ST_RESTART:
1380                 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1381                              fc_rport_state(rdata));
1382                 mutex_unlock(&rdata->rp_mutex);
1383                 rjt_data.reason = ELS_RJT_BUSY;
1384                 rjt_data.explan = ELS_EXPL_NONE;
1385                 goto reject;
1386         }
1387
1388         /*
1389          * Get session payload size from incoming PLOGI.
1390          */
1391         rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1392         fc_frame_free(rx_fp);
1393
1394         /*
1395          * Send LS_ACC.  If this fails, the originator should retry.
1396          */
1397         sp = lport->tt.seq_start_next(sp);
1398         if (!sp)
1399                 goto out;
1400         fp = fc_frame_alloc(lport, sizeof(*pl));
1401         if (!fp)
1402                 goto out;
1403
1404         fc_plogi_fill(lport, fp, ELS_LS_ACC);
1405         f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1406         ep = fc_seq_exch(sp);
1407         fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1408                        FC_TYPE_ELS, f_ctl, 0);
1409         lport->tt.seq_send(lport, sp, fp);
1410         fc_rport_enter_prli(rdata);
1411 out:
1412         mutex_unlock(&rdata->rp_mutex);
1413         return;
1414
1415 reject:
1416         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1417         fc_frame_free(fp);
1418 }
1419
1420 /**
1421  * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1422  * @rdata: The remote port that sent the PRLI request
1423  * @sp:    The sequence that the PRLI was on
1424  * @rx_fp: The PRLI request frame
1425  *
1426  * Locking Note: The rport lock is exected to be held before calling
1427  * this function.
1428  */
1429 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1430                                    struct fc_seq *sp, struct fc_frame *rx_fp)
1431 {
1432         struct fc_lport *lport = rdata->local_port;
1433         struct fc_exch *ep;
1434         struct fc_frame *fp;
1435         struct fc_frame_header *fh;
1436         struct {
1437                 struct fc_els_prli prli;
1438                 struct fc_els_spp spp;
1439         } *pp;
1440         struct fc_els_spp *rspp;        /* request service param page */
1441         struct fc_els_spp *spp; /* response spp */
1442         unsigned int len;
1443         unsigned int plen;
1444         enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1445         enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1446         enum fc_els_spp_resp resp;
1447         struct fc_seq_els_data rjt_data;
1448         u32 f_ctl;
1449         u32 fcp_parm;
1450         u32 roles = FC_RPORT_ROLE_UNKNOWN;
1451         rjt_data.fp = NULL;
1452
1453         fh = fc_frame_header_get(rx_fp);
1454
1455         FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1456                      fc_rport_state(rdata));
1457
1458         switch (rdata->rp_state) {
1459         case RPORT_ST_PRLI:
1460         case RPORT_ST_RTV:
1461         case RPORT_ST_READY:
1462         case RPORT_ST_ADISC:
1463                 reason = ELS_RJT_NONE;
1464                 break;
1465         default:
1466                 fc_frame_free(rx_fp);
1467                 return;
1468                 break;
1469         }
1470         len = fr_len(rx_fp) - sizeof(*fh);
1471         pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1472         if (pp == NULL) {
1473                 reason = ELS_RJT_PROT;
1474                 explan = ELS_EXPL_INV_LEN;
1475         } else {
1476                 plen = ntohs(pp->prli.prli_len);
1477                 if ((plen % 4) != 0 || plen > len) {
1478                         reason = ELS_RJT_PROT;
1479                         explan = ELS_EXPL_INV_LEN;
1480                 } else if (plen < len) {
1481                         len = plen;
1482                 }
1483                 plen = pp->prli.prli_spp_len;
1484                 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1485                     plen > len || len < sizeof(*pp)) {
1486                         reason = ELS_RJT_PROT;
1487                         explan = ELS_EXPL_INV_LEN;
1488                 }
1489                 rspp = &pp->spp;
1490         }
1491         if (reason != ELS_RJT_NONE ||
1492             (fp = fc_frame_alloc(lport, len)) == NULL) {
1493                 rjt_data.reason = reason;
1494                 rjt_data.explan = explan;
1495                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1496         } else {
1497                 sp = lport->tt.seq_start_next(sp);
1498                 WARN_ON(!sp);
1499                 pp = fc_frame_payload_get(fp, len);
1500                 WARN_ON(!pp);
1501                 memset(pp, 0, len);
1502                 pp->prli.prli_cmd = ELS_LS_ACC;
1503                 pp->prli.prli_spp_len = plen;
1504                 pp->prli.prli_len = htons(len);
1505                 len -= sizeof(struct fc_els_prli);
1506
1507                 /* reinitialize remote port roles */
1508                 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1509
1510                 /*
1511                  * Go through all the service parameter pages and build
1512                  * response.  If plen indicates longer SPP than standard,
1513                  * use that.  The entire response has been pre-cleared above.
1514                  */
1515                 spp = &pp->spp;
1516                 while (len >= plen) {
1517                         spp->spp_type = rspp->spp_type;
1518                         spp->spp_type_ext = rspp->spp_type_ext;
1519                         spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1520                         resp = FC_SPP_RESP_ACK;
1521                         if (rspp->spp_flags & FC_SPP_RPA_VAL)
1522                                 resp = FC_SPP_RESP_NO_PA;
1523                         switch (rspp->spp_type) {
1524                         case 0: /* common to all FC-4 types */
1525                                 break;
1526                         case FC_TYPE_FCP:
1527                                 fcp_parm = ntohl(rspp->spp_params);
1528                                 if (fcp_parm & FCP_SPPF_RETRY)
1529                                         rdata->flags |= FC_RP_FLAGS_RETRY;
1530                                 rdata->supported_classes = FC_COS_CLASS3;
1531                                 if (fcp_parm & FCP_SPPF_INIT_FCN)
1532                                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1533                                 if (fcp_parm & FCP_SPPF_TARG_FCN)
1534                                         roles |= FC_RPORT_ROLE_FCP_TARGET;
1535                                 rdata->ids.roles = roles;
1536
1537                                 spp->spp_params =
1538                                         htonl(lport->service_params);
1539                                 break;
1540                         default:
1541                                 resp = FC_SPP_RESP_INVL;
1542                                 break;
1543                         }
1544                         spp->spp_flags |= resp;
1545                         len -= plen;
1546                         rspp = (struct fc_els_spp *)((char *)rspp + plen);
1547                         spp = (struct fc_els_spp *)((char *)spp + plen);
1548                 }
1549
1550                 /*
1551                  * Send LS_ACC.  If this fails, the originator should retry.
1552                  */
1553                 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1554                 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1555                 ep = fc_seq_exch(sp);
1556                 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1557                                FC_TYPE_ELS, f_ctl, 0);
1558                 lport->tt.seq_send(lport, sp, fp);
1559
1560                 /*
1561                  * Get lock and re-check state.
1562                  */
1563                 switch (rdata->rp_state) {
1564                 case RPORT_ST_PRLI:
1565                         fc_rport_enter_ready(rdata);
1566                         break;
1567                 case RPORT_ST_READY:
1568                 case RPORT_ST_ADISC:
1569                         break;
1570                 default:
1571                         break;
1572                 }
1573         }
1574         fc_frame_free(rx_fp);
1575 }
1576
1577 /**
1578  * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1579  * @rdata: The remote port that sent the PRLO request
1580  * @sp:    The sequence that the PRLO was on
1581  * @fp:    The PRLO request frame
1582  *
1583  * Locking Note: The rport lock is exected to be held before calling
1584  * this function.
1585  */
1586 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1587                                    struct fc_seq *sp,
1588                                    struct fc_frame *fp)
1589 {
1590         struct fc_lport *lport = rdata->local_port;
1591
1592         struct fc_frame_header *fh;
1593         struct fc_seq_els_data rjt_data;
1594
1595         fh = fc_frame_header_get(fp);
1596
1597         FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1598                      fc_rport_state(rdata));
1599
1600         rjt_data.fp = NULL;
1601         rjt_data.reason = ELS_RJT_UNAB;
1602         rjt_data.explan = ELS_EXPL_NONE;
1603         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1604         fc_frame_free(fp);
1605 }
1606
1607 /**
1608  * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1609  * @lport: The local port that received the LOGO request
1610  * @sp:    The sequence that the LOGO request was on
1611  * @fp:    The LOGO request frame
1612  *
1613  * Locking Note: The rport lock is exected to be held before calling
1614  * this function.
1615  */
1616 static void fc_rport_recv_logo_req(struct fc_lport *lport,
1617                                    struct fc_seq *sp,
1618                                    struct fc_frame *fp)
1619 {
1620         struct fc_frame_header *fh;
1621         struct fc_rport_priv *rdata;
1622         u32 sid;
1623
1624         lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1625
1626         fh = fc_frame_header_get(fp);
1627         sid = ntoh24(fh->fh_s_id);
1628
1629         mutex_lock(&lport->disc.disc_mutex);
1630         rdata = lport->tt.rport_lookup(lport, sid);
1631         if (rdata) {
1632                 mutex_lock(&rdata->rp_mutex);
1633                 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1634                              fc_rport_state(rdata));
1635
1636                 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1637
1638                 /*
1639                  * If the remote port was created due to discovery, set state
1640                  * to log back in.  It may have seen a stale RSCN about us.
1641                  */
1642                 if (rdata->disc_id)
1643                         fc_rport_state_enter(rdata, RPORT_ST_RESTART);
1644                 mutex_unlock(&rdata->rp_mutex);
1645         } else
1646                 FC_RPORT_ID_DBG(lport, sid,
1647                                 "Received LOGO from non-logged-in port\n");
1648         mutex_unlock(&lport->disc.disc_mutex);
1649         fc_frame_free(fp);
1650 }
1651
1652 /**
1653  * fc_rport_flush_queue() - Flush the rport_event_queue
1654  */
1655 static void fc_rport_flush_queue(void)
1656 {
1657         flush_workqueue(rport_event_queue);
1658 }
1659
1660 /**
1661  * fc_rport_init() - Initialize the remote port layer for a local port
1662  * @lport: The local port to initialize the remote port layer for
1663  */
1664 int fc_rport_init(struct fc_lport *lport)
1665 {
1666         if (!lport->tt.rport_lookup)
1667                 lport->tt.rport_lookup = fc_rport_lookup;
1668
1669         if (!lport->tt.rport_create)
1670                 lport->tt.rport_create = fc_rport_create;
1671
1672         if (!lport->tt.rport_login)
1673                 lport->tt.rport_login = fc_rport_login;
1674
1675         if (!lport->tt.rport_logoff)
1676                 lport->tt.rport_logoff = fc_rport_logoff;
1677
1678         if (!lport->tt.rport_recv_req)
1679                 lport->tt.rport_recv_req = fc_rport_recv_req;
1680
1681         if (!lport->tt.rport_flush_queue)
1682                 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1683
1684         if (!lport->tt.rport_destroy)
1685                 lport->tt.rport_destroy = fc_rport_destroy;
1686
1687         return 0;
1688 }
1689 EXPORT_SYMBOL(fc_rport_init);
1690
1691 /**
1692  * fc_setup_rport() - Initialize the rport_event_queue
1693  */
1694 int fc_setup_rport()
1695 {
1696         rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1697         if (!rport_event_queue)
1698                 return -ENOMEM;
1699         return 0;
1700 }
1701
1702 /**
1703  * fc_destroy_rport() - Destroy the rport_event_queue
1704  */
1705 void fc_destroy_rport()
1706 {
1707         destroy_workqueue(rport_event_queue);
1708 }
1709
1710 /**
1711  * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1712  * @rport: The remote port whose I/O should be terminated
1713  */
1714 void fc_rport_terminate_io(struct fc_rport *rport)
1715 {
1716         struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1717         struct fc_lport *lport = rpriv->local_port;
1718
1719         lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1720         lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
1721 }
1722 EXPORT_SYMBOL(fc_rport_terminate_io);