fc_lport.c 40.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/*
 * Copyright(c) 2007 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Maintained at www.Open-FCoE.org
 */

/*
 * PORT LOCKING NOTES
 *
 * These comments only apply to the 'port code' which consists of the lport,
 * disc and rport blocks.
 *
 * MOTIVATION
 *
 * The lport, disc and rport blocks all have mutexes that are used to protect
 * those objects. The main motivation for these locks is to prevent from
 * having an lport reset just before we send a frame. In that scenario the
 * lport's FID would get set to zero and then we'd send a frame with an
 * invalid SID. We also need to ensure that states don't change unexpectedly
 * while processing another state.
 *
 * HEIRARCHY
 *
 * The following heirarchy defines the locking rules. A greater lock
 * may be held before acquiring a lesser lock, but a lesser lock should never
 * be held while attempting to acquire a greater lock. Here is the heirarchy-
 *
 * lport > disc, lport > rport, disc > rport
 *
 * CALLBACKS
 *
 * The callbacks cause complications with this scheme. There is a callback
 * from the rport (to either lport or disc) and a callback from disc
 * (to the lport).
 *
 * As rports exit the rport state machine a callback is made to the owner of
 * the rport to notify success or failure. Since the callback is likely to
 * cause the lport or disc to grab its lock we cannot hold the rport lock
 * while making the callback. To ensure that the rport is not free'd while
 * processing the callback the rport callbacks are serialized through a
 * single-threaded workqueue. An rport would never be free'd while in a
 * callback handler becuase no other rport work in this queue can be executed
 * at the same time.
 *
 * When discovery succeeds or fails a callback is made to the lport as
 * notification. Currently, succesful discovery causes the lport to take no
 * action. A failure will cause the lport to reset. There is likely a circular
 * locking problem with this implementation.
 */

/*
 * LPORT LOCKING
 *
 * The critical sections protected by the lport's mutex are quite broad and
 * may be improved upon in the future. The lport code and its locking doesn't
 * influence the I/O path, so excessive locking doesn't penalize I/O
 * performance.
 *
 * The strategy is to lock whenever processing a request or response. Note
 * that every _enter_* function corresponds to a state change. They generally
 * change the lports state and then send a request out on the wire. We lock
 * before calling any of these functions to protect that state change. This
 * means that the entry points into the lport block manage the locks while
 * the state machine can transition between states (i.e. _enter_* functions)
 * while always staying protected.
 *
 * When handling responses we also hold the lport mutex broadly. When the
 * lport receives the response frame it locks the mutex and then calls the
 * appropriate handler for the particuar response. Generally a response will
 * trigger a state change and so the lock must already be held.
 *
 * Retries also have to consider the locking. The retries occur from a work
 * context and the work function will lock the lport and then retry the state
 * (i.e. _enter_* function).
 */

#include <linux/timer.h>
#include <asm/unaligned.h>

#include <scsi/fc/fc_gs.h>

#include <scsi/libfc.h>
#include <scsi/fc_encode.h>

98 99
#include "fc_libfc.h"

100 101 102 103 104 105 106 107 108 109 110
/* Fabric IDs to use for point-to-point mode, chosen on whims. */
#define FC_LOCAL_PTP_FID_LO   0x010101
#define FC_LOCAL_PTP_FID_HI   0x010102

#define	DNS_DELAY	      3 /* Discovery delay after RSCN (in seconds)*/

static void fc_lport_error(struct fc_lport *, struct fc_frame *);

static void fc_lport_enter_reset(struct fc_lport *);
static void fc_lport_enter_flogi(struct fc_lport *);
static void fc_lport_enter_dns(struct fc_lport *);
111
static void fc_lport_enter_ns(struct fc_lport *, enum fc_lport_state);
112 113 114 115 116
static void fc_lport_enter_scr(struct fc_lport *);
static void fc_lport_enter_ready(struct fc_lport *);
static void fc_lport_enter_logo(struct fc_lport *);

static const char *fc_lport_state_names[] = {
117
	[LPORT_ST_DISABLED] = "disabled",
118 119
	[LPORT_ST_FLOGI] =    "FLOGI",
	[LPORT_ST_DNS] =      "dNS",
120
	[LPORT_ST_RNN_ID] =   "RNN_ID",
121
	[LPORT_ST_RSNN_NN] =  "RSNN_NN",
122
	[LPORT_ST_RSPN_ID] =  "RSPN_ID",
123 124 125 126 127 128 129 130 131 132 133 134 135 136
	[LPORT_ST_RFT_ID] =   "RFT_ID",
	[LPORT_ST_SCR] =      "SCR",
	[LPORT_ST_READY] =    "Ready",
	[LPORT_ST_LOGO] =     "LOGO",
	[LPORT_ST_RESET] =    "reset",
};

static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
{
	fc_frame_free(fp);
	return 0;
}

/**
137
 * fc_lport_rport_callback() - Event handler for rport events
138
 * @lport: The lport which is receiving the event
139
 * @rdata: private remote port data
140 141 142 143 144 145
 * @event: The event that occured
 *
 * Locking Note: The rport lock should not be held when calling
 *		 this function.
 */
static void fc_lport_rport_callback(struct fc_lport *lport,
146
				    struct fc_rport_priv *rdata,
147 148
				    enum fc_rport_event event)
{
149
	FC_LPORT_DBG(lport, "Received a %d event for port (%6x)\n", event,
150
		     rdata->ids.port_id);
151

152
	mutex_lock(&lport->lp_mutex);
153
	switch (event) {
154
	case RPORT_EV_READY:
155 156
		if (lport->state == LPORT_ST_DNS) {
			lport->dns_rp = rdata;
157
			fc_lport_enter_ns(lport, LPORT_ST_RNN_ID);
158 159 160 161 162 163 164 165 166
		} else {
			FC_LPORT_DBG(lport, "Received an READY event "
				     "on port (%6x) for the directory "
				     "server, but the lport is not "
				     "in the DNS state, it's in the "
				     "%d state", rdata->ids.port_id,
				     lport->state);
			lport->tt.rport_logoff(rdata);
		}
167 168 169 170
		break;
	case RPORT_EV_LOGO:
	case RPORT_EV_FAILED:
	case RPORT_EV_STOP:
171
		lport->dns_rp = NULL;
172 173 174 175
		break;
	case RPORT_EV_NONE:
		break;
	}
176
	mutex_unlock(&lport->lp_mutex);
177 178 179
}

