fc_lport.c 56.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
/*
 * 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.
 *
35
 * HIERARCHY
36
 *
37
 * The following hierarchy defines the locking rules. A greater lock
38
 * may be held before acquiring a lesser lock, but a lesser lock should never
39
 * be held while attempting to acquire a greater lock. Here is the hierarchy-
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 *
 * 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
L
Lucas De Marchi 已提交
55
 * callback handler because no other rport work in this queue can be executed
56 57 58
 * at the same time.
 *
 * When discovery succeeds or fails a callback is made to the lport as
59
 * notification. Currently, successful discovery causes the lport to take no
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
 * 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>
V
Vasu Dev 已提交
91
#include <linux/delay.h>
92
#include <linux/module.h>
93
#include <linux/slab.h>
94 95 96 97 98 99
#include <asm/unaligned.h>

#include <scsi/fc/fc_gs.h>

#include <scsi/libfc.h>
#include <scsi/fc_encode.h>
100
#include <linux/scatterlist.h>
101

102 103
#include "fc_libfc.h"

104 105 106 107 108 109 110 111 112 113 114
/* 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 *);
115
static void fc_lport_enter_ns(struct fc_lport *, enum fc_lport_state);
116 117 118
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 *);
119 120
static void fc_lport_enter_fdmi(struct fc_lport *lport);
static void fc_lport_enter_ms(struct fc_lport *, enum fc_lport_state);
121 122

static const char *fc_lport_state_names[] = {
123
	[LPORT_ST_DISABLED] = "disabled",
124 125
	[LPORT_ST_FLOGI] =    "FLOGI",
	[LPORT_ST_DNS] =      "dNS",
126
	[LPORT_ST_RNN_ID] =   "RNN_ID",
127
	[LPORT_ST_RSNN_NN] =  "RSNN_NN",
128
	[LPORT_ST_RSPN_ID] =  "RSPN_ID",
129
	[LPORT_ST_RFT_ID] =   "RFT_ID",
130
	[LPORT_ST_RFF_ID] =   "RFF_ID",
131 132 133 134 135
	[LPORT_ST_FDMI] =     "FDMI",
	[LPORT_ST_RHBA] =     "RHBA",
	[LPORT_ST_RPA] =      "RPA",
	[LPORT_ST_DHBA] =     "DHBA",
	[LPORT_ST_DPRT] =     "DPRT",
136 137 138 139 140 141
	[LPORT_ST_SCR] =      "SCR",
	[LPORT_ST_READY] =    "Ready",
	[LPORT_ST_LOGO] =     "LOGO",
	[LPORT_ST_RESET] =    "reset",
};

142 143 144 145 146
/**
 * struct fc_bsg_info - FC Passthrough managemet structure
 * @job:      The passthrough job
 * @lport:    The local port to pass through a command
 * @rsp_code: The expected response code
147
 * @sg:	      job->reply_payload.sg_list
148 149 150 151 152 153 154 155 156 157 158 159
 * @nents:    job->reply_payload.sg_cnt
 * @offset:   The offset into the response data
 */
struct fc_bsg_info {
	struct fc_bsg_job *job;
	struct fc_lport *lport;
	u16 rsp_code;
	struct scatterlist *sg;
	u32 nents;
	size_t offset;
};

160 161 162 163 164
/**
 * fc_frame_drop() - Dummy frame handler
 * @lport: The local port the frame was received on
 * @fp:	   The received frame
 */
165 166 167 168 169 170 171
static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp)
{
	fc_frame_free(fp);
	return 0;
}

/**
172
 * fc_lport_rport_callback() - Event handler for rport events
173
 * @lport: The lport which is receiving the event
174
 * @rdata: private remote port data
L
Lucas De Marchi 已提交
175
 * @event: The event that occurred
176 177 178 179 180
 *
 * Locking Note: The rport lock should not be held when calling
 *		 this function.
 */
static void fc_lport_rport_callback(struct fc_lport *lport,
181
				    struct fc_rport_priv *rdata,
182 183
				    enum fc_rport_event event)
{
184
	FC_LPORT_DBG(lport, "Received a %d event for port (%6.6x)\n", event,
185
		     rdata->ids.port_id);
186

187
	mutex_lock(&lport->lp_mutex);
188
	switch (event) {
189
	case RPORT_EV_READY:
190
		if (lport->state == LPORT_ST_DNS) {
191
			lport->dns_rdata = rdata;
192
			fc_lport_enter_ns(lport, LPORT_ST_RNN_ID);
193 194 195
		} else if (lport->state == LPORT_ST_FDMI) {
			lport->ms_rdata = rdata;
			fc_lport_enter_ms(lport, LPORT_ST_DHBA);
196 197
		} else {
			FC_LPORT_DBG(lport, "Received an READY event "
198
				     "on port (%6.6x) for the directory "
199
				     "server, but the lport is not "
200
				     "in the DNS or FDMI state, it's in the "
201 202 203 204
				     "%d state", rdata->ids.port_id,
				     lport->state);
			lport->tt.rport_logoff(rdata);
		}
205 206 207 208
		break;
	case RPORT_EV_LOGO:
	case RPORT_EV_FAILED:
	case RPORT_EV_STOP:
209 210 211 212
		if (rdata->ids.port_id == FC_FID_DIR_SERV)
			lport->dns_rdata = NULL;
		else if (rdata->ids.port_id == FC_FID_MGMT_SERV)
			lport->ms_rdata = NULL;
213 214 215 216
		break;
	case RPORT_EV_NONE:
		break;
	}
217
	mutex_unlock(&lport->lp_mutex);
218 219 220
}

/**
221
 * fc_lport_state() - Return a string which represents the lport's state
222 223 224 225 226 227 228 229 230 231 232 233 234
 * @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;
}

/**
235
 * fc_lport_ptp_setup() - Create an rport for point-to-point mode
236 237
 * @lport:	 The lport to attach the ptp rport to
 * @remote_fid:	 The FID of the ptp rport
238 239 240 241 242 243 244
 * @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)
{
245
	mutex_lock(&lport->disc.disc_mutex);
246
	if (lport->ptp_rdata) {
247
		lport->tt.rport_logoff(lport->ptp_rdata);
248 249
		kref_put(&lport->ptp_rdata->kref, lport->tt.rport_destroy);
	}
250
	lport->ptp_rdata = lport->tt.rport_create(lport, remote_fid);
251
	kref_get(&lport->ptp_rdata->kref);
252 253
	lport->ptp_rdata->ids.port_name = remote_wwpn;
	lport->ptp_rdata->ids.node_name = remote_wwnn;
254
	mutex_unlock(&lport->disc.disc_mutex);
255

256
	lport->tt.rport_login(lport->ptp_rdata);
257 258 259 260

	fc_lport_enter_ready(lport);
}

261 262 263 264
/**
 * fc_get_host_port_state() - Return the port state of the given Scsi_Host
 * @shost:  The SCSI host whose port state is to be determined
 */
265 266
void fc_get_host_port_state(struct Scsi_Host *shost)
{
267
	struct fc_lport *lport = shost_priv(shost);
268

269 270
	mutex_lock(&lport->lp_mutex);
	if (!lport->link_up)
271
		fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
272
	else
273
		switch (lport->state) {
274 275 276 277 278 279
		case LPORT_ST_READY:
			fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
			break;
		default:
			fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
		}
280
	mutex_unlock(&lport->lp_mutex);
281 282 283
}
EXPORT_SYMBOL(fc_get_host_port_state);

284 285 286 287
/**
 * fc_get_host_speed() - Return the speed of the given Scsi_Host
 * @shost: The SCSI host whose port speed is to be determined
 */
