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 * Locking Note: This function expects that the lport mutex is locked before
66 static void fc_disc_stop_rports(struct fc_disc
*disc
)
68 struct fc_lport
*lport
;
69 struct fc_rport_priv
*rdata
;
71 lport
= fc_disc_lport(disc
);
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
);
84 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
85 * @disc: The discovery object to which the RSCN applies
88 * Locking Note: This function expects that the disc_mutex is locked
89 * before it is called.
91 static void fc_disc_recv_rscn_req(struct fc_disc
*disc
, struct fc_frame
*fp
)
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
;
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
;
104 lport
= fc_disc_lport(disc
);
106 FC_DISC_DBG(disc
, "Received an RSCN event\n");
108 /* make sure the frame contains an RSCN message */
109 rp
= fc_frame_payload_get(fp
, sizeof(*rp
));
112 /* make sure the page length is as expected (4 bytes) */
113 if (rp
->rscn_page_len
!= sizeof(*pp
))
115 /* get the RSCN payload length */
116 len
= ntohs(rp
->rscn_plen
);
117 if (len
< sizeof(*rp
))
119 /* make sure the frame contains the expected payload */
120 rp
= fc_frame_payload_get(fp
, len
);
123 /* payload must be a multiple of the RSCN page size */
125 if (len
% sizeof(*pp
))
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
;
134 * if we get an address format other than port
135 * (area, domain, fabric), then do a full discovery
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
);
147 dp
->port_id
= ntoh24(pp
->rscn_fid
);
148 list_add_tail(&dp
->peers
, &disc_ports
);
150 case ELS_ADDR_FMT_AREA
:
151 case ELS_ADDR_FMT_DOM
:
152 case ELS_ADDR_FMT_FAB
:
154 FC_DISC_DBG(disc
, "Address format is (%d)\n", fmt
);
159 fc_seq_els_rsp_send(fp
, ELS_LS_ACC
, NULL
);
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.
167 list_for_each_entry_safe(dp
, next
, &disc_ports
, peers
) {
168 list_del(&dp
->peers
);
170 redisc
= fc_disc_single(lport
, dp
);
174 FC_DISC_DBG(disc
, "RSCN received: rediscovering\n");
175 fc_disc_restart(disc
);
177 FC_DISC_DBG(disc
, "RSCN received: not rediscovering. "
178 "redisc %d state %d in_prog %d\n",
179 redisc
, lport
->state
, disc
->pending
);
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
);
192 * fc_disc_recv_req() - Handle incoming requests
193 * @lport: The local port receiving the request
194 * @fp: The request frame
196 * Locking Note: This function is called from the EM and will lock
197 * the disc_mutex before calling the handler for the
200 static void fc_disc_recv_req(struct fc_lport
*lport
, struct fc_frame
*fp
)
203 struct fc_disc
*disc
= &lport
->disc
;
205 op
= fc_frame_payload_op(fp
);
208 mutex_lock(&disc
->disc_mutex
);
209 fc_disc_recv_rscn_req(disc
, fp
);
210 mutex_unlock(&disc
->disc_mutex
);
213 FC_DISC_DBG(disc
, "Received an unsupported request, "
214 "the opcode is (%x)\n", op
);
221 * fc_disc_restart() - Restart discovery
222 * @disc: The discovery object to be restarted
224 * Locking Note: This function expects that the disc mutex
227 static void fc_disc_restart(struct fc_disc
*disc
)
229 if (!disc
->disc_callback
)
232 FC_DISC_DBG(disc
, "Restarting discovery\n");
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.
243 disc
->disc_id
= (disc
->disc_id
+ 2) | 1;
244 disc
->retry_count
= 0;
245 fc_disc_gpn_ft_req(disc
);
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
253 static void fc_disc_start(void (*disc_callback
)(struct fc_lport
*,
255 struct fc_lport
*lport
)
257 struct fc_disc
*disc
= &lport
->disc
;
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.
264 mutex_lock(&disc
->disc_mutex
);
265 disc
->disc_callback
= disc_callback
;
266 fc_disc_restart(disc
);
267 mutex_unlock(&disc
->disc_mutex
);
271 * fc_disc_done() - Discovery has been completed
272 * @disc: The discovery context
273 * @event: The discovery completion status
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
279 static void fc_disc_done(struct fc_disc
*disc
, enum fc_disc_event event
)
281 struct fc_lport
*lport
= fc_disc_lport(disc
);
282 struct fc_rport_priv
*rdata
;
284 FC_DISC_DBG(disc
, "Discovery complete\n");
287 if (disc
->requested
) {
288 fc_disc_restart(disc
);
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.
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.
301 list_for_each_entry(rdata
, &disc
->rports
, peers
) {
302 if (!kref_get_unless_zero(&rdata
->kref
))
304 if (rdata
->disc_id
) {
305 if (rdata
->disc_id
== disc
->disc_id
)
306 fc_rport_login(rdata
);
308 fc_rport_logoff(rdata
);
310 kref_put(&rdata
->kref
, fc_rport_destroy
);
312 mutex_unlock(&disc
->disc_mutex
);
313 disc
->disc_callback(lport
, event
);
314 mutex_lock(&disc
->disc_mutex
);
318 * fc_disc_error() - Handle error on dNS request
319 * @disc: The discovery context
320 * @fp: The error code encoded as a frame pointer
322 static void fc_disc_error(struct fc_disc
*disc
, struct fc_frame
*fp
)
324 struct fc_lport
*lport
= fc_disc_lport(disc
);
325 unsigned long delay
= 0;
327 FC_DISC_DBG(disc
, "Error %ld, retries %d/%d\n",
328 PTR_ERR(fp
), disc
->retry_count
,
329 FC_DISC_RETRY_LIMIT
);
331 if (!fp
|| PTR_ERR(fp
) == -FC_EX_TIMEOUT
) {
333 * Memory allocation failure, or the exchange timed out,
336 if (disc
->retry_count
< FC_DISC_RETRY_LIMIT
) {
337 /* go ahead and retry */
339 delay
= msecs_to_jiffies(FC_DISC_RETRY_DELAY
);
341 delay
= msecs_to_jiffies(lport
->e_d_tov
);
343 /* timeout faster first time */
344 if (!disc
->retry_count
)
348 schedule_delayed_work(&disc
->disc_work
, delay
);
350 fc_disc_done(disc
, DISC_EV_FAILED
);
351 } else if (PTR_ERR(fp
) == -FC_EX_CLOSED
) {
353 * if discovery fails due to lport reset, clear
354 * pending flag so that subsequent discovery can
362 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
363 * @lport: The discovery context
365 * Locking Note: This function expects that the disc_mutex is locked
366 * before it is called.
368 static void fc_disc_gpn_ft_req(struct fc_disc
*disc
)
371 struct fc_lport
*lport
= fc_disc_lport(disc
);
373 WARN_ON(!fc_lport_test_ready(lport
));
380 fp
= fc_frame_alloc(lport
,
381 sizeof(struct fc_ct_hdr
) +
382 sizeof(struct fc_ns_gid_ft
));
386 if (lport
->tt
.elsct_send(lport
, 0, fp
,
389 disc
, 3 * lport
->r_a_tov
))
392 fc_disc_error(disc
, NULL
);
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
401 * Goes through the list of IDs and names resulting from a request.
403 static int fc_disc_gpn_ft_parse(struct fc_disc
*disc
, void *buf
, size_t len
)
405 struct fc_lport
*lport
;
406 struct fc_gpn_ft_resp
*np
;
411 struct fc_rport_identifiers ids
;
412 struct fc_rport_priv
*rdata
;
414 lport
= fc_disc_lport(disc
);
418 * Handle partial name record left over from previous call.
422 np
= (struct fc_gpn_ft_resp
*)bp
;
423 tlen
= disc
->buf_len
;
426 WARN_ON(tlen
>= sizeof(*np
));
427 plen
= sizeof(*np
) - tlen
;
429 WARN_ON(plen
>= sizeof(*np
));
432 np
= &disc
->partial_buf
;
433 memcpy((char *)np
+ tlen
, bp
, plen
);
436 * Set bp so that the loop below will advance it to the
437 * first valid full name element.
442 disc
->buf_len
= (unsigned char) plen
;
443 if (plen
== sizeof(*np
))
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".
454 while (plen
>= sizeof(*np
)) {
455 ids
.port_id
= ntoh24(np
->fp_fid
);
456 ids
.port_name
= ntohll(np
->fp_wwpn
);
458 if (ids
.port_id
!= lport
->port_id
&&
459 ids
.port_name
!= lport
->wwpn
) {
460 rdata
= fc_rport_create(lport
, ids
.port_id
);
462 rdata
->ids
.port_name
= ids
.port_name
;
463 rdata
->disc_id
= disc
->disc_id
;
465 printk(KERN_WARNING
"libfc: Failed to allocate "
466 "memory for the newly discovered port "
467 "(%6.6x)\n", ids
.port_id
);
472 if (np
->fp_flags
& FC_NS_FID_LAST
) {
473 fc_disc_done(disc
, DISC_EV_SUCCESS
);
479 np
= (struct fc_gpn_ft_resp
*)bp
;
484 * Save any partial record at the end of the buffer for next time.
486 if (error
== 0 && len
> 0 && len
< sizeof(*np
)) {
487 if (np
!= &disc
->partial_buf
) {
488 FC_DISC_DBG(disc
, "Partial buffer remains "
490 memcpy(&disc
->partial_buf
, np
, len
);
492 disc
->buf_len
= (unsigned char) len
;
498 * fc_disc_timeout() - Handler for discovery timeouts
499 * @work: Structure holding discovery context that needs to retry discovery
501 static void fc_disc_timeout(struct work_struct
*work
)
503 struct fc_disc
*disc
= container_of(work
,
506 mutex_lock(&disc
->disc_mutex
);
507 fc_disc_gpn_ft_req(disc
);
508 mutex_unlock(&disc
->disc_mutex
);
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
517 * Locking Note: This function is called without disc mutex held, and
518 * should do all its processing with the mutex held
520 static void fc_disc_gpn_ft_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
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
;
531 mutex_lock(&disc
->disc_mutex
);
532 FC_DISC_DBG(disc
, "Received a GPN_FT response\n");
535 fc_disc_error(disc
, fp
);
536 mutex_unlock(&disc
->disc_mutex
);
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
));
547 FC_DISC_DBG(disc
, "GPN_FT response too short, len %d\n",
549 event
= DISC_EV_FAILED
;
550 } else if (ntohs(cp
->ct_cmd
) == FC_FS_ACC
) {
552 /* Accepted, parse the response. */
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
,
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
;
564 FC_DISC_DBG(disc
, "GPN_FT unexpected response code "
565 "%x\n", ntohs(cp
->ct_cmd
));
566 event
= DISC_EV_FAILED
;
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
);
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
;
577 fc_disc_error(disc
, ERR_PTR(error
));
578 else if (event
!= DISC_EV_NONE
)
579 fc_disc_done(disc
, event
);
581 mutex_unlock(&disc
->disc_mutex
);
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
590 * Locking Note: This function is called without disc mutex held.
592 static void fc_disc_gpn_id_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
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
;
603 lport
= rdata
->local_port
;
606 if (PTR_ERR(fp
) == -FC_EX_CLOSED
)
611 cp
= fc_frame_payload_get(fp
, sizeof(*cp
));
614 if (ntohs(cp
->ct_cmd
) == FC_FS_ACC
) {
615 if (fr_len(fp
) < sizeof(struct fc_frame_header
) +
616 sizeof(*cp
) + sizeof(*pn
))
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
);
633 new_rdata
->disc_id
= disc
->disc_id
;
634 fc_rport_login(new_rdata
);
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
);
646 FC_DISC_DBG(disc
, "GPN_ID unexpected response code %x\n",
649 mutex_lock(&disc
->disc_mutex
);
650 fc_disc_restart(disc
);
651 mutex_unlock(&disc
->disc_mutex
);
654 kref_put(&rdata
->kref
, fc_rport_destroy
);
658 * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request
659 * @lport: The local port to initiate discovery on
660 * @rdata: remote port private data
662 * Locking Note: This function expects that the disc_mutex is locked
663 * before it is called.
664 * On failure, an error code is returned.
666 static int fc_disc_gpn_id_req(struct fc_lport
*lport
,
667 struct fc_rport_priv
*rdata
)
671 fp
= fc_frame_alloc(lport
, sizeof(struct fc_ct_hdr
) +
672 sizeof(struct fc_ns_fid
));
675 if (!lport
->tt
.elsct_send(lport
, rdata
->ids
.port_id
, fp
, FC_NS_GPN_ID
,
676 fc_disc_gpn_id_resp
, rdata
,
679 kref_get(&rdata
->kref
);
684 * fc_disc_single() - Discover the directory information for a single target
685 * @lport: The local port the remote port is associated with
686 * @dp: The port to rediscover
688 * Locking Note: This function expects that the disc_mutex is locked
689 * before it is called.
691 static int fc_disc_single(struct fc_lport
*lport
, struct fc_disc_port
*dp
)
693 struct fc_rport_priv
*rdata
;
695 rdata
= fc_rport_create(lport
, dp
->port_id
);
699 return fc_disc_gpn_id_req(lport
, rdata
);
703 * fc_disc_stop() - Stop discovery for a given lport
704 * @lport: The local port that discovery should stop on
706 static void fc_disc_stop(struct fc_lport
*lport
)
708 struct fc_disc
*disc
= &lport
->disc
;
711 cancel_delayed_work_sync(&disc
->disc_work
);
712 fc_disc_stop_rports(disc
);
716 * fc_disc_stop_final() - Stop discovery for a given lport
717 * @lport: The lport that discovery should stop on
719 * This function will block until discovery has been
720 * completely stopped and all rports have been deleted.
722 static void fc_disc_stop_final(struct fc_lport
*lport
)
725 fc_rport_flush_queue();
729 * fc_disc_config() - Configure the discovery layer for a local port
730 * @lport: The local port that needs the discovery layer to be configured
731 * @priv: Private data structre for users of the discovery layer
733 void fc_disc_config(struct fc_lport
*lport
, void *priv
)
735 struct fc_disc
*disc
;
737 if (!lport
->tt
.disc_start
)
738 lport
->tt
.disc_start
= fc_disc_start
;
740 if (!lport
->tt
.disc_stop
)
741 lport
->tt
.disc_stop
= fc_disc_stop
;
743 if (!lport
->tt
.disc_stop_final
)
744 lport
->tt
.disc_stop_final
= fc_disc_stop_final
;
746 if (!lport
->tt
.disc_recv_req
)
747 lport
->tt
.disc_recv_req
= fc_disc_recv_req
;
753 EXPORT_SYMBOL(fc_disc_config
);
756 * fc_disc_init() - Initialize the discovery layer for a local port
757 * @lport: The local port that needs the discovery layer to be initialized
759 void fc_disc_init(struct fc_lport
*lport
)
761 struct fc_disc
*disc
= &lport
->disc
;
763 INIT_DELAYED_WORK(&disc
->disc_work
, fc_disc_timeout
);
764 mutex_init(&disc
->disc_mutex
);
765 INIT_LIST_HEAD(&disc
->rports
);
767 EXPORT_SYMBOL(fc_disc_init
);