/**
180
 * fc_lport_state() - Return a string which represents the lport's state
181 182 183 184 185 186 187 188 189 190 191 192 193
 * @lport: The lport whose state is to converted to a string
 */
static const char *fc_lport_state(struct fc_lport *lport)
{
	const char *cp;

	cp = fc_lport_state_names[lport->state];
	if (!cp)
		cp = "unknown";
	return cp;
}

/**
194
 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
195 196 197 198 199 200 201 202 203
 * @lport: The lport to attach the ptp rport to
 * @fid: The FID of the ptp rport
 * @remote_wwpn: The WWPN of the ptp rport
 * @remote_wwnn: The WWNN of the ptp rport
 */
static void fc_lport_ptp_setup(struct fc_lport *lport,
			       u32 remote_fid, u64 remote_wwpn,
			       u64 remote_wwnn)
{
204 205
	mutex_lock(&lport->disc.disc_mutex);
	if (lport->ptp_rp)
206
		lport->tt.rport_logoff(lport->ptp_rp);
207 208 209
	lport->ptp_rp = lport->tt.rport_create(lport, remote_fid);
	lport->ptp_rp->ids.port_name = remote_wwpn;
	lport->ptp_rp->ids.node_name = remote_wwnn;
210
	mutex_unlock(&lport->disc.disc_mutex);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

	lport->tt.rport_login(lport->ptp_rp);

	fc_lport_enter_ready(lport);
}

void fc_get_host_port_type(struct Scsi_Host *shost)
{
	/* TODO - currently just NPORT */
	fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
}
EXPORT_SYMBOL(fc_get_host_port_type);

void fc_get_host_port_state(struct Scsi_Host *shost)
{
	struct fc_lport *lp = shost_priv(shost);

228 229 230
	mutex_lock(&lp->lp_mutex);
	if (!lp->link_up)
		fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
231
	else
232 233 234 235 236 237 238 239
		switch (lp->state) {
		case LPORT_ST_READY:
			fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
			break;
		default:
			fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
		}
	mutex_unlock(&lp->lp_mutex);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
}
EXPORT_SYMBOL(fc_get_host_port_state);

void fc_get_host_speed(struct Scsi_Host *shost)
{
	struct fc_lport *lport = shost_priv(shost);

	fc_host_speed(shost) = lport->link_speed;
}
EXPORT_SYMBOL(fc_get_host_speed);

struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
{
	struct fc_host_statistics *fcoe_stats;
	struct fc_lport *lp = shost_priv(shost);
	struct timespec v0, v1;
256
	unsigned int cpu;
257 258 259 260 261 262 263 264

	fcoe_stats = &lp->host_stats;
	memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));

	jiffies_to_timespec(jiffies, &v0);
	jiffies_to_timespec(lp->boot_time, &v1);
	fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);

265 266 267 268 269
	for_each_possible_cpu(cpu) {
		struct fcoe_dev_stats *stats;

		stats = per_cpu_ptr(lp->dev_stats, cpu);

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
		fcoe_stats->tx_frames += stats->TxFrames;
		fcoe_stats->tx_words += stats->TxWords;
		fcoe_stats->rx_frames += stats->RxFrames;
		fcoe_stats->rx_words += stats->RxWords;
		fcoe_stats->error_frames += stats->ErrorFrames;
		fcoe_stats->invalid_crc_count += stats->InvalidCRCCount;
		fcoe_stats->fcp_input_requests += stats->InputRequests;
		fcoe_stats->fcp_output_requests += stats->OutputRequests;
		fcoe_stats->fcp_control_requests += stats->ControlRequests;
		fcoe_stats->fcp_input_megabytes += stats->InputMegabytes;
		fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes;
		fcoe_stats->link_failure_count += stats->LinkFailureCount;
	}
	fcoe_stats->lip_count = -1;
	fcoe_stats->nos_count = -1;
	fcoe_stats->loss_of_sync_count = -1;
	fcoe_stats->loss_of_signal_count = -1;
	fcoe_stats->prim_seq_protocol_err_count = -1;
	fcoe_stats->dumped_frames = -1;
	return fcoe_stats;
}
EXPORT_SYMBOL(fc_get_host_stats);

/*
 * Fill in FLOGI command for request.
 */
static void
fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi,
		    unsigned int op)
{
	struct fc_els_csp *sp;
	struct fc_els_cssp *cp;

	memset(flogi, 0, sizeof(*flogi));
	flogi->fl_cmd = (u8) op;
	put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn);
	put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn);
	sp = &flogi->fl_csp;
	sp->sp_hi_ver = 0x20;
	sp->sp_lo_ver = 0x20;
	sp->sp_bb_cred = htons(10);	/* this gets set by gateway */
	sp->sp_bb_data = htons((u16) lport->mfs);
	cp = &flogi->fl_cssp[3 - 1];	/* class 3 parameters */
	cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ);
	if (op != ELS_FLOGI) {
		sp->sp_features = htons(FC_SP_FT_CIRO);
		sp->sp_tot_seq = htons(255);	/* seq. we accept */
		sp->sp_rel_off = htons(0x1f);
		sp->sp_e_d_tov = htonl(lport->e_d_tov);

		cp->cp_rdfs = htons((u16) lport->mfs);
		cp->cp_con_seq = htons(255);
		cp->cp_open_seq = 1;
	}
}

/*
 * Add a supported FC-4 type.
 */
static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type)
{
	__be32 *mp;

	mp = &lport->fcts.ff_type_map[type / FC_NS_BPW];
	*mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW));
}

/**
338
 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
339 340 341 342
 * @lport: Fibre Channel local port recieving the RLIR
 * @sp: current sequence in the RLIR exchange
 * @fp: RLIR request frame
 *
343
 * Locking Note: The lport lock is expected to be held before calling
344 345 346 347 348
 * this function.
 */
static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp,
				   struct fc_lport *lport)
{
349 350
	FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
		     fc_lport_state(lport));
351 352 353 354 355 356

	lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
	fc_frame_free(fp);
}

/**
357
 * fc_lport_recv_echo_req() - Handle received ECHO request
358 359 360 361
 * @lport: Fibre Channel local port recieving the ECHO
 * @sp: current sequence in the ECHO exchange
 * @fp: ECHO request frame
 *
362
 * Locking Note: The lport lock is expected to be held before calling
363 364 365 366 367 368 369 370 371 372 373 374
 * this function.
 */
