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