fc_disc.c 20.6 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
/*
 * 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>
36
#include <linux/slab.h>
37
#include <linux/err.h>
38
#include <linux/export.h>
39 40 41 42 43 44
#include <asm/unaligned.h>

#include <scsi/fc/fc_gs.h>

#include <scsi/libfc.h>

45 46
#include "fc_libfc.h"

47 48 49 50 51
#define FC_DISC_RETRY_LIMIT	3	/* max retries */
#define FC_DISC_RETRY_DELAY	500UL	/* (msecs) delay */

static void fc_disc_gpn_ft_req(struct fc_disc *);
static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
52
static void fc_disc_done(struct fc_disc *, enum fc_disc_event);
53
static void fc_disc_timeout(struct work_struct *);
54
static int fc_disc_single(struct fc_lport *, struct fc_disc_port *);
55 56 57
static void fc_disc_restart(struct fc_disc *);

/**
58 59
 * fc_disc_stop_rports() - Delete all the remote ports associated with the lport
 * @disc: The discovery job to stop remote ports on
60 61 62 63
 *
 * Locking Note: This function expects that the lport mutex is locked before
 * calling it.
 */
64
static void fc_disc_stop_rports(struct fc_disc *disc)
65 66
{
	struct fc_lport *lport;
67
	struct fc_rport_priv *rdata;
68

69
	lport = fc_disc_lport(disc);
70

71 72 73
	rcu_read_lock();
	list_for_each_entry_rcu(rdata, &disc->rports, peers) {
		if (kref_get_unless_zero(&rdata->kref)) {
74
			fc_rport_logoff(rdata);
75
			kref_put(&rdata->kref, fc_rport_destroy);
76 77 78
		}
	}
	rcu_read_unlock();
79 80 81
}

/**
82
 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
83
 * @disc:  The discovery object to which the RSCN applies
84
 * @fp:	   The RSCN frame
85 86 87 88
 *
 * Locking Note: This function expects that the disc_mutex is locked
 *		 before it is called.
 */
89
static void fc_disc_recv_rscn_req(struct fc_disc *disc, struct fc_frame *fp)
90 91 92 93 94 95 96 97 98 99 100 101
{
	struct fc_lport *lport;
	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;

102
	lport = fc_disc_lport(disc);
103

104
	FC_DISC_DBG(disc, "Received an RSCN event\n");
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

	/* 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:
137
			FC_DISC_DBG(disc, "Port address format for port "
138
				    "(%6.6x)\n", ntoh24(pp->rscn_fid));
139 140 141 142 143 144
			dp = kzalloc(sizeof(*dp), GFP_KERNEL);
			if (!dp) {
				redisc = 1;
				break;
			}
			dp->lp = lport;
145
			dp->port_id = ntoh24(pp->rscn_fid);
146 147 148 149 150 151
			list_add_tail(&dp->peers, &disc_ports);
			break;
		case ELS_ADDR_FMT_AREA:
		case ELS_ADDR_FMT_DOM:
		case ELS_ADDR_FMT_FAB:
		default:
152
			FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
153 154 155 156
			redisc = 1;
			break;
		}
	}
157
	fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
158 159 160 161 162 163 164 165 166 167 168 169 170

	/*
	 * If not doing a complete rediscovery, do GPN_ID on
	 * the individual ports mentioned in the list.
	 * If any of these get an error, do a full rediscovery.
	 * In any case, go through the list and free the entries.
	 */
	list_for_each_entry_safe(dp, next, &disc_ports, peers) {
		list_del(&dp->peers);
		if (!redisc)
			redisc = fc_disc_single(lport, dp);
		kfree(dp);
	}
171
	if (redisc) {
172
		FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
173 174
		fc_disc_restart(disc);
	} else {
175 176 177
		FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
			    "redisc %d state %d in_prog %d\n",
			    redisc, lport->state, disc->pending);
178 179 180 181
	}
	fc_frame_free(fp);
	return;
reject:
182
	FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
183 184
	rjt_data.reason = ELS_RJT_LOGIC;
	rjt_data.explan = ELS_EXPL_NONE;
185
	fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
186 187 188 189
	fc_frame_free(fp);
}