static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp,
				   struct fc_lport *lport)
{
	struct fc_frame *fp;
	struct fc_exch *ep = fc_seq_exch(sp);
	unsigned int len;
	void *pp;
	void *dp;
	u32 f_ctl;

375
	FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
376
		     fc_lport_state(lport));
377 378 379 380 381 382 383 384 385 386 387

	len = fr_len(in_fp) - sizeof(struct fc_frame_header);
	pp = fc_frame_payload_get(in_fp, len);

	if (len < sizeof(__be32))
		len = sizeof(__be32);

	fp = fc_frame_alloc(lport, len);
	if (fp) {
		dp = fc_frame_payload_get(fp, len);
		memcpy(dp, pp, len);
388
		*((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
389 390 391 392 393 394 395 396 397 398
		sp = lport->tt.seq_start_next(sp);
		f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
		fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
			       FC_TYPE_ELS, f_ctl, 0);
		lport->tt.seq_send(lport, sp, fp);
	}
	fc_frame_free(in_fp);
}

/**
399 400 401 402
 * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
 * @sp:	   The sequence in the RNID exchange
 * @fp:	   The RNID request frame
 * @lport: The local port recieving the RNID
403
 *
404
 * Locking Note: The lport lock is expected to be held before calling
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
 * this function.
 */
static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp,
				   struct fc_lport *lport)
{
	struct fc_frame *fp;
	struct fc_exch *ep = fc_seq_exch(sp);
	struct fc_els_rnid *req;
	struct {
		struct fc_els_rnid_resp rnid;
		struct fc_els_rnid_cid cid;
		struct fc_els_rnid_gen gen;
	} *rp;
	struct fc_seq_els_data rjt_data;
	u8 fmt;
	size_t len;
	u32 f_ctl;

423 424
	FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
		     fc_lport_state(lport));
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465

	req = fc_frame_payload_get(in_fp, sizeof(*req));
	if (!req) {
		rjt_data.fp = NULL;
		rjt_data.reason = ELS_RJT_LOGIC;
		rjt_data.explan = ELS_EXPL_NONE;
		lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
	} else {
		fmt = req->rnid_fmt;
		len = sizeof(*rp);
		if (fmt != ELS_RNIDF_GEN ||
		    ntohl(lport->rnid_gen.rnid_atype) == 0) {
			fmt = ELS_RNIDF_NONE;	/* nothing to provide */
			len -= sizeof(rp->gen);
		}
		fp = fc_frame_alloc(lport, len);
		if (fp) {
			rp = fc_frame_payload_get(fp, len);
			memset(rp, 0, len);
			rp->rnid.rnid_cmd = ELS_LS_ACC;
			rp->rnid.rnid_fmt = fmt;
			rp->rnid.rnid_cid_len = sizeof(rp->cid);
			rp->cid.rnid_wwpn = htonll(lport->wwpn);
			rp->cid.rnid_wwnn = htonll(lport->wwnn);
			if (fmt == ELS_RNIDF_GEN) {
				rp->rnid.rnid_sid_len = sizeof(rp->gen);
				memcpy(&rp->gen, &lport->rnid_gen,
				       sizeof(rp->gen));
			}
			sp = lport->tt.seq_start_next(sp);
			f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ;
			f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT;
			fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
				       FC_TYPE_ELS, f_ctl, 0);
			lport->tt.seq_send(lport, sp, fp);
		}
	}
	fc_frame_free(in_fp);
}

/**
466
 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
 * @lport: Fibre Channel local port recieving the LOGO
 * @sp: current sequence in the LOGO exchange
 * @fp: LOGO request frame
 *
 * Locking Note: The lport lock is exected to be held before calling
 * this function.
 */
static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp,
				   struct fc_lport *lport)
{
	lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
	fc_lport_enter_reset(lport);
	fc_frame_free(fp);
}

/**
483
 * fc_fabric_login() - Start the lport state machine
484 485 486 487 488 489 490 491 492 493
 * @lport: The lport that should log into the fabric
 *
 * Locking Note: This function should not be called
 *		 with the lport lock held.
 */
int fc_fabric_login(struct fc_lport *lport)
{
	int rc = -1;

	mutex_lock(&lport->lp_mutex);
494
	if (lport->state == LPORT_ST_DISABLED) {
495 496 497 498 499 500 501 502 503 504
		fc_lport_enter_reset(lport);
		rc = 0;
	}
	mutex_unlock(&lport->lp_mutex);

	return rc;
}
EXPORT_SYMBOL(fc_fabric_login);

/**
505
 * __fc_linkup() - Handler for transport linkup events
506
 * @lport: The lport whose link is up
507 508
 *
 * Locking: must be called with the lp_mutex held
509
 */
510
void __fc_linkup(struct fc_lport *lport)
511
{
512 513
	if (!lport->link_up) {
		lport->link_up = 1;
514 515 516 517

		if (lport->state == LPORT_ST_RESET)
			fc_lport_enter_flogi(lport);
	}
518 519 520 521 522 523 524 525 526 527 528 529 530
}

/**
 * fc_linkup() - Handler for transport linkup events
 * @lport: The lport whose link is up
 */
void fc_linkup(struct fc_lport *lport)
{
	printk(KERN_INFO "libfc: Link up on port (%6x)\n",
	       fc_host_port_id(lport->host));

	mutex_lock(&lport->lp_mutex);
	__fc_linkup(lport);
531 532 533 534 535
	mutex_unlock(&lport->lp_mutex);
}
EXPORT_SYMBOL(fc_linkup);

/**
536
 * __fc_linkdown() - Handler for transport linkdown events
537
 * @lport: The lport whose link is down
538 539
 *
 * Locking: must be called with the lp_mutex held
540
 */
541
void __fc_linkdown(struct fc_lport *lport)
542
{
543 544
	if (lport->link_up) {
		lport->link_up = 0;
545 546 547
		fc_lport_enter_reset(lport);
		lport->tt.fcp_cleanup(lport);
	}
548 549 550 551 552 553 554 555 556 557 558 559 560
}

/**
 * fc_linkdown() - Handler for transport linkdown events
 * @lport: The lport whose link is down
 */
void fc_linkdown(struct fc_lport *lport)
{
	printk(KERN_INFO "libfc: Link down on port (%6x)\n",
	       fc_host_port_id(lport->host));

	mutex_lock(&lport->lp_mutex);
	__fc_linkdown(lport);
561 562 563 564 565
	mutex_unlock(&lport->lp_mutex);
}
EXPORT_SYMBOL(fc_linkdown);