288 289 290 291 292 293 294 295
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);

296 297 298 299
/**
 * fc_get_host_stats() - Return the Scsi_Host's statistics
 * @shost: The SCSI host whose statistics are to be returned
 */
300 301 302
struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost)
{
	struct fc_host_statistics *fcoe_stats;
303
	struct fc_lport *lport = shost_priv(shost);
304
	struct timespec v0, v1;
305
	unsigned int cpu;
306 307
	u64 fcp_in_bytes = 0;
	u64 fcp_out_bytes = 0;
308

309
	fcoe_stats = &lport->host_stats;
310 311 312
	memset(fcoe_stats, 0, sizeof(struct fc_host_statistics));

	jiffies_to_timespec(jiffies, &v0);
313
	jiffies_to_timespec(lport->boot_time, &v1);
314 315
	fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec);

316 317 318
	for_each_possible_cpu(cpu) {
		struct fcoe_dev_stats *stats;

319
		stats = per_cpu_ptr(lport->dev_stats, cpu);
320

321 322 323 324 325 326 327 328 329
		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;
330 331
		fcp_in_bytes += stats->InputBytes;
		fcp_out_bytes += stats->OutputBytes;
332 333
		fcoe_stats->link_failure_count += stats->LinkFailureCount;
	}
334 335
	fcoe_stats->fcp_input_megabytes = div_u64(fcp_in_bytes, 1000000);
	fcoe_stats->fcp_output_megabytes = div_u64(fcp_out_bytes, 1000000);
336 337 338 339 340 341 342 343 344 345
	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);

346 347 348 349 350
/**
 * fc_lport_flogi_fill() - Fill in FLOGI command for request
 * @lport: The local port the FLOGI is for
 * @flogi: The FLOGI command
 * @op:	   The opcode
351
 */
352 353 354
static void fc_lport_flogi_fill(struct fc_lport *lport,
				struct fc_els_flogi *flogi,
				unsigned int op)
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
{
	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;
	}
}

382 383 384 385
/**
 * fc_lport_add_fc4_type() - Add a supported FC-4 type to a local port
 * @lport: The local port to add a new FC-4 type to
 * @type:  The new FC-4 type
386 387 388 389 390 391 392 393 394 395
 */
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));
}

/**
396
 * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report.
L
Lucas De Marchi 已提交
397
 * @lport: Fibre Channel local port receiving the RLIR
398
 * @fp:	   The RLIR request frame
399
 *
400
 * Locking Note: The lport lock is expected to be held before calling
401 402
 * this function.
 */
403
static void fc_lport_recv_rlir_req(struct fc_lport *lport, struct fc_frame *fp)
404
{
405 406
	FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n",
		     fc_lport_state(lport));
407

408
	lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
409 410 411 412
	fc_frame_free(fp);
}

/**
413
 * fc_lport_recv_echo_req() - Handle received ECHO request
L
Lucas De Marchi 已提交
414
 * @lport: The local port receiving the ECHO
415
 * @fp:	   ECHO request frame
416
 *
417
 * Locking Note: The lport lock is expected to be held before calling
418 419
 * this function.
 */
420 421
static void fc_lport_recv_echo_req(struct fc_lport *lport,
				   struct fc_frame *in_fp)
422 423 424 425 426 427
{
	struct fc_frame *fp;
	unsigned int len;
	void *pp;
	void *dp;

428
	FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n",
429
		     fc_lport_state(lport));
430 431 432 433 434 435 436 437 438 439 440

	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);
441
		*((__be32 *)dp) = htonl(ELS_LS_ACC << 24);
442 443
		fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
		lport->tt.frame_send(lport, fp);
444 445 446 447 448
	}
	fc_frame_free(in_fp);
}

/**
449
 * fc_lport_recv_rnid_req() - Handle received Request Node ID data request
L
Lucas De Marchi 已提交
450
 * @lport: The local port receiving the RNID
451
 * @fp:	   The RNID request frame
452
 *
453
 * Locking Note: The lport lock is expected to be held before calling
454 455
 * this function.
 */
456 457
static void fc_lport_recv_rnid_req(struct fc_lport *lport,
				   struct fc_frame *in_fp)
458 459 460 461 462 463 464 465 466 467 468 469
{
	struct fc_frame *fp;
	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;

470 471
	FC_LPORT_DBG(lport, "Received RNID request while in state %s\n",
		     fc_lport_state(lport));
472 473 474 475 476

	req = fc_frame_payload_get(in_fp, sizeof(*req));
	if (!req) {
		rjt_data.reason = ELS_RJT_LOGIC;
		rjt_data.explan = ELS_EXPL_NONE;
477
		lport->tt.seq_els_rsp_send(in_fp, ELS_LS_RJT, &rjt_data);
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
	} 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));
			}
500 501
			fc_fill_reply_hdr(fp, in_fp, FC_RCTL_ELS_REP, 0);
			lport->tt.frame_send(lport, fp);
502 503 504 505 506 507
		}
	}
	fc_frame_free(in_fp);
}

/**
508
 * fc_lport_recv_logo_req() - Handle received fabric LOGO request
L
Lucas De Marchi 已提交
509
 * @lport: The local port receiving the LOGO
510
 * @fp:	   The LOGO request frame
511 512 513 514
 *
 * Locking Note: The lport lock is exected to be held before calling
 * this function.
 */
515
static void fc_lport_recv_logo_req(struct fc_lport *lport, struct fc_frame *fp)
516
{
517
	lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
518 519 520 521 522
	fc_lport_enter_reset(lport);
	fc_frame_free(fp);
}

/**
523
 * fc_fabric_login() - Start the lport state machine
524
 * @lport: The local port that should log into the fabric
525 526 527 528 529 530 531 532 533
 *
 * 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);
534 535 536
	if (lport->state == LPORT_ST_DISABLED ||
	    lport->state == LPORT_ST_LOGO) {
		fc_lport_state_enter(lport, LPORT_ST_RESET);
537 538 539 540 541 542 543 544 545 546
		fc_lport_enter_reset(lport);
		rc = 0;
	}
	mutex_unlock(&lport->lp_mutex);

	return rc;
}
EXPORT_SYMBOL(fc_fabric_login);

/**
547
 * __fc_linkup() - Handler for transport linkup events
548
 * @lport: The lport whose link is up
549 550
 *
 * Locking: must be called with the lp_mutex held
551
 */
552
void __fc_linkup(struct fc_lport *lport)
553
{
554 555
	if (!lport->link_up) {
		lport->link_up = 1;
556 557 558 559

		if (lport->state == LPORT_ST_RESET)
			fc_lport_enter_flogi(lport);
	}
560 561 562 563
}

/**
 * fc_linkup() - Handler for transport linkup events
564
 * @lport: The local port whose link is up
565 566 567
 */
void fc_linkup(struct fc_lport *lport)
{
568
	printk(KERN_INFO "host%d: libfc: Link up on port (%6.6x)\n",
569
	       lport->host->host_no, lport->port_id);
570 571 572

	mutex_lock(&lport->lp_mutex);
	__fc_linkup(lport);
573 574 575 576 577
	mutex_unlock(&lport->lp_mutex);
}
EXPORT_SYMBOL(fc_linkup);

/**
578
 * __fc_linkdown() - Handler for transport linkdown events
579
 * @lport: The lport whose link is down
580 581
 *
 * Locking: must be called with the lp_mutex held
582
 */
