fc_disc.c 20.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
/*
 * Copyright(c) 2007 - 2008 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
 */

/*
 * Target Discovery
 *
 * This block discovers all FC-4 remote ports, including FCP initiators. It
 * also handles RSCN events and re-discovery if necessary.
 */

/*
 * DISC LOCKING
 *
 * The disc mutex is can be locked when acquiring rport locks, but may not
 * be held when acquiring the lport lock. Refer to fc_lport.c for more
 * details.
 */

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

#include <scsi/fc/fc_gs.h>

#include <scsi/libfc.h>

#define FC_DISC_RETRY_LIMIT	3	/* max retries */
#define FC_DISC_RETRY_DELAY	500UL	/* (msecs) delay */

#define	FC_DISC_DELAY		3

static void fc_disc_gpn_ft_req(struct fc_disc *);
static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
50
static int fc_disc_new_target(struct fc_disc *, struct fc_rport_priv *,
51 52 53 54 55 56 57
			      struct fc_rport_identifiers *);
static void fc_disc_done(struct fc_disc *);
static void fc_disc_timeout(struct work_struct *);
static void fc_disc_single(struct fc_disc *, struct fc_disc_port *);
static void fc_disc_restart(struct fc_disc *);

/**
58
 * fc_disc_lookup_rport() - lookup a remote port by port_id
59 60 61
 * @lport: Fibre Channel host port instance
 * @port_id: remote port port_id to match
 */
62 63
struct fc_rport_priv *fc_disc_lookup_rport(const struct fc_lport *lport,
					   u32 port_id)
64 65
{
	const struct fc_disc *disc = &lport->disc;
66
	struct fc_rport_priv *rdata;
67 68

	list_for_each_entry(rdata, &disc->rports, peers) {
69 70
		if (rdata->ids.port_id == port_id &&
		    rdata->rp_state != RPORT_ST_DELETE)
71
			return rdata;
72
	}
73
	return NULL;
74 75 76
}

/**
77
 * fc_disc_stop_rports() - delete all the remote ports associated with the lport
78 79 80 81 82 83 84 85
 * @disc: The discovery job to stop rports on
 *
 * Locking Note: This function expects that the lport mutex is locked before
 * calling it.
 */
void fc_disc_stop_rports(struct fc_disc *disc)
{
	struct fc_lport *lport;
86
	struct fc_rport_priv *rdata, *next;
87 88 89 90

	lport = disc->lport;

	mutex_lock(&disc->disc_mutex);
91
	list_for_each_entry_safe(rdata, next, &disc->rports, peers)
92
		lport->tt.rport_logoff(rdata);
93 94 95 96
	mutex_unlock(&disc->disc_mutex);
}

/**
97
 * fc_disc_rport_callback() - Event handler for rport events
98
 * @lport: The lport which is receiving the event
99
 * @rdata: private remote port data
100 101 102 103 104 105
 * @event: The event that occured
 *
 * Locking Note: The rport lock should not be held when calling
 *		 this function.
 */
static void fc_disc_rport_callback(struct fc_lport *lport,
106
				   struct fc_rport_priv *rdata,
107 108 109 110
				   enum fc_rport_event event)
{
	struct fc_disc *disc = &lport->disc;

111
	FC_DISC_DBG(disc, "Received a %d event for port (%6x)\n", event,
112
		    rdata->ids.port_id);
113

114
	switch (event) {
115
	case RPORT_EV_READY:
116 117 118 119 120
		break;
	case RPORT_EV_LOGO:
	case RPORT_EV_FAILED:
	case RPORT_EV_STOP:
		mutex_lock(&disc->disc_mutex);
121
		list_del(&rdata->peers);
122 123 124 125
		mutex_unlock(&disc->disc_mutex);
		break;
	default:
		break;
126 127 128 129 130
	}

}

/**
131
 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
132 133 134 135 136 137 138 139 140 141 142
 * @sp: Current sequence of the RSCN exchange
 * @fp: RSCN Frame
 * @lport: Fibre Channel host port instance
 *
 * Locking Note: This function expects that the disc_mutex is locked
 *		 before it is called.
 */
