Linux Audio

Check our new training course

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