583
void __fc_linkdown(struct fc_lport *lport)
584
{
585 586
	if (lport->link_up) {
		lport->link_up = 0;
587 588 589
		fc_lport_enter_reset(lport);
		lport->tt.fcp_cleanup(lport);
	}
590 591 592 593
}

/**
 * fc_linkdown() - Handler for transport linkdown events
594
 * @lport: The local port whose link is down
595 596 597
 */
void fc_linkdown(struct fc_lport *lport)
{
598
	printk(KERN_INFO "host%d: libfc: Link down on port (%6.6x)\n",
599
	       lport->host->host_no, lport->port_id);
600 601 602

	mutex_lock(&lport->lp_mutex);
	__fc_linkdown(lport);
603 604 605 606 607
	mutex_unlock(&lport->lp_mutex);
}
EXPORT_SYMBOL(fc_linkdown);

/**
608
 * fc_fabric_logoff() - Logout of the fabric
609
 * @lport: The local port to logoff the fabric
610 611 612
 *
 * Return value:
 *	0 for success, -1 for failure
613
 */
614 615 616 617
int fc_fabric_logoff(struct fc_lport *lport)
{
	lport->tt.disc_stop_final(lport);
	mutex_lock(&lport->lp_mutex);
618 619
	if (lport->dns_rdata)
		lport->tt.rport_logoff(lport->dns_rdata);
620 621 622
	mutex_unlock(&lport->lp_mutex);
	lport->tt.rport_flush_queue();
	mutex_lock(&lport->lp_mutex);
623 624
	fc_lport_enter_logo(lport);
	mutex_unlock(&lport->lp_mutex);
625
	cancel_delayed_work_sync(&lport->retry_work);
626 627 628 629 630
	return 0;
}
EXPORT_SYMBOL(fc_fabric_logoff);

/**
631 632
 * fc_lport_destroy() - Unregister a fc_lport
 * @lport: The local port to unregister
633 634 635 636 637 638
 *
 * Note:
 * exit routine for fc_lport instance
 * clean-up all the allocated memory
 * and free up other system resources.
 *
639
 */
640 641
int fc_lport_destroy(struct fc_lport *lport)
{
642
	mutex_lock(&lport->lp_mutex);
643
	lport->state = LPORT_ST_DISABLED;
644
	lport->link_up = 0;
645
	lport->tt.frame_send = fc_frame_drop;
646 647
	mutex_unlock(&lport->lp_mutex);

648
	lport->tt.fcp_abort_io(lport);
649
	lport->tt.disc_stop_final(lport);
650
	lport->tt.exch_mgr_reset(lport, 0, 0);
651
	fc_fc4_del_lport(lport);
652 653 654 655 656
	return 0;
}
EXPORT_SYMBOL(fc_lport_destroy);

/**
657 658 659
 * fc_set_mfs() - Set the maximum frame size for a local port
 * @lport: The local port to set the MFS for
 * @mfs:   The new MFS
660
 */
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
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);

/**
689
 * fc_lport_disc_callback() - Callback for discovery events
690
 * @lport: The local port receiving the event
691 692
 * @event: The discovery event
 */
693 694
static void fc_lport_disc_callback(struct fc_lport *lport,
				   enum fc_disc_event event)
695 696 697
{
	switch (event) {
	case DISC_EV_SUCCESS:
698
		FC_LPORT_DBG(lport, "Discovery succeeded\n");
699 700
		break;
	case DISC_EV_FAILED:
701
		printk(KERN_ERR "host%d: libfc: "
702
		       "Discovery failed for port (%6.6x)\n",
703
		       lport->host->host_no, lport->port_id);
704 705 706 707 708 709 710 711 712 713 714
		mutex_lock(&lport->lp_mutex);
		fc_lport_enter_reset(lport);
		mutex_unlock(&lport->lp_mutex);
		break;
	case DISC_EV_NONE:
		WARN_ON(1);
		break;
	}
}

/**
715
 * fc_rport_enter_ready() - Enter the ready state and start discovery
716
 * @lport: The local port that is ready
717 718 719 720 721 722
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
static void fc_lport_enter_ready(struct fc_lport *lport)
{
723 724
	FC_LPORT_DBG(lport, "Entered READY from state %s\n",
		     fc_lport_state(lport));
725 726

	fc_lport_state_enter(lport, LPORT_ST_READY);
727 728 729
	if (lport->vport)
		fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE);
	fc_vports_linkchange(lport);
730

731
	if (!lport->ptp_rdata)
732
		lport->tt.disc_start(fc_lport_disc_callback, lport);
733 734
}

735 736 737 738 739 740 741 742 743 744 745 746 747
/**
 * fc_lport_set_port_id() - set the local port Port ID
 * @lport: The local port which will have its Port ID set.
 * @port_id: The new port ID.
 * @fp: The frame containing the incoming request, or NULL.
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this function.
 */
static void fc_lport_set_port_id(struct fc_lport *lport, u32 port_id,
				 struct fc_frame *fp)
{
	if (port_id)
748
		printk(KERN_INFO "host%d: Assigned Port ID %6.6x\n",
749 750
		       lport->host->host_no, port_id);

751 752 753
	lport->port_id = port_id;

	/* Update the fc_host */
754
	fc_host_port_id(lport->host) = port_id;
755

756 757 758 759
	if (lport->tt.lport_set_port_id)
		lport->tt.lport_set_port_id(lport, port_id, fp);
}

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 785 786 787
/**
 * fc_lport_set_port_id() - set the local port Port ID for point-to-multipoint
 * @lport: The local port which will have its Port ID set.
 * @port_id: The new port ID.
 *
 * Called by the lower-level driver when transport sets the local port_id.
 * This is used in VN_port to VN_port mode for FCoE, and causes FLOGI and
 * discovery to be skipped.
 */
void fc_lport_set_local_id(struct fc_lport *lport, u32 port_id)
{
	mutex_lock(&lport->lp_mutex);

	fc_lport_set_port_id(lport, port_id, NULL);

	switch (lport->state) {
	case LPORT_ST_RESET:
	case LPORT_ST_FLOGI:
		if (port_id)
			fc_lport_enter_ready(lport);
		break;
	default:
		break;
	}
	mutex_unlock(&lport->lp_mutex);
}
EXPORT_SYMBOL(fc_lport_set_local_id);

788
/**
789
 * fc_lport_recv_flogi_req() - Receive a FLOGI request
L
Lucas De Marchi 已提交
790
 * @lport: The local port that received the request
791
 * @rx_fp: The FLOGI frame
792 793 794 795 796
 *
 * 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.
 *
797
 * Locking Note: The lport lock is expected to be held before calling
798 799
 * this function.
 */
800 801
static void fc_lport_recv_flogi_req(struct fc_lport *lport,
				    struct fc_frame *rx_fp)
802 803
{
	struct fc_frame *fp;
804
	struct fc_frame_header *fh;
805 806 807 808 809 810
	struct fc_els_flogi *flp;
	struct fc_els_flogi *new_flp;
	u64 remote_wwpn;
	u32 remote_fid;
	u32 local_fid;

811 812
	FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n",
		     fc_lport_state(lport));
813

814
	remote_fid = fc_frame_sid(rx_fp);
815 816 817 818 819
	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) {
820
		printk(KERN_WARNING "host%d: libfc: Received FLOGI from port "
821
		       "with same WWPN %16.16llx\n",
822
		       lport->host->host_no, remote_wwpn);
823 824
		goto out;
	}
825
	FC_LPORT_DBG(lport, "FLOGI from port WWPN %16.16llx\n", remote_wwpn);
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840

	/*
	 * 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;
	}

841
	fc_lport_set_port_id(lport, local_fid, rx_fp);
842 843 844 845 846 847 848 849 850 851 852

	fp = fc_frame_alloc(lport, sizeof(*flp));
	if (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.
		 */