static void fc_disc_recv_rscn_req(struct fc_seq *sp, struct fc_frame *fp,
				  struct fc_disc *disc)
{
	struct fc_lport *lport;
143
	struct fc_rport_priv *rdata;
144 145 146 147 148 149 150 151 152 153 154 155
	struct fc_els_rscn *rp;
	struct fc_els_rscn_page *pp;
	struct fc_seq_els_data rjt_data;
	unsigned int len;
	int redisc = 0;
	enum fc_els_rscn_ev_qual ev_qual;
	enum fc_els_rscn_addr_fmt fmt;
	LIST_HEAD(disc_ports);
	struct fc_disc_port *dp, *next;

	lport = disc->lport;

156
	FC_DISC_DBG(disc, "Received an RSCN event\n");
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

	/* make sure the frame contains an RSCN message */
	rp = fc_frame_payload_get(fp, sizeof(*rp));
	if (!rp)
		goto reject;
	/* make sure the page length is as expected (4 bytes) */
	if (rp->rscn_page_len != sizeof(*pp))
		goto reject;
	/* get the RSCN payload length */
	len = ntohs(rp->rscn_plen);
	if (len < sizeof(*rp))
		goto reject;
	/* make sure the frame contains the expected payload */
	rp = fc_frame_payload_get(fp, len);
	if (!rp)
		goto reject;
	/* payload must be a multiple of the RSCN page size */
	len -= sizeof(*rp);
	if (len % sizeof(*pp))
		goto reject;

	for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
		ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
		ev_qual &= ELS_RSCN_EV_QUAL_MASK;
		fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
		fmt &= ELS_RSCN_ADDR_FMT_MASK;
		/*
		 * if we get an address format other than port
		 * (area, domain, fabric), then do a full discovery
		 */
		switch (fmt) {
		case ELS_ADDR_FMT_PORT:
189 190
			FC_DISC_DBG(disc, "Port address format for port "
				    "(%6x)\n", ntoh24(pp->rscn_fid));
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
			dp = kzalloc(sizeof(*dp), GFP_KERNEL);
			if (!dp) {
				redisc = 1;
				break;
			}
			dp->lp = lport;
			dp->ids.port_id = ntoh24(pp->rscn_fid);
			dp->ids.port_name = -1;
			dp->ids.node_name = -1;
			dp->ids.roles = FC_RPORT_ROLE_UNKNOWN;
			list_add_tail(&dp->peers, &disc_ports);
			break;
		case ELS_ADDR_FMT_AREA:
		case ELS_ADDR_FMT_DOM:
		case ELS_ADDR_FMT_FAB:
		default:
207
			FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
208 209 210 211 212 213
			redisc = 1;
			break;
		}
	}
	lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
	if (redisc) {
214
		FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
215 216
		fc_disc_restart(disc);
	} else {
217 218 219
		FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
			    "redisc %d state %d in_prog %d\n",
			    redisc, lport->state, disc->pending);
220 221
		list_for_each_entry_safe(dp, next, &disc_ports, peers) {
			list_del(&dp->peers);
222 223 224
			rdata = lport->tt.rport_lookup(lport, dp->ids.port_id);
			if (rdata) {
				lport->tt.rport_logoff(rdata);
225 226 227 228 229 230 231
			}
			fc_disc_single(disc, dp);
		}
	}
	fc_frame_free(fp);
	return;
reject:
232
	FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
233 234 235 236 237 238 239 240
	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);
	fc_frame_free(fp);
}

/**
241
 * fc_disc_recv_req() - Handle incoming requests
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
 * @sp: Current sequence of the request exchange
 * @fp: The frame
 * @lport: The FC local port
 *
 * Locking Note: This function is called from the EM and will lock
 *		 the disc_mutex before calling the handler for the
 *		 request.
 */
static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp,
			     struct fc_lport *lport)
{
	u8 op;
	struct fc_disc *disc = &lport->disc;

	op = fc_frame_payload_op(fp);
	switch (op) {
	case ELS_RSCN:
		mutex_lock(&disc->disc_mutex);
		fc_disc_recv_rscn_req(sp, fp, disc);
		mutex_unlock(&disc->disc_mutex);
		break;
	default:
264 265
		FC_DISC_DBG(disc, "Received an unsupported request, "
			    "the opcode is (%x)\n", op);
266 267 268 269 270
		break;
	}
}