/**
566
 * fc_fabric_logoff() - Logout of the fabric
567 568 569 570
 * @lport:	      fc_lport pointer to logoff the fabric
 *
 * Return value:
 *	0 for success, -1 for failure
571
 */
572 573 574 575
int fc_fabric_logoff(struct fc_lport *lport)
{
	lport->tt.disc_stop_final(lport);
	mutex_lock(&lport->lp_mutex);
576 577 578 579 580
	if (lport->dns_rp)
		lport->tt.rport_logoff(lport->dns_rp);
	mutex_unlock(&lport->lp_mutex);
	lport->tt.rport_flush_queue();
	mutex_lock(&lport->lp_mutex);
581 582
	fc_lport_enter_logo(lport);
	mutex_unlock(&lport->lp_mutex);
583
	cancel_delayed_work_sync(&lport->retry_work);
584 585 586 587 588
	return 0;
}
EXPORT_SYMBOL(fc_fabric_logoff);

/**
589
 * fc_lport_destroy() - unregister a fc_lport
590 591 592 593 594 595 596 597 598
 * @lport:	      fc_lport pointer to unregister
 *
 * Return value:
 *	None
 * Note:
 * exit routine for fc_lport instance
 * clean-up all the allocated memory
 * and free up other system resources.
 *
599
 */
600 601
int fc_lport_destroy(struct fc_lport *lport)
{
602
	mutex_lock(&lport->lp_mutex);
603
	lport->state = LPORT_ST_DISABLED;
604
	lport->link_up = 0;
605
	lport->tt.frame_send = fc_frame_drop;
606 607
	mutex_unlock(&lport->lp_mutex);

608
	lport->tt.fcp_abort_io(lport);
609
	lport->tt.disc_stop_final(lport);
610
	lport->tt.exch_mgr_reset(lport, 0, 0);
611 612 613 614 615
	return 0;
}
EXPORT_SYMBOL(fc_lport_destroy);

/**
616
 * fc_set_mfs() - sets up the mfs for the corresponding fc_lport
617 618 619 620 621 622
 * @lport: fc_lport pointer to unregister
 * @mfs: the new mfs for fc_lport
 *
 * Set mfs for the given fc_lport to the new mfs.
 *
 * Return: 0 for success
623
 */
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
int fc_set_mfs(struct fc_lport *lport, u32 mfs)
{
	unsigned int old_mfs;
	int rc = -EINVAL;

	mutex_lock(&lport->lp_mutex);

	old_mfs = lport->mfs;

	if (mfs >= FC_MIN_MAX_FRAME) {
		mfs &= ~3;
		if (mfs > FC_MAX_FRAME)
			mfs = FC_MAX_FRAME;
		mfs -= sizeof(struct fc_frame_header);
		lport->mfs = mfs;
		rc = 0;
	}

	if (!rc && mfs < old_mfs)
		fc_lport_enter_reset(lport);

	mutex_unlock(&lport->lp_mutex);

	return rc;
}
EXPORT_SYMBOL(fc_set_mfs);

/**
652
 * fc_lport_disc_callback() - Callback for discovery events
653 654 655 656 657 658 659
 * @lport: FC local port
 * @event: The discovery event
 */
void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event)
{
	switch (event) {
	case DISC_EV_SUCCESS:
660
		FC_LPORT_DBG(lport, "Discovery succeeded\n");
661 662
		break;
	case DISC_EV_FAILED:
663 664
		printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n",
		       fc_host_port_id(lport->host));
665 666 667 668 669 670 671 672 673 674 675
		mutex_lock(&lport->lp_mutex);
		fc_lport_enter_reset(lport);
		mutex_unlock(&lport->lp_mutex);
		break;
	case DISC_EV_NONE:
		WARN_ON(1);
		break;
	}
}

/**
676
 * fc_rport_enter_ready() - Enter the ready state and start discovery
677 678 679 680 681 682 683
 * @lport: Fibre Channel local port that is ready
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
static void fc_lport_enter_ready(struct fc_lport *lport)
{
684 685
	FC_LPORT_DBG(lport, "Entered READY from state %s\n",
		     fc_lport_state(lport));
686 687

	fc_lport_state_enter(lport, LPORT_ST_READY);
688 689 690
	if (lport->vport)
		fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
	fc_vports_linkchange(lport);
691

692 693
	if (!lport->ptp_rp)
		lport->tt.disc_start(fc_lport_disc_callback, lport);
694 695 696
}

/**
697
 * fc_lport_recv_flogi_req() - Receive a FLOGI request
698 699 700 701 702 703 704 705
 * @sp_in: The sequence the FLOGI is on
 * @rx_fp: The frame the FLOGI is in
 * @lport: The lport that recieved the request
 *
 * A received FLOGI request indicates a point-to-point connection.
 * Accept it with the common service parameters indicating our N port.
 * Set up to do a PLOGI if we have the higher-number WWPN.
 *
706
 * Locking Note: The lport lock is expected to be held before calling
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
 * this function.
 */
static void fc_lport_recv_flogi_req(struct fc_seq *sp_in,
				    struct fc_frame *rx_fp,
				    struct fc_lport *lport)
{
	struct fc_frame *fp;
	struct fc_frame_header *fh;
	struct fc_seq *sp;
	struct fc_exch *ep;
	struct fc_els_flogi *flp;
	struct fc_els_flogi *new_flp;
	u64 remote_wwpn;
	u32 remote_fid;
	u32 local_fid;
	u32 f_ctl;

724 725
	FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
		     fc_lport_state(lport));
726 727 728 729 730 731 732 733

	fh = fc_frame_header_get(rx_fp);
	remote_fid = ntoh24(fh->fh_s_id);
	flp = fc_frame_payload_get(rx_fp, sizeof(*flp));
	if (!flp)
		goto out;
	remote_wwpn = get_unaligned_be64(&flp->fl_wwpn);
	if (remote_wwpn == lport->wwpn) {
734 735
		printk(KERN_WARNING "libfc: Received FLOGI from port "
		       "with same WWPN %llx\n", remote_wwpn);
736 737
		goto out;
	}