853 854 855 856 857
		fc_fill_reply_hdr(fp, rx_fp, FC_RCTL_ELS_REP, 0);
		fh = fc_frame_header_get(fp);
		hton24(fh->fh_s_id, local_fid);
		hton24(fh->fh_d_id, remote_fid);
		lport->tt.frame_send(lport, fp);
858 859 860 861 862 863 864 865 866 867 868

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

/**
869
 * fc_lport_recv_els_req() - The generic lport ELS request handler
870 871
 * @lport: The local port that received the request
 * @fp:	   The request frame
872 873 874 875 876
 *
 * 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
L
Lucas De Marchi 已提交
877
 *		 lock held because it will grab the lock.
878
 */
879 880
static void fc_lport_recv_els_req(struct fc_lport *lport,
				  struct fc_frame *fp)
881
{
882
	void (*recv)(struct fc_lport *, struct fc_frame *);
883 884 885 886 887 888 889 890

	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.
	 */
891 892
	if (!lport->link_up)
		fc_frame_free(fp);
893
	else {
894 895 896
		/*
		 * Check opcode.
		 */
897
		recv = lport->tt.rport_recv_req;
898 899
		switch (fc_frame_payload_op(fp)) {
		case ELS_FLOGI:
900 901
			if (!lport->point_to_multipoint)
				recv = fc_lport_recv_flogi_req;
902 903
			break;
		case ELS_LOGO:
904
			if (fc_frame_sid(fp) == FC_FID_FLOGI)
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
				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;
		}

921
		recv(lport, fp);
922 923 924 925
	}
	mutex_unlock(&lport->lp_mutex);
}

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
static int fc_lport_els_prli(struct fc_rport_priv *rdata, u32 spp_len,
			     const struct fc_els_spp *spp_in,
			     struct fc_els_spp *spp_out)
{
	return FC_SPP_RESP_INVL;
}

struct fc4_prov fc_lport_els_prov = {
	.prli = fc_lport_els_prli,
	.recv = fc_lport_recv_els_req,
};

/**
 * fc_lport_recv_req() - The generic lport request handler
 * @lport: The lport that received the request
 * @fp: The frame the request is in
 *
 * Locking Note: This function should not be called with the lport
L
Lucas De Marchi 已提交
944
 *		 lock held because it may grab the lock.
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
 */
static void fc_lport_recv_req(struct fc_lport *lport,
			      struct fc_frame *fp)
{
	struct fc_frame_header *fh = fc_frame_header_get(fp);
	struct fc_seq *sp = fr_seq(fp);
	struct fc4_prov *prov;

	/*
	 * Use RCU read lock and module_lock to be sure module doesn't
	 * deregister and get unloaded while we're calling it.
	 * try_module_get() is inlined and accepts a NULL parameter.
	 * Only ELSes and FCP target ops should come through here.
	 * The locking is unfortunate, and a better scheme is being sought.
	 */

	rcu_read_lock();
	if (fh->fh_type >= FC_FC4_PROV_SIZE)
		goto drop;
	prov = rcu_dereference(fc_passive_prov[fh->fh_type]);
	if (!prov || !try_module_get(prov->module))
		goto drop;
	rcu_read_unlock();
	prov->recv(lport, fp);
	module_put(prov->module);
	return;
drop:
	rcu_read_unlock();
	FC_LPORT_DBG(lport, "dropping unexpected frame type %x\n", fh->fh_type);
	fc_frame_free(fp);
	lport->tt.exch_done(sp);
}

978
/**
979 980
 * fc_lport_reset() - Reset a local port
 * @lport: The local port which should be reset
981 982 983 984 985 986
 *
 * Locking Note: This functions should not be called with the
 *		 lport lock held.
 */
int fc_lport_reset(struct fc_lport *lport)
{
987
	cancel_delayed_work_sync(&lport->retry_work);
988 989 990 991 992 993 994 995
	mutex_lock(&lport->lp_mutex);
	fc_lport_enter_reset(lport);
	mutex_unlock(&lport->lp_mutex);
	return 0;
}
EXPORT_SYMBOL(fc_lport_reset);

/**
996 997
 * fc_lport_reset_locked() - Reset the local port w/ the lport lock held
 * @lport: The local port to be reset
998 999 1000 1001
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
1002
static void fc_lport_reset_locked(struct fc_lport *lport)
1003
{
1004 1005
	if (lport->dns_rdata)
		lport->tt.rport_logoff(lport->dns_rdata);
1006

1007 1008 1009 1010 1011
	if (lport->ptp_rdata) {
		lport->tt.rport_logoff(lport->ptp_rdata);
		kref_put(&lport->ptp_rdata->kref, lport->tt.rport_destroy);
		lport->ptp_rdata = NULL;
	}
1012 1013 1014

	lport->tt.disc_stop(lport);

1015
	lport->tt.exch_mgr_reset(lport, 0, 0);
1016
	fc_host_fabric_name(lport->host) = 0;
1017

1018
	if (lport->port_id && (!lport->point_to_multipoint || !lport->link_up))
1019
		fc_lport_set_port_id(lport, 0, NULL);
1020
}
1021

1022 1023
/**
 * fc_lport_enter_reset() - Reset the local port
1024
 * @lport: The local port to be reset
1025 1026 1027 1028 1029 1030 1031 1032 1033
 *
 * 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));

1034 1035 1036
	if (lport->state == LPORT_ST_DISABLED || lport->state == LPORT_ST_LOGO)
		return;

1037 1038 1039 1040 1041 1042
	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);
	}
1043
	fc_lport_state_enter(lport, LPORT_ST_RESET);
1044 1045
	fc_host_post_event(lport->host, fc_get_event_number(),
			   FCH_EVT_LIPRESET, 0);
1046
	fc_vports_linkchange(lport);
1047
	fc_lport_reset_locked(lport);
1048
	if (lport->link_up)
1049 1050 1051
		fc_lport_enter_flogi(lport);
}

1052
/**
1053 1054
 * fc_lport_enter_disabled() - Disable the local port
 * @lport: The local port to be reset
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
 *
 * 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);
1065
	fc_vports_linkchange(lport);
1066 1067 1068
	fc_lport_reset_locked(lport);
}

1069
/**
1070
 * fc_lport_error() - Handler for any errors
1071 1072
 * @lport: The local port that the error was on
 * @fp:	   The error code encoded in a frame pointer
1073 1074 1075 1076 1077 1078 1079 1080
 *
 * 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;
1081 1082 1083
	FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n",
		     PTR_ERR(fp), fc_lport_state(lport),
		     lport->retry_count);
1084

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
	if (PTR_ERR(fp) == -FC_EX_CLOSED)
		return;

	/*
	 * Memory allocation failure, or the exchange timed out
	 * or we received LS_RJT.
	 * 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
		fc_lport_enter_reset(lport);
1103 1104 1105
}

/**
1106
 * fc_lport_ns_resp() - Handle response to a name server
1107 1108 1109
 *			registration exchange
 * @sp:	    current sequence in exchange
 * @fp:	    response frame
1110 1111 1112
 * @lp_arg: Fibre Channel host port instance
 *
 * Locking Note: This function will be called without the lport lock
1113
 * held, but it will lock, call an _enter_* function or fc_lport_error()
1114 1115
 * and then unlock the lport.
 */
1116 1117
static void fc_lport_ns_resp(struct fc_seq *sp, struct fc_frame *fp,
			     void *lp_arg)
