]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/scsi/libfc/fc_rport.c
Merge branch 'ixp4xx' of git://git.kernel.org/pub/scm/linux/kernel/git/chris/linux-2.6
[mv-sheeva.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 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                         mutex_unlock(&rdata->rp_mutex);
314                         mutex_unlock(&lport->disc.disc_mutex);
315                 }
316
317                 if (rport_ops && rport_ops->event_callback) {
318                         FC_RPORT_DBG(rdata, "callback ev %d\n", event);
319                         rport_ops->event_callback(lport, rdata, event);
320                 }
321                 cancel_delayed_work_sync(&rdata->retry_work);
322
323                 /*
324                  * Reset any outstanding exchanges before freeing rport.
325                  */
326                 lport->tt.exch_mgr_reset(lport, 0, port_id);
327                 lport->tt.exch_mgr_reset(lport, port_id, 0);
328
329                 if (rport) {
330                         rpriv = rport->dd_data;
331                         rpriv->rp_state = RPORT_ST_DELETE;
332                         mutex_lock(&rdata->rp_mutex);
333                         rdata->rport = NULL;
334                         mutex_unlock(&rdata->rp_mutex);
335                         fc_remote_port_delete(rport);
336                 }
337                 if (restart) {
338                         mutex_lock(&rdata->rp_mutex);
339                         FC_RPORT_DBG(rdata, "work restart\n");
340                         fc_rport_enter_plogi(rdata);
341                         mutex_unlock(&rdata->rp_mutex);
342                 } else
343                         kref_put(&rdata->kref, lport->tt.rport_destroy);
344                 break;
345
346         default:
347                 mutex_unlock(&rdata->rp_mutex);
348                 break;
349         }
350 }
351
352 /**
353  * fc_rport_login() - Start the remote port login state machine
354  * @rdata: The remote port to be logged in to
355  *
356  * Locking Note: Called without the rport lock held. This
357  * function will hold the rport lock, call an _enter_*
358  * function and then unlock the rport.
359  *
360  * This indicates the intent to be logged into the remote port.
361  * If it appears we are already logged in, ADISC is used to verify
362  * the setup.
363  */
364 int fc_rport_login(struct fc_rport_priv *rdata)
365 {
366         mutex_lock(&rdata->rp_mutex);
367
368         switch (rdata->rp_state) {
369         case RPORT_ST_READY:
370                 FC_RPORT_DBG(rdata, "ADISC port\n");
371                 fc_rport_enter_adisc(rdata);
372                 break;
373         case RPORT_ST_RESTART:
374                 break;
375         case RPORT_ST_DELETE:
376                 FC_RPORT_DBG(rdata, "Restart deleted port\n");
377                 fc_rport_state_enter(rdata, RPORT_ST_RESTART);
378                 break;
379         default:
380                 FC_RPORT_DBG(rdata, "Login to port\n");
381                 fc_rport_enter_plogi(rdata);
382                 break;
383         }
384         mutex_unlock(&rdata->rp_mutex);
385
386         return 0;
387 }
388
389 /**
390  * fc_rport_enter_delete() - Schedule a remote port to be deleted
391  * @rdata: The remote port to be deleted
392  * @event: The event to report as the reason for deletion
393  *
394  * Locking Note: Called with the rport lock held.
395  *
396  * Allow state change into DELETE only once.
397  *
398  * Call queue_work only if there's no event already pending.
399  * Set the new event so that the old pending event will not occur.
400  * Since we have the mutex, even if fc_rport_work() is already started,
401  * it'll see the new event.
402  */
403 static void fc_rport_enter_delete(struct fc_rport_priv *rdata,
404                                   enum fc_rport_event event)
405 {
406         if (rdata->rp_state == RPORT_ST_DELETE)
407                 return;
408
409         FC_RPORT_DBG(rdata, "Delete port\n");
410
411         fc_rport_state_enter(rdata, RPORT_ST_DELETE);
412
413         if (rdata->event == RPORT_EV_NONE)
414                 queue_work(rport_event_queue, &rdata->event_work);
415         rdata->event = event;
416 }
417
418 /**
419  * fc_rport_logoff() - Logoff and remove a remote port
420  * @rdata: The remote port to be logged off of
421  *
422  * Locking Note: Called without the rport lock held. This
423  * function will hold the rport lock, call an _enter_*
424  * function and then unlock the rport.
425  */
426 int fc_rport_logoff(struct fc_rport_priv *rdata)
427 {
428         mutex_lock(&rdata->rp_mutex);
429
430         FC_RPORT_DBG(rdata, "Remove port\n");
431
432         if (rdata->rp_state == RPORT_ST_DELETE) {
433                 FC_RPORT_DBG(rdata, "Port in Delete state, not removing\n");
434                 goto out;
435         }
436
437         if (rdata->rp_state == RPORT_ST_RESTART)
438                 FC_RPORT_DBG(rdata, "Port in Restart state, deleting\n");
439         else
440                 fc_rport_enter_logo(rdata);
441
442         /*
443          * Change the state to Delete so that we discard
444          * the response.
445          */
446         fc_rport_enter_delete(rdata, RPORT_EV_STOP);
447 out:
448         mutex_unlock(&rdata->rp_mutex);
449         return 0;
450 }
451
452 /**
453  * fc_rport_enter_ready() - Transition to the RPORT_ST_READY state
454  * @rdata: The remote port that is ready
455  *
456  * Locking Note: The rport lock is expected to be held before calling
457  * this routine.
458  */
459 static void fc_rport_enter_ready(struct fc_rport_priv *rdata)
460 {
461         fc_rport_state_enter(rdata, RPORT_ST_READY);
462
463         FC_RPORT_DBG(rdata, "Port is Ready\n");
464
465         if (rdata->event == RPORT_EV_NONE)
466                 queue_work(rport_event_queue, &rdata->event_work);
467         rdata->event = RPORT_EV_READY;
468 }
469
470 /**
471  * fc_rport_timeout() - Handler for the retry_work timer
472  * @work: Handle to the remote port that has timed out
473  *
474  * Locking Note: Called without the rport lock held. This
475  * function will hold the rport lock, call an _enter_*
476  * function and then unlock the rport.
477  */
478 static void fc_rport_timeout(struct work_struct *work)
479 {
480         struct fc_rport_priv *rdata =
481                 container_of(work, struct fc_rport_priv, retry_work.work);
482
483         mutex_lock(&rdata->rp_mutex);
484
485         switch (rdata->rp_state) {
486         case RPORT_ST_PLOGI:
487                 fc_rport_enter_plogi(rdata);
488                 break;
489         case RPORT_ST_PRLI:
490                 fc_rport_enter_prli(rdata);
491                 break;
492         case RPORT_ST_RTV:
493                 fc_rport_enter_rtv(rdata);
494                 break;
495         case RPORT_ST_LOGO:
496                 fc_rport_enter_logo(rdata);
497                 break;
498         case RPORT_ST_ADISC:
499                 fc_rport_enter_adisc(rdata);
500                 break;
501         case RPORT_ST_READY:
502         case RPORT_ST_INIT:
503         case RPORT_ST_DELETE:
504         case RPORT_ST_RESTART:
505                 break;
506         }
507
508         mutex_unlock(&rdata->rp_mutex);
509 }
510
511 /**
512  * fc_rport_error() - Error handler, called once retries have been exhausted
513  * @rdata: The remote port the error is happened on
514  * @fp:    The error code encapsulated in a frame pointer
515  *
516  * Locking Note: The rport lock is expected to be held before
517  * calling this routine
518  */
519 static void fc_rport_error(struct fc_rport_priv *rdata, struct fc_frame *fp)
520 {
521         FC_RPORT_DBG(rdata, "Error %ld in state %s, retries %d\n",
522                      IS_ERR(fp) ? -PTR_ERR(fp) : 0,
523                      fc_rport_state(rdata), rdata->retries);
524
525         switch (rdata->rp_state) {
526         case RPORT_ST_PLOGI:
527         case RPORT_ST_LOGO:
528                 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
529                 break;
530         case RPORT_ST_RTV:
531                 fc_rport_enter_ready(rdata);
532                 break;
533         case RPORT_ST_PRLI:
534         case RPORT_ST_ADISC:
535                 fc_rport_enter_logo(rdata);
536                 break;
537         case RPORT_ST_DELETE:
538         case RPORT_ST_RESTART:
539         case RPORT_ST_READY:
540         case RPORT_ST_INIT:
541                 break;
542         }
543 }
544
545 /**
546  * fc_rport_error_retry() - Handler for remote port state retries
547  * @rdata: The remote port whose state is to be retried
548  * @fp:    The error code encapsulated in a frame pointer
549  *
550  * If the error was an exchange timeout retry immediately,
551  * otherwise wait for E_D_TOV.
552  *
553  * Locking Note: The rport lock is expected to be held before
554  * calling this routine
555  */
556 static void fc_rport_error_retry(struct fc_rport_priv *rdata,
557                                  struct fc_frame *fp)
558 {
559         unsigned long delay = FC_DEF_E_D_TOV;
560
561         /* make sure this isn't an FC_EX_CLOSED error, never retry those */
562         if (PTR_ERR(fp) == -FC_EX_CLOSED)
563                 return fc_rport_error(rdata, fp);
564
565         if (rdata->retries < rdata->local_port->max_rport_retry_count) {
566                 FC_RPORT_DBG(rdata, "Error %ld in state %s, retrying\n",
567                              PTR_ERR(fp), fc_rport_state(rdata));
568                 rdata->retries++;
569                 /* no additional delay on exchange timeouts */
570                 if (PTR_ERR(fp) == -FC_EX_TIMEOUT)
571                         delay = 0;
572                 schedule_delayed_work(&rdata->retry_work, delay);
573                 return;
574         }
575
576         return fc_rport_error(rdata, fp);
577 }
578
579 /**
580  * fc_rport_plogi_recv_resp() - Handler for ELS PLOGI responses
581  * @sp:        The sequence the PLOGI is on
582  * @fp:        The PLOGI response frame
583  * @rdata_arg: The remote port that sent the PLOGI response
584  *
585  * Locking Note: This function will be called without the rport lock
586  * held, but it will lock, call an _enter_* function or fc_rport_error
587  * and then unlock the rport.
588  */
589 static void fc_rport_plogi_resp(struct fc_seq *sp, struct fc_frame *fp,
590                                 void *rdata_arg)
591 {
592         struct fc_rport_priv *rdata = rdata_arg;
593         struct fc_lport *lport = rdata->local_port;
594         struct fc_els_flogi *plp = NULL;
595         unsigned int tov;
596         u16 csp_seq;
597         u16 cssp_seq;
598         u8 op;
599
600         mutex_lock(&rdata->rp_mutex);
601
602         FC_RPORT_DBG(rdata, "Received a PLOGI %s\n", fc_els_resp_type(fp));
603
604         if (rdata->rp_state != RPORT_ST_PLOGI) {
605                 FC_RPORT_DBG(rdata, "Received a PLOGI response, but in state "
606                              "%s\n", fc_rport_state(rdata));
607                 if (IS_ERR(fp))
608                         goto err;
609                 goto out;
610         }
611
612         if (IS_ERR(fp)) {
613                 fc_rport_error_retry(rdata, fp);
614                 goto err;
615         }
616
617         op = fc_frame_payload_op(fp);
618         if (op == ELS_LS_ACC &&
619             (plp = fc_frame_payload_get(fp, sizeof(*plp))) != NULL) {
620                 rdata->ids.port_name = get_unaligned_be64(&plp->fl_wwpn);
621                 rdata->ids.node_name = get_unaligned_be64(&plp->fl_wwnn);
622
623                 tov = ntohl(plp->fl_csp.sp_e_d_tov);
624                 if (ntohs(plp->fl_csp.sp_features) & FC_SP_FT_EDTR)
625                         tov /= 1000;
626                 if (tov > rdata->e_d_tov)
627                         rdata->e_d_tov = tov;
628                 csp_seq = ntohs(plp->fl_csp.sp_tot_seq);
629                 cssp_seq = ntohs(plp->fl_cssp[3 - 1].cp_con_seq);
630                 if (cssp_seq < csp_seq)
631                         csp_seq = cssp_seq;
632                 rdata->max_seq = csp_seq;
633                 rdata->maxframe_size = fc_plogi_get_maxframe(plp, lport->mfs);
634                 fc_rport_enter_prli(rdata);
635         } else
636                 fc_rport_error_retry(rdata, fp);
637
638 out:
639         fc_frame_free(fp);
640 err:
641         mutex_unlock(&rdata->rp_mutex);
642         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
643 }
644
645 /**
646  * fc_rport_enter_plogi() - Send Port Login (PLOGI) request
647  * @rdata: The remote port to send a PLOGI to
648  *
649  * Locking Note: The rport lock is expected to be held before calling
650  * this routine.
651  */
652 static void fc_rport_enter_plogi(struct fc_rport_priv *rdata)
653 {
654         struct fc_lport *lport = rdata->local_port;
655         struct fc_frame *fp;
656
657         FC_RPORT_DBG(rdata, "Port entered PLOGI state from %s state\n",
658                      fc_rport_state(rdata));
659
660         fc_rport_state_enter(rdata, RPORT_ST_PLOGI);
661
662         rdata->maxframe_size = FC_MIN_MAX_PAYLOAD;
663         fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
664         if (!fp) {
665                 fc_rport_error_retry(rdata, fp);
666                 return;
667         }
668         rdata->e_d_tov = lport->e_d_tov;
669
670         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PLOGI,
671                                   fc_rport_plogi_resp, rdata,
672                                   2 * lport->r_a_tov))
673                 fc_rport_error_retry(rdata, NULL);
674         else
675                 kref_get(&rdata->kref);
676 }
677
678 /**
679  * fc_rport_prli_resp() - Process Login (PRLI) response handler
680  * @sp:        The sequence the PRLI response was on
681  * @fp:        The PRLI response frame
682  * @rdata_arg: The remote port that sent the PRLI response
683  *
684  * Locking Note: This function will be called without the rport lock
685  * held, but it will lock, call an _enter_* function or fc_rport_error
686  * and then unlock the rport.
687  */
688 static void fc_rport_prli_resp(struct fc_seq *sp, struct fc_frame *fp,
689                                void *rdata_arg)
690 {
691         struct fc_rport_priv *rdata = rdata_arg;
692         struct {
693                 struct fc_els_prli prli;
694                 struct fc_els_spp spp;
695         } *pp;
696         u32 roles = FC_RPORT_ROLE_UNKNOWN;
697         u32 fcp_parm = 0;
698         u8 op;
699
700         mutex_lock(&rdata->rp_mutex);
701
702         FC_RPORT_DBG(rdata, "Received a PRLI %s\n", fc_els_resp_type(fp));
703
704         if (rdata->rp_state != RPORT_ST_PRLI) {
705                 FC_RPORT_DBG(rdata, "Received a PRLI response, but in state "
706                              "%s\n", fc_rport_state(rdata));
707                 if (IS_ERR(fp))
708                         goto err;
709                 goto out;
710         }
711
712         if (IS_ERR(fp)) {
713                 fc_rport_error_retry(rdata, fp);
714                 goto err;
715         }
716
717         /* reinitialize remote port roles */
718         rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
719
720         op = fc_frame_payload_op(fp);
721         if (op == ELS_LS_ACC) {
722                 pp = fc_frame_payload_get(fp, sizeof(*pp));
723                 if (pp && pp->prli.prli_spp_len >= sizeof(pp->spp)) {
724                         fcp_parm = ntohl(pp->spp.spp_params);
725                         if (fcp_parm & FCP_SPPF_RETRY)
726                                 rdata->flags |= FC_RP_FLAGS_RETRY;
727                 }
728
729                 rdata->supported_classes = FC_COS_CLASS3;
730                 if (fcp_parm & FCP_SPPF_INIT_FCN)
731                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
732                 if (fcp_parm & FCP_SPPF_TARG_FCN)
733                         roles |= FC_RPORT_ROLE_FCP_TARGET;
734
735                 rdata->ids.roles = roles;
736                 fc_rport_enter_rtv(rdata);
737
738         } else {
739                 FC_RPORT_DBG(rdata, "Bad ELS response for PRLI command\n");
740                 fc_rport_enter_delete(rdata, RPORT_EV_FAILED);
741         }
742
743 out:
744         fc_frame_free(fp);
745 err:
746         mutex_unlock(&rdata->rp_mutex);
747         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
748 }
749
750 /**
751  * fc_rport_logo_resp() - Handler for logout (LOGO) responses
752  * @sp:        The sequence the LOGO was on
753  * @fp:        The LOGO response frame
754  * @rdata_arg: The remote port that sent the LOGO response
755  *
756  * Locking Note: This function will be called without the rport lock
757  * held, but it will lock, call an _enter_* function or fc_rport_error
758  * and then unlock the rport.
759  */
760 static void fc_rport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
761                                void *rdata_arg)
762 {
763         struct fc_rport_priv *rdata = rdata_arg;
764         u8 op;
765
766         mutex_lock(&rdata->rp_mutex);
767
768         FC_RPORT_DBG(rdata, "Received a LOGO %s\n", fc_els_resp_type(fp));
769
770         if (rdata->rp_state != RPORT_ST_LOGO) {
771                 FC_RPORT_DBG(rdata, "Received a LOGO response, but in state "
772                              "%s\n", fc_rport_state(rdata));
773                 if (IS_ERR(fp))
774                         goto err;
775                 goto out;
776         }
777
778         if (IS_ERR(fp)) {
779                 fc_rport_error_retry(rdata, fp);
780                 goto err;
781         }
782
783         op = fc_frame_payload_op(fp);
784         if (op != ELS_LS_ACC)
785                 FC_RPORT_DBG(rdata, "Bad ELS response op %x for LOGO command\n",
786                              op);
787         fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
788
789 out:
790         fc_frame_free(fp);
791 err:
792         mutex_unlock(&rdata->rp_mutex);
793         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
794 }
795
796 /**
797  * fc_rport_enter_prli() - Send Process Login (PRLI) request
798  * @rdata: The remote port to send the PRLI request to
799  *
800  * Locking Note: The rport lock is expected to be held before calling
801  * this routine.
802  */
803 static void fc_rport_enter_prli(struct fc_rport_priv *rdata)
804 {
805         struct fc_lport *lport = rdata->local_port;
806         struct {
807                 struct fc_els_prli prli;
808                 struct fc_els_spp spp;
809         } *pp;
810         struct fc_frame *fp;
811
812         /*
813          * If the rport is one of the well known addresses
814          * we skip PRLI and RTV and go straight to READY.
815          */
816         if (rdata->ids.port_id >= FC_FID_DOM_MGR) {
817                 fc_rport_enter_ready(rdata);
818                 return;
819         }
820
821         FC_RPORT_DBG(rdata, "Port entered PRLI state from %s state\n",
822                      fc_rport_state(rdata));
823
824         fc_rport_state_enter(rdata, RPORT_ST_PRLI);
825
826         fp = fc_frame_alloc(lport, sizeof(*pp));
827         if (!fp) {
828                 fc_rport_error_retry(rdata, fp);
829                 return;
830         }
831
832         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_PRLI,
833                                   fc_rport_prli_resp, rdata,
834                                   2 * lport->r_a_tov))
835                 fc_rport_error_retry(rdata, NULL);
836         else
837                 kref_get(&rdata->kref);
838 }
839
840 /**
841  * fc_rport_els_rtv_resp() - Handler for Request Timeout Value (RTV) responses
842  * @sp:        The sequence the RTV was on
843  * @fp:        The RTV response frame
844  * @rdata_arg: The remote port that sent the RTV response
845  *
846  * Many targets don't seem to support this.
847  *
848  * Locking Note: This function will be called without the rport lock
849  * held, but it will lock, call an _enter_* function or fc_rport_error
850  * and then unlock the rport.
851  */
852 static void fc_rport_rtv_resp(struct fc_seq *sp, struct fc_frame *fp,
853                               void *rdata_arg)
854 {
855         struct fc_rport_priv *rdata = rdata_arg;
856         u8 op;
857
858         mutex_lock(&rdata->rp_mutex);
859
860         FC_RPORT_DBG(rdata, "Received a RTV %s\n", fc_els_resp_type(fp));
861
862         if (rdata->rp_state != RPORT_ST_RTV) {
863                 FC_RPORT_DBG(rdata, "Received a RTV response, but in state "
864                              "%s\n", fc_rport_state(rdata));
865                 if (IS_ERR(fp))
866                         goto err;
867                 goto out;
868         }
869
870         if (IS_ERR(fp)) {
871                 fc_rport_error(rdata, fp);
872                 goto err;
873         }
874
875         op = fc_frame_payload_op(fp);
876         if (op == ELS_LS_ACC) {
877                 struct fc_els_rtv_acc *rtv;
878                 u32 toq;
879                 u32 tov;
880
881                 rtv = fc_frame_payload_get(fp, sizeof(*rtv));
882                 if (rtv) {
883                         toq = ntohl(rtv->rtv_toq);
884                         tov = ntohl(rtv->rtv_r_a_tov);
885                         if (tov == 0)
886                                 tov = 1;
887                         rdata->r_a_tov = tov;
888                         tov = ntohl(rtv->rtv_e_d_tov);
889                         if (toq & FC_ELS_RTV_EDRES)
890                                 tov /= 1000000;
891                         if (tov == 0)
892                                 tov = 1;
893                         rdata->e_d_tov = tov;
894                 }
895         }
896
897         fc_rport_enter_ready(rdata);
898
899 out:
900         fc_frame_free(fp);
901 err:
902         mutex_unlock(&rdata->rp_mutex);
903         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
904 }
905
906 /**
907  * fc_rport_enter_rtv() - Send Request Timeout Value (RTV) request
908  * @rdata: The remote port to send the RTV request to
909  *
910  * Locking Note: The rport lock is expected to be held before calling
911  * this routine.
912  */
913 static void fc_rport_enter_rtv(struct fc_rport_priv *rdata)
914 {
915         struct fc_frame *fp;
916         struct fc_lport *lport = rdata->local_port;
917
918         FC_RPORT_DBG(rdata, "Port entered RTV state from %s state\n",
919                      fc_rport_state(rdata));
920
921         fc_rport_state_enter(rdata, RPORT_ST_RTV);
922
923         fp = fc_frame_alloc(lport, sizeof(struct fc_els_rtv));
924         if (!fp) {
925                 fc_rport_error_retry(rdata, fp);
926                 return;
927         }
928
929         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_RTV,
930                                   fc_rport_rtv_resp, rdata,
931                                   2 * lport->r_a_tov))
932                 fc_rport_error_retry(rdata, NULL);
933         else
934                 kref_get(&rdata->kref);
935 }
936
937 /**
938  * fc_rport_enter_logo() - Send a logout (LOGO) request
939  * @rdata: The remote port to send the LOGO request to
940  *
941  * Locking Note: The rport lock is expected to be held before calling
942  * this routine.
943  */
944 static void fc_rport_enter_logo(struct fc_rport_priv *rdata)
945 {
946         struct fc_lport *lport = rdata->local_port;
947         struct fc_frame *fp;
948
949         FC_RPORT_DBG(rdata, "Port entered LOGO state from %s state\n",
950                      fc_rport_state(rdata));
951
952         fc_rport_state_enter(rdata, RPORT_ST_LOGO);
953
954         fp = fc_frame_alloc(lport, sizeof(struct fc_els_logo));
955         if (!fp) {
956                 fc_rport_error_retry(rdata, fp);
957                 return;
958         }
959
960         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_LOGO,
961                                   fc_rport_logo_resp, rdata,
962                                   2 * lport->r_a_tov))
963                 fc_rport_error_retry(rdata, NULL);
964         else
965                 kref_get(&rdata->kref);
966 }
967
968 /**
969  * fc_rport_els_adisc_resp() - Handler for Address Discovery (ADISC) responses
970  * @sp:        The sequence the ADISC response was on
971  * @fp:        The ADISC response frame
972  * @rdata_arg: The remote port that sent the ADISC response
973  *
974  * Locking Note: This function will be called without the rport lock
975  * held, but it will lock, call an _enter_* function or fc_rport_error
976  * and then unlock the rport.
977  */
978 static void fc_rport_adisc_resp(struct fc_seq *sp, struct fc_frame *fp,
979                                 void *rdata_arg)
980 {
981         struct fc_rport_priv *rdata = rdata_arg;
982         struct fc_els_adisc *adisc;
983         u8 op;
984
985         mutex_lock(&rdata->rp_mutex);
986
987         FC_RPORT_DBG(rdata, "Received a ADISC response\n");
988
989         if (rdata->rp_state != RPORT_ST_ADISC) {
990                 FC_RPORT_DBG(rdata, "Received a ADISC resp but in state %s\n",
991                              fc_rport_state(rdata));
992                 if (IS_ERR(fp))
993                         goto err;
994                 goto out;
995         }
996
997         if (IS_ERR(fp)) {
998                 fc_rport_error(rdata, fp);
999                 goto err;
1000         }
1001
1002         /*
1003          * If address verification failed.  Consider us logged out of the rport.
1004          * Since the rport is still in discovery, we want to be
1005          * logged in, so go to PLOGI state.  Otherwise, go back to READY.
1006          */
1007         op = fc_frame_payload_op(fp);
1008         adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1009         if (op != ELS_LS_ACC || !adisc ||
1010             ntoh24(adisc->adisc_port_id) != rdata->ids.port_id ||
1011             get_unaligned_be64(&adisc->adisc_wwpn) != rdata->ids.port_name ||
1012             get_unaligned_be64(&adisc->adisc_wwnn) != rdata->ids.node_name) {
1013                 FC_RPORT_DBG(rdata, "ADISC error or mismatch\n");
1014                 fc_rport_enter_plogi(rdata);
1015         } else {
1016                 FC_RPORT_DBG(rdata, "ADISC OK\n");
1017                 fc_rport_enter_ready(rdata);
1018         }
1019 out:
1020         fc_frame_free(fp);
1021 err:
1022         mutex_unlock(&rdata->rp_mutex);
1023         kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
1024 }
1025
1026 /**
1027  * fc_rport_enter_adisc() - Send Address Discover (ADISC) request
1028  * @rdata: The remote port to send the ADISC request to
1029  *
1030  * Locking Note: The rport lock is expected to be held before calling
1031  * this routine.
1032  */
1033 static void fc_rport_enter_adisc(struct fc_rport_priv *rdata)
1034 {
1035         struct fc_lport *lport = rdata->local_port;
1036         struct fc_frame *fp;
1037
1038         FC_RPORT_DBG(rdata, "sending ADISC from %s state\n",
1039                      fc_rport_state(rdata));
1040
1041         fc_rport_state_enter(rdata, RPORT_ST_ADISC);
1042
1043         fp = fc_frame_alloc(lport, sizeof(struct fc_els_adisc));
1044         if (!fp) {
1045                 fc_rport_error_retry(rdata, fp);
1046                 return;
1047         }
1048         if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, ELS_ADISC,
1049                                   fc_rport_adisc_resp, rdata,
1050                                   2 * lport->r_a_tov))
1051                 fc_rport_error_retry(rdata, NULL);
1052         else
1053                 kref_get(&rdata->kref);
1054 }
1055
1056 /**
1057  * fc_rport_recv_adisc_req() - Handler for Address Discovery (ADISC) requests
1058  * @rdata: The remote port that sent the ADISC request
1059  * @sp:    The sequence the ADISC request was on
1060  * @in_fp: The ADISC request frame
1061  *
1062  * Locking Note:  Called with the lport and rport locks held.
1063  */
1064 static void fc_rport_recv_adisc_req(struct fc_rport_priv *rdata,
1065                                     struct fc_seq *sp, struct fc_frame *in_fp)
1066 {
1067         struct fc_lport *lport = rdata->local_port;
1068         struct fc_frame *fp;
1069         struct fc_exch *ep = fc_seq_exch(sp);
1070         struct fc_els_adisc *adisc;
1071         struct fc_seq_els_data rjt_data;
1072         u32 f_ctl;
1073
1074         FC_RPORT_DBG(rdata, "Received ADISC request\n");
1075
1076         adisc = fc_frame_payload_get(in_fp, sizeof(*adisc));
1077         if (!adisc) {
1078                 rjt_data.fp = NULL;
1079                 rjt_data.reason = ELS_RJT_PROT;
1080                 rjt_data.explan = ELS_EXPL_INV_LEN;
1081                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1082                 goto drop;
1083         }
1084
1085         fp = fc_frame_alloc(lport, sizeof(*adisc));
1086         if (!fp)
1087                 goto drop;
1088         fc_adisc_fill(lport, fp);
1089         adisc = fc_frame_payload_get(fp, sizeof(*adisc));
1090         adisc->adisc_cmd = ELS_LS_ACC;
1091         sp = lport->tt.seq_start_next(sp);
1092         f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1093         fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1094                        FC_TYPE_ELS, f_ctl, 0);
1095         lport->tt.seq_send(lport, sp, fp);
1096 drop:
1097         fc_frame_free(in_fp);
1098 }
1099
1100 /**
1101  * fc_rport_recv_rls_req() - Handle received Read Link Status request
1102  * @rdata: The remote port that sent the RLS request
1103  * @sp: The sequence that the RLS was on
1104  * @rx_fp: The PRLI request frame
1105  *
1106  * Locking Note: The rport lock is expected to be held before calling
1107  * this function.
1108  */
1109 static void fc_rport_recv_rls_req(struct fc_rport_priv *rdata,
1110                                   struct fc_seq *sp, struct fc_frame *rx_fp)
1111
1112 {
1113         struct fc_lport *lport = rdata->local_port;
1114         struct fc_frame *fp;
1115         struct fc_exch *ep = fc_seq_exch(sp);
1116         struct fc_els_rls *rls;
1117         struct fc_els_rls_resp *rsp;
1118         struct fc_els_lesb *lesb;
1119         struct fc_seq_els_data rjt_data;
1120         struct fc_host_statistics *hst;
1121         u32 f_ctl;
1122
1123         FC_RPORT_DBG(rdata, "Received RLS request while in state %s\n",
1124                      fc_rport_state(rdata));
1125
1126         rls = fc_frame_payload_get(rx_fp, sizeof(*rls));
1127         if (!rls) {
1128                 rjt_data.reason = ELS_RJT_PROT;
1129                 rjt_data.explan = ELS_EXPL_INV_LEN;
1130                 goto out_rjt;
1131         }
1132
1133         fp = fc_frame_alloc(lport, sizeof(*rsp));
1134         if (!fp) {
1135                 rjt_data.reason = ELS_RJT_UNAB;
1136                 rjt_data.explan = ELS_EXPL_INSUF_RES;
1137                 goto out_rjt;
1138         }
1139
1140         rsp = fc_frame_payload_get(fp, sizeof(*rsp));
1141         memset(rsp, 0, sizeof(*rsp));
1142         rsp->rls_cmd = ELS_LS_ACC;
1143         lesb = &rsp->rls_lesb;
1144         if (lport->tt.get_lesb) {
1145                 /* get LESB from LLD if it supports it */
1146                 lport->tt.get_lesb(lport, lesb);
1147         } else {
1148                 fc_get_host_stats(lport->host);
1149                 hst = &lport->host_stats;
1150                 lesb->lesb_link_fail = htonl(hst->link_failure_count);
1151                 lesb->lesb_sync_loss = htonl(hst->loss_of_sync_count);
1152                 lesb->lesb_sig_loss = htonl(hst->loss_of_signal_count);
1153                 lesb->lesb_prim_err = htonl(hst->prim_seq_protocol_err_count);
1154                 lesb->lesb_inv_word = htonl(hst->invalid_tx_word_count);
1155                 lesb->lesb_inv_crc = htonl(hst->invalid_crc_count);
1156         }
1157
1158         sp = lport->tt.seq_start_next(sp);
1159         f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
1160         fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1161                        FC_TYPE_ELS, f_ctl, 0);
1162         lport->tt.seq_send(lport, sp, fp);
1163         goto out;
1164
1165 out_rjt:
1166         rjt_data.fp = NULL;
1167         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1168 out:
1169         fc_frame_free(rx_fp);
1170 }
1171
1172 /**
1173  * fc_rport_recv_els_req() - Handler for validated ELS requests
1174  * @lport: The local port that received the ELS request
1175  * @sp:    The sequence that the ELS request was on
1176  * @fp:    The ELS request frame
1177  *
1178  * Handle incoming ELS requests that require port login.
1179  * The ELS opcode has already been validated by the caller.
1180  *
1181  * Locking Note: Called with the lport lock held.
1182  */
1183 static void fc_rport_recv_els_req(struct fc_lport *lport,
1184                                   struct fc_seq *sp, struct fc_frame *fp)
1185 {
1186         struct fc_rport_priv *rdata;
1187         struct fc_frame_header *fh;
1188         struct fc_seq_els_data els_data;
1189
1190         els_data.fp = NULL;
1191         els_data.reason = ELS_RJT_UNAB;
1192         els_data.explan = ELS_EXPL_PLOGI_REQD;
1193
1194         fh = fc_frame_header_get(fp);
1195
1196         mutex_lock(&lport->disc.disc_mutex);
1197         rdata = lport->tt.rport_lookup(lport, ntoh24(fh->fh_s_id));
1198         if (!rdata) {
1199                 mutex_unlock(&lport->disc.disc_mutex);
1200                 goto reject;
1201         }
1202         mutex_lock(&rdata->rp_mutex);
1203         mutex_unlock(&lport->disc.disc_mutex);
1204
1205         switch (rdata->rp_state) {
1206         case RPORT_ST_PRLI:
1207         case RPORT_ST_RTV:
1208         case RPORT_ST_READY:
1209         case RPORT_ST_ADISC:
1210                 break;
1211         default:
1212                 mutex_unlock(&rdata->rp_mutex);
1213                 goto reject;
1214         }
1215
1216         switch (fc_frame_payload_op(fp)) {
1217         case ELS_PRLI:
1218                 fc_rport_recv_prli_req(rdata, sp, fp);
1219                 break;
1220         case ELS_PRLO:
1221                 fc_rport_recv_prlo_req(rdata, sp, fp);
1222                 break;
1223         case ELS_ADISC:
1224                 fc_rport_recv_adisc_req(rdata, sp, fp);
1225                 break;
1226         case ELS_RRQ:
1227                 els_data.fp = fp;
1228                 lport->tt.seq_els_rsp_send(sp, ELS_RRQ, &els_data);
1229                 break;
1230         case ELS_REC:
1231                 els_data.fp = fp;
1232                 lport->tt.seq_els_rsp_send(sp, ELS_REC, &els_data);
1233                 break;
1234         case ELS_RLS:
1235                 fc_rport_recv_rls_req(rdata, sp, fp);
1236                 break;
1237         default:
1238                 fc_frame_free(fp);      /* can't happen */
1239                 break;
1240         }
1241
1242         mutex_unlock(&rdata->rp_mutex);
1243         return;
1244
1245 reject:
1246         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1247         fc_frame_free(fp);
1248 }
1249
1250 /**
1251  * fc_rport_recv_req() - Handler for requests
1252  * @sp:    The sequence the request was on
1253  * @fp:    The request frame
1254  * @lport: The local port that received the request
1255  *
1256  * Locking Note: Called with the lport lock held.
1257  */
1258 void fc_rport_recv_req(struct fc_seq *sp, struct fc_frame *fp,
1259                        struct fc_lport *lport)
1260 {
1261         struct fc_seq_els_data els_data;
1262
1263         /*
1264          * Handle PLOGI and LOGO requests separately, since they
1265          * don't require prior login.
1266          * Check for unsupported opcodes first and reject them.
1267          * For some ops, it would be incorrect to reject with "PLOGI required".
1268          */
1269         switch (fc_frame_payload_op(fp)) {
1270         case ELS_PLOGI:
1271                 fc_rport_recv_plogi_req(lport, sp, fp);
1272                 break;
1273         case ELS_LOGO:
1274                 fc_rport_recv_logo_req(lport, sp, fp);
1275                 break;
1276         case ELS_PRLI:
1277         case ELS_PRLO:
1278         case ELS_ADISC:
1279         case ELS_RRQ:
1280         case ELS_REC:
1281         case ELS_RLS:
1282                 fc_rport_recv_els_req(lport, sp, fp);
1283                 break;
1284         default:
1285                 fc_frame_free(fp);
1286                 els_data.fp = NULL;
1287                 els_data.reason = ELS_RJT_UNSUP;
1288                 els_data.explan = ELS_EXPL_NONE;
1289                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &els_data);
1290                 break;
1291         }
1292 }
1293
1294 /**
1295  * fc_rport_recv_plogi_req() - Handler for Port Login (PLOGI) requests
1296  * @lport: The local port that received the PLOGI request
1297  * @sp:    The sequence that the PLOGI request was on
1298  * @rx_fp: The PLOGI request frame
1299  *
1300  * Locking Note: The rport lock is held before calling this function.
1301  */
1302 static void fc_rport_recv_plogi_req(struct fc_lport *lport,
1303                                     struct fc_seq *sp, struct fc_frame *rx_fp)
1304 {
1305         struct fc_disc *disc;
1306         struct fc_rport_priv *rdata;
1307         struct fc_frame *fp = rx_fp;
1308         struct fc_exch *ep;
1309         struct fc_frame_header *fh;
1310         struct fc_els_flogi *pl;
1311         struct fc_seq_els_data rjt_data;
1312         u32 sid, f_ctl;
1313
1314         rjt_data.fp = NULL;
1315         fh = fc_frame_header_get(fp);
1316         sid = ntoh24(fh->fh_s_id);
1317
1318         FC_RPORT_ID_DBG(lport, sid, "Received PLOGI request\n");
1319
1320         pl = fc_frame_payload_get(fp, sizeof(*pl));
1321         if (!pl) {
1322                 FC_RPORT_ID_DBG(lport, sid, "Received PLOGI too short\n");
1323                 rjt_data.reason = ELS_RJT_PROT;
1324                 rjt_data.explan = ELS_EXPL_INV_LEN;
1325                 goto reject;
1326         }
1327
1328         disc = &lport->disc;
1329         mutex_lock(&disc->disc_mutex);
1330         rdata = lport->tt.rport_create(lport, sid);
1331         if (!rdata) {
1332                 mutex_unlock(&disc->disc_mutex);
1333                 rjt_data.reason = ELS_RJT_UNAB;
1334                 rjt_data.explan = ELS_EXPL_INSUF_RES;
1335                 goto reject;
1336         }
1337
1338         mutex_lock(&rdata->rp_mutex);
1339         mutex_unlock(&disc->disc_mutex);
1340
1341         rdata->ids.port_name = get_unaligned_be64(&pl->fl_wwpn);
1342         rdata->ids.node_name = get_unaligned_be64(&pl->fl_wwnn);
1343
1344         /*
1345          * If the rport was just created, possibly due to the incoming PLOGI,
1346          * set the state appropriately and accept the PLOGI.
1347          *
1348          * If we had also sent a PLOGI, and if the received PLOGI is from a
1349          * higher WWPN, we accept it, otherwise an LS_RJT is sent with reason
1350          * "command already in progress".
1351          *
1352          * XXX TBD: If the session was ready before, the PLOGI should result in
1353          * all outstanding exchanges being reset.
1354          */
1355         switch (rdata->rp_state) {
1356         case RPORT_ST_INIT:
1357                 FC_RPORT_DBG(rdata, "Received PLOGI in INIT state\n");
1358                 break;
1359         case RPORT_ST_PLOGI:
1360                 FC_RPORT_DBG(rdata, "Received PLOGI in PLOGI state\n");
1361                 if (rdata->ids.port_name < lport->wwpn) {
1362                         mutex_unlock(&rdata->rp_mutex);
1363                         rjt_data.reason = ELS_RJT_INPROG;
1364                         rjt_data.explan = ELS_EXPL_NONE;
1365                         goto reject;
1366                 }
1367                 break;
1368         case RPORT_ST_PRLI:
1369         case RPORT_ST_RTV:
1370         case RPORT_ST_READY:
1371         case RPORT_ST_ADISC:
1372                 FC_RPORT_DBG(rdata, "Received PLOGI in logged-in state %d "
1373                              "- ignored for now\n", rdata->rp_state);
1374                 /* XXX TBD - should reset */
1375                 break;
1376         case RPORT_ST_DELETE:
1377         case RPORT_ST_LOGO:
1378         case RPORT_ST_RESTART:
1379                 FC_RPORT_DBG(rdata, "Received PLOGI in state %s - send busy\n",
1380                              fc_rport_state(rdata));
1381                 mutex_unlock(&rdata->rp_mutex);
1382                 rjt_data.reason = ELS_RJT_BUSY;
1383                 rjt_data.explan = ELS_EXPL_NONE;
1384                 goto reject;
1385         }
1386
1387         /*
1388          * Get session payload size from incoming PLOGI.
1389          */
1390         rdata->maxframe_size = fc_plogi_get_maxframe(pl, lport->mfs);
1391         fc_frame_free(rx_fp);
1392
1393         /*
1394          * Send LS_ACC.  If this fails, the originator should retry.
1395          */
1396         sp = lport->tt.seq_start_next(sp);
1397         if (!sp)
1398                 goto out;
1399         fp = fc_frame_alloc(lport, sizeof(*pl));
1400         if (!fp)
1401                 goto out;
1402
1403         fc_plogi_fill(lport, fp, ELS_LS_ACC);
1404         f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1405         ep = fc_seq_exch(sp);
1406         fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1407                        FC_TYPE_ELS, f_ctl, 0);
1408         lport->tt.seq_send(lport, sp, fp);
1409         fc_rport_enter_prli(rdata);
1410 out:
1411         mutex_unlock(&rdata->rp_mutex);
1412         return;
1413
1414 reject:
1415         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1416         fc_frame_free(fp);
1417 }
1418
1419 /**
1420  * fc_rport_recv_prli_req() - Handler for process login (PRLI) requests
1421  * @rdata: The remote port that sent the PRLI request
1422  * @sp:    The sequence that the PRLI was on
1423  * @rx_fp: The PRLI request frame
1424  *
1425  * Locking Note: The rport lock is exected to be held before calling
1426  * this function.
1427  */
1428 static void fc_rport_recv_prli_req(struct fc_rport_priv *rdata,
1429                                    struct fc_seq *sp, struct fc_frame *rx_fp)
1430 {
1431         struct fc_lport *lport = rdata->local_port;
1432         struct fc_exch *ep;
1433         struct fc_frame *fp;
1434         struct fc_frame_header *fh;
1435         struct {
1436                 struct fc_els_prli prli;
1437                 struct fc_els_spp spp;
1438         } *pp;
1439         struct fc_els_spp *rspp;        /* request service param page */
1440         struct fc_els_spp *spp; /* response spp */
1441         unsigned int len;
1442         unsigned int plen;
1443         enum fc_els_rjt_reason reason = ELS_RJT_UNAB;
1444         enum fc_els_rjt_explan explan = ELS_EXPL_NONE;
1445         enum fc_els_spp_resp resp;
1446         struct fc_seq_els_data rjt_data;
1447         u32 f_ctl;
1448         u32 fcp_parm;
1449         u32 roles = FC_RPORT_ROLE_UNKNOWN;
1450         rjt_data.fp = NULL;
1451
1452         fh = fc_frame_header_get(rx_fp);
1453
1454         FC_RPORT_DBG(rdata, "Received PRLI request while in state %s\n",
1455                      fc_rport_state(rdata));
1456
1457         switch (rdata->rp_state) {
1458         case RPORT_ST_PRLI:
1459         case RPORT_ST_RTV:
1460         case RPORT_ST_READY:
1461         case RPORT_ST_ADISC:
1462                 reason = ELS_RJT_NONE;
1463                 break;
1464         default:
1465                 fc_frame_free(rx_fp);
1466                 return;
1467                 break;
1468         }
1469         len = fr_len(rx_fp) - sizeof(*fh);
1470         pp = fc_frame_payload_get(rx_fp, sizeof(*pp));
1471         if (pp == NULL) {
1472                 reason = ELS_RJT_PROT;
1473                 explan = ELS_EXPL_INV_LEN;
1474         } else {
1475                 plen = ntohs(pp->prli.prli_len);
1476                 if ((plen % 4) != 0 || plen > len) {
1477                         reason = ELS_RJT_PROT;
1478                         explan = ELS_EXPL_INV_LEN;
1479                 } else if (plen < len) {
1480                         len = plen;
1481                 }
1482                 plen = pp->prli.prli_spp_len;
1483                 if ((plen % 4) != 0 || plen < sizeof(*spp) ||
1484                     plen > len || len < sizeof(*pp)) {
1485                         reason = ELS_RJT_PROT;
1486                         explan = ELS_EXPL_INV_LEN;
1487                 }
1488                 rspp = &pp->spp;
1489         }
1490         if (reason != ELS_RJT_NONE ||
1491             (fp = fc_frame_alloc(lport, len)) == NULL) {
1492                 rjt_data.reason = reason;
1493                 rjt_data.explan = explan;
1494                 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1495         } else {
1496                 sp = lport->tt.seq_start_next(sp);
1497                 WARN_ON(!sp);
1498                 pp = fc_frame_payload_get(fp, len);
1499                 WARN_ON(!pp);
1500                 memset(pp, 0, len);
1501                 pp->prli.prli_cmd = ELS_LS_ACC;
1502                 pp->prli.prli_spp_len = plen;
1503                 pp->prli.prli_len = htons(len);
1504                 len -= sizeof(struct fc_els_prli);
1505
1506                 /* reinitialize remote port roles */
1507                 rdata->ids.roles = FC_RPORT_ROLE_UNKNOWN;
1508
1509                 /*
1510                  * Go through all the service parameter pages and build
1511                  * response.  If plen indicates longer SPP than standard,
1512                  * use that.  The entire response has been pre-cleared above.
1513                  */
1514                 spp = &pp->spp;
1515                 while (len >= plen) {
1516                         spp->spp_type = rspp->spp_type;
1517                         spp->spp_type_ext = rspp->spp_type_ext;
1518                         spp->spp_flags = rspp->spp_flags & FC_SPP_EST_IMG_PAIR;
1519                         resp = FC_SPP_RESP_ACK;
1520                         if (rspp->spp_flags & FC_SPP_RPA_VAL)
1521                                 resp = FC_SPP_RESP_NO_PA;
1522                         switch (rspp->spp_type) {
1523                         case 0: /* common to all FC-4 types */
1524                                 break;
1525                         case FC_TYPE_FCP:
1526                                 fcp_parm = ntohl(rspp->spp_params);
1527                                 if (fcp_parm & FCP_SPPF_RETRY)
1528                                         rdata->flags |= FC_RP_FLAGS_RETRY;
1529                                 rdata->supported_classes = FC_COS_CLASS3;
1530                                 if (fcp_parm & FCP_SPPF_INIT_FCN)
1531                                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1532                                 if (fcp_parm & FCP_SPPF_TARG_FCN)
1533                                         roles |= FC_RPORT_ROLE_FCP_TARGET;
1534                                 rdata->ids.roles = roles;
1535
1536                                 spp->spp_params =
1537                                         htonl(lport->service_params);
1538                                 break;
1539                         default:
1540                                 resp = FC_SPP_RESP_INVL;
1541                                 break;
1542                         }
1543                         spp->spp_flags |= resp;
1544                         len -= plen;
1545                         rspp = (struct fc_els_spp *)((char *)rspp + plen);
1546                         spp = (struct fc_els_spp *)((char *)spp + plen);
1547                 }
1548
1549                 /*
1550                  * Send LS_ACC.  If this fails, the originator should retry.
1551                  */
1552                 f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
1553                 f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
1554                 ep = fc_seq_exch(sp);
1555                 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
1556                                FC_TYPE_ELS, f_ctl, 0);
1557                 lport->tt.seq_send(lport, sp, fp);
1558
1559                 /*
1560                  * Get lock and re-check state.
1561                  */
1562                 switch (rdata->rp_state) {
1563                 case RPORT_ST_PRLI:
1564                         fc_rport_enter_ready(rdata);
1565                         break;
1566                 case RPORT_ST_READY:
1567                 case RPORT_ST_ADISC:
1568                         break;
1569                 default:
1570                         break;
1571                 }
1572         }
1573         fc_frame_free(rx_fp);
1574 }
1575
1576 /**
1577  * fc_rport_recv_prlo_req() - Handler for process logout (PRLO) requests
1578  * @rdata: The remote port that sent the PRLO request
1579  * @sp:    The sequence that the PRLO was on
1580  * @fp:    The PRLO request frame
1581  *
1582  * Locking Note: The rport lock is exected to be held before calling
1583  * this function.
1584  */
1585 static void fc_rport_recv_prlo_req(struct fc_rport_priv *rdata,
1586                                    struct fc_seq *sp,
1587                                    struct fc_frame *fp)
1588 {
1589         struct fc_lport *lport = rdata->local_port;
1590
1591         struct fc_frame_header *fh;
1592         struct fc_seq_els_data rjt_data;
1593
1594         fh = fc_frame_header_get(fp);
1595
1596         FC_RPORT_DBG(rdata, "Received PRLO request while in state %s\n",
1597                      fc_rport_state(rdata));
1598
1599         rjt_data.fp = NULL;
1600         rjt_data.reason = ELS_RJT_UNAB;
1601         rjt_data.explan = ELS_EXPL_NONE;
1602         lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
1603         fc_frame_free(fp);
1604 }
1605
1606 /**
1607  * fc_rport_recv_logo_req() - Handler for logout (LOGO) requests
1608  * @lport: The local port that received the LOGO request
1609  * @sp:    The sequence that the LOGO request was on
1610  * @fp:    The LOGO request frame
1611  *
1612  * Locking Note: The rport lock is exected to be held before calling
1613  * this function.
1614  */
1615 static void fc_rport_recv_logo_req(struct fc_lport *lport,
1616                                    struct fc_seq *sp,
1617                                    struct fc_frame *fp)
1618 {
1619         struct fc_frame_header *fh;
1620         struct fc_rport_priv *rdata;
1621         u32 sid;
1622
1623         lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
1624
1625         fh = fc_frame_header_get(fp);
1626         sid = ntoh24(fh->fh_s_id);
1627
1628         mutex_lock(&lport->disc.disc_mutex);
1629         rdata = lport->tt.rport_lookup(lport, sid);
1630         if (rdata) {
1631                 mutex_lock(&rdata->rp_mutex);
1632                 FC_RPORT_DBG(rdata, "Received LOGO request while in state %s\n",
1633                              fc_rport_state(rdata));
1634
1635                 fc_rport_enter_delete(rdata, RPORT_EV_LOGO);
1636
1637                 /*
1638                  * If the remote port was created due to discovery, set state
1639                  * to log back in.  It may have seen a stale RSCN about us.
1640                  */
1641                 if (rdata->disc_id)
1642                         fc_rport_state_enter(rdata, RPORT_ST_RESTART);
1643                 mutex_unlock(&rdata->rp_mutex);
1644         } else
1645                 FC_RPORT_ID_DBG(lport, sid,
1646                                 "Received LOGO from non-logged-in port\n");
1647         mutex_unlock(&lport->disc.disc_mutex);
1648         fc_frame_free(fp);
1649 }
1650
1651 /**
1652  * fc_rport_flush_queue() - Flush the rport_event_queue
1653  */
1654 static void fc_rport_flush_queue(void)
1655 {
1656         flush_workqueue(rport_event_queue);
1657 }
1658
1659 /**
1660  * fc_rport_init() - Initialize the remote port layer for a local port
1661  * @lport: The local port to initialize the remote port layer for
1662  */
1663 int fc_rport_init(struct fc_lport *lport)
1664 {
1665         if (!lport->tt.rport_lookup)
1666                 lport->tt.rport_lookup = fc_rport_lookup;
1667
1668         if (!lport->tt.rport_create)
1669                 lport->tt.rport_create = fc_rport_create;
1670
1671         if (!lport->tt.rport_login)
1672                 lport->tt.rport_login = fc_rport_login;
1673
1674         if (!lport->tt.rport_logoff)
1675                 lport->tt.rport_logoff = fc_rport_logoff;
1676
1677         if (!lport->tt.rport_recv_req)
1678                 lport->tt.rport_recv_req = fc_rport_recv_req;
1679
1680         if (!lport->tt.rport_flush_queue)
1681                 lport->tt.rport_flush_queue = fc_rport_flush_queue;
1682
1683         if (!lport->tt.rport_destroy)
1684                 lport->tt.rport_destroy = fc_rport_destroy;
1685
1686         return 0;
1687 }
1688 EXPORT_SYMBOL(fc_rport_init);
1689
1690 /**
1691  * fc_setup_rport() - Initialize the rport_event_queue
1692  */
1693 int fc_setup_rport()
1694 {
1695         rport_event_queue = create_singlethread_workqueue("fc_rport_eq");
1696         if (!rport_event_queue)
1697                 return -ENOMEM;
1698         return 0;
1699 }
1700
1701 /**
1702  * fc_destroy_rport() - Destroy the rport_event_queue
1703  */
1704 void fc_destroy_rport()
1705 {
1706         destroy_workqueue(rport_event_queue);
1707 }
1708
1709 /**
1710  * fc_rport_terminate_io() - Stop all outstanding I/O on a remote port
1711  * @rport: The remote port whose I/O should be terminated
1712  */
1713 void fc_rport_terminate_io(struct fc_rport *rport)
1714 {
1715         struct fc_rport_libfc_priv *rpriv = rport->dd_data;
1716         struct fc_lport *lport = rpriv->local_port;
1717
1718         lport->tt.exch_mgr_reset(lport, 0, rport->port_id);
1719         lport->tt.exch_mgr_reset(lport, rport->port_id, 0);
1720 }
1721 EXPORT_SYMBOL(fc_rport_terminate_io);