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