1118 1119 1120 1121 1122
{
	struct fc_lport *lport = lp_arg;
	struct fc_frame_header *fh;
	struct fc_ct_hdr *ct;

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

1125 1126 1127 1128 1129
	if (fp == ERR_PTR(-FC_EX_CLOSED))
		return;

	mutex_lock(&lport->lp_mutex);

1130
	if (lport->state < LPORT_ST_RNN_ID || lport->state > LPORT_ST_RFF_ID) {
1131
		FC_LPORT_DBG(lport, "Received a name server response, "
1132
			     "but in state %s\n", fc_lport_state(lport));
1133 1134
		if (IS_ERR(fp))
			goto err;
1135 1136 1137
		goto out;
	}

1138 1139 1140 1141 1142
	if (IS_ERR(fp)) {
		fc_lport_error(lport, fp);
		goto err;
	}

1143 1144 1145 1146 1147 1148 1149
	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)
1150 1151
		switch (lport->state) {
		case LPORT_ST_RNN_ID:
1152
			fc_lport_enter_ns(lport, LPORT_ST_RSNN_NN);
1153 1154
			break;
		case LPORT_ST_RSNN_NN:
1155
			fc_lport_enter_ns(lport, LPORT_ST_RSPN_ID);
1156 1157
			break;
		case LPORT_ST_RSPN_ID:
1158
			fc_lport_enter_ns(lport, LPORT_ST_RFT_ID);
1159 1160
			break;
		case LPORT_ST_RFT_ID:
1161 1162 1163
			fc_lport_enter_ns(lport, LPORT_ST_RFF_ID);
			break;
		case LPORT_ST_RFF_ID:
1164 1165 1166 1167
			if (lport->fdmi_enabled)
				fc_lport_enter_fdmi(lport);
			else
				fc_lport_enter_scr(lport);
1168 1169 1170 1171 1172
			break;
		default:
			/* should have already been caught by state checks */
			break;
		}
1173 1174 1175 1176 1177 1178 1179 1180
	else
		fc_lport_error(lport, fp);
out:
	fc_frame_free(fp);
err:
	mutex_unlock(&lport->lp_mutex);
}

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
/**
 * fc_lport_ms_resp() - Handle response to a management server
 *			exchange
 * @sp:	    current sequence in exchange
 * @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.
 */
static void fc_lport_ms_resp(struct fc_seq *sp, struct fc_frame *fp,
			     void *lp_arg)
{
	struct fc_lport *lport = lp_arg;
	struct fc_frame_header *fh;
	struct fc_ct_hdr *ct;

	FC_LPORT_DBG(lport, "Received a ms %s\n", fc_els_resp_type(fp));

	if (fp == ERR_PTR(-FC_EX_CLOSED))
		return;

	mutex_lock(&lport->lp_mutex);

	if (lport->state < LPORT_ST_RHBA || lport->state > LPORT_ST_DPRT) {
		FC_LPORT_DBG(lport, "Received a management server response, "
			     "but in state %s\n", fc_lport_state(lport));
		if (IS_ERR(fp))
			goto err;
		goto out;
	}

	if (IS_ERR(fp)) {
		fc_lport_error(lport, fp);
		goto err;
	}

	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_MGMT &&
	    ct->ct_fs_subtype == FC_FDMI_SUBTYPE) {
		FC_LPORT_DBG(lport, "Received a management server response, "
				    "reason=%d explain=%d\n",
				    ct->ct_reason,
				    ct->ct_explan);

		switch (lport->state) {
		case LPORT_ST_RHBA:
			if (ntohs(ct->ct_cmd) == FC_FS_ACC)
				fc_lport_enter_ms(lport, LPORT_ST_RPA);
			else /* Error Skip RPA */
				fc_lport_enter_scr(lport);
			break;
		case LPORT_ST_RPA:
			fc_lport_enter_scr(lport);
			break;
		case LPORT_ST_DPRT:
			fc_lport_enter_ms(lport, LPORT_ST_RHBA);
			break;
		case LPORT_ST_DHBA:
			fc_lport_enter_ms(lport, LPORT_ST_DPRT);
			break;
		default:
			/* should have already been caught by state checks */
			break;
		}
	} else {
		/* Invalid Frame? */
		fc_lport_error(lport, fp);
	}
out:
	fc_frame_free(fp);
err:
	mutex_unlock(&lport->lp_mutex);
}