/**
190
 * fc_disc_recv_req() - Handle incoming requests
191
 * @lport: The local port receiving the request
192
 * @fp:	   The request frame
193 194 195 196 197
 *
 * Locking Note: This function is called from the EM and will lock
 *		 the disc_mutex before calling the handler for the
 *		 request.
 */
198
static void fc_disc_recv_req(struct fc_lport *lport, struct fc_frame *fp)
199 200 201 202 203 204 205 206
{
	u8 op;
	struct fc_disc *disc = &lport->disc;

	op = fc_frame_payload_op(fp);
	switch (op) {
	case ELS_RSCN:
		mutex_lock(&disc->disc_mutex);
207
		fc_disc_recv_rscn_req(disc, fp);
208 209 210
		mutex_unlock(&disc->disc_mutex);
		break;
	default:
211 212
		FC_DISC_DBG(disc, "Received an unsupported request, "
			    "the opcode is (%x)\n", op);
213
		fc_frame_free(fp);
214 215 216 217 218
		break;
	}
}

/**
219
 * fc_disc_restart() - Restart discovery
220
 * @disc: The discovery object to be restarted
221 222 223 224 225 226
 *
 * Locking Note: This function expects that the disc mutex
 *		 is already locked.
 */
static void fc_disc_restart(struct fc_disc *disc)
{
227 228 229
	if (!disc->disc_callback)
		return;

230
	FC_DISC_DBG(disc, "Restarting discovery\n");
231 232

	disc->requested = 1;
233 234 235 236 237 238 239 240 241
	if (disc->pending)
		return;

	/*
	 * Advance disc_id.  This is an arbitrary non-zero number that will
	 * match the value in the fc_rport_priv after discovery for all
	 * freshly-discovered remote ports.  Avoid wrapping to zero.
	 */
	disc->disc_id = (disc->disc_id + 2) | 1;
242
	disc->retry_count = 0;
243
	fc_disc_gpn_ft_req(disc);
244 245 246
}

/**
247 248 249
 * fc_disc_start() - Start discovery on a local port
 * @lport:	   The local port to have discovery started on
 * @disc_callback: Callback function to be called when discovery is complete
250 251 252 253 254 255 256 257 258 259 260 261 262 263
 */
static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
						enum fc_disc_event),
			  struct fc_lport *lport)
{
	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;
264
	fc_disc_restart(disc);
265 266 267 268
	mutex_unlock(&disc->disc_mutex);
}

/**
269
 * fc_disc_done() - Discovery has been completed
270 271
 * @disc:  The discovery context
 * @event: The discovery completion status
272
 *
273 274 275
 * 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
276
 */
277
static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
278
{
279
	struct fc_lport *lport = fc_disc_lport(disc);
280
	struct fc_rport_priv *rdata;
281

282
	FC_DISC_DBG(disc, "Discovery complete\n");
283

284 285 286 287 288 289 290
	disc->pending = 0;
	if (disc->requested) {
		fc_disc_restart(disc);
		return;
	}

	/*
291 292
	 * Go through all remote ports.	 If they were found in the latest
	 * discovery, reverify or log them in.	Otherwise, log them out.
293 294 295
	 * Skip ports which were never discovered.  These are the dNS port
	 * and ports which were created by PLOGI.
	 */
296
	rcu_read_lock();
297
	list_for_each_entry_rcu(rdata, &disc->rports, peers) {
298
		if (!kref_get_unless_zero(&rdata->kref))
299
			continue;
300 301
		if (rdata->disc_id) {
			if (rdata->disc_id == disc->disc_id)
302
				fc_rport_login(rdata);
303
			else
304
				fc_rport_logoff(rdata);
305
		}
306
		kref_put(&rdata->kref, fc_rport_destroy);
307
	}
308
	rcu_read_unlock();
309 310 311
	mutex_unlock(&disc->disc_mutex);
	disc->disc_callback(lport, event);
	mutex_lock(&disc->disc_mutex);
312 313 314
}

/**
315
 * fc_disc_error() - Handle error on dNS request
316 317
 * @disc: The discovery context
 * @fp:	  The error code encoded as a frame pointer
318 319 320
 */
