[SCSI] libfc: have rport_create do a lookup for pre-existing rports first
[deliverable/linux.git] / drivers / scsi / libfc / fc_disc.c
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/err.h>
37 #include <asm/unaligned.h>
38
39 #include <scsi/fc/fc_gs.h>
40
41 #include <scsi/libfc.h>
42
43 #define FC_DISC_RETRY_LIMIT 3 /* max retries */
44 #define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */
45
46 static void fc_disc_gpn_ft_req(struct fc_disc *);
47 static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
48 static int fc_disc_new_target(struct fc_disc *, struct fc_rport_priv *,
49 struct fc_rport_identifiers *);
50 static void fc_disc_done(struct fc_disc *, enum fc_disc_event);
51 static void fc_disc_timeout(struct work_struct *);
52 static void fc_disc_single(struct fc_disc *, struct fc_disc_port *);
53 static void fc_disc_restart(struct fc_disc *);
54
55 /**
56 * fc_disc_lookup_rport() - lookup a remote port by port_id
57 * @lport: Fibre Channel host port instance
58 * @port_id: remote port port_id to match
59 */
60 struct fc_rport_priv *fc_disc_lookup_rport(const struct fc_lport *lport,
61 u32 port_id)
62 {
63 const struct fc_disc *disc = &lport->disc;
64 struct fc_rport_priv *rdata;
65
66 list_for_each_entry(rdata, &disc->rports, peers) {
67 if (rdata->ids.port_id == port_id &&
68 rdata->rp_state != RPORT_ST_DELETE)
69 return rdata;
70 }
71 return NULL;
72 }
73
74 /**
75 * fc_disc_stop_rports() - delete all the remote ports associated with the lport
76 * @disc: The discovery job to stop rports on
77 *
78 * Locking Note: This function expects that the lport mutex is locked before
79 * calling it.
80 */
81 void fc_disc_stop_rports(struct fc_disc *disc)
82 {
83 struct fc_lport *lport;
84 struct fc_rport_priv *rdata, *next;
85
86 lport = disc->lport;
87
88 mutex_lock(&disc->disc_mutex);
89 list_for_each_entry_safe(rdata, next, &disc->rports, peers)
90 lport->tt.rport_logoff(rdata);
91 mutex_unlock(&disc->disc_mutex);
92 }
93
94 /**
95 * fc_disc_rport_callback() - Event handler for rport events
96 * @lport: The lport which is receiving the event
97 * @rdata: private remote port data
98 * @event: The event that occured
99 *
100 * Locking Note: The rport lock should not be held when calling
101 * this function.
102 */
103 static void fc_disc_rport_callback(struct fc_lport *lport,
104 struct fc_rport_priv *rdata,
105 enum fc_rport_event event)
106 {
107 struct fc_disc *disc = &lport->disc;
108
109 FC_DISC_DBG(disc, "Received a %d event for port (%6x)\n", event,
110 rdata->ids.port_id);
111
112 switch (event) {
113 case RPORT_EV_READY:
114 break;
115 case RPORT_EV_LOGO:
116 case RPORT_EV_FAILED:
117 case RPORT_EV_STOP:
118 break;
119 default:
120 break;
121 }
122
123 }
124
125 /**
126 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
127 * @sp: Current sequence of the RSCN exchange
128 * @fp: RSCN Frame
129 * @lport: Fibre Channel host port instance
130 *
131 * Locking Note: This function expects that the disc_mutex is locked
132 * before it is called.
133 */
134 static void fc_disc_recv_rscn_req(struct fc_seq *sp, struct fc_frame *fp,
135 struct fc_disc *disc)
136 {
137 struct fc_lport *lport;
138 struct fc_rport_priv *rdata;
139 struct fc_els_rscn *rp;
140 struct fc_els_rscn_page *pp;
141 struct fc_seq_els_data rjt_data;
142 unsigned int len;
143 int redisc = 0;
144 enum fc_els_rscn_ev_qual ev_qual;
145 enum fc_els_rscn_addr_fmt fmt;
146 LIST_HEAD(disc_ports);
147 struct fc_disc_port *dp, *next;
148
149 lport = disc->lport;
150
151 FC_DISC_DBG(disc, "Received an RSCN event\n");
152
153 /* make sure the frame contains an RSCN message */
154 rp = fc_frame_payload_get(fp, sizeof(*rp));
155 if (!rp)
156 goto reject;
157 /* make sure the page length is as expected (4 bytes) */
158 if (rp->rscn_page_len != sizeof(*pp))
159 goto reject;
160 /* get the RSCN payload length */
161 len = ntohs(rp->rscn_plen);
162 if (len < sizeof(*rp))
163 goto reject;
164 /* make sure the frame contains the expected payload */
165 rp = fc_frame_payload_get(fp, len);
166 if (!rp)
167 goto reject;
168 /* payload must be a multiple of the RSCN page size */
169 len -= sizeof(*rp);
170 if (len % sizeof(*pp))
171 goto reject;
172
173 for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
174 ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
175 ev_qual &= ELS_RSCN_EV_QUAL_MASK;
176 fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
177 fmt &= ELS_RSCN_ADDR_FMT_MASK;
178 /*
179 * if we get an address format other than port
180 * (area, domain, fabric), then do a full discovery
181 */
182 switch (fmt) {
183 case ELS_ADDR_FMT_PORT:
184 FC_DISC_DBG(disc, "Port address format for port "
185 "(%6x)\n", ntoh24(pp->rscn_fid));
186 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
187 if (!dp) {
188 redisc = 1;
189 break;
190 }
191 dp->lp = lport;
192 dp->ids.port_id = ntoh24(pp->rscn_fid);
193 dp->ids.port_name = -1;
194 dp->ids.node_name = -1;
195 dp->ids.roles = FC_RPORT_ROLE_UNKNOWN;
196 list_add_tail(&dp->peers, &disc_ports);
197 break;
198 case ELS_ADDR_FMT_AREA:
199 case ELS_ADDR_FMT_DOM:
200 case ELS_ADDR_FMT_FAB:
201 default:
202 FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
203 redisc = 1;
204 break;
205 }
206 }
207 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
208 if (redisc) {
209 FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
210 fc_disc_restart(disc);
211 } else {
212 FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
213 "redisc %d state %d in_prog %d\n",
214 redisc, lport->state, disc->pending);
215 list_for_each_entry_safe(dp, next, &disc_ports, peers) {
216 list_del(&dp->peers);
217 rdata = lport->tt.rport_lookup(lport, dp->ids.port_id);
218 if (rdata) {
219 lport->tt.rport_logoff(rdata);
220 }
221 fc_disc_single(disc, dp);
222 }
223 }
224 fc_frame_free(fp);
225 return;
226 reject:
227 FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
228 rjt_data.fp = NULL;
229 rjt_data.reason = ELS_RJT_LOGIC;
230 rjt_data.explan = ELS_EXPL_NONE;
231 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
232 fc_frame_free(fp);
233 }
234
235 /**
236 * fc_disc_recv_req() - Handle incoming requests
237 * @sp: Current sequence of the request exchange
238 * @fp: The frame
239 * @lport: The FC local port
240 *
241 * Locking Note: This function is called from the EM and will lock
242 * the disc_mutex before calling the handler for the
243 * request.
244 */
245 static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp,
246 struct fc_lport *lport)
247 {
248 u8 op;
249 struct fc_disc *disc = &lport->disc;
250
251 op = fc_frame_payload_op(fp);
252 switch (op) {
253 case ELS_RSCN:
254 mutex_lock(&disc->disc_mutex);
255 fc_disc_recv_rscn_req(sp, fp, disc);
256 mutex_unlock(&disc->disc_mutex);
257 break;
258 default:
259 FC_DISC_DBG(disc, "Received an unsupported request, "
260 "the opcode is (%x)\n", op);
261 break;
262 }
263 }
264
265 /**
266 * fc_disc_restart() - Restart discovery
267 * @lport: FC discovery context
268 *
269 * Locking Note: This function expects that the disc mutex
270 * is already locked.
271 */
272 static void fc_disc_restart(struct fc_disc *disc)
273 {
274 struct fc_rport_priv *rdata, *next;
275 struct fc_lport *lport = disc->lport;
276
277 FC_DISC_DBG(disc, "Restarting discovery\n");
278
279 list_for_each_entry_safe(rdata, next, &disc->rports, peers)
280 lport->tt.rport_logoff(rdata);
281
282 disc->requested = 1;
283 if (!disc->pending)
284 fc_disc_gpn_ft_req(disc);
285 }
286
287 /**
288 * fc_disc_start() - Fibre Channel Target discovery
289 * @lport: FC local port
290 *
291 * Returns non-zero if discovery cannot be started.
292 */
293 static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
294 enum fc_disc_event),
295 struct fc_lport *lport)
296 {
297 struct fc_rport_priv *rdata;
298 struct fc_disc *disc = &lport->disc;
299
300 /*
301 * At this point we may have a new disc job or an existing
302 * one. Either way, let's lock when we make changes to it
303 * and send the GPN_FT request.
304 */
305 mutex_lock(&disc->disc_mutex);
306
307 disc->disc_callback = disc_callback;
308
309 /*
310 * If not ready, or already running discovery, just set request flag.
311 */
312 disc->requested = 1;
313
314 if (disc->pending) {
315 mutex_unlock(&disc->disc_mutex);
316 return;
317 }
318
319 /*
320 * Handle point-to-point mode as a simple discovery
321 * of the remote port. Yucky, yucky, yuck, yuck!
322 */
323 rdata = disc->lport->ptp_rp;
324 if (rdata) {
325 kref_get(&rdata->kref);
326 if (!fc_disc_new_target(disc, rdata, &rdata->ids)) {
327 fc_disc_done(disc, DISC_EV_SUCCESS);
328 }
329 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
330 } else {
331 fc_disc_gpn_ft_req(disc); /* get ports by FC-4 type */
332 }
333
334 mutex_unlock(&disc->disc_mutex);
335 }
336
337 static struct fc_rport_operations fc_disc_rport_ops = {
338 .event_callback = fc_disc_rport_callback,
339 };
340
341 /**
342 * fc_disc_new_target() - Handle new target found by discovery
343 * @lport: FC local port
344 * @rdata: The previous FC remote port priv (NULL if new remote port)
345 * @ids: Identifiers for the new FC remote port
346 *
347 * Locking Note: This function expects that the disc_mutex is locked
348 * before it is called.
349 */
350 static int fc_disc_new_target(struct fc_disc *disc,
351 struct fc_rport_priv *rdata,
352 struct fc_rport_identifiers *ids)
353 {
354 struct fc_lport *lport = disc->lport;
355 int error = 0;
356
357 if (rdata && ids->port_name) {
358 if (rdata->ids.port_name == -1) {
359 /*
360 * Set WWN and fall through to notify of create.
361 */
362 rdata->ids.port_name = ids->port_name;
363 rdata->ids.node_name = ids->node_name;
364 } else if (rdata->ids.port_name != ids->port_name) {
365 /*
366 * This is a new port with the same FCID as
367 * a previously-discovered port. Presumably the old
368 * port logged out and a new port logged in and was
369 * assigned the same FCID. This should be rare.
370 * Delete the old one and fall thru to re-create.
371 */
372 lport->tt.rport_logoff(rdata);
373 rdata = NULL;
374 }
375 }
376 if (((ids->port_name != -1) || (ids->port_id != -1)) &&
377 ids->port_id != fc_host_port_id(lport->host) &&
378 ids->port_name != lport->wwpn) {
379 if (!rdata) {
380 rdata = lport->tt.rport_create(lport, ids);
381 if (!rdata)
382 error = -ENOMEM;
383 }
384 if (rdata) {
385 rdata->ops = &fc_disc_rport_ops;
386 lport->tt.rport_login(rdata);
387 }
388 }
389 return error;
390 }
391
392 /**
393 * fc_disc_done() - Discovery has been completed
394 * @disc: FC discovery context
395 * @event: discovery completion status
396 *
397 * Locking Note: This function expects that the disc mutex is locked before
398 * it is called. The discovery callback is then made with the lock released,
399 * and the lock is re-taken before returning from this function
400 */
401 static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
402 {
403 struct fc_lport *lport = disc->lport;
404
405 FC_DISC_DBG(disc, "Discovery complete\n");
406
407 if (disc->requested)
408 fc_disc_gpn_ft_req(disc);
409 else
410 disc->pending = 0;
411
412 mutex_unlock(&disc->disc_mutex);
413 disc->disc_callback(lport, event);
414 mutex_lock(&disc->disc_mutex);
415 }
416
417 /**
418 * fc_disc_error() - Handle error on dNS request
419 * @disc: FC discovery context
420 * @fp: The frame pointer
421 */
422 static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
423 {
424 struct fc_lport *lport = disc->lport;
425 unsigned long delay = 0;
426
427 FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
428 PTR_ERR(fp), disc->retry_count,
429 FC_DISC_RETRY_LIMIT);
430
431 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
432 /*
433 * Memory allocation failure, or the exchange timed out,
434 * retry after delay.
435 */
436 if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
437 /* go ahead and retry */
438 if (!fp)
439 delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
440 else {
441 delay = msecs_to_jiffies(lport->e_d_tov);
442
443 /* timeout faster first time */
444 if (!disc->retry_count)
445 delay /= 4;
446 }
447 disc->retry_count++;
448 schedule_delayed_work(&disc->disc_work, delay);
449 } else
450 fc_disc_done(disc, DISC_EV_FAILED);
451 }
452 }
453
454 /**
455 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
456 * @lport: FC discovery context
457 *
458 * Locking Note: This function expects that the disc_mutex is locked
459 * before it is called.
460 */
461 static void fc_disc_gpn_ft_req(struct fc_disc *disc)
462 {
463 struct fc_frame *fp;
464 struct fc_lport *lport = disc->lport;
465
466 WARN_ON(!fc_lport_test_ready(lport));
467
468 disc->pending = 1;
469 disc->requested = 0;
470
471 disc->buf_len = 0;
472 disc->seq_count = 0;
473 fp = fc_frame_alloc(lport,
474 sizeof(struct fc_ct_hdr) +
475 sizeof(struct fc_ns_gid_ft));
476 if (!fp)
477 goto err;
478
479 if (lport->tt.elsct_send(lport, 0, fp,
480 FC_NS_GPN_FT,
481 fc_disc_gpn_ft_resp,
482 disc, lport->e_d_tov))
483 return;
484 err:
485 fc_disc_error(disc, fp);
486 }
487
488 /**
489 * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
490 * @lport: Fibre Channel host port instance
491 * @buf: GPN_FT response buffer
492 * @len: size of response buffer
493 *
494 * Goes through the list of IDs and names resulting from a request.
495 */
496 static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
497 {
498 struct fc_lport *lport;
499 struct fc_gpn_ft_resp *np;
500 char *bp;
501 size_t plen;
502 size_t tlen;
503 int error = 0;
504 struct fc_rport_identifiers ids;
505 struct fc_rport_priv *rdata;
506
507 lport = disc->lport;
508
509 /*
510 * Handle partial name record left over from previous call.
511 */
512 bp = buf;
513 plen = len;
514 np = (struct fc_gpn_ft_resp *)bp;
515 tlen = disc->buf_len;
516 if (tlen) {
517 WARN_ON(tlen >= sizeof(*np));
518 plen = sizeof(*np) - tlen;
519 WARN_ON(plen <= 0);
520 WARN_ON(plen >= sizeof(*np));
521 if (plen > len)
522 plen = len;
523 np = &disc->partial_buf;
524 memcpy((char *)np + tlen, bp, plen);
525
526 /*
527 * Set bp so that the loop below will advance it to the
528 * first valid full name element.
529 */
530 bp -= tlen;
531 len += tlen;
532 plen += tlen;
533 disc->buf_len = (unsigned char) plen;
534 if (plen == sizeof(*np))
535 disc->buf_len = 0;
536 }
537
538 /*
539 * Handle full name records, including the one filled from above.
540 * Normally, np == bp and plen == len, but from the partial case above,
541 * bp, len describe the overall buffer, and np, plen describe the
542 * partial buffer, which if would usually be full now.
543 * After the first time through the loop, things return to "normal".
544 */
545 while (plen >= sizeof(*np)) {
546 ids.port_id = ntoh24(np->fp_fid);
547 ids.port_name = ntohll(np->fp_wwpn);
548 ids.node_name = -1;
549 ids.roles = FC_RPORT_ROLE_UNKNOWN;
550
551 if (ids.port_id != fc_host_port_id(lport->host) &&
552 ids.port_name != lport->wwpn) {
553 rdata = lport->tt.rport_create(lport, &ids);
554 if (rdata) {
555 rdata->ops = &fc_disc_rport_ops;
556 lport->tt.rport_login(rdata);
557 } else
558 printk(KERN_WARNING "libfc: Failed to allocate "
559 "memory for the newly discovered port "
560 "(%6x)\n", ids.port_id);
561 }
562
563 if (np->fp_flags & FC_NS_FID_LAST) {
564 fc_disc_done(disc, DISC_EV_SUCCESS);
565 len = 0;
566 break;
567 }
568 len -= sizeof(*np);
569 bp += sizeof(*np);
570 np = (struct fc_gpn_ft_resp *)bp;
571 plen = len;
572 }
573
574 /*
575 * Save any partial record at the end of the buffer for next time.
576 */
577 if (error == 0 && len > 0 && len < sizeof(*np)) {
578 if (np != &disc->partial_buf) {
579 FC_DISC_DBG(disc, "Partial buffer remains "
580 "for discovery\n");
581 memcpy(&disc->partial_buf, np, len);
582 }
583 disc->buf_len = (unsigned char) len;
584 } else {
585 disc->buf_len = 0;
586 }
587 return error;
588 }
589
590 /**
591 * fc_disc_timeout() - Retry handler for the disc component
592 * @work: Structure holding disc obj that needs retry discovery
593 *
594 * Handle retry of memory allocation for remote ports.
595 */
596 static void fc_disc_timeout(struct work_struct *work)
597 {
598 struct fc_disc *disc = container_of(work,
599 struct fc_disc,
600 disc_work.work);
601 mutex_lock(&disc->disc_mutex);
602 if (disc->requested && !disc->pending)
603 fc_disc_gpn_ft_req(disc);
604 mutex_unlock(&disc->disc_mutex);
605 }
606
607 /**
608 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
609 * @sp: Current sequence of GPN_FT exchange
610 * @fp: response frame
611 * @lp_arg: Fibre Channel host port instance
612 *
613 * Locking Note: This function is called without disc mutex held, and
614 * should do all its processing with the mutex held
615 */
616 static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
617 void *disc_arg)
618 {
619 struct fc_disc *disc = disc_arg;
620 struct fc_ct_hdr *cp;
621 struct fc_frame_header *fh;
622 unsigned int seq_cnt;
623 void *buf = NULL;
624 unsigned int len;
625 int error;
626
627 mutex_lock(&disc->disc_mutex);
628 FC_DISC_DBG(disc, "Received a GPN_FT response\n");
629
630 if (IS_ERR(fp)) {
631 fc_disc_error(disc, fp);
632 mutex_unlock(&disc->disc_mutex);
633 return;
634 }
635
636 WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
637 fh = fc_frame_header_get(fp);
638 len = fr_len(fp) - sizeof(*fh);
639 seq_cnt = ntohs(fh->fh_seq_cnt);
640 if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 &&
641 disc->seq_count == 0) {
642 cp = fc_frame_payload_get(fp, sizeof(*cp));
643 if (!cp) {
644 FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
645 fr_len(fp));
646 } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
647
648 /* Accepted, parse the response. */
649 buf = cp + 1;
650 len -= sizeof(*cp);
651 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
652 FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
653 "(check zoning)\n", cp->ct_reason,
654 cp->ct_explan);
655 fc_disc_done(disc, DISC_EV_FAILED);
656 } else {
657 FC_DISC_DBG(disc, "GPN_FT unexpected response code "
658 "%x\n", ntohs(cp->ct_cmd));
659 }
660 } else if (fr_sof(fp) == FC_SOF_N3 &&
661 seq_cnt == disc->seq_count) {
662 buf = fh + 1;
663 } else {
664 FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
665 "seq_cnt %x expected %x sof %x eof %x\n",
666 seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
667 }
668 if (buf) {
669 error = fc_disc_gpn_ft_parse(disc, buf, len);
670 if (error)
671 fc_disc_error(disc, fp);
672 else
673 disc->seq_count++;
674 }
675 fc_frame_free(fp);
676
677 mutex_unlock(&disc->disc_mutex);
678 }
679
680 /**
681 * fc_disc_single() - Discover the directory information for a single target
682 * @lport: FC local port
683 * @dp: The port to rediscover
684 *
685 * Locking Note: This function expects that the disc_mutex is locked
686 * before it is called.
687 */
688 static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp)
689 {
690 struct fc_lport *lport;
691 struct fc_rport_priv *rdata;
692
693 lport = disc->lport;
694
695 if (dp->ids.port_id == fc_host_port_id(lport->host))
696 goto out;
697
698 rdata = lport->tt.rport_create(lport, &dp->ids);
699 if (rdata) {
700 rdata->ops = &fc_disc_rport_ops;
701 kfree(dp);
702 lport->tt.rport_login(rdata);
703 }
704 return;
705 out:
706 kfree(dp);
707 }
708
709 /**
710 * fc_disc_stop() - Stop discovery for a given lport
711 * @lport: The lport that discovery should stop for
712 */
713 void fc_disc_stop(struct fc_lport *lport)
714 {
715 struct fc_disc *disc = &lport->disc;
716
717 if (disc) {
718 cancel_delayed_work_sync(&disc->disc_work);
719 fc_disc_stop_rports(disc);
720 }
721 }
722
723 /**
724 * fc_disc_stop_final() - Stop discovery for a given lport
725 * @lport: The lport that discovery should stop for
726 *
727 * This function will block until discovery has been
728 * completely stopped and all rports have been deleted.
729 */
730 void fc_disc_stop_final(struct fc_lport *lport)
731 {
732 fc_disc_stop(lport);
733 lport->tt.rport_flush_queue();
734 }
735
736 /**
737 * fc_disc_init() - Initialize the discovery block
738 * @lport: FC local port
739 */
740 int fc_disc_init(struct fc_lport *lport)
741 {
742 struct fc_disc *disc;
743
744 if (!lport->tt.disc_start)
745 lport->tt.disc_start = fc_disc_start;
746
747 if (!lport->tt.disc_stop)
748 lport->tt.disc_stop = fc_disc_stop;
749
750 if (!lport->tt.disc_stop_final)
751 lport->tt.disc_stop_final = fc_disc_stop_final;
752
753 if (!lport->tt.disc_recv_req)
754 lport->tt.disc_recv_req = fc_disc_recv_req;
755
756 if (!lport->tt.rport_lookup)
757 lport->tt.rport_lookup = fc_disc_lookup_rport;
758
759 disc = &lport->disc;
760 INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
761 mutex_init(&disc->disc_mutex);
762 INIT_LIST_HEAD(&disc->rports);
763
764 disc->lport = lport;
765
766 return 0;
767 }
768 EXPORT_SYMBOL(fc_disc_init);
This page took 0.046306 seconds and 6 git commands to generate.