738
	FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn);
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784

	/*
	 * XXX what is the right thing to do for FIDs?
	 * The originator might expect our S_ID to be 0xfffffe.
	 * But if so, both of us could end up with the same FID.
	 */
	local_fid = FC_LOCAL_PTP_FID_LO;
	if (remote_wwpn < lport->wwpn) {
		local_fid = FC_LOCAL_PTP_FID_HI;
		if (!remote_fid || remote_fid == local_fid)
			remote_fid = FC_LOCAL_PTP_FID_LO;
	} else if (!remote_fid) {
		remote_fid = FC_LOCAL_PTP_FID_HI;
	}

	fc_host_port_id(lport->host) = local_fid;

	fp = fc_frame_alloc(lport, sizeof(*flp));
	if (fp) {
		sp = lport->tt.seq_start_next(fr_seq(rx_fp));
		new_flp = fc_frame_payload_get(fp, sizeof(*flp));
		fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI);
		new_flp->fl_cmd = (u8) ELS_LS_ACC;

		/*
		 * Send the response.  If this fails, the originator should
		 * repeat the sequence.
		 */
		f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ;
		ep = fc_seq_exch(sp);
		fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid,
			       FC_TYPE_ELS, f_ctl, 0);
		lport->tt.seq_send(lport, sp, fp);

	} else {
		fc_lport_error(lport, fp);
	}
	fc_lport_ptp_setup(lport, remote_fid, remote_wwpn,
			   get_unaligned_be64(&flp->fl_wwnn));

out:
	sp = fr_seq(rx_fp);
	fc_frame_free(rx_fp);
}

/**
785
 * fc_lport_recv_req() - The generic lport request handler
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
 * @lport: The lport that received the request
 * @sp: The sequence the request is on
 * @fp: The frame the request is in
 *
 * This function will see if the lport handles the request or
 * if an rport should handle the request.
 *
 * Locking Note: This function should not be called with the lport
 *		 lock held becuase it will grab the lock.
 */
static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp,
			      struct fc_frame *fp)
{
	struct fc_frame_header *fh = fc_frame_header_get(fp);
	void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *);

	mutex_lock(&lport->lp_mutex);

	/*
	 * Handle special ELS cases like FLOGI, LOGO, and
	 * RSCN here.  These don't require a session.
	 * Even if we had a session, it might not be ready.
	 */
809 810 811 812
	if (!lport->link_up)
		fc_frame_free(fp);
	else if (fh->fh_type == FC_TYPE_ELS &&
		 fh->fh_r_ctl == FC_RCTL_ELS_REQ) {
813 814 815
		/*
		 * Check opcode.
		 */
816
		recv = lport->tt.rport_recv_req;
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
		switch (fc_frame_payload_op(fp)) {
		case ELS_FLOGI:
			recv = fc_lport_recv_flogi_req;
			break;
		case ELS_LOGO:
			fh = fc_frame_header_get(fp);
			if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI)
				recv = fc_lport_recv_logo_req;
			break;
		case ELS_RSCN:
			recv = lport->tt.disc_recv_req;
			break;
		case ELS_ECHO:
			recv = fc_lport_recv_echo_req;
			break;
		case ELS_RLIR:
			recv = fc_lport_recv_rlir_req;
			break;
		case ELS_RNID:
			recv = fc_lport_recv_rnid_req;
			break;
		}

840
		recv(sp, fp, lport);
841
	} else {
842 843
		FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n",
			     fr_eof(fp));
844 845 846 847 848 849 850 851 852 853 854 855
		fc_frame_free(fp);
	}
	mutex_unlock(&lport->lp_mutex);

	/*
	 *  The common exch_done for all request may not be good
	 *  if any request requires longer hold on exhange. XXX
	 */
	lport->tt.exch_done(sp);
}

/**
856
 * fc_lport_reset() - Reset an lport
857 858 859 860 861 862 863
 * @lport: The lport which should be reset
 *
 * Locking Note: This functions should not be called with the
 *		 lport lock held.
 */
int fc_lport_reset(struct fc_lport *lport)
{
864
	cancel_delayed_work_sync(&lport->retry_work);
865 866 867 868 869 870 871 872
	mutex_lock(&lport->lp_mutex);
	fc_lport_enter_reset(lport);
	mutex_unlock(&lport->lp_mutex);
	return 0;
}
EXPORT_SYMBOL(fc_lport_reset);

/**
873
 * fc_lport_reset_locked() - Reset the local port
874 875 876 877 878
 * @lport: Fibre Channel local port to be reset
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
879
static void fc_lport_reset_locked(struct fc_lport *lport)
880 881 882 883
{
	if (lport->dns_rp)
		lport->tt.rport_logoff(lport->dns_rp);

884
	lport->ptp_rp = NULL;
885 886 887

	lport->tt.disc_stop(lport);

888
	lport->tt.exch_mgr_reset(lport, 0, 0);
889 890
	fc_host_fabric_name(lport->host) = 0;
	fc_host_port_id(lport->host) = 0;
891
}
892

893 894 895 896 897 898 899 900 901 902 903 904
/**
 * fc_lport_enter_reset() - Reset the local port
 * @lport: Fibre Channel local port to be reset
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
static void fc_lport_enter_reset(struct fc_lport *lport)
{
	FC_LPORT_DBG(lport, "Entered RESET state from %s state\n",
		     fc_lport_state(lport));

905 906 907 908 909 910
	if (lport->vport) {
		if (lport->link_up)
			fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING);
		else
			fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN);
	}
911
	fc_lport_state_enter(lport, LPORT_ST_RESET);
912
	fc_vports_linkchange(lport);
913
	fc_lport_reset_locked(lport);
914
	if (lport->link_up)
915 916 917
		fc_lport_enter_flogi(lport);
}

918 919 920 921 922 923 924 925 926 927 928 929 930
/**
 * fc_lport_enter_disabled() - disable the local port
 * @lport: Fibre Channel local port to be reset
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
static void fc_lport_enter_disabled(struct fc_lport *lport)
{
	FC_LPORT_DBG(lport, "Entered disabled state from %s state\n",
		     fc_lport_state(lport));

	fc_lport_state_enter(lport, LPORT_ST_DISABLED);
931
	fc_vports_linkchange(lport);
932 933 934
	fc_lport_reset_locked(lport);
}

935
/**
936
 * fc_lport_error() - Handler for any errors
937 938 939 940 941 942 943 944 945 946
 * @lport: The fc_lport object
 * @fp: The frame pointer
 *
 * If the error was caused by a resource allocation failure
 * then wait for half a second and retry, otherwise retry
 * after the e_d_tov time.
 */