/**
271
 * fc_disc_restart() - Restart discovery
272 273 274 275 276 277 278
 * @lport: FC discovery context
 *
 * Locking Note: This function expects that the disc mutex
 *		 is already locked.
 */
static void fc_disc_restart(struct fc_disc *disc)
{
279
	struct fc_rport_priv *rdata, *next;
280 281
	struct fc_lport *lport = disc->lport;

282
	FC_DISC_DBG(disc, "Restarting discovery\n");
283

284
	list_for_each_entry_safe(rdata, next, &disc->rports, peers)
285
		lport->tt.rport_logoff(rdata);
286 287 288 289 290 291 292

	disc->requested = 1;
	if (!disc->pending)
		fc_disc_gpn_ft_req(disc);
}

/**
293
 * fc_disc_start() - Fibre Channel Target discovery
294 295 296 297 298 299 300 301
 * @lport: FC local port
 *
 * Returns non-zero if discovery cannot be started.
 */
static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
						enum fc_disc_event),
			  struct fc_lport *lport)
{
302
	struct fc_rport_priv *rdata;
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
	struct fc_disc *disc = &lport->disc;

	/*
	 * At this point we may have a new disc job or an existing
	 * one. Either way, let's lock when we make changes to it
	 * and send the GPN_FT request.
	 */
	mutex_lock(&disc->disc_mutex);

	disc->disc_callback = disc_callback;

	/*
	 * If not ready, or already running discovery, just set request flag.
	 */
	disc->requested = 1;

	if (disc->pending) {
		mutex_unlock(&disc->disc_mutex);
		return;
	}

	/*
	 * Handle point-to-point mode as a simple discovery
	 * of the remote port. Yucky, yucky, yuck, yuck!
	 */
328 329
	rdata = disc->lport->ptp_rp;
	if (rdata) {
330 331
		kref_get(&rdata->kref);
		if (!fc_disc_new_target(disc, rdata, &rdata->ids)) {
332 333 334
			disc->event = DISC_EV_SUCCESS;
			fc_disc_done(disc);
		}
335
		kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
336 337 338 339 340 341 342 343 344 345 346 347
	} else {
		fc_disc_gpn_ft_req(disc);	/* get ports by FC-4 type */
	}

	mutex_unlock(&disc->disc_mutex);
}

static struct fc_rport_operations fc_disc_rport_ops = {
	.event_callback = fc_disc_rport_callback,
};

/**
348
 * fc_disc_new_target() - Handle new target found by discovery
349
 * @lport: FC local port
350
 * @rdata: The previous FC remote port priv (NULL if new remote port)
351 352 353 354 355 356
 * @ids: Identifiers for the new FC remote port
 *
 * Locking Note: This function expects that the disc_mutex is locked
 *		 before it is called.
 */
static int fc_disc_new_target(struct fc_disc *disc,
357
			      struct fc_rport_priv *rdata,
358 359 360 361 362
			      struct fc_rport_identifiers *ids)
{
	struct fc_lport *lport = disc->lport;
	int error = 0;

363 364
	if (rdata && ids->port_name) {
		if (rdata->ids.port_name == -1) {
365 366 367
			/*
			 * Set WWN and fall through to notify of create.
			 */
368 369 370
			rdata->ids.port_name = ids->port_name;
			rdata->ids.node_name = ids->node_name;
		} else if (rdata->ids.port_name != ids->port_name) {
371 372 373 374 375 376 377
			/*
			 * This is a new port with the same FCID as
			 * a previously-discovered port.  Presumably the old
			 * port logged out and a new port logged in and was
			 * assigned the same FCID.  This should be rare.
			 * Delete the old one and fall thru to re-create.
			 */
378
			lport->tt.rport_logoff(rdata);
379
			rdata = NULL;
380 381 382 383 384
		}
	}
	if (((ids->port_name != -1) || (ids->port_id != -1)) &&
	    ids->port_id != fc_host_port_id(lport->host) &&
	    ids->port_name != lport->wwpn) {
385
		if (!rdata) {
386
			rdata = lport->tt.rport_lookup(lport, ids->port_id);
387
			if (!rdata) {
388
				rdata = lport->tt.rport_create(lport, ids);
389 390
				if (!rdata)
					error = -ENOMEM;
391 392 393
				else
					list_add_tail(&rdata->peers,
						      &disc->rports);
394 395
			}
		}
396
		if (rdata) {
397
			rdata->ops = &fc_disc_rport_ops;
398
			lport->tt.rport_login(rdata);
399 400 401 402 403 404
		}
	}
	return error;
}