static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
{
321
	struct fc_lport *lport = fc_disc_lport(disc);
322
	unsigned long delay = 0;
323 324 325 326

	FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
		    PTR_ERR(fp), disc->retry_count,
		    FC_DISC_RETRY_LIMIT);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

	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);
346 347
		} else
			fc_disc_done(disc, DISC_EV_FAILED);
348 349 350 351 352 353 354
	} else if (PTR_ERR(fp) == -FC_EX_CLOSED) {
		/*
		 * if discovery fails due to lport reset, clear
		 * pending flag so that subsequent discovery can
		 * continue
		 */
		disc->pending = 0;
355 356 357 358
	}
}

/**
359
 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
360
 * @lport: The discovery context
361 362 363 364 365 366 367
 *
 * 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;
368
	struct fc_lport *lport = fc_disc_lport(disc);
369 370 371 372 373 374 375 376 377 378 379 380 381 382

	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;

383
	if (lport->tt.elsct_send(lport, 0, fp,
384 385
				 FC_NS_GPN_FT,
				 fc_disc_gpn_ft_resp,
386
				 disc, 3 * lport->r_a_tov))
387 388
		return;
err:
389
	fc_disc_error(disc, NULL);
390 391 392
}

/**
393
 * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
394 395 396
 * @lport: The local port the GPN_FT was received on
 * @buf:   The GPN_FT response buffer
 * @len:   The size of response buffer
397 398
 *
 * Goes through the list of IDs and names resulting from a request.
399 400 401 402 403 404 405 406 407
 */
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;
408
	struct fc_rport_identifiers ids;
409
	struct fc_rport_priv *rdata;
410

411
	lport = fc_disc_lport(disc);
412
	disc->seq_count++;
413 414 415 416 417 418 419 420

	/*
	 * Handle partial name record left over from previous call.
	 */
	bp = buf;
	plen = len;
	np = (struct fc_gpn_ft_resp *)bp;
	tlen = disc->buf_len;
421
	disc->buf_len = 0;
422 423 424 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
	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)) {
452 453 454
		ids.port_id = ntoh24(np->fp_fid);
		ids.port_name = ntohll(np->fp_wwpn);

455
		if (ids.port_id != lport->port_id &&
456
		    ids.port_name != lport->wwpn) {
457
			rdata = fc_rport_create(lport, ids.port_id);
458 459
			if (rdata) {
				rdata->ids.port_name = ids.port_name;
460
				rdata->disc_id = disc->disc_id;
461
			} else {
462 463
				printk(KERN_WARNING "libfc: Failed to allocate "
				       "memory for the newly discovered port "
464
				       "(%6.6x)\n", ids.port_id);
465 466
				error = -ENOMEM;
			}
467 468 469
		}

		if (np->fp_flags & FC_NS_FID_LAST) {
470
			fc_disc_done(disc, DISC_EV_SUCCESS);
471 472 473 474 475 476 477 478 479 480 481 482 483 484
			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) {
485 486
			FC_DISC_DBG(disc, "Partial buffer remains "
				    "for discovery\n");
487 488 489 490 491 492 493
			memcpy(&disc->partial_buf, np, len);
		}
		disc->buf_len = (unsigned char) len;
	}
	return error;
}

494
/**
495 496
 * fc_disc_timeout() - Handler for discovery timeouts
 * @work: Structure holding discovery context that needs to retry discovery
497 498 499 500 501 502 503
 */
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);
504
	fc_disc_gpn_ft_req(disc);
505 506 507 508
	mutex_unlock(&disc->disc_mutex);
}

/**
509
 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
510 511 512
 * @sp:	    The sequence that the GPN_FT response was received on
 * @fp:	    The GPN_FT response frame
 * @lp_arg: The discovery context
513
 *
514 515
 * Locking Note: This function is called without disc mutex held, and
 *		 should do all its processing with the mutex held
516 517 518 519 520 521 522
 */
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;
523
	enum fc_disc_event event = DISC_EV_NONE;
524 525
	unsigned int seq_cnt;
	unsigned int len;
526
	int error = 0;
527

528
	mutex_lock(&disc->disc_mutex);
529
	FC_DISC_DBG(disc, "Received a GPN_FT response\n");