1260
/**
1261
 * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request
1262 1263
 * @sp:	    current sequence in SCR exchange
 * @fp:	    response frame
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
 * @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;

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

1278 1279 1280 1281 1282 1283
	if (fp == ERR_PTR(-FC_EX_CLOSED))
		return;

	mutex_lock(&lport->lp_mutex);

	if (lport->state != LPORT_ST_SCR) {
1284 1285
		FC_LPORT_DBG(lport, "Received a SCR response, but in state "
			     "%s\n", fc_lport_state(lport));
1286 1287
		if (IS_ERR(fp))
			goto err;
1288 1289 1290
		goto out;
	}

1291 1292 1293 1294 1295
	if (IS_ERR(fp)) {
		fc_lport_error(lport, fp);
		goto err;
	}

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
	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);
}

/**
1309 1310
 * fc_lport_enter_scr() - Send a SCR (State Change Register) request
 * @lport: The local port to register for state changes
1311 1312 1313 1314 1315 1316 1317 1318
 *
 * 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;

1319 1320
	FC_LPORT_DBG(lport, "Entered SCR state from %s state\n",
		     fc_lport_state(lport));
1321 1322 1323 1324 1325 1326 1327 1328 1329

	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;
	}

1330
	if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR,
1331 1332
				  fc_lport_scr_resp, lport,
				  2 * lport->r_a_tov))
1333
		fc_lport_error(lport, NULL);
1334 1335 1336
}

/**
1337
 * fc_lport_enter_ns() - register some object with the name server
1338 1339 1340 1341 1342
 * @lport: Fibre Channel local port to register
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
1343
static void fc_lport_enter_ns(struct fc_lport *lport, enum fc_lport_state state)
1344 1345
{
	struct fc_frame *fp;
1346 1347
	enum fc_ns_req cmd;
	int size = sizeof(struct fc_ct_hdr);
1348 1349
	size_t len;

1350 1351
	FC_LPORT_DBG(lport, "Entered %s state from %s state\n",
		     fc_lport_state_names[state],
1352 1353
		     fc_lport_state(lport));

1354
	fc_lport_state_enter(lport, state);
1355

1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
	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;
1381 1382 1383 1384
	case LPORT_ST_RFF_ID:
		cmd = FC_NS_RFF_ID;
		size += sizeof(struct fc_ns_rff_id);
		break;
1385 1386
	default:
		fc_lport_error(lport, NULL);
1387 1388 1389
		return;
	}

1390
	fp = fc_frame_alloc(lport, size);
1391 1392 1393 1394 1395
	if (!fp) {
		fc_lport_error(lport, fp);
		return;
	}

1396
	if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, cmd,
1397
				  fc_lport_ns_resp,
1398
				  lport, 3 * lport->r_a_tov))
1399 1400 1401
		fc_lport_error(lport, fp);
}

1402 1403 1404 1405 1406
static struct fc_rport_operations fc_lport_rport_ops = {
	.event_callback = fc_lport_rport_callback,
};

/**
1407 1408
 * fc_rport_enter_dns() - Create a fc_rport for the name server
 * @lport: The local port requesting a remote port for the name server
1409 1410 1411 1412 1413 1414
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
static void fc_lport_enter_dns(struct fc_lport *lport)
{
1415
	struct fc_rport_priv *rdata;
1416

1417 1418
	FC_LPORT_DBG(lport, "Entered DNS state from %s state\n",
		     fc_lport_state(lport));
1419 1420 1421

	fc_lport_state_enter(lport, LPORT_ST_DNS);

1422
	mutex_lock(&lport->disc.disc_mutex);
1423
	rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV);
1424
	mutex_unlock(&lport->disc.disc_mutex);
1425
	if (!rdata)
1426 1427 1428
		goto err;

	rdata->ops = &fc_lport_rport_ops;
1429
	lport->tt.rport_login(rdata);
1430 1431 1432 1433 1434 1435
	return;

err:
	fc_lport_error(lport, NULL);
}

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 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 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
/**
 * fc_lport_enter_ms() - management server commands
 * @lport: Fibre Channel local port to register
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
static void fc_lport_enter_ms(struct fc_lport *lport, enum fc_lport_state state)
{
	struct fc_frame *fp;
	enum fc_fdmi_req cmd;
	int size = sizeof(struct fc_ct_hdr);
	size_t len;
	int numattrs;

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

	fc_lport_state_enter(lport, state);

	switch (state) {
	case LPORT_ST_RHBA:
		cmd = FC_FDMI_RHBA;
		/* Number of HBA Attributes */
		numattrs = 10;
		len = sizeof(struct fc_fdmi_rhba);
		len -= sizeof(struct fc_fdmi_attr_entry);
		len += (numattrs * FC_FDMI_ATTR_ENTRY_HEADER_LEN);
		len += FC_FDMI_HBA_ATTR_NODENAME_LEN;
		len += FC_FDMI_HBA_ATTR_MANUFACTURER_LEN;
		len += FC_FDMI_HBA_ATTR_SERIALNUMBER_LEN;
		len += FC_FDMI_HBA_ATTR_MODEL_LEN;
		len += FC_FDMI_HBA_ATTR_MODELDESCR_LEN;
		len += FC_FDMI_HBA_ATTR_HARDWAREVERSION_LEN;
		len += FC_FDMI_HBA_ATTR_DRIVERVERSION_LEN;
		len += FC_FDMI_HBA_ATTR_OPTIONROMVERSION_LEN;
		len += FC_FDMI_HBA_ATTR_FIRMWAREVERSION_LEN;
		len += FC_FDMI_HBA_ATTR_OSNAMEVERSION_LEN;

		size += len;
		break;
	case LPORT_ST_RPA:
		cmd = FC_FDMI_RPA;
		/* Number of Port Attributes */
		numattrs = 6;
		len = sizeof(struct fc_fdmi_rpa);
		len -= sizeof(struct fc_fdmi_attr_entry);
		len += (numattrs * FC_FDMI_ATTR_ENTRY_HEADER_LEN);
		len += FC_FDMI_PORT_ATTR_FC4TYPES_LEN;
		len += FC_FDMI_PORT_ATTR_SUPPORTEDSPEED_LEN;
		len += FC_FDMI_PORT_ATTR_CURRENTPORTSPEED_LEN;
		len += FC_FDMI_PORT_ATTR_MAXFRAMESIZE_LEN;
		len += FC_FDMI_PORT_ATTR_OSDEVICENAME_LEN;
		len += FC_FDMI_PORT_ATTR_HOSTNAME_LEN;

		size += len;
		break;
	case LPORT_ST_DPRT:
		cmd = FC_FDMI_DPRT;
		len = sizeof(struct fc_fdmi_dprt);
		size += len;
		break;
	case LPORT_ST_DHBA:
		cmd = FC_FDMI_DHBA;
		len = sizeof(struct fc_fdmi_dhba);
		size += len;
		break;
	default:
		fc_lport_error(lport, NULL);
		return;
	}

	FC_LPORT_DBG(lport, "Cmd=0x%x Len %d size %d\n",
			     cmd, (int)len, size);
	fp = fc_frame_alloc(lport, size);
	if (!fp) {
		fc_lport_error(lport, fp);
		return;
	}

	if (!lport->tt.elsct_send(lport, FC_FID_MGMT_SERV, fp, cmd,
				  fc_lport_ms_resp,
				  lport, 3 * lport->r_a_tov))
		fc_lport_error(lport, fp);
}

/**
 * fc_rport_enter_fdmi() - Create a fc_rport for the management server
 * @lport: The local port requesting a remote port for the management server
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
static void fc_lport_enter_fdmi(struct fc_lport *lport)
{
	struct fc_rport_priv *rdata;

	FC_LPORT_DBG(lport, "Entered FDMI state from %s state\n",
		     fc_lport_state(lport));

	fc_lport_state_enter(lport, LPORT_ST_FDMI);

	mutex_lock(&lport->disc.disc_mutex);
	rdata = lport->tt.rport_create(lport, FC_FID_MGMT_SERV);
	mutex_unlock(&lport->disc.disc_mutex);
	if (!rdata)
		goto err;

	rdata->ops = &fc_lport_rport_ops;
	lport->tt.rport_login(rdata);
	return;

err:
	fc_lport_error(lport, NULL);
}

1553
/**
1554 1555
 * fc_lport_timeout() - Handler for the retry_work timer
 * @work: The work struct of the local port
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
 */
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) {
1566
	case LPORT_ST_DISABLED:
1567 1568
		WARN_ON(1);
		break;
1569 1570
	case LPORT_ST_READY:
		break;
1571 1572
	case LPORT_ST_RESET:
		break;
1573 1574 1575 1576 1577 1578
	case LPORT_ST_FLOGI:
		fc_lport_enter_flogi(lport);
		break;
	case LPORT_ST_DNS:
		fc_lport_enter_dns(lport);
		break;
1579
	case LPORT_ST_RNN_ID:
1580
	case LPORT_ST_RSNN_NN:
1581
	case LPORT_ST_RSPN_ID:
1582
	case LPORT_ST_RFT_ID:
1583
	case LPORT_ST_RFF_ID:
1584
		fc_lport_enter_ns(lport, lport->state);
1585
		break;
1586 1587 1588 1589 1590 1591 1592 1593 1594
	case LPORT_ST_FDMI:
		fc_lport_enter_fdmi(lport);
		break;
	case LPORT_ST_RHBA:
	case LPORT_ST_RPA:
	case LPORT_ST_DHBA:
	case LPORT_ST_DPRT:
		fc_lport_enter_ms(lport, lport->state);
		break;
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
	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);
}

/**
1607
 * fc_lport_logo_resp() - Handle response to LOGO request
1608 1609 1610
 * @sp:	    The sequence that the LOGO was on
 * @fp:	    The LOGO frame
 * @lp_arg: The lport port that received the LOGO request
1611 1612
 *
 * Locking Note: This function will be called without the lport lock
1613
 * held, but it will lock, call an _enter_* function or fc_lport_error()
1614 1615
 * and then unlock the lport.
 */
1616
void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp,
1617
			void *lp_arg)
1618 1619 1620 1621
{
	struct fc_lport *lport = lp_arg;
	u8 op;

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

1624 1625 1626 1627 1628 1629
	if (fp == ERR_PTR(-FC_EX_CLOSED))
		return;

	mutex_lock(&lport->lp_mutex);

	if (lport->state != LPORT_ST_LOGO) {
1630 1631
		FC_LPORT_DBG(lport, "Received a LOGO response, but in state "
			     "%s\n", fc_lport_state(lport));
1632 1633
		if (IS_ERR(fp))
			goto err;
1634 1635 1636
		goto out;
	}

1637 1638 1639 1640 1641
	if (IS_ERR(fp)) {
		fc_lport_error(lport, fp);
		goto err;
	}

1642 1643
	op = fc_frame_payload_op(fp);
	if (op == ELS_LS_ACC)
1644
		fc_lport_enter_disabled(lport);
1645 1646 1647 1648 1649 1650 1651 1652
	else
		fc_lport_error(lport, fp);

out:
	fc_frame_free(fp);
err:
	mutex_unlock(&lport->lp_mutex);
}
1653
EXPORT_SYMBOL(fc_lport_logo_resp);
1654 1655

