Linux Audio

Check our new training course

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