530 531 532

	if (IS_ERR(fp)) {
		fc_disc_error(disc, fp);
533
		mutex_unlock(&disc->disc_mutex);
534 535 536 537 538 539 540
		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);
541
	if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 && disc->seq_count == 0) {
542 543
		cp = fc_frame_payload_get(fp, sizeof(*cp));
		if (!cp) {
544 545
			FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
				    fr_len(fp));
546
			event = DISC_EV_FAILED;
547 548
		} else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {

549
			/* Accepted, parse the response. */
550
			len -= sizeof(*cp);
551
			error = fc_disc_gpn_ft_parse(disc, cp + 1, len);
552
		} else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
553 554 555
			FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
				    "(check zoning)\n", cp->ct_reason,
				    cp->ct_explan);
556
			event = DISC_EV_FAILED;
557 558 559
			if (cp->ct_reason == FC_FS_RJT_UNABL &&
			    cp->ct_explan == FC_FS_EXP_FTNR)
				event = DISC_EV_SUCCESS;
560
		} else {
561 562
			FC_DISC_DBG(disc, "GPN_FT unexpected response code "
				    "%x\n", ntohs(cp->ct_cmd));
563
			event = DISC_EV_FAILED;
564
		}
565 566
	} else if (fr_sof(fp) == FC_SOF_N3 && seq_cnt == disc->seq_count) {
		error = fc_disc_gpn_ft_parse(disc, fh + 1, len);
567
	} else {
568 569 570
		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));
571
		event = DISC_EV_FAILED;
572
	}
573 574 575 576
	if (error)
		fc_disc_error(disc, fp);
	else if (event != DISC_EV_NONE)
		fc_disc_done(disc, event);
577
	fc_frame_free(fp);
578
	mutex_unlock(&disc->disc_mutex);
579 580 581
}

/**
582
 * fc_disc_gpn_id_resp() - Handle a response frame from Get Port Names (GPN_ID)
583 584 585
 * @sp:	       The sequence the GPN_ID is on
 * @fp:	       The response frame
 * @rdata_arg: The remote port that sent the GPN_ID response
586
 *
587
 * Locking Note: This function is called without disc mutex held.
588
 */
589 590
static void fc_disc_gpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
				void *rdata_arg)
591
{
592 593
	struct fc_rport_priv *rdata = rdata_arg;
	struct fc_rport_priv *new_rdata;
594
	struct fc_lport *lport;
595 596 597 598
	struct fc_disc *disc;
	struct fc_ct_hdr *cp;
	struct fc_ns_gid_pn *pn;
	u64 port_name;
599

600 601
	lport = rdata->local_port;
	disc = &lport->disc;
602

603
	if (PTR_ERR(fp) == -FC_EX_CLOSED)
604
		goto out;
605 606 607 608 609 610 611 612 613 614 615 616
	if (IS_ERR(fp))
		goto redisc;

	cp = fc_frame_payload_get(fp, sizeof(*cp));
	if (!cp)
		goto redisc;
	if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
		if (fr_len(fp) < sizeof(struct fc_frame_header) +
		    sizeof(*cp) + sizeof(*pn))
			goto redisc;
		pn = (struct fc_ns_gid_pn *)(cp + 1);
		port_name = get_unaligned_be64(&pn->fn_wwpn);
617
		mutex_lock(&rdata->rp_mutex);
618 619 620 621
		if (rdata->ids.port_name == -1)
			rdata->ids.port_name = port_name;
		else if (rdata->ids.port_name != port_name) {
			FC_DISC_DBG(disc, "GPN_ID accepted.  WWPN changed. "
622
				    "Port-id %6.6x wwpn %16.16llx\n",
623
				    rdata->ids.port_id, port_name);
624
			mutex_unlock(&rdata->rp_mutex);
625
			fc_rport_logoff(rdata);
626
			mutex_lock(&lport->disc.disc_mutex);
627
			new_rdata = fc_rport_create(lport, rdata->ids.port_id);
628
			mutex_unlock(&lport->disc.disc_mutex);
629 630
			if (new_rdata) {
				new_rdata->disc_id = disc->disc_id;
631
				fc_rport_login(new_rdata);
632 633 634
			}
			goto out;
		}
635
		rdata->disc_id = disc->disc_id;
636
		mutex_unlock(&rdata->rp_mutex);
637
		fc_rport_login(rdata);
638 639 640
	} else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
		FC_DISC_DBG(disc, "GPN_ID rejected reason %x exp %x\n",
			    cp->ct_reason, cp->ct_explan);