/**
1656
 * fc_rport_enter_logo() - Logout of the fabric
1657
 * @lport: The local port to be logged out
1658 1659 1660 1661 1662 1663 1664 1665 1666
 *
 * 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;

1667 1668
	FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n",
		     fc_lport_state(lport));
1669 1670

	fc_lport_state_enter(lport, LPORT_ST_LOGO);
1671
	fc_vports_linkchange(lport);
1672 1673 1674 1675 1676 1677 1678

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

1679
	if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO,
1680 1681
				  fc_lport_logo_resp, lport,
				  2 * lport->r_a_tov))
1682
		fc_lport_error(lport, NULL);
1683 1684 1685
}

/**
1686
 * fc_lport_flogi_resp() - Handle response to FLOGI request
1687 1688 1689
 * @sp:	    The sequence that the FLOGI was on
 * @fp:	    The FLOGI response frame
 * @lp_arg: The lport port that received the FLOGI response
1690 1691
 *
 * Locking Note: This function will be called without the lport lock
1692
 * held, but it will lock, call an _enter_* function or fc_lport_error()
1693 1694
 * and then unlock the lport.
 */
1695
void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp,
1696
			 void *lp_arg)
1697 1698
{
	struct fc_lport *lport = lp_arg;
1699
	struct fc_frame_header *fh;
1700 1701 1702 1703 1704 1705 1706
	struct fc_els_flogi *flp;
	u32 did;
	u16 csp_flags;
	unsigned int r_a_tov;
	unsigned int e_d_tov;
	u16 mfs;

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

1709 1710 1711 1712 1713 1714
	if (fp == ERR_PTR(-FC_EX_CLOSED))
		return;

	mutex_lock(&lport->lp_mutex);

	if (lport->state != LPORT_ST_FLOGI) {
1715 1716
		FC_LPORT_DBG(lport, "Received a FLOGI response, but in state "
			     "%s\n", fc_lport_state(lport));
1717 1718
		if (IS_ERR(fp))
			goto err;
1719 1720 1721
		goto out;
	}

1722 1723 1724 1725 1726
	if (IS_ERR(fp)) {
		fc_lport_error(lport, fp);
		goto err;
	}

1727
	fh = fc_frame_header_get(fp);
1728
	did = fc_frame_did(fp);
1729 1730 1731
	if (fh->fh_r_ctl != FC_RCTL_ELS_REP || did == 0 ||
	    fc_frame_payload_op(fp) != ELS_LS_ACC) {
		FC_LPORT_DBG(lport, "FLOGI not accepted or bad response\n");
1732
		fc_lport_error(lport, fp);
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
		goto err;
	}

	flp = fc_frame_payload_get(fp, sizeof(*flp));
	if (!flp) {
		FC_LPORT_DBG(lport, "FLOGI bad response\n");
		fc_lport_error(lport, fp);
		goto err;
	}

	mfs = ntohs(flp->fl_csp.sp_bb_data) &
		FC_SP_BB_DATA_MASK;
	if (mfs >= FC_SP_MIN_MAX_PAYLOAD &&
1746
	    mfs <= lport->mfs) {
1747
		lport->mfs = mfs;
1748 1749 1750 1751 1752 1753 1754 1755
		fc_host_maxframe_size(lport->host) = mfs;
	} else {
		FC_LPORT_DBG(lport, "FLOGI bad mfs:%hu response, "
			     "lport->mfs:%hu\n", mfs, lport->mfs);
		fc_lport_error(lport, fp);
		goto err;
	}

1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
	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;

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

	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;
		fc_lport_set_port_id(lport, did, fp);
		printk(KERN_INFO "host%d: libfc: "
		       "Port (%6.6x) entered "
		       "point-to-point mode\n",
		       lport->host->host_no, did);
		fc_lport_ptp_setup(lport, fc_frame_sid(fp),
				   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_set_port_id(lport, did, fp);
		fc_lport_enter_dns(lport);
1785
	}
1786 1787 1788 1789 1790 1791

out:
	fc_frame_free(fp);
err:
	mutex_unlock(&lport->lp_mutex);
}
1792
EXPORT_SYMBOL(fc_lport_flogi_resp);
1793 1794

/**
1795
 * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager
1796 1797 1798 1799 1800
 * @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.
 */
1801
static void fc_lport_enter_flogi(struct fc_lport *lport)
1802 1803 1804
{
	struct fc_frame *fp;

1805 1806
	FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n",
		     fc_lport_state(lport));
1807 1808 1809

	fc_lport_state_enter(lport, LPORT_ST_FLOGI);

1810 1811 1812 1813 1814 1815
	if (lport->point_to_multipoint) {
		if (lport->port_id)
			fc_lport_enter_ready(lport);
		return;
	}

1816 1817 1818 1819
	fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
	if (!fp)
		return fc_lport_error(lport, fp);

1820 1821
	if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
				  lport->vport ? ELS_FDISC : ELS_FLOGI,
1822 1823 1824
				  fc_lport_flogi_resp, lport,
				  lport->vport ? 2 * lport->r_a_tov :
				  lport->e_d_tov))
1825
		fc_lport_error(lport, NULL);
1826 1827
}

1828 1829 1830 1831
/**
 * fc_lport_config() - Configure a fc_lport
 * @lport: The local port to be configured
 */
1832 1833 1834 1835 1836
int fc_lport_config(struct fc_lport *lport)
{
	INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout);
	mutex_init(&lport->lp_mutex);

1837
	fc_lport_state_enter(lport, LPORT_ST_DISABLED);
1838 1839 1840

	fc_lport_add_fc4_type(lport, FC_TYPE_FCP);
	fc_lport_add_fc4_type(lport, FC_TYPE_CT);
1841
	fc_fc4_conf_lport_params(lport, FC_TYPE_FCP);
1842 1843 1844 1845 1846

	return 0;
}
EXPORT_SYMBOL(fc_lport_config);

1847 1848 1849 1850
/**
 * fc_lport_init() - Initialize the lport layer for a local port
 * @lport: The local port to initialize the exchange layer for
 */
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
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;
1879
	fc_fc4_add_lport(lport);
1880 1881 1882 1883

	return 0;
}
EXPORT_SYMBOL(fc_lport_init);
1884 1885

/**
1886 1887 1888 1889
 * fc_lport_bsg_resp() - The common response handler for FC Passthrough requests
 * @sp:	      The sequence for the FC Passthrough response
 * @fp:	      The response frame
 * @info_arg: The BSG info that the response is for
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
 */