/**
405
 * fc_disc_done() - Discovery has been completed
406
 * @disc: FC discovery context
407 408 409
 * Locking Note: This function expects that the disc mutex is locked before
 * it is called. The discovery callback is then made with the lock released,
 * and the lock is re-taken before returning from this function
410 411 412 413
 */
static void fc_disc_done(struct fc_disc *disc)
{
	struct fc_lport *lport = disc->lport;
414
	enum fc_disc_event event;
415

416
	FC_DISC_DBG(disc, "Discovery complete\n");
417

418
	event = disc->event;
419 420 421 422 423 424
	disc->event = DISC_EV_NONE;

	if (disc->requested)
		fc_disc_gpn_ft_req(disc);
	else
		disc->pending = 0;
425 426 427 428

	mutex_unlock(&disc->disc_mutex);
	disc->disc_callback(lport, event);
	mutex_lock(&disc->disc_mutex);
429 430 431
}

/**
432
 * fc_disc_error() - Handle error on dNS request
433 434 435 436 437 438 439
 * @disc: FC discovery context
 * @fp: The frame pointer
 */
static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
{
	struct fc_lport *lport = disc->lport;
	unsigned long delay = 0;
440 441 442 443

	FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
		    PTR_ERR(fp), disc->retry_count,
		    FC_DISC_RETRY_LIMIT);
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

	if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
		/*
		 * Memory allocation failure, or the exchange timed out,
		 * retry after delay.
		 */
		if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
			/* go ahead and retry */
			if (!fp)
				delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
			else {
				delay = msecs_to_jiffies(lport->e_d_tov);

				/* timeout faster first time */
				if (!disc->retry_count)
					delay /= 4;
			}
			disc->retry_count++;
			schedule_delayed_work(&disc->disc_work, delay);
		} else {
			/* exceeded retries */
			disc->event = DISC_EV_FAILED;
			fc_disc_done(disc);
		}
	}
}

/**
472
 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
 * @lport: FC discovery context
 *
 * Locking Note: This function expects that the disc_mutex is locked
 *		 before it is called.
 */
static void fc_disc_gpn_ft_req(struct fc_disc *disc)
{
	struct fc_frame *fp;
	struct fc_lport *lport = disc->lport;

	WARN_ON(!fc_lport_test_ready(lport));

	disc->pending = 1;
	disc->requested = 0;

	disc->buf_len = 0;
	disc->seq_count = 0;
	fp = fc_frame_alloc(lport,
			    sizeof(struct fc_ct_hdr) +
			    sizeof(struct fc_ns_gid_ft));
	if (!fp)
		goto err;

496
	if (lport->tt.elsct_send(lport, 0, fp,
497 498 499 500 501 502 503 504 505
				 FC_NS_GPN_FT,
				 fc_disc_gpn_ft_resp,
				 disc, lport->e_d_tov))
		return;
err:
	fc_disc_error(disc, fp);
}

/**
506
 * fc_disc_gpn_ft_parse() - Parse the list of IDs and names resulting from a request
507 508 509 510 511 512 513 514 515 516 517 518
 * @lport: Fibre Channel host port instance
 * @buf: GPN_FT response buffer
 * @len: size of response buffer
 */
