root/drivers/scsi/libfc/fc_rport.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. fc_rport_lookup
  2. fc_rport_create
  3. fc_rport_destroy
  4. fc_rport_state
  5. fc_set_rport_loss_tmo
  6. fc_plogi_get_maxframe
  7. fc_rport_state_enter
  8. fc_rport_work
  9. fc_rport_login
  10. fc_rport_enter_delete
  11. fc_rport_logoff
  12. fc_rport_enter_ready
  13. fc_rport_timeout
  14. fc_rport_error
  15. fc_rport_error_retry
  16. fc_rport_login_complete
  17. fc_rport_flogi_resp
  18. fc_rport_enter_flogi
  19. fc_rport_recv_flogi_req
  20. fc_rport_plogi_resp
  21. fc_rport_compatible_roles
  22. fc_rport_enter_plogi
  23. fc_rport_prli_resp
  24. fc_rport_enter_prli
  25. fc_rport_rtv_resp
  26. fc_rport_enter_rtv
  27. fc_rport_recv_rtv_req
  28. fc_rport_logo_resp
  29. fc_rport_enter_logo
  30. fc_rport_adisc_resp
  31. fc_rport_enter_adisc
  32. fc_rport_recv_adisc_req
  33. fc_rport_recv_rls_req
  34. fc_rport_recv_els_req
  35. fc_rport_recv_req
  36. fc_rport_recv_plogi_req
  37. fc_rport_recv_prli_req
  38. fc_rport_recv_prlo_req
  39. fc_rport_recv_logo_req
  40. fc_rport_flush_queue
  41. fc_rport_fcp_prli
  42. fc_rport_t0_prli
  43. fc_setup_rport
  44. fc_destroy_rport
  45. fc_rport_terminate_io

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

/* [<][>][^][v][top][bottom][index][help] */