static void fc_lport_bsg_resp(struct fc_seq *sp, struct fc_frame *fp,
			      void *info_arg)
{
	struct fc_bsg_info *info = info_arg;
	struct fc_bsg_job *job = info->job;
	struct fc_lport *lport = info->lport;
	struct fc_frame_header *fh;
	size_t len;
	void *buf;

	if (IS_ERR(fp)) {
		job->reply->result = (PTR_ERR(fp) == -FC_EX_CLOSED) ?
			-ECONNABORTED : -ETIMEDOUT;
		job->reply_len = sizeof(uint32_t);
		job->state_flags |= FC_RQST_STATE_DONE;
		job->job_done(job);
		kfree(info);
		return;
	}

	mutex_lock(&lport->lp_mutex);
	fh = fc_frame_header_get(fp);
	len = fr_len(fp) - sizeof(*fh);
	buf = fc_frame_payload_get(fp, 0);

	if (fr_sof(fp) == FC_SOF_I3 && !ntohs(fh->fh_seq_cnt)) {
		/* Get the response code from the first frame payload */
		unsigned short cmd = (info->rsp_code == FC_FS_ACC) ?
			ntohs(((struct fc_ct_hdr *)buf)->ct_cmd) :
			(unsigned short)fc_frame_payload_op(fp);

		/* Save the reply status of the job */
		job->reply->reply_data.ctels_reply.status =
			(cmd == info->rsp_code) ?
			FC_CTELS_STATUS_OK : FC_CTELS_STATUS_REJECT;
	}

	job->reply->reply_payload_rcv_len +=
		fc_copy_buffer_to_sglist(buf, len, info->sg, &info->nents,
1930
					 &info->offset, NULL);
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948

	if (fr_eof(fp) == FC_EOF_T &&
	    (ntoh24(fh->fh_f_ctl) & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) ==
	    (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) {
		if (job->reply->reply_payload_rcv_len >
		    job->reply_payload.payload_len)
			job->reply->reply_payload_rcv_len =
				job->reply_payload.payload_len;
		job->reply->result = 0;
		job->state_flags |= FC_RQST_STATE_DONE;
		job->job_done(job);
		kfree(info);
	}
	fc_frame_free(fp);
	mutex_unlock(&lport->lp_mutex);
}

/**
1949 1950
 * fc_lport_els_request() - Send ELS passthrough request
 * @job:   The BSG Passthrough job
1951
 * @lport: The local port sending the request
1952
 * @did:   The destination port id
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
static int fc_lport_els_request(struct fc_bsg_job *job,
				struct fc_lport *lport,
				u32 did, u32 tov)
{
	struct fc_bsg_info *info;
	struct fc_frame *fp;
	struct fc_frame_header *fh;
	char *pp;
	int len;

1967
	fp = fc_frame_alloc(lport, job->request_payload.payload_len);
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
	if (!fp)
		return -ENOMEM;

	len = job->request_payload.payload_len;
	pp = fc_frame_payload_get(fp, len);

	sg_copy_to_buffer(job->request_payload.sg_list,
			  job->request_payload.sg_cnt,
			  pp, len);

	fh = fc_frame_header_get(fp);
	fh->fh_r_ctl = FC_RCTL_ELS_REQ;
	hton24(fh->fh_d_id, did);
1981
	hton24(fh->fh_s_id, lport->port_id);
1982
	fh->fh_type = FC_TYPE_ELS;
1983
	hton24(fh->fh_f_ctl, FC_FCTL_REQ);
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
	fh->fh_cs_ctl = 0;
	fh->fh_df_ctl = 0;
	fh->fh_parm_offset = 0;

	info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
	if (!info) {
		fc_frame_free(fp);
		return -ENOMEM;
	}

	info->job = job;
	info->lport = lport;
	info->rsp_code = ELS_LS_ACC;
	info->nents = job->reply_payload.sg_cnt;
	info->sg = job->reply_payload.sg_list;

	if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
2001 2002
				     NULL, info, tov)) {
		kfree(info);
2003
		return -ECOMM;
2004
	}
2005 2006 2007 2008
	return 0;
}

/**
2009 2010
 * fc_lport_ct_request() - Send CT Passthrough request
 * @job:   The BSG Passthrough job
2011 2012
 * @lport: The local port sending the request
 * @did:   The destination FC-ID
2013
 * @tov:   The timeout period to wait for the response
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
 *
 * Locking Note: The lport lock is expected to be held before calling
 * this routine.
 */
static int fc_lport_ct_request(struct fc_bsg_job *job,
			       struct fc_lport *lport, u32 did, u32 tov)
{
	struct fc_bsg_info *info;
	struct fc_frame *fp;
	struct fc_frame_header *fh;
	struct fc_ct_req *ct;
	size_t len;

	fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
			    job->request_payload.payload_len);
	if (!fp)
		return -ENOMEM;

	len = job->request_payload.payload_len;
	ct = fc_frame_payload_get(fp, len);

	sg_copy_to_buffer(job->request_payload.sg_list,
			  job->request_payload.sg_cnt,
			  ct, len);

	fh = fc_frame_header_get(fp);
	fh->fh_r_ctl = FC_RCTL_DD_UNSOL_CTL;
	hton24(fh->fh_d_id, did);
2042
	hton24(fh->fh_s_id, lport->port_id);
2043
	fh->fh_type = FC_TYPE_CT;
2044
	hton24(fh->fh_f_ctl, FC_FCTL_REQ);
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
	fh->fh_cs_ctl = 0;
	fh->fh_df_ctl = 0;
	fh->fh_parm_offset = 0;

	info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL);
	if (!info) {
		fc_frame_free(fp);
		return -ENOMEM;
	}

	info->job = job;
	info->lport = lport;
	info->rsp_code = FC_FS_ACC;
	info->nents = job->reply_payload.sg_cnt;
	info->sg = job->reply_payload.sg_list;

	if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp,
2062 2063
				     NULL, info, tov)) {
		kfree(info);
2064
		return -ECOMM;
2065
	}
2066 2067 2068 2069 2070
	return 0;
}

/**
 * fc_lport_bsg_request() - The common entry point for sending
2071 2072
 *			    FC Passthrough requests
 * @job: The BSG passthrough job
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
 */
int fc_lport_bsg_request(struct fc_bsg_job *job)
{
	struct request *rsp = job->req->next_rq;
	struct Scsi_Host *shost = job->shost;
	struct fc_lport *lport = shost_priv(shost);
	struct fc_rport *rport;
	struct fc_rport_priv *rdata;
	int rc = -EINVAL;
	u32 did;

	job->reply->reply_payload_rcv_len = 0;
2085 2086
	if (rsp)
		rsp->resid_len = job->reply_payload.payload_len;
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113

	mutex_lock(&lport->lp_mutex);

	switch (job->request->msgcode) {
	case FC_BSG_RPT_ELS:
		rport = job->rport;
		if (!rport)
			break;

		rdata = rport->dd_data;
		rc = fc_lport_els_request(job, lport, rport->port_id,
					  rdata->e_d_tov);
		break;

	case FC_BSG_RPT_CT:
		rport = job->rport;
		if (!rport)
			break;

		rdata = rport->dd_data;
		rc = fc_lport_ct_request(job, lport, rport->port_id,
					 rdata->e_d_tov);
		break;

	case FC_BSG_HST_CT:
		did = ntoh24(job->request->rqst_data.h_ct.port_id);
		if (did == FC_FID_DIR_SERV)
2114
			rdata = lport->dns_rdata;
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
		else
			rdata = lport->tt.rport_lookup(lport, did);

		if (!rdata)
			break;

		rc = fc_lport_ct_request(job, lport, did, rdata->e_d_tov);
		break;

	case FC_BSG_HST_ELS_NOLOGIN:
		did = ntoh24(job->request->rqst_data.h_els.port_id);
		rc = fc_lport_els_request(job, lport, did, lport->e_d_tov);
		break;
	}

	mutex_unlock(&lport->lp_mutex);
	return rc;
}
EXPORT_SYMBOL(fc_lport_bsg_request);