static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp)
{
	unsigned long delay = 0;
947 948 949
	FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
		     PTR_ERR(fp), fc_lport_state(lport),
		     lport->retry_count);
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965

	if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
		/*
		 * Memory allocation failure, or the exchange timed out.
		 *  Retry after delay
		 */
		if (lport->retry_count < lport->max_retry_count) {
			lport->retry_count++;
			if (!fp)
				delay = msecs_to_jiffies(500);
			else
				delay =	msecs_to_jiffies(lport->e_d_tov);

			schedule_delayed_work(&lport->retry_work, delay);
		} else {
			switch (lport->state) {
966
			case LPORT_ST_DISABLED:
967 968
			case LPORT_ST_READY:
			case LPORT_ST_RESET:
969
			case LPORT_ST_RNN_ID:
970
			case LPORT_ST_RSNN_NN:
971
			case LPORT_ST_RSPN_ID:
972 973 974 975 976 977 978 979 980 981 982 983 984
			case LPORT_ST_RFT_ID:
			case LPORT_ST_SCR:
			case LPORT_ST_DNS:
			case LPORT_ST_FLOGI:
			case LPORT_ST_LOGO:
				fc_lport_enter_reset(lport);
				break;
			}
		}
	}
}

/**
985 986 987
 * fc_lport_ns_resp() - Handle response to a name server
 * 			registration exchange
 * @sp: current sequence in exchange
988 989 990 991 992 993 994
 * @fp: response frame
 * @lp_arg: Fibre Channel host port instance
 *
 * Locking Note: This function will be called without the lport lock
 * held, but it will lock, call an _enter_* function or fc_lport_error
 * and then unlock the lport.
 */
995 996
static void fc_lport_ns_resp(struct fc_seq *sp, struct fc_frame *fp,
			     void *lp_arg)
997 998 999 1000 1001
{
	struct fc_lport *lport = lp_arg;
	struct fc_frame_header *fh;
	struct fc_ct_hdr *ct;

1002
	FC_LPORT_DBG(lport, "Received a ns %s\n", fc_els_resp_type(fp));
1003

1004 1005 1006 1007 1008
	if (fp == ERR_PTR(-FC_EX_CLOSED))
		return;

	mutex_lock(&lport->lp_mutex);

1009 1010 1011
	if (lport->state < LPORT_ST_RNN_ID || lport->state > LPORT_ST_RFT_ID) {
		FC_LPORT_DBG(lport, "Received a name server response, "
				    "but in state %s\n", fc_lport_state(lport));
1012 1013
		if (IS_ERR(fp))
			goto err;
1014 1015 1016
		goto out;
	}

1017 1018 1019 1020 1021
	if (IS_ERR(fp)) {
		fc_lport_error(lport, fp);
		goto err;
	}

1022 1023 1024 1025 1026 1027 1028
	fh = fc_frame_header_get(fp);
	ct = fc_frame_payload_get(fp, sizeof(*ct));

	if (fh && ct && fh->fh_type == FC_TYPE_CT &&
	    ct->ct_fs_type == FC_FST_DIR &&
	    ct->ct_fs_subtype == FC_NS_SUBTYPE &&
	    ntohs(ct->ct_cmd) == FC_FS_ACC)
1029 1030
		switch (lport->state) {
		case LPORT_ST_RNN_ID:
1031
			fc_lport_enter_ns(lport, LPORT_ST_RSNN_NN);
1032 1033
			break;
		case LPORT_ST_RSNN_NN:
1034
			fc_lport_enter_ns(lport, LPORT_ST_RSPN_ID);
1035 1036
			break;
		case LPORT_ST_RSPN_ID:
1037
			fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
1038 1039 1040 1041 1042 1043 1044 1045
			break;
		case LPORT_ST_RFT_ID:
			fc_lport_enter_scr(lport);
			break;
		default:
			/* should have already been caught by state checks */
			break;
		}
1046 1047 1048 1049 1050 1051 1052 1053
	else
		fc_lport_error(lport, fp);
out:
	fc_frame_free(fp);
err:
	mutex_unlock(&lport->lp_mutex);
}

1054
/**
1055
 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
 * @sp: current sequence in SCR exchange
 * @fp: response frame
 * @lp_arg: Fibre Channel lport port instance that sent the registration request
 *
 * Locking Note: This function will be called without the lport lock
 * held, but it will lock, call an _enter_* function or fc_lport_error
 * and then unlock the lport.
 */
static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp,
			      void *lp_arg)
{
	struct fc_lport *lport = lp_arg;
	u8 op;

1070 1071
	FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp));

1072 1073 1074 1075 1076 1077
	if (fp == ERR_PTR(-FC_EX_CLOSED))
		return;

	mutex_lock(&lport->lp_mutex);

	if (lport->state != LPORT_ST_SCR) {
1078 1079
		FC_LPORT_DBG(lport, "Received a SCR response, but in state "
			     "%s\n", fc_lport_state(lport));
1080 1081
		if (IS_ERR(fp))
			goto err;
1082 1083 1084
		goto out;
	}

1085 1086 1087 1088 1089
	if (IS_ERR(fp)) {
		fc_lport_error(lport, fp);
		goto err;
	}

1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
	op = fc_frame_payload_op(fp);
	if (op == ELS_LS_ACC)
		fc_lport_enter_ready(lport);
	else
		fc_lport_error(lport, fp);

out:
	fc_frame_free(fp);
err:
	mutex_unlock(&lport->lp_mutex);
}