static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
{
	struct fc_lport *lport;
	struct fc_gpn_ft_resp *np;
	char *bp;
	size_t plen;
	size_t tlen;
	int error = 0;
519
	struct fc_rport_identifiers ids;
520
	struct fc_rport_priv *rdata;
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

	lport = disc->lport;

	/*
	 * Handle partial name record left over from previous call.
	 */
	bp = buf;
	plen = len;
	np = (struct fc_gpn_ft_resp *)bp;
	tlen = disc->buf_len;
	if (tlen) {
		WARN_ON(tlen >= sizeof(*np));
		plen = sizeof(*np) - tlen;
		WARN_ON(plen <= 0);
		WARN_ON(plen >= sizeof(*np));
		if (plen > len)
			plen = len;
		np = &disc->partial_buf;
		memcpy((char *)np + tlen, bp, plen);

		/*
		 * Set bp so that the loop below will advance it to the
		 * first valid full name element.
		 */
		bp -= tlen;
		len += tlen;
		plen += tlen;
		disc->buf_len = (unsigned char) plen;
		if (plen == sizeof(*np))
			disc->buf_len = 0;
	}

	/*
	 * Handle full name records, including the one filled from above.
	 * Normally, np == bp and plen == len, but from the partial case above,
	 * bp, len describe the overall buffer, and np, plen describe the
	 * partial buffer, which if would usually be full now.
	 * After the first time through the loop, things return to "normal".
	 */
	while (plen >= sizeof(*np)) {
561 562 563 564 565 566 567
		ids.port_id = ntoh24(np->fp_fid);
		ids.port_name = ntohll(np->fp_wwpn);
		ids.node_name = -1;
		ids.roles = FC_RPORT_ROLE_UNKNOWN;

		if (ids.port_id != fc_host_port_id(lport->host) &&
		    ids.port_name != lport->wwpn) {
568 569
			rdata = lport->tt.rport_create(lport, &ids);
			if (rdata) {
570
				rdata->ops = &fc_disc_rport_ops;
571
				list_add_tail(&rdata->peers, &disc->rports);
572
				lport->tt.rport_login(rdata);
573
			} else
574 575
				printk(KERN_WARNING "libfc: Failed to allocate "
				       "memory for the newly discovered port "
576
				       "(%6x)\n", ids.port_id);
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
		}

		if (np->fp_flags & FC_NS_FID_LAST) {
			disc->event = DISC_EV_SUCCESS;
			fc_disc_done(disc);
			len = 0;
			break;
		}
		len -= sizeof(*np);
		bp += sizeof(*np);
		np = (struct fc_gpn_ft_resp *)bp;
		plen = len;
	}

	/*
	 * Save any partial record at the end of the buffer for next time.
	 */
	if (error == 0 && len > 0 && len < sizeof(*np)) {
		if (np != &disc->partial_buf) {
596 597
			FC_DISC_DBG(disc, "Partial buffer remains "
				    "for discovery\n");
598 599 600 601 602 603 604 605 606
			memcpy(&disc->partial_buf, np, len);
		}
		disc->buf_len = (unsigned char) len;
	} else {
		disc->buf_len = 0;
	}
	return error;
}

607 608 609 610
/**
 * fc_disc_timeout() - Retry handler for the disc component
 * @work: Structure holding disc obj that needs retry discovery
 *
611 612 613 614 615 616 617 618 619 620 621 622 623 624
 * Handle retry of memory allocation for remote ports.
 */
static void fc_disc_timeout(struct work_struct *work)
{
	struct fc_disc *disc = container_of(work,
					    struct fc_disc,
					    disc_work.work);
	mutex_lock(&disc->disc_mutex);
	if (disc->requested && !disc->pending)
		fc_disc_gpn_ft_req(disc);
	mutex_unlock(&disc->disc_mutex);
}

/**
625
 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
626 627 628 629
 * @sp: Current sequence of GPN_FT exchange
 * @fp: response frame
 * @lp_arg: Fibre Channel host port instance
 *
630 631
 * Locking Note: This function is called without disc mutex held, and
 *		 should do all its processing with the mutex held
632 633 634 635 636 637 638 639 640 641 642 643
 */
static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
				void *disc_arg)
{
	struct fc_disc *disc = disc_arg;
	struct fc_ct_hdr *cp;
	struct fc_frame_header *fh;
	unsigned int seq_cnt;
	void *buf = NULL;
	unsigned int len;
	int error;

644
	mutex_lock(&disc->disc_mutex);
645
	FC_DISC_DBG(disc, "Received a GPN_FT response\n");
646 647 648

	if (IS_ERR(fp)) {
		fc_disc_error(disc, fp);
649
		mutex_unlock(&disc->disc_mutex);
650 651 652 653 654 655 656 657 658 659 660
		return;
	}

	WARN_ON(!fc_frame_is_linear(fp));	/* buffer must be contiguous */
	fh = fc_frame_header_get(fp);
	len = fr_len(fp) - sizeof(*fh);
	seq_cnt = ntohs(fh->fh_seq_cnt);
	if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 &&
	    disc->seq_count == 0) {
		cp = fc_frame_payload_get(fp, sizeof(*cp));
		if (!cp) {
661 662
			FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
				    fr_len(fp));
663 664
		} else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {

665
			/* Accepted, parse the response. */
666 667 668
			buf = cp + 1;
			len -= sizeof(*cp);
		} else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
