• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16  *
17  * Maintained at www.Open-FCoE.org
18  */
19 
20 /*
21  * Target Discovery
22  *
23  * This block discovers all FC-4 remote ports, including FCP initiators. It
24  * also handles RSCN events and re-discovery if necessary.
25  */
26 
27 /*
28  * DISC LOCKING
29  *
30  * The disc mutex is can be locked when acquiring rport locks, but may not
31  * be held when acquiring the lport lock. Refer to fc_lport.c for more
32  * details.
33  */
34 
35 #include <linux/timer.h>
36 #include <linux/slab.h>
37 #include <linux/err.h>
38 #include <linux/export.h>
39 #include <linux/rculist.h>
40 
41 #include <asm/unaligned.h>
42 
43 #include <scsi/fc/fc_gs.h>
44 
45 #include <scsi/libfc.h>
46 
47 #include "fc_libfc.h"
48 
49 #define FC_DISC_RETRY_LIMIT	3	/* max retries */
50 #define FC_DISC_RETRY_DELAY	500UL	/* (msecs) delay */
51 
52 static void fc_disc_gpn_ft_req(struct fc_disc *);
53 static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
54 static void fc_disc_done(struct fc_disc *, enum fc_disc_event);
55 static void fc_disc_timeout(struct work_struct *);
56 static int fc_disc_single(struct fc_lport *, struct fc_disc_port *);
57 static void fc_disc_restart(struct fc_disc *);
58 
59 /**
60  * fc_disc_stop_rports() - Delete all the remote ports associated with the lport
61  * @disc: The discovery job to stop remote ports on
62  *
63  * Locking Note: This function expects that the lport mutex is locked before
64  * calling it.
65  */
fc_disc_stop_rports(struct fc_disc * disc)66 static void fc_disc_stop_rports(struct fc_disc *disc)
67 {
68 	struct fc_lport *lport;
69 	struct fc_rport_priv *rdata;
70 
71 	lport = fc_disc_lport(disc);
72 
73 	rcu_read_lock();
74 	list_for_each_entry_rcu(rdata, &disc->rports, peers) {
75 		if (kref_get_unless_zero(&rdata->kref)) {
76 			fc_rport_logoff(rdata);
77 			kref_put(&rdata->kref, fc_rport_destroy);
78 		}
79 	}
80 	rcu_read_unlock();
81 }
82 
83 /**
84  * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
85  * @disc:  The discovery object to which the RSCN applies
86  * @fp:	   The RSCN frame
87  *
88  * Locking Note: This function expects that the disc_mutex is locked
89  *		 before it is called.
90  */
fc_disc_recv_rscn_req(struct fc_disc * disc,struct fc_frame * fp)91 static void fc_disc_recv_rscn_req(struct fc_disc *disc, struct fc_frame *fp)
92 {
93 	struct fc_lport *lport;
94 	struct fc_els_rscn *rp;
95 	struct fc_els_rscn_page *pp;
96 	struct fc_seq_els_data rjt_data;
97 	unsigned int len;
98 	int redisc = 0;
99 	enum fc_els_rscn_ev_qual ev_qual;
100 	enum fc_els_rscn_addr_fmt fmt;
101 	LIST_HEAD(disc_ports);
102 	struct fc_disc_port *dp, *next;
103 
104 	lport = fc_disc_lport(disc);
105 
106 	FC_DISC_DBG(disc, "Received an RSCN event\n");
107 
108 	/* make sure the frame contains an RSCN message */
109 	rp = fc_frame_payload_get(fp, sizeof(*rp));
110 	if (!rp)
111 		goto reject;
112 	/* make sure the page length is as expected (4 bytes) */
113 	if (rp->rscn_page_len != sizeof(*pp))
114 		goto reject;
115 	/* get the RSCN payload length */
116 	len = ntohs(rp->rscn_plen);
117 	if (len < sizeof(*rp))
118 		goto reject;
119 	/* make sure the frame contains the expected payload */
120 	rp = fc_frame_payload_get(fp, len);
121 	if (!rp)
122 		goto reject;
123 	/* payload must be a multiple of the RSCN page size */
124 	len -= sizeof(*rp);
125 	if (len % sizeof(*pp))
126 		goto reject;
127 
128 	for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
129 		ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
130 		ev_qual &= ELS_RSCN_EV_QUAL_MASK;
131 		fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
132 		fmt &= ELS_RSCN_ADDR_FMT_MASK;
133 		/*
134 		 * if we get an address format other than port
135 		 * (area, domain, fabric), then do a full discovery
136 		 */
137 		switch (fmt) {
138 		case ELS_ADDR_FMT_PORT:
139 			FC_DISC_DBG(disc, "Port address format for port "
140 				    "(%6.6x)\n", ntoh24(pp->rscn_fid));
141 			dp = kzalloc(sizeof(*dp), GFP_KERNEL);
142 			if (!dp) {
143 				redisc = 1;
144 				break;
145 			}
146 			dp->lp = lport;
147 			dp->port_id = ntoh24(pp->rscn_fid);
148 			list_add_tail(&dp->peers, &disc_ports);
149 			break;
150 		case ELS_ADDR_FMT_AREA:
151 		case ELS_ADDR_FMT_DOM:
152 		case ELS_ADDR_FMT_FAB:
153 		default:
154 			FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
155 			redisc = 1;
156 			break;
157 		}
158 	}
159 	fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
160 
161 	/*
162 	 * If not doing a complete rediscovery, do GPN_ID on
163 	 * the individual ports mentioned in the list.
164 	 * If any of these get an error, do a full rediscovery.
165 	 * In any case, go through the list and free the entries.
166 	 */
167 	list_for_each_entry_safe(dp, next, &disc_ports, peers) {
168 		list_del(&dp->peers);
169 		if (!redisc)
170 			redisc = fc_disc_single(lport, dp);
171 		kfree(dp);
172 	}
173 	if (redisc) {
174 		FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
175 		fc_disc_restart(disc);
176 	} else {
177 		FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
178 			    "redisc %d state %d in_prog %d\n",
179 			    redisc, lport->state, disc->pending);
180 	}
181 	fc_frame_free(fp);
182 	return;
183 reject:
184 	FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
185 	rjt_data.reason = ELS_RJT_LOGIC;
186 	rjt_data.explan = ELS_EXPL_NONE;
187 	fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
188 	fc_frame_free(fp);
189 }
190 
191 /**
192  * fc_disc_recv_req() - Handle incoming requests
193  * @lport: The local port receiving the request
194  * @fp:	   The request frame
195  *
196  * Locking Note: This function is called from the EM and will lock
197  *		 the disc_mutex before calling the handler for the
198  *		 request.
199  */
fc_disc_recv_req(struct fc_lport * lport,struct fc_frame * fp)200 static void fc_disc_recv_req(struct fc_lport *lport, struct fc_frame *fp)
201 {
202 	u8 op;
203 	struct fc_disc *disc = &lport->disc;
204 
205 	op = fc_frame_payload_op(fp);
206 	switch (op) {
207 	case ELS_RSCN:
208 		mutex_lock(&disc->disc_mutex);
209 		fc_disc_recv_rscn_req(disc, fp);
210 		mutex_unlock(&disc->disc_mutex);
211 		break;
212 	default:
213 		FC_DISC_DBG(disc, "Received an unsupported request, "
214 			    "the opcode is (%x)\n", op);
215 		fc_frame_free(fp);
216 		break;
217 	}
218 }
219 
220 /**
221  * fc_disc_restart() - Restart discovery
222  * @disc: The discovery object to be restarted
223  *
224  * Locking Note: This function expects that the disc mutex
225  *		 is already locked.
226  */
fc_disc_restart(struct fc_disc * disc)227 static void fc_disc_restart(struct fc_disc *disc)
228 {
229 	if (!disc->disc_callback)
230 		return;
231 
232 	FC_DISC_DBG(disc, "Restarting discovery\n");
233 
234 	disc->requested = 1;
235 	if (disc->pending)
236 		return;
237 
238 	/*
239 	 * Advance disc_id.  This is an arbitrary non-zero number that will
240 	 * match the value in the fc_rport_priv after discovery for all
241 	 * freshly-discovered remote ports.  Avoid wrapping to zero.
242 	 */
243 	disc->disc_id = (disc->disc_id + 2) | 1;
244 	disc->retry_count = 0;
245 	fc_disc_gpn_ft_req(disc);
246 }
247 
248 /**
249  * fc_disc_start() - Start discovery on a local port
250  * @lport:	   The local port to have discovery started on
251  * @disc_callback: Callback function to be called when discovery is complete
252  */
fc_disc_start(void (* disc_callback)(struct fc_lport *,enum fc_disc_event),struct fc_lport * lport)253 static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
254 						enum fc_disc_event),
255 			  struct fc_lport *lport)
256 {
257 	struct fc_disc *disc = &lport->disc;
258 
259 	/*
260 	 * At this point we may have a new disc job or an existing
261 	 * one. Either way, let's lock when we make changes to it
262 	 * and send the GPN_FT request.
263 	 */
264 	mutex_lock(&disc->disc_mutex);
265 	disc->disc_callback = disc_callback;
266 	fc_disc_restart(disc);
267 	mutex_unlock(&disc->disc_mutex);
268 }
269 
270 /**
271  * fc_disc_done() - Discovery has been completed
272  * @disc:  The discovery context
273  * @event: The discovery completion status
274  *
275  * Locking Note: This function expects that the disc mutex is locked before
276  * it is called. The discovery callback is then made with the lock released,
277  * and the lock is re-taken before returning from this function
278  */
fc_disc_done(struct fc_disc * disc,enum fc_disc_event event)279 static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
280 {
281 	struct fc_lport *lport = fc_disc_lport(disc);
282 	struct fc_rport_priv *rdata;
283 
284 	FC_DISC_DBG(disc, "Discovery complete\n");
285 
286 	disc->pending = 0;
287 	if (disc->requested) {
288 		fc_disc_restart(disc);
289 		return;
290 	}
291 
292 	/*
293 	 * Go through all remote ports.	 If they were found in the latest
294 	 * discovery, reverify or log them in.	Otherwise, log them out.
295 	 * Skip ports which were never discovered.  These are the dNS port
296 	 * and ports which were created by PLOGI.
297 	 *
298 	 * We don't need to use the _rcu variant here as the rport list
299 	 * is protected by the disc mutex which is already held on entry.
300 	 */
301 	list_for_each_entry(rdata, &disc->rports, peers) {
302 		if (!kref_get_unless_zero(&rdata->kref))
303 			continue;
304 		if (rdata->disc_id) {
305 			if (rdata->disc_id == disc->disc_id)
306 				fc_rport_login(rdata);
307 			else
308 				fc_rport_logoff(rdata);
309 		}
310 		kref_put(&rdata->kref, fc_rport_destroy);
311 	}
312 	mutex_unlock(&disc->disc_mutex);
313 	disc->disc_callback(lport, event);
314 	mutex_lock(&disc->disc_mutex);
315 }
316 
317 /**
318  * fc_disc_error() - Handle error on dNS request
319  * @disc: The discovery context
320  * @fp:	  The error code encoded as a frame pointer
321  */
fc_disc_error(struct fc_disc * disc,struct fc_frame * fp)322 static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
323 {
324 	struct fc_lport *lport = fc_disc_lport(disc);
325 	unsigned long delay = 0;
326 
327 	FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
328 		    PTR_ERR(fp), disc->retry_count,
329 		    FC_DISC_RETRY_LIMIT);
330 
331 	if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
332 		/*
333 		 * Memory allocation failure, or the exchange timed out,
334 		 * retry after delay.
335 		 */
336 		if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
337 			/* go ahead and retry */
338 			if (!fp)
339 				delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
340 			else {
341 				delay = msecs_to_jiffies(lport->e_d_tov);
342 
343 				/* timeout faster first time */
344 				if (!disc->retry_count)
345 					delay /= 4;
346 			}
347 			disc->retry_count++;
348 			schedule_delayed_work(&disc->disc_work, delay);
349 		} else
350 			fc_disc_done(disc, DISC_EV_FAILED);
351 	} else if (PTR_ERR(fp) == -FC_EX_CLOSED) {
352 		/*
353 		 * if discovery fails due to lport reset, clear
354 		 * pending flag so that subsequent discovery can
355 		 * continue
356 		 */
357 		disc->pending = 0;
358 	}
359 }
360 
361 /**
362  * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
363  * @lport: The discovery context
364  *
365  * Locking Note: This function expects that the disc_mutex is locked
366  *		 before it is called.
367  */
fc_disc_gpn_ft_req(struct fc_disc * disc)368 static void fc_disc_gpn_ft_req(struct fc_disc *disc)
369 {
370 	struct fc_frame *fp;
371 	struct fc_lport *lport = fc_disc_lport(disc);
372 
373 	WARN_ON(!fc_lport_test_ready(lport));
374 
375 	disc->pending = 1;
376 	disc->requested = 0;
377 
378 	disc->buf_len = 0;
379 	disc->seq_count = 0;
380 	fp = fc_frame_alloc(lport,
381 			    sizeof(struct fc_ct_hdr) +
382 			    sizeof(struct fc_ns_gid_ft));
383 	if (!fp)
384 		goto err;
385 
386 	if (lport->tt.elsct_send(lport, 0, fp,
387 				 FC_NS_GPN_FT,
388 				 fc_disc_gpn_ft_resp,
389 				 disc, 3 * lport->r_a_tov))
390 		return;
391 err:
392 	fc_disc_error(disc, NULL);
393 }
394 
395 /**
396  * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
397  * @lport: The local port the GPN_FT was received on
398  * @buf:   The GPN_FT response buffer
399  * @len:   The size of response buffer
400  *
401  * Goes through the list of IDs and names resulting from a request.
402  */
fc_disc_gpn_ft_parse(struct fc_disc * disc,void * buf,size_t len)403 static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
404 {
405 	struct fc_lport *lport;
406 	struct fc_gpn_ft_resp *np;
407 	char *bp;
408 	size_t plen;
409 	size_t tlen;
410 	int error = 0;
411 	struct fc_rport_identifiers ids;
412 	struct fc_rport_priv *rdata;
413 
414 	lport = fc_disc_lport(disc);
415 	disc->seq_count++;
416 
417 	/*
418 	 * Handle partial name record left over from previous call.
419 	 */
420 	bp = buf;
421 	plen = len;
422 	np = (struct fc_gpn_ft_resp *)bp;
423 	tlen = disc->buf_len;
424 	disc->buf_len = 0;
425 	if (tlen) {
426 		WARN_ON(tlen >= sizeof(*np));
427 		plen = sizeof(*np) - tlen;
428 		WARN_ON(plen <= 0);
429 		WARN_ON(plen >= sizeof(*np));
430 		if (plen > len)
431 			plen = len;
432 		np = &disc->partial_buf;
433 		memcpy((char *)np + tlen, bp, plen);
434 
435 		/*
436 		 * Set bp so that the loop below will advance it to the
437 		 * first valid full name element.
438 		 */
439 		bp -= tlen;
440 		len += tlen;
441 		plen += tlen;
442 		disc->buf_len = (unsigned char) plen;
443 		if (plen == sizeof(*np))
444 			disc->buf_len = 0;
445 	}
446 
447 	/*
448 	 * Handle full name records, including the one filled from above.
449 	 * Normally, np == bp and plen == len, but from the partial case above,
450 	 * bp, len describe the overall buffer, and np, plen describe the
451 	 * partial buffer, which if would usually be full now.
452 	 * After the first time through the loop, things return to "normal".
453 	 */
454 	while (plen >= sizeof(*np)) {
455 		ids.port_id = ntoh24(np->fp_fid);
456 		ids.port_name = ntohll(np->fp_wwpn);
457 
458 		if (ids.port_id != lport->port_id &&
459 		    ids.port_name != lport->wwpn) {
460 			rdata = fc_rport_create(lport, ids.port_id);
461 			if (rdata) {
462 				rdata->ids.port_name = ids.port_name;
463 				rdata->disc_id = disc->disc_id;
464 			} else {
465 				printk(KERN_WARNING "libfc: Failed to allocate "
466 				       "memory for the newly discovered port "
467 				       "(%6.6x)\n", ids.port_id);
468 				error = -ENOMEM;
469 			}
470 		}
471 
472 		if (np->fp_flags & FC_NS_FID_LAST) {
473 			fc_disc_done(disc, DISC_EV_SUCCESS);
474 			len = 0;
475 			break;
476 		}
477 		len -= sizeof(*np);
478 		bp += sizeof(*np);
479 		np = (struct fc_gpn_ft_resp *)bp;
480 		plen = len;
481 	}
482 
483 	/*
484 	 * Save any partial record at the end of the buffer for next time.
485 	 */
486 	if (error == 0 && len > 0 && len < sizeof(*np)) {
487 		if (np != &disc->partial_buf) {
488 			FC_DISC_DBG(disc, "Partial buffer remains "
489 				    "for discovery\n");
490 			memcpy(&disc->partial_buf, np, len);
491 		}
492 		disc->buf_len = (unsigned char) len;
493 	}
494 	return error;
495 }
496 
497 /**
498  * fc_disc_timeout() - Handler for discovery timeouts
499  * @work: Structure holding discovery context that needs to retry discovery
500  */
fc_disc_timeout(struct work_struct * work)501 static void fc_disc_timeout(struct work_struct *work)
502 {
503 	struct fc_disc *disc = container_of(work,
504 					    struct fc_disc,
505 					    disc_work.work);
506 	mutex_lock(&disc->disc_mutex);
507 	fc_disc_gpn_ft_req(disc);
508 	mutex_unlock(&disc->disc_mutex);
509 }
510 
511 /**
512  * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
513  * @sp:	    The sequence that the GPN_FT response was received on
514  * @fp:	    The GPN_FT response frame
515  * @lp_arg: The discovery context
516  *
517  * Locking Note: This function is called without disc mutex held, and
518  *		 should do all its processing with the mutex held
519  */
fc_disc_gpn_ft_resp(struct fc_seq * sp,struct fc_frame * fp,void * disc_arg)520 static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
521 				void *disc_arg)
522 {
523 	struct fc_disc *disc = disc_arg;
524 	struct fc_ct_hdr *cp;
525 	struct fc_frame_header *fh;
526 	enum fc_disc_event event = DISC_EV_NONE;
527 	unsigned int seq_cnt;
528 	unsigned int len;
529 	int error = 0;
530 
531 	mutex_lock(&disc->disc_mutex);
532 	FC_DISC_DBG(disc, "Received a GPN_FT response\n");
533 
534 	if (IS_ERR(fp)) {
535 		fc_disc_error(disc, fp);
536 		mutex_unlock(&disc->disc_mutex);
537 		return;
538 	}
539 
540 	WARN_ON(!fc_frame_is_linear(fp));	/* buffer must be contiguous */
541 	fh = fc_frame_header_get(fp);
542 	len = fr_len(fp) - sizeof(*fh);
543 	seq_cnt = ntohs(fh->fh_seq_cnt);
544 	if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 && disc->seq_count == 0) {
545 		cp = fc_frame_payload_get(fp, sizeof(*cp));
546 		if (!cp) {
547 			FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
548 				    fr_len(fp));
549 			event = DISC_EV_FAILED;
550 		} else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
551 
552 			/* Accepted, parse the response. */
553 			len -= sizeof(*cp);
554 			error = fc_disc_gpn_ft_parse(disc, cp + 1, len);
555 		} else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
556 			FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
557 				    "(check zoning)\n", cp->ct_reason,
558 				    cp->ct_explan);
559 			event = DISC_EV_FAILED;
560 			if (cp->ct_reason == FC_FS_RJT_UNABL &&
561 			    cp->ct_explan == FC_FS_EXP_FTNR)
562 				event = DISC_EV_SUCCESS;
563 		} else {
564 			FC_DISC_DBG(disc, "GPN_FT unexpected response code "
565 				    "%x\n", ntohs(cp->ct_cmd));
566 			event = DISC_EV_FAILED;
567 		}
568 	} else if (fr_sof(fp) == FC_SOF_N3 && seq_cnt == disc->seq_count) {
569 		error = fc_disc_gpn_ft_parse(disc, fh + 1, len);
570 	} else {
571 		FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
572 			    "seq_cnt %x expected %x sof %x eof %x\n",
573 			    seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
574 		event = DISC_EV_FAILED;
575 	}
576 	if (error)
577 		fc_disc_error(disc, ERR_PTR(error));
578 	else if (event != DISC_EV_NONE)
579 		fc_disc_done(disc, event);
580 	fc_frame_free(fp);
581 	mutex_unlock(&disc->disc_mutex);
582 }
583 
584 /**
585  * fc_disc_gpn_id_resp() - Handle a response frame from Get Port Names (GPN_ID)
586  * @sp:	       The sequence the GPN_ID is on
587  * @fp:	       The response frame
588  * @rdata_arg: The remote port that sent the GPN_ID response
589  *
590  * Locking Note: This function is called without disc mutex held.
591  */
fc_disc_gpn_id_resp(struct fc_seq * sp,struct fc_frame * fp,void * rdata_arg)592 static void fc_disc_gpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
593 				void *rdata_arg)
594 {
595 	struct fc_rport_priv *rdata = rdata_arg;
596 	struct fc_rport_priv *new_rdata;
597 	struct fc_lport *lport;
598 	struct fc_disc *disc;
599 	struct fc_ct_hdr *cp;
600 	struct fc_ns_gid_pn *pn;
601 	u64 port_name;
602 
603 	lport = rdata->local_port;
604 	disc = &lport->disc;
605 
606 	if (PTR_ERR(fp) == -FC_EX_CLOSED)
607 		goto out;
608 	if (IS_ERR(fp))
609 		goto redisc;
610 
611 	cp = fc_frame_payload_get(fp, sizeof(*cp));
612 	if (!cp)
613 		goto redisc;
614 	if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
615 		if (fr_len(fp) < sizeof(struct fc_frame_header) +
616 		    sizeof(*cp) + sizeof(*pn))
617 			goto redisc;
618 		pn = (struct fc_ns_gid_pn *)(cp + 1);
619 		port_name = get_unaligned_be64(&pn->fn_wwpn);
620 		mutex_lock(&rdata->rp_mutex);
621 		if (rdata->ids.port_name == -1)
622 			rdata->ids.port_name = port_name;
623 		else if (rdata->ids.port_name != port_name) {
624 			FC_DISC_DBG(disc, "GPN_ID accepted.  WWPN changed. "
625 				    "Port-id %6.6x wwpn %16.16llx\n",
626 				    rdata->ids.port_id, port_name);
627 			mutex_unlock(&rdata->rp_mutex);
628 			fc_rport_logoff(rdata);
629 			mutex_lock(&lport->disc.disc_mutex);
630 			new_rdata = fc_rport_create(lport, rdata->ids.port_id);
631 			mutex_unlock(&lport->disc.disc_mutex);
632 			if (new_rdata) {
633 				new_rdata->disc_id = disc->disc_id;
634 				fc_rport_login(new_rdata);
635 			}
636 			goto out;
637 		}
638 		rdata->disc_id = disc->disc_id;
639 		mutex_unlock(&rdata->rp_mutex);
640 		fc_rport_login(rdata);
641 	} else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
642 		FC_DISC_DBG(disc, "GPN_ID rejected reason %x exp %x\n",
643 			    cp->ct_reason, cp->ct_explan);
644 		fc_rport_logoff(rdata);
645 	} else {
646 		FC_DISC_DBG(disc, "GPN_ID unexpected response code %x\n",
647 			    ntohs(cp->ct_cmd));
648 redisc:
649 		mutex_lock(&disc->disc_mutex);
650 		fc_disc_restart(disc);
651 		mutex_unlock(&disc->disc_mutex);
652 	}
653 out:
654 	kref_put(&rdata->kref, fc_rport_destroy);
655 	if (!IS_ERR(fp))
656 		fc_frame_free(fp);
657 }
658 
659 /**
660  * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request
661  * @lport: The local port to initiate discovery on
662  * @rdata: remote port private data
663  *
664  * Locking Note: This function expects that the disc_mutex is locked
665  *		 before it is called.
666  * On failure, an error code is returned.
667  */
fc_disc_gpn_id_req(struct fc_lport * lport,struct fc_rport_priv * rdata)668 static int fc_disc_gpn_id_req(struct fc_lport *lport,
669 			      struct fc_rport_priv *rdata)
670 {
671 	struct fc_frame *fp;
672 
673 	fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
674 			    sizeof(struct fc_ns_fid));
675 	if (!fp)
676 		return -ENOMEM;
677 	if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID,
678 				  fc_disc_gpn_id_resp, rdata,
679 				  3 * lport->r_a_tov))
680 		return -ENOMEM;
681 	kref_get(&rdata->kref);
682 	return 0;
683 }
684 
685 /**
686  * fc_disc_single() - Discover the directory information for a single target
687  * @lport: The local port the remote port is associated with
688  * @dp:	   The port to rediscover
689  *
690  * Locking Note: This function expects that the disc_mutex is locked
691  *		 before it is called.
692  */
fc_disc_single(struct fc_lport * lport,struct fc_disc_port * dp)693 static int fc_disc_single(struct fc_lport *lport, struct fc_disc_port *dp)
694 {
695 	struct fc_rport_priv *rdata;
696 
697 	rdata = fc_rport_create(lport, dp->port_id);
698 	if (!rdata)
699 		return -ENOMEM;
700 	rdata->disc_id = 0;
701 	return fc_disc_gpn_id_req(lport, rdata);
702 }
703 
704 /**
705  * fc_disc_stop() - Stop discovery for a given lport
706  * @lport: The local port that discovery should stop on
707  */
fc_disc_stop(struct fc_lport * lport)708 static void fc_disc_stop(struct fc_lport *lport)
709 {
710 	struct fc_disc *disc = &lport->disc;
711 
712 	if (disc->pending)
713 		cancel_delayed_work_sync(&disc->disc_work);
714 	fc_disc_stop_rports(disc);
715 }
716 
717 /**
718  * fc_disc_stop_final() - Stop discovery for a given lport
719  * @lport: The lport that discovery should stop on
720  *
721  * This function will block until discovery has been
722  * completely stopped and all rports have been deleted.
723  */
fc_disc_stop_final(struct fc_lport * lport)724 static void fc_disc_stop_final(struct fc_lport *lport)
725 {
726 	fc_disc_stop(lport);
727 	fc_rport_flush_queue();
728 }
729 
730 /**
731  * fc_disc_config() - Configure the discovery layer for a local port
732  * @lport: The local port that needs the discovery layer to be configured
733  * @priv: Private data structre for users of the discovery layer
734  */
fc_disc_config(struct fc_lport * lport,void * priv)735 void fc_disc_config(struct fc_lport *lport, void *priv)
736 {
737 	struct fc_disc *disc = &lport->disc;
738 
739 	if (!lport->tt.disc_start)
740 		lport->tt.disc_start = fc_disc_start;
741 
742 	if (!lport->tt.disc_stop)
743 		lport->tt.disc_stop = fc_disc_stop;
744 
745 	if (!lport->tt.disc_stop_final)
746 		lport->tt.disc_stop_final = fc_disc_stop_final;
747 
748 	if (!lport->tt.disc_recv_req)
749 		lport->tt.disc_recv_req = fc_disc_recv_req;
750 
751 	disc = &lport->disc;
752 
753 	disc->priv = priv;
754 }
755 EXPORT_SYMBOL(fc_disc_config);
756 
757 /**
758  * fc_disc_init() - Initialize the discovery layer for a local port
759  * @lport: The local port that needs the discovery layer to be initialized
760  */
fc_disc_init(struct fc_lport * lport)761 void fc_disc_init(struct fc_lport *lport)
762 {
763 	struct fc_disc *disc = &lport->disc;
764 
765 	INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
766 	mutex_init(&disc->disc_mutex);
767 	INIT_LIST_HEAD(&disc->rports);
768 }
769 EXPORT_SYMBOL(fc_disc_init);
770