/**
1103
 * fc_lport_enter_scr() - Send a State Change Register (SCR) request
1104 1105 1106 1107 1108 1109 1110 1111 1112
 * @lport: Fibre Channel local port to register for state changes
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
static void fc_lport_enter_scr(struct fc_lport *lport)
{
	struct fc_frame *fp;

1113 1114
	FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
		     fc_lport_state(lport));
1115 1116 1117 1118 1119 1120 1121 1122 1123

	fc_lport_state_enter(lport, LPORT_ST_SCR);

	fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr));
	if (!fp) {
		fc_lport_error(lport, fp);
		return;
	}

1124
	if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
1125
				  fc_lport_scr_resp, lport, lport->e_d_tov))
1126
		fc_lport_error(lport, NULL);
1127 1128 1129
}

/**
1130
 * fc_lport_enter_ns() - register some object with the name server
1131 1132 1133 1134 1135
 * @lport: Fibre Channel local port to register
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
1136
static void fc_lport_enter_ns(struct fc_lport *lport, enum fc_lport_state state)
1137 1138
{
	struct fc_frame *fp;
1139 1140
	enum fc_ns_req cmd;
	int size = sizeof(struct fc_ct_hdr);
1141 1142
	size_t len;

1143 1144
	FC_LPORT_DBG(lport, "Entered %s state from %s state\n",
		     fc_lport_state_names[state],
1145 1146
		     fc_lport_state(lport));

1147
	fc_lport_state_enter(lport, state);
1148

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
	switch (state) {
	case LPORT_ST_RNN_ID:
		cmd = FC_NS_RNN_ID;
		size += sizeof(struct fc_ns_rn_id);
		break;
	case LPORT_ST_RSNN_NN:
		len = strnlen(fc_host_symbolic_name(lport->host), 255);
		/* if there is no symbolic name, skip to RFT_ID */
		if (!len)
			return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
		cmd = FC_NS_RSNN_NN;
		size += sizeof(struct fc_ns_rsnn) + len;
		break;
	case LPORT_ST_RSPN_ID:
		len = strnlen(fc_host_symbolic_name(lport->host), 255);
		/* if there is no symbolic name, skip to RFT_ID */
		if (!len)
			return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
		cmd = FC_NS_RSPN_ID;
		size += sizeof(struct fc_ns_rspn) + len;
		break;
	case LPORT_ST_RFT_ID:
		cmd = FC_NS_RFT_ID;
		size += sizeof(struct fc_ns_rft);
		break;
	default:
		fc_lport_error(lport, NULL);
1176 1177 1178
		return;
	}

1179
	fp = fc_frame_alloc(lport, size);
1180 1181 1182 1183 1184
	if (!fp) {
		fc_lport_error(lport, fp);
		return;
	}

1185
	if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, cmd,
1186
				  fc_lport_ns_resp,
1187 1188 1189 1190
				  lport, lport->e_d_tov))
		fc_lport_error(lport, fp);
}

1191 1192 1193 1194 1195
static struct fc_rport_operations fc_lport_rport_ops = {
	.event_callback = fc_lport_rport_callback,
};

/**
1196
 * fc_rport_enter_dns() - Create a rport to the name server
1197 1198 1199 1200 1201 1202 1203
 * @lport: Fibre Channel local port requesting a rport for the name server
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
static void fc_lport_enter_dns(struct fc_lport *lport)
{
1204
	struct fc_rport_priv *rdata;
1205

1206 1207
	FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
		     fc_lport_state(lport));
1208 1209 1210

	fc_lport_state_enter(lport, LPORT_ST_DNS);

1211
	mutex_lock(&lport->disc.disc_mutex);
1212
	rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
1213
	mutex_unlock(&lport->disc.disc_mutex);
1214
	if (!rdata)
1215 1216 1217
		goto err;

	rdata->ops = &fc_lport_rport_ops;
1218
	lport->tt.rport_login(rdata);
1219 1220 1221 1222 1223 1224 1225
	return;

err:
	fc_lport_error(lport, NULL);
}

/**
1226
 * fc_lport_timeout() - Handler for the retry_work timer.
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
 * @work: The work struct of the fc_lport
 */
static void fc_lport_timeout(struct work_struct *work)
{
	struct fc_lport *lport =
		container_of(work, struct fc_lport,
			     retry_work.work);

	mutex_lock(&lport->lp_mutex);

	switch (lport->state) {
1238
	case LPORT_ST_DISABLED:
1239 1240
		WARN_ON(1);
		break;
1241 1242 1243
	case LPORT_ST_READY:
		WARN_ON(1);
		break;
1244 1245
	case LPORT_ST_RESET:
		break;
1246 1247 1248 1249 1250 1251
	case LPORT_ST_FLOGI:
		fc_lport_enter_flogi(lport);
		break;
	case LPORT_ST_DNS:
		fc_lport_enter_dns(lport);
		break;
1252
	case LPORT_ST_RNN_ID:
1253
	case LPORT_ST_RSNN_NN:
1254
	case LPORT_ST_RSPN_ID:
1255
	case LPORT_ST_RFT_ID:
1256
		fc_lport_enter_ns(lport, lport->state);
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
		break;
	case LPORT_ST_SCR:
		fc_lport_enter_scr(lport);
		break;
	case LPORT_ST_LOGO:
		fc_lport_enter_logo(lport);
		break;
	}

	mutex_unlock(&lport->lp_mutex);
}

/**
1270
 * fc_lport_logo_resp() - Handle response to LOGO request
1271 1272 1273 1274 1275 1276 1277 1278
 * @sp: current sequence in LOGO exchange
 * @fp: response frame
 * @lp_arg: Fibre Channel lport port instance that sent the LOGO request
 *
 * Locking Note: This function will be called without the lport lock
 * held, but it will lock, call an _enter_* function or fc_lport_error
 * and then unlock the lport.
 */
1279
void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1280 1281 1282 1283 1284
			       void *lp_arg)
{
	struct fc_lport *lport = lp_arg;
	u8 op;

1285 1286
	FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp));

1287 1288 1289 1290 1291 1292
	if (fp == ERR_PTR(-FC_EX_CLOSED))
		return;

	mutex_lock(&lport->lp_mutex);

	if (lport->state != LPORT_ST_LOGO) {
1293 1294
		FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
			     "%s\n", fc_lport_state(lport));
1295 1296
		if (IS_ERR(fp))
			goto err;
1297 1298 1299
		goto out;
	}

1300 1301 1302 1303 1304
	if (IS_ERR(fp)) {
		fc_lport_error(lport, fp);
		goto err;
	}

1305 1306
	op = fc_frame_payload_op(fp);
	if (op == ELS_LS_ACC)
1307
		fc_lport_enter_disabled(lport);
1308 1309 1310 1311 1312 1313 1314 1315
	else
		fc_lport_error(lport, fp);

out:
	fc_frame_free(fp);
err:
	mutex_unlock(&lport->lp_mutex);
}
1316
EXPORT_SYMBOL(fc_lport_logo_resp);
1317 1318