641
		fc_rport_logoff(rdata);
642 643 644 645
	} else {
		FC_DISC_DBG(disc, "GPN_ID unexpected response code %x\n",
			    ntohs(cp->ct_cmd));
redisc:
646
		mutex_lock(&disc->disc_mutex);
647
		fc_disc_restart(disc);
648
		mutex_unlock(&disc->disc_mutex);
649 650
	}
out:
651
	kref_put(&rdata->kref, fc_rport_destroy);
652 653 654 655
}

/**
 * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request
656
 * @lport: The local port to initiate discovery on
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
 * @rdata: remote port private data
 *
 * Locking Note: This function expects that the disc_mutex is locked
 *		 before it is called.
 * On failure, an error code is returned.
 */
static int fc_disc_gpn_id_req(struct fc_lport *lport,
			      struct fc_rport_priv *rdata)
{
	struct fc_frame *fp;

	fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
			    sizeof(struct fc_ns_fid));
	if (!fp)
		return -ENOMEM;
	if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID,
673 674
				  fc_disc_gpn_id_resp, rdata,
				  3 * lport->r_a_tov))
675 676 677 678 679 680 681
		return -ENOMEM;
	kref_get(&rdata->kref);
	return 0;
}

/**
 * fc_disc_single() - Discover the directory information for a single target
682 683
 * @lport: The local port the remote port is associated with
 * @dp:	   The port to rediscover
684 685 686 687 688 689 690 691
 *
 * Locking Note: This function expects that the disc_mutex is locked
 *		 before it is called.
 */
static int fc_disc_single(struct fc_lport *lport, struct fc_disc_port *dp)
{
	struct fc_rport_priv *rdata;

692
	rdata = fc_rport_create(lport, dp->port_id);
693 694 695 696
	if (!rdata)
		return -ENOMEM;
	rdata->disc_id = 0;
	return fc_disc_gpn_id_req(lport, rdata);
697 698 699
}

/**
700
 * fc_disc_stop() - Stop discovery for a given lport
701
 * @lport: The local port that discovery should stop on
702
 */
703
static void fc_disc_stop(struct fc_lport *lport)
704 705 706
{
	struct fc_disc *disc = &lport->disc;

707
	if (disc->pending)
708
		cancel_delayed_work_sync(&disc->disc_work);
709
	fc_disc_stop_rports(disc);
710 711 712
}

/**
713
 * fc_disc_stop_final() - Stop discovery for a given lport
714
 * @lport: The lport that discovery should stop on
715 716 717 718
 *
 * This function will block until discovery has been
 * completely stopped and all rports have been deleted.
 */
719
static void fc_disc_stop_final(struct fc_lport *lport)
720 721 722 723 724 725
{
	fc_disc_stop(lport);
	lport->tt.rport_flush_queue();
}

/**
726 727
 * fc_disc_config() - Configure the discovery layer for a local port
 * @lport: The local port that needs the discovery layer to be configured
728
 * @priv: Private data structre for users of the discovery layer
729
 */
730
void fc_disc_config(struct fc_lport *lport, void *priv)
731
{
732
	struct fc_disc *disc = &lport->disc;
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

	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;

	disc = &lport->disc;

748
	disc->priv = priv;
749 750
}
EXPORT_SYMBOL(fc_disc_config);
751

752 753 754 755 756 757 758 759 760 761 762
/**
 * fc_disc_init() - Initialize the discovery layer for a local port
 * @lport: The local port that needs the discovery layer to be initialized
 */
void fc_disc_init(struct fc_lport *lport)
{
	struct fc_disc *disc = &lport->disc;

	INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
	mutex_init(&disc->disc_mutex);
	INIT_LIST_HEAD(&disc->rports);
763 764
}
EXPORT_SYMBOL(fc_disc_init);