2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
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.
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
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.
17 * Maintained at www.Open-FCoE.org
23 * This block discovers all FC-4 remote ports, including FCP initiators. It
24 * also handles RSCN events and re-discovery if necessary.
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
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>
41 #include <asm/unaligned.h>
43 #include <scsi/fc/fc_gs.h>
45 #include <scsi/libfc.h>
49 #define FC_DISC_RETRY_LIMIT 3 /* max retries */
50 #define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */
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
*);
60 * fc_disc_stop_rports() - Delete all the remote ports associated with the lport
61 * @disc: The discovery job to stop remote ports on
63 static void fc_disc_stop_rports(struct fc_disc
*disc
)
65 struct fc_rport_priv
*rdata
;
67 lockdep_assert_held(&disc
->disc_mutex
);
69 list_for_each_entry(rdata
, &disc
->rports
, peers
) {
70 if (kref_get_unless_zero(&rdata
->kref
)) {
71 fc_rport_logoff(rdata
);
72 kref_put(&rdata
->kref
, fc_rport_destroy
);
78 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
79 * @disc: The discovery object to which the RSCN applies
82 static void fc_disc_recv_rscn_req(struct fc_disc
*disc
, struct fc_frame
*fp
)
84 struct fc_lport
*lport
;
85 struct fc_els_rscn
*rp
;
86 struct fc_els_rscn_page
*pp
;
87 struct fc_seq_els_data rjt_data
;
90 enum fc_els_rscn_ev_qual ev_qual
;
91 enum fc_els_rscn_addr_fmt fmt
;
92 LIST_HEAD(disc_ports
);
93 struct fc_disc_port
*dp
, *next
;
95 lockdep_assert_held(&disc
->disc_mutex
);
97 lport
= fc_disc_lport(disc
);
99 FC_DISC_DBG(disc
, "Received an RSCN event\n");
101 /* make sure the frame contains an RSCN message */
102 rp
= fc_frame_payload_get(fp
, sizeof(*rp
));
105 /* make sure the page length is as expected (4 bytes) */
106 if (rp
->rscn_page_len
!= sizeof(*pp
))
108 /* get the RSCN payload length */
109 len
= ntohs(rp
->rscn_plen
);
110 if (len
< sizeof(*rp
))
112 /* make sure the frame contains the expected payload */
113 rp
= fc_frame_payload_get(fp
, len
);
116 /* payload must be a multiple of the RSCN page size */
118 if (len
% sizeof(*pp
))
121 for (pp
= (void *)(rp
+ 1); len
> 0; len
-= sizeof(*pp
), pp
++) {
122 ev_qual
= pp
->rscn_page_flags
>> ELS_RSCN_EV_QUAL_BIT
;
123 ev_qual
&= ELS_RSCN_EV_QUAL_MASK
;
124 fmt
= pp
->rscn_page_flags
>> ELS_RSCN_ADDR_FMT_BIT
;
125 fmt
&= ELS_RSCN_ADDR_FMT_MASK
;
127 * if we get an address format other than port
128 * (area, domain, fabric), then do a full discovery
131 case ELS_ADDR_FMT_PORT
:
132 FC_DISC_DBG(disc
, "Port address format for port "
133 "(%6.6x)\n", ntoh24(pp
->rscn_fid
));
134 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
140 dp
->port_id
= ntoh24(pp
->rscn_fid
);
141 list_add_tail(&dp
->peers
, &disc_ports
);
143 case ELS_ADDR_FMT_AREA
:
144 case ELS_ADDR_FMT_DOM
:
145 case ELS_ADDR_FMT_FAB
:
147 FC_DISC_DBG(disc
, "Address format is (%d)\n", fmt
);
152 fc_seq_els_rsp_send(fp
, ELS_LS_ACC
, NULL
);
155 * If not doing a complete rediscovery, do GPN_ID on
156 * the individual ports mentioned in the list.
157 * If any of these get an error, do a full rediscovery.
158 * In any case, go through the list and free the entries.
160 list_for_each_entry_safe(dp
, next
, &disc_ports
, peers
) {
161 list_del(&dp
->peers
);
163 redisc
= fc_disc_single(lport
, dp
);
167 FC_DISC_DBG(disc
, "RSCN received: rediscovering\n");
168 fc_disc_restart(disc
);
170 FC_DISC_DBG(disc
, "RSCN received: not rediscovering. "
171 "redisc %d state %d in_prog %d\n",
172 redisc
, lport
->state
, disc
->pending
);
177 FC_DISC_DBG(disc
, "Received a bad RSCN frame\n");
178 rjt_data
.reason
= ELS_RJT_LOGIC
;
179 rjt_data
.explan
= ELS_EXPL_NONE
;
180 fc_seq_els_rsp_send(fp
, ELS_LS_RJT
, &rjt_data
);
185 * fc_disc_recv_req() - Handle incoming requests
186 * @lport: The local port receiving the request
187 * @fp: The request frame
189 * Locking Note: This function is called from the EM and will lock
190 * the disc_mutex before calling the handler for the
193 static void fc_disc_recv_req(struct fc_lport
*lport
, struct fc_frame
*fp
)
196 struct fc_disc
*disc
= &lport
->disc
;
198 op
= fc_frame_payload_op(fp
);
201 mutex_lock(&disc
->disc_mutex
);
202 fc_disc_recv_rscn_req(disc
, fp
);
203 mutex_unlock(&disc
->disc_mutex
);
206 FC_DISC_DBG(disc
, "Received an unsupported request, "
207 "the opcode is (%x)\n", op
);
214 * fc_disc_restart() - Restart discovery
215 * @disc: The discovery object to be restarted
217 static void fc_disc_restart(struct fc_disc
*disc
)
219 lockdep_assert_held(&disc
->disc_mutex
);
221 if (!disc
->disc_callback
)
224 FC_DISC_DBG(disc
, "Restarting discovery\n");
231 * Advance disc_id. This is an arbitrary non-zero number that will
232 * match the value in the fc_rport_priv after discovery for all
233 * freshly-discovered remote ports. Avoid wrapping to zero.
235 disc
->disc_id
= (disc
->disc_id
+ 2) | 1;
236 disc
->retry_count
= 0;
237 fc_disc_gpn_ft_req(disc
);
241 * fc_disc_start() - Start discovery on a local port
242 * @lport: The local port to have discovery started on
243 * @disc_callback: Callback function to be called when discovery is complete
245 static void fc_disc_start(void (*disc_callback
)(struct fc_lport
*,
247 struct fc_lport
*lport
)
249 struct fc_disc
*disc
= &lport
->disc
;
252 * At this point we may have a new disc job or an existing
253 * one. Either way, let's lock when we make changes to it
254 * and send the GPN_FT request.
256 mutex_lock(&disc
->disc_mutex
);
257 disc
->disc_callback
= disc_callback
;
258 fc_disc_restart(disc
);
259 mutex_unlock(&disc
->disc_mutex
);
263 * fc_disc_done() - Discovery has been completed
264 * @disc: The discovery context
265 * @event: The discovery completion status
267 static void fc_disc_done(struct fc_disc
*disc
, enum fc_disc_event event
)
269 struct fc_lport
*lport
= fc_disc_lport(disc
);
270 struct fc_rport_priv
*rdata
;
272 lockdep_assert_held(&disc
->disc_mutex
);
273 FC_DISC_DBG(disc
, "Discovery complete\n");
276 if (disc
->requested
) {
277 fc_disc_restart(disc
);
282 * Go through all remote ports. If they were found in the latest
283 * discovery, reverify or log them in. Otherwise, log them out.
284 * Skip ports which were never discovered. These are the dNS port
285 * and ports which were created by PLOGI.
287 * We don't need to use the _rcu variant here as the rport list
288 * is protected by the disc mutex which is already held on entry.
290 list_for_each_entry(rdata
, &disc
->rports
, peers
) {
291 if (!kref_get_unless_zero(&rdata
->kref
))
293 if (rdata
->disc_id
) {
294 if (rdata
->disc_id
== disc
->disc_id
)
295 fc_rport_login(rdata
);
297 fc_rport_logoff(rdata
);
299 kref_put(&rdata
->kref
, fc_rport_destroy
);
301 mutex_unlock(&disc
->disc_mutex
);
302 disc
->disc_callback(lport
, event
);
303 mutex_lock(&disc
->disc_mutex
);
307 * fc_disc_error() - Handle error on dNS request
308 * @disc: The discovery context
309 * @fp: The error code encoded as a frame pointer
311 static void fc_disc_error(struct fc_disc
*disc
, struct fc_frame
*fp
)
313 struct fc_lport
*lport
= fc_disc_lport(disc
);
314 unsigned long delay
= 0;
316 FC_DISC_DBG(disc
, "Error %ld, retries %d/%d\n",
317 PTR_ERR(fp
), disc
->retry_count
,
318 FC_DISC_RETRY_LIMIT
);
320 if (!fp
|| PTR_ERR(fp
) == -FC_EX_TIMEOUT
) {
322 * Memory allocation failure, or the exchange timed out,
325 if (disc
->retry_count
< FC_DISC_RETRY_LIMIT
) {
326 /* go ahead and retry */
328 delay
= msecs_to_jiffies(FC_DISC_RETRY_DELAY
);
330 delay
= msecs_to_jiffies(lport
->e_d_tov
);
332 /* timeout faster first time */
333 if (!disc
->retry_count
)
337 schedule_delayed_work(&disc
->disc_work
, delay
);
339 fc_disc_done(disc
, DISC_EV_FAILED
);
340 } else if (PTR_ERR(fp
) == -FC_EX_CLOSED
) {
342 * if discovery fails due to lport reset, clear
343 * pending flag so that subsequent discovery can
351 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
352 * @lport: The discovery context
354 static void fc_disc_gpn_ft_req(struct fc_disc
*disc
)
357 struct fc_lport
*lport
= fc_disc_lport(disc
);
359 lockdep_assert_held(&disc
->disc_mutex
);
361 WARN_ON(!fc_lport_test_ready(lport
));
368 fp
= fc_frame_alloc(lport
,
369 sizeof(struct fc_ct_hdr
) +
370 sizeof(struct fc_ns_gid_ft
));
374 if (lport
->tt
.elsct_send(lport
, 0, fp
,
377 disc
, 3 * lport
->r_a_tov
))
380 fc_disc_error(disc
, NULL
);
384 * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
385 * @lport: The local port the GPN_FT was received on
386 * @buf: The GPN_FT response buffer
387 * @len: The size of response buffer
389 * Goes through the list of IDs and names resulting from a request.
391 static int fc_disc_gpn_ft_parse(struct fc_disc
*disc
, void *buf
, size_t len
)
393 struct fc_lport
*lport
;
394 struct fc_gpn_ft_resp
*np
;
399 struct fc_rport_identifiers ids
;
400 struct fc_rport_priv
*rdata
;
402 lport
= fc_disc_lport(disc
);
406 * Handle partial name record left over from previous call.
410 np
= (struct fc_gpn_ft_resp
*)bp
;
411 tlen
= disc
->buf_len
;
414 WARN_ON(tlen
>= sizeof(*np
));
415 plen
= sizeof(*np
) - tlen
;
417 WARN_ON(plen
>= sizeof(*np
));
420 np
= &disc
->partial_buf
;
421 memcpy((char *)np
+ tlen
, bp
, plen
);
424 * Set bp so that the loop below will advance it to the
425 * first valid full name element.
430 disc
->buf_len
= (unsigned char) plen
;
431 if (plen
== sizeof(*np
))
436 * Handle full name records, including the one filled from above.
437 * Normally, np == bp and plen == len, but from the partial case above,
438 * bp, len describe the overall buffer, and np, plen describe the
439 * partial buffer, which if would usually be full now.
440 * After the first time through the loop, things return to "normal".
442 while (plen
>= sizeof(*np
)) {
443 ids
.port_id
= ntoh24(np
->fp_fid
);
444 ids
.port_name
= ntohll(np
->fp_wwpn
);
446 if (ids
.port_id
!= lport
->port_id
&&
447 ids
.port_name
!= lport
->wwpn
) {
448 rdata
= fc_rport_create(lport
, ids
.port_id
);
450 rdata
->ids
.port_name
= ids
.port_name
;
451 rdata
->disc_id
= disc
->disc_id
;
453 printk(KERN_WARNING
"libfc: Failed to allocate "
454 "memory for the newly discovered port "
455 "(%6.6x)\n", ids
.port_id
);
460 if (np
->fp_flags
& FC_NS_FID_LAST
) {
461 fc_disc_done(disc
, DISC_EV_SUCCESS
);
467 np
= (struct fc_gpn_ft_resp
*)bp
;
472 * Save any partial record at the end of the buffer for next time.
474 if (error
== 0 && len
> 0 && len
< sizeof(*np
)) {
475 if (np
!= &disc
->partial_buf
) {
476 FC_DISC_DBG(disc
, "Partial buffer remains "
478 memcpy(&disc
->partial_buf
, np
, len
);
480 disc
->buf_len
= (unsigned char) len
;
486 * fc_disc_timeout() - Handler for discovery timeouts
487 * @work: Structure holding discovery context that needs to retry discovery
489 static void fc_disc_timeout(struct work_struct
*work
)
491 struct fc_disc
*disc
= container_of(work
,
494 mutex_lock(&disc
->disc_mutex
);
495 fc_disc_gpn_ft_req(disc
);
496 mutex_unlock(&disc
->disc_mutex
);
500 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
501 * @sp: The sequence that the GPN_FT response was received on
502 * @fp: The GPN_FT response frame
503 * @lp_arg: The discovery context
505 * Locking Note: This function is called without disc mutex held, and
506 * should do all its processing with the mutex held
508 static void fc_disc_gpn_ft_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
511 struct fc_disc
*disc
= disc_arg
;
512 struct fc_ct_hdr
*cp
;
513 struct fc_frame_header
*fh
;
514 enum fc_disc_event event
= DISC_EV_NONE
;
515 unsigned int seq_cnt
;
519 mutex_lock(&disc
->disc_mutex
);
520 FC_DISC_DBG(disc
, "Received a GPN_FT response\n");
523 fc_disc_error(disc
, fp
);
524 mutex_unlock(&disc
->disc_mutex
);
528 WARN_ON(!fc_frame_is_linear(fp
)); /* buffer must be contiguous */
529 fh
= fc_frame_header_get(fp
);
530 len
= fr_len(fp
) - sizeof(*fh
);
531 seq_cnt
= ntohs(fh
->fh_seq_cnt
);
532 if (fr_sof(fp
) == FC_SOF_I3
&& seq_cnt
== 0 && disc
->seq_count
== 0) {
533 cp
= fc_frame_payload_get(fp
, sizeof(*cp
));
535 FC_DISC_DBG(disc
, "GPN_FT response too short, len %d\n",
537 event
= DISC_EV_FAILED
;
538 } else if (ntohs(cp
->ct_cmd
) == FC_FS_ACC
) {
540 /* Accepted, parse the response. */
542 error
= fc_disc_gpn_ft_parse(disc
, cp
+ 1, len
);
543 } else if (ntohs(cp
->ct_cmd
) == FC_FS_RJT
) {
544 FC_DISC_DBG(disc
, "GPN_FT rejected reason %x exp %x "
545 "(check zoning)\n", cp
->ct_reason
,
547 event
= DISC_EV_FAILED
;
548 if (cp
->ct_reason
== FC_FS_RJT_UNABL
&&
549 cp
->ct_explan
== FC_FS_EXP_FTNR
)
550 event
= DISC_EV_SUCCESS
;
552 FC_DISC_DBG(disc
, "GPN_FT unexpected response code "
553 "%x\n", ntohs(cp
->ct_cmd
));
554 event
= DISC_EV_FAILED
;
556 } else if (fr_sof(fp
) == FC_SOF_N3
&& seq_cnt
== disc
->seq_count
) {
557 error
= fc_disc_gpn_ft_parse(disc
, fh
+ 1, len
);
559 FC_DISC_DBG(disc
, "GPN_FT unexpected frame - out of sequence? "
560 "seq_cnt %x expected %x sof %x eof %x\n",
561 seq_cnt
, disc
->seq_count
, fr_sof(fp
), fr_eof(fp
));
562 event
= DISC_EV_FAILED
;
565 fc_disc_error(disc
, ERR_PTR(error
));
566 else if (event
!= DISC_EV_NONE
)
567 fc_disc_done(disc
, event
);
569 mutex_unlock(&disc
->disc_mutex
);
573 * fc_disc_gpn_id_resp() - Handle a response frame from Get Port Names (GPN_ID)
574 * @sp: The sequence the GPN_ID is on
575 * @fp: The response frame
576 * @rdata_arg: The remote port that sent the GPN_ID response
578 * Locking Note: This function is called without disc mutex held.
580 static void fc_disc_gpn_id_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
583 struct fc_rport_priv
*rdata
= rdata_arg
;
584 struct fc_rport_priv
*new_rdata
;
585 struct fc_lport
*lport
;
586 struct fc_disc
*disc
;
587 struct fc_ct_hdr
*cp
;
588 struct fc_ns_gid_pn
*pn
;
591 lport
= rdata
->local_port
;
594 if (PTR_ERR(fp
) == -FC_EX_CLOSED
)
599 cp
= fc_frame_payload_get(fp
, sizeof(*cp
));
602 if (ntohs(cp
->ct_cmd
) == FC_FS_ACC
) {
603 if (fr_len(fp
) < sizeof(struct fc_frame_header
) +
604 sizeof(*cp
) + sizeof(*pn
))
606 pn
= (struct fc_ns_gid_pn
*)(cp
+ 1);
607 port_name
= get_unaligned_be64(&pn
->fn_wwpn
);
608 mutex_lock(&rdata
->rp_mutex
);
609 if (rdata
->ids
.port_name
== -1)
610 rdata
->ids
.port_name
= port_name
;
611 else if (rdata
->ids
.port_name
!= port_name
) {
612 FC_DISC_DBG(disc
, "GPN_ID accepted. WWPN changed. "
613 "Port-id %6.6x wwpn %16.16llx\n",
614 rdata
->ids
.port_id
, port_name
);
615 mutex_unlock(&rdata
->rp_mutex
);
616 fc_rport_logoff(rdata
);
617 mutex_lock(&lport
->disc
.disc_mutex
);
618 new_rdata
= fc_rport_create(lport
, rdata
->ids
.port_id
);
619 mutex_unlock(&lport
->disc
.disc_mutex
);
621 new_rdata
->disc_id
= disc
->disc_id
;
622 fc_rport_login(new_rdata
);
626 rdata
->disc_id
= disc
->disc_id
;
627 mutex_unlock(&rdata
->rp_mutex
);
628 fc_rport_login(rdata
);
629 } else if (ntohs(cp
->ct_cmd
) == FC_FS_RJT
) {
630 FC_DISC_DBG(disc
, "GPN_ID rejected reason %x exp %x\n",
631 cp
->ct_reason
, cp
->ct_explan
);
632 fc_rport_logoff(rdata
);
634 FC_DISC_DBG(disc
, "GPN_ID unexpected response code %x\n",
637 mutex_lock(&disc
->disc_mutex
);
638 fc_disc_restart(disc
);
639 mutex_unlock(&disc
->disc_mutex
);
642 kref_put(&rdata
->kref
, fc_rport_destroy
);
646 * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request
647 * @lport: The local port to initiate discovery on
648 * @rdata: remote port private data
650 * On failure, an error code is returned.
652 static int fc_disc_gpn_id_req(struct fc_lport
*lport
,
653 struct fc_rport_priv
*rdata
)
657 lockdep_assert_held(&lport
->disc
.disc_mutex
);
658 fp
= fc_frame_alloc(lport
, sizeof(struct fc_ct_hdr
) +
659 sizeof(struct fc_ns_fid
));
662 if (!lport
->tt
.elsct_send(lport
, rdata
->ids
.port_id
, fp
, FC_NS_GPN_ID
,
663 fc_disc_gpn_id_resp
, rdata
,
666 kref_get(&rdata
->kref
);
671 * fc_disc_single() - Discover the directory information for a single target
672 * @lport: The local port the remote port is associated with
673 * @dp: The port to rediscover
675 static int fc_disc_single(struct fc_lport
*lport
, struct fc_disc_port
*dp
)
677 struct fc_rport_priv
*rdata
;
679 lockdep_assert_held(&lport
->disc
.disc_mutex
);
681 rdata
= fc_rport_create(lport
, dp
->port_id
);
685 return fc_disc_gpn_id_req(lport
, rdata
);
689 * fc_disc_stop() - Stop discovery for a given lport
690 * @lport: The local port that discovery should stop on
692 static void fc_disc_stop(struct fc_lport
*lport
)
694 struct fc_disc
*disc
= &lport
->disc
;
697 cancel_delayed_work_sync(&disc
->disc_work
);
698 mutex_lock(&disc
->disc_mutex
);
699 fc_disc_stop_rports(disc
);
700 mutex_unlock(&disc
->disc_mutex
);
704 * fc_disc_stop_final() - Stop discovery for a given lport
705 * @lport: The lport that discovery should stop on
707 * This function will block until discovery has been
708 * completely stopped and all rports have been deleted.
710 static void fc_disc_stop_final(struct fc_lport
*lport
)
713 fc_rport_flush_queue();
717 * fc_disc_config() - Configure the discovery layer for a local port
718 * @lport: The local port that needs the discovery layer to be configured
719 * @priv: Private data structre for users of the discovery layer
721 void fc_disc_config(struct fc_lport
*lport
, void *priv
)
723 struct fc_disc
*disc
;
725 if (!lport
->tt
.disc_start
)
726 lport
->tt
.disc_start
= fc_disc_start
;
728 if (!lport
->tt
.disc_stop
)
729 lport
->tt
.disc_stop
= fc_disc_stop
;
731 if (!lport
->tt
.disc_stop_final
)
732 lport
->tt
.disc_stop_final
= fc_disc_stop_final
;
734 if (!lport
->tt
.disc_recv_req
)
735 lport
->tt
.disc_recv_req
= fc_disc_recv_req
;
741 EXPORT_SYMBOL(fc_disc_config
);
744 * fc_disc_init() - Initialize the discovery layer for a local port
745 * @lport: The local port that needs the discovery layer to be initialized
747 void fc_disc_init(struct fc_lport
*lport
)
749 struct fc_disc
*disc
= &lport
->disc
;
751 INIT_DELAYED_WORK(&disc
->disc_work
, fc_disc_timeout
);
752 mutex_init(&disc
->disc_mutex
);
753 INIT_LIST_HEAD(&disc
->rports
);
755 EXPORT_SYMBOL(fc_disc_init
);