669 670 671
			FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
				    "(check zoning)\n", cp->ct_reason,
				    cp->ct_explan);
672 673 674
			disc->event = DISC_EV_FAILED;
			fc_disc_done(disc);
		} else {
675 676
			FC_DISC_DBG(disc, "GPN_FT unexpected response code "
				    "%x\n", ntohs(cp->ct_cmd));
677 678 679 680 681
		}
	} else if (fr_sof(fp) == FC_SOF_N3 &&
		   seq_cnt == disc->seq_count) {
		buf = fh + 1;
	} else {
682 683 684
		FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
			    "seq_cnt %x expected %x sof %x eof %x\n",
			    seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
685 686 687 688 689 690 691 692 693
	}
	if (buf) {
		error = fc_disc_gpn_ft_parse(disc, buf, len);
		if (error)
			fc_disc_error(disc, fp);
		else
			disc->seq_count++;
	}
	fc_frame_free(fp);
694 695

	mutex_unlock(&disc->disc_mutex);
696 697 698
}

/**
699
 * fc_disc_single() - Discover the directory information for a single target
700 701 702 703 704 705 706 707 708
 * @lport: FC local port
 * @dp: The port to rediscover
 *
 * Locking Note: This function expects that the disc_mutex is locked
 *		 before it is called.
 */
static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp)
{
	struct fc_lport *lport;
709
	struct fc_rport_priv *rdata;
710 711 712 713 714 715

	lport = disc->lport;

	if (dp->ids.port_id == fc_host_port_id(lport->host))
		goto out;

716 717
	rdata = lport->tt.rport_create(lport, &dp->ids);
	if (rdata) {
718 719
		rdata->ops = &fc_disc_rport_ops;
		kfree(dp);
720
		list_add_tail(&rdata->peers, &disc->rports);
721
		lport->tt.rport_login(rdata);
722 723 724 725 726 727 728
	}
	return;
out:
	kfree(dp);
}

/**
729
 * fc_disc_stop() - Stop discovery for a given lport
730 731 732 733 734 735 736 737 738 739 740 741 742
 * @lport: The lport that discovery should stop for
 */
void fc_disc_stop(struct fc_lport *lport)
{
	struct fc_disc *disc = &lport->disc;

	if (disc) {
		cancel_delayed_work_sync(&disc->disc_work);
		fc_disc_stop_rports(disc);
	}
}

/**
743
 * fc_disc_stop_final() - Stop discovery for a given lport
744 745 746 747 748 749 750 751 752 753 754 755
 * @lport: The lport that discovery should stop for
 *
 * This function will block until discovery has been
 * completely stopped and all rports have been deleted.
 */
void fc_disc_stop_final(struct fc_lport *lport)
{
	fc_disc_stop(lport);
	lport->tt.rport_flush_queue();
}

/**
756
 * fc_disc_init() - Initialize the discovery block
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 785 786 787 788 789
 * @lport: FC local port
 */
int fc_disc_init(struct fc_lport *lport)
{
	struct fc_disc *disc;

	if (!lport->tt.disc_start)
		lport->tt.disc_start = fc_disc_start;

	if (!lport->tt.disc_stop)
		lport->tt.disc_stop = fc_disc_stop;

	if (!lport->tt.disc_stop_final)
		lport->tt.disc_stop_final = fc_disc_stop_final;

	if (!lport->tt.disc_recv_req)
		lport->tt.disc_recv_req = fc_disc_recv_req;

	if (!lport->tt.rport_lookup)
		lport->tt.rport_lookup = fc_disc_lookup_rport;

	disc = &lport->disc;
	INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
	mutex_init(&disc->disc_mutex);
	INIT_LIST_HEAD(&disc->rports);

	disc->lport = lport;
	disc->delay = FC_DISC_DELAY;
	disc->event = DISC_EV_NONE;

	return 0;
}
EXPORT_SYMBOL(fc_disc_init);