/**
1319
 * fc_rport_enter_logo() - Logout of the fabric
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
 * @lport: Fibre Channel local port to be logged out
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
static void fc_lport_enter_logo(struct fc_lport *lport)
{
	struct fc_frame *fp;
	struct fc_els_logo *logo;

1330 1331
	FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
		     fc_lport_state(lport));
1332 1333

	fc_lport_state_enter(lport, LPORT_ST_LOGO);
1334
	fc_vports_linkchange(lport);
1335 1336 1337 1338 1339 1340 1341

	fp = fc_frame_alloc(lport, sizeof(*logo));
	if (!fp) {
		fc_lport_error(lport, fp);
		return;
	}

1342 1343
	if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
				  fc_lport_logo_resp, lport, lport->e_d_tov))
1344
		fc_lport_error(lport, NULL);
1345 1346 1347
}

/**
1348
 * fc_lport_flogi_resp() - Handle response to FLOGI request
1349 1350 1351 1352 1353 1354 1355 1356
 * @sp: current sequence in FLOGI exchange
 * @fp: response frame
 * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request
 *
 * Locking Note: This function will be called without the lport lock
 * held, but it will lock, call an _enter_* function or fc_lport_error
 * and then unlock the lport.
 */
1357
void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
				void *lp_arg)
{
	struct fc_lport *lport = lp_arg;
	struct fc_frame_header *fh;
	struct fc_els_flogi *flp;
	u32 did;
	u16 csp_flags;
	unsigned int r_a_tov;
	unsigned int e_d_tov;
	u16 mfs;

1369 1370
	FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp));

1371 1372 1373 1374 1375 1376
	if (fp == ERR_PTR(-FC_EX_CLOSED))
		return;

	mutex_lock(&lport->lp_mutex);

	if (lport->state != LPORT_ST_FLOGI) {
1377 1378
		FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
			     "%s\n", fc_lport_state(lport));
1379 1380
		if (IS_ERR(fp))
			goto err;
1381 1382 1383
		goto out;
	}

1384 1385 1386 1387 1388
	if (IS_ERR(fp)) {
		fc_lport_error(lport, fp);
		goto err;
	}

1389 1390 1391 1392
	fh = fc_frame_header_get(fp);
	did = ntoh24(fh->fh_d_id);
	if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) {

1393 1394
		printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n",
		       did);
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
		fc_host_port_id(lport->host) = did;

		flp = fc_frame_payload_get(fp, sizeof(*flp));
		if (flp) {
			mfs = ntohs(flp->fl_csp.sp_bb_data) &
				FC_SP_BB_DATA_MASK;
			if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
			    mfs < lport->mfs)
				lport->mfs = mfs;
			csp_flags = ntohs(flp->fl_csp.sp_features);
			r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov);
			e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov);
			if (csp_flags & FC_SP_FT_EDTR)
				e_d_tov /= 1000000;
1409 1410 1411

			lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC);

1412 1413 1414 1415
			if ((csp_flags & FC_SP_FT_FPORT) == 0) {
				if (e_d_tov > lport->e_d_tov)
					lport->e_d_tov = e_d_tov;
				lport->r_a_tov = 2 * e_d_tov;
1416 1417
				printk(KERN_INFO "libfc: Port (%6x) entered "
				       "point to point mode\n", did);
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
				fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id),
						   get_unaligned_be64(
							   &flp->fl_wwpn),
						   get_unaligned_be64(
							   &flp->fl_wwnn));
			} else {
				lport->e_d_tov = e_d_tov;
				lport->r_a_tov = r_a_tov;
				fc_host_fabric_name(lport->host) =
					get_unaligned_be64(&flp->fl_wwnn);
				fc_lport_enter_dns(lport);
			}
		}
	} else {
1432
		FC_LPORT_DBG(lport, "Bad FLOGI response\n");
1433 1434 1435 1436 1437 1438 1439
	}

out:
	fc_frame_free(fp);
err:
	mutex_unlock(&lport->lp_mutex);
}
1440
EXPORT_SYMBOL(fc_lport_flogi_resp);
1441 1442

/**
1443
 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
1444 1445 1446 1447 1448 1449 1450 1451 1452
 * @lport: Fibre Channel local port to be logged in to the fabric
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
void fc_lport_enter_flogi(struct fc_lport *lport)
{
	struct fc_frame *fp;

1453 1454
	FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
		     fc_lport_state(lport));
1455 1456 1457 1458 1459 1460 1461

	fc_lport_state_enter(lport, LPORT_ST_FLOGI);

	fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
	if (!fp)
		return fc_lport_error(lport, fp);

1462 1463
	if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
				  lport->vport ? ELS_FDISC : ELS_FLOGI,
1464
				  fc_lport_flogi_resp, lport, lport->e_d_tov))
1465
		fc_lport_error(lport, NULL);
1466 1467 1468 1469 1470 1471 1472 1473
}

/* Configure a fc_lport */
int fc_lport_config(struct fc_lport *lport)
{
	INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
	mutex_init(&lport->lp_mutex);

1474
	fc_lport_state_enter(lport, LPORT_ST_DISABLED);
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514

	fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
	fc_lport_add_fc4_type(lport, FC_TYPE_CT);

	return 0;
}
EXPORT_SYMBOL(fc_lport_config);

int fc_lport_init(struct fc_lport *lport)
{
	if (!lport->tt.lport_recv)
		lport->tt.lport_recv = fc_lport_recv_req;

	if (!lport->tt.lport_reset)
		lport->tt.lport_reset = fc_lport_reset;

	fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
	fc_host_node_name(lport->host) = lport->wwnn;
	fc_host_port_name(lport->host) = lport->wwpn;
	fc_host_supported_classes(lport->host) = FC_COS_CLASS3;
	memset(fc_host_supported_fc4s(lport->host), 0,
	       sizeof(fc_host_supported_fc4s(lport->host)));
	fc_host_supported_fc4s(lport->host)[2] = 1;
	fc_host_supported_fc4s(lport->host)[7] = 1;

	/* This value is also unchanging */
	memset(fc_host_active_fc4s(lport->host), 0,
	       sizeof(fc_host_active_fc4s(lport->host)));
	fc_host_active_fc4s(lport->host)[2] = 1;
	fc_host_active_fc4s(lport->host)[7] = 1;
	fc_host_maxframe_size(lport->host) = lport->mfs;
	fc_host_supported_speeds(lport->host) = 0;
	if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT)
		fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT;
	if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT)
		fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT;

	return 0;
}
EXPORT_SYMBOL(fc_lport_init);