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/err.h>
37 #include <asm/unaligned.h>
39 #include <scsi/fc/fc_gs.h>
41 #include <scsi/libfc.h>
43 #define FC_DISC_RETRY_LIMIT 3 /* max retries */
44 #define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */
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
*);
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
60 struct fc_rport_priv
*fc_disc_lookup_rport(const struct fc_lport
*lport
,
63 const struct fc_disc
*disc
= &lport
->disc
;
64 struct fc_rport_priv
*rdata
;
66 list_for_each_entry(rdata
, &disc
->rports
, peers
) {
67 if (rdata
->ids
.port_id
== port_id
&&
68 rdata
->rp_state
!= RPORT_ST_DELETE
)
75 * fc_disc_stop_rports() - delete all the remote ports associated with the lport
76 * @disc: The discovery job to stop rports on
78 * Locking Note: This function expects that the lport mutex is locked before
81 void fc_disc_stop_rports(struct fc_disc
*disc
)
83 struct fc_lport
*lport
;
84 struct fc_rport_priv
*rdata
, *next
;
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
);
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
100 * Locking Note: The rport lock should not be held when calling
103 static void fc_disc_rport_callback(struct fc_lport
*lport
,
104 struct fc_rport_priv
*rdata
,
105 enum fc_rport_event event
)
107 struct fc_disc
*disc
= &lport
->disc
;
109 FC_DISC_DBG(disc
, "Received a %d event for port (%6x)\n", event
,
116 case RPORT_EV_FAILED
:
126 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
127 * @sp: Current sequence of the RSCN exchange
129 * @lport: Fibre Channel host port instance
131 * Locking Note: This function expects that the disc_mutex is locked
132 * before it is called.
134 static void fc_disc_recv_rscn_req(struct fc_seq
*sp
, struct fc_frame
*fp
,
135 struct fc_disc
*disc
)
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
;
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
;
151 FC_DISC_DBG(disc
, "Received an RSCN event\n");
153 /* make sure the frame contains an RSCN message */
154 rp
= fc_frame_payload_get(fp
, sizeof(*rp
));
157 /* make sure the page length is as expected (4 bytes) */
158 if (rp
->rscn_page_len
!= sizeof(*pp
))
160 /* get the RSCN payload length */
161 len
= ntohs(rp
->rscn_plen
);
162 if (len
< sizeof(*rp
))
164 /* make sure the frame contains the expected payload */
165 rp
= fc_frame_payload_get(fp
, len
);
168 /* payload must be a multiple of the RSCN page size */
170 if (len
% sizeof(*pp
))
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
;
179 * if we get an address format other than port
180 * (area, domain, fabric), then do a full discovery
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
);
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
);
198 case ELS_ADDR_FMT_AREA
:
199 case ELS_ADDR_FMT_DOM
:
200 case ELS_ADDR_FMT_FAB
:
202 FC_DISC_DBG(disc
, "Address format is (%d)\n", fmt
);
207 lport
->tt
.seq_els_rsp_send(sp
, ELS_LS_ACC
, NULL
);
209 FC_DISC_DBG(disc
, "RSCN received: rediscovering\n");
210 fc_disc_restart(disc
);
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
);
219 lport
->tt
.rport_logoff(rdata
);
221 fc_disc_single(disc
, dp
);
227 FC_DISC_DBG(disc
, "Received a bad RSCN frame\n");
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
);
236 * fc_disc_recv_req() - Handle incoming requests
237 * @sp: Current sequence of the request exchange
239 * @lport: The FC local port
241 * Locking Note: This function is called from the EM and will lock
242 * the disc_mutex before calling the handler for the
245 static void fc_disc_recv_req(struct fc_seq
*sp
, struct fc_frame
*fp
,
246 struct fc_lport
*lport
)
249 struct fc_disc
*disc
= &lport
->disc
;
251 op
= fc_frame_payload_op(fp
);
254 mutex_lock(&disc
->disc_mutex
);
255 fc_disc_recv_rscn_req(sp
, fp
, disc
);
256 mutex_unlock(&disc
->disc_mutex
);
259 FC_DISC_DBG(disc
, "Received an unsupported request, "
260 "the opcode is (%x)\n", op
);
266 * fc_disc_restart() - Restart discovery
267 * @lport: FC discovery context
269 * Locking Note: This function expects that the disc mutex
272 static void fc_disc_restart(struct fc_disc
*disc
)
274 struct fc_rport_priv
*rdata
, *next
;
275 struct fc_lport
*lport
= disc
->lport
;
277 FC_DISC_DBG(disc
, "Restarting discovery\n");
279 list_for_each_entry_safe(rdata
, next
, &disc
->rports
, peers
)
280 lport
->tt
.rport_logoff(rdata
);
284 fc_disc_gpn_ft_req(disc
);
288 * fc_disc_start() - Fibre Channel Target discovery
289 * @lport: FC local port
291 * Returns non-zero if discovery cannot be started.
293 static void fc_disc_start(void (*disc_callback
)(struct fc_lport
*,
295 struct fc_lport
*lport
)
297 struct fc_rport_priv
*rdata
;
298 struct fc_disc
*disc
= &lport
->disc
;
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.
305 mutex_lock(&disc
->disc_mutex
);
307 disc
->disc_callback
= disc_callback
;
310 * If not ready, or already running discovery, just set request flag.
315 mutex_unlock(&disc
->disc_mutex
);
320 * Handle point-to-point mode as a simple discovery
321 * of the remote port. Yucky, yucky, yuck, yuck!
323 rdata
= disc
->lport
->ptp_rp
;
325 kref_get(&rdata
->kref
);
326 if (!fc_disc_new_target(disc
, rdata
, &rdata
->ids
)) {
327 fc_disc_done(disc
, DISC_EV_SUCCESS
);
329 kref_put(&rdata
->kref
, rdata
->local_port
->tt
.rport_destroy
);
331 fc_disc_gpn_ft_req(disc
); /* get ports by FC-4 type */
334 mutex_unlock(&disc
->disc_mutex
);
337 static struct fc_rport_operations fc_disc_rport_ops
= {
338 .event_callback
= fc_disc_rport_callback
,
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
347 * Locking Note: This function expects that the disc_mutex is locked
348 * before it is called.
350 static int fc_disc_new_target(struct fc_disc
*disc
,
351 struct fc_rport_priv
*rdata
,
352 struct fc_rport_identifiers
*ids
)
354 struct fc_lport
*lport
= disc
->lport
;
357 if (rdata
&& ids
->port_name
) {
358 if (rdata
->ids
.port_name
== -1) {
360 * Set WWN and fall through to notify of create.
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
) {
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.
372 lport
->tt
.rport_logoff(rdata
);
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
) {
380 rdata
= lport
->tt
.rport_create(lport
, ids
);
385 rdata
->ops
= &fc_disc_rport_ops
;
386 lport
->tt
.rport_login(rdata
);
393 * fc_disc_done() - Discovery has been completed
394 * @disc: FC discovery context
395 * @event: discovery completion status
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
401 static void fc_disc_done(struct fc_disc
*disc
, enum fc_disc_event event
)
403 struct fc_lport
*lport
= disc
->lport
;
405 FC_DISC_DBG(disc
, "Discovery complete\n");
408 fc_disc_gpn_ft_req(disc
);
412 mutex_unlock(&disc
->disc_mutex
);
413 disc
->disc_callback(lport
, event
);
414 mutex_lock(&disc
->disc_mutex
);
418 * fc_disc_error() - Handle error on dNS request
419 * @disc: FC discovery context
420 * @fp: The frame pointer
422 static void fc_disc_error(struct fc_disc
*disc
, struct fc_frame
*fp
)
424 struct fc_lport
*lport
= disc
->lport
;
425 unsigned long delay
= 0;
427 FC_DISC_DBG(disc
, "Error %ld, retries %d/%d\n",
428 PTR_ERR(fp
), disc
->retry_count
,
429 FC_DISC_RETRY_LIMIT
);
431 if (!fp
|| PTR_ERR(fp
) == -FC_EX_TIMEOUT
) {
433 * Memory allocation failure, or the exchange timed out,
436 if (disc
->retry_count
< FC_DISC_RETRY_LIMIT
) {
437 /* go ahead and retry */
439 delay
= msecs_to_jiffies(FC_DISC_RETRY_DELAY
);
441 delay
= msecs_to_jiffies(lport
->e_d_tov
);
443 /* timeout faster first time */
444 if (!disc
->retry_count
)
448 schedule_delayed_work(&disc
->disc_work
, delay
);
450 fc_disc_done(disc
, DISC_EV_FAILED
);
455 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
456 * @lport: FC discovery context
458 * Locking Note: This function expects that the disc_mutex is locked
459 * before it is called.
461 static void fc_disc_gpn_ft_req(struct fc_disc
*disc
)
464 struct fc_lport
*lport
= disc
->lport
;
466 WARN_ON(!fc_lport_test_ready(lport
));
473 fp
= fc_frame_alloc(lport
,
474 sizeof(struct fc_ct_hdr
) +
475 sizeof(struct fc_ns_gid_ft
));
479 if (lport
->tt
.elsct_send(lport
, 0, fp
,
482 disc
, lport
->e_d_tov
))
485 fc_disc_error(disc
, fp
);
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
494 * Goes through the list of IDs and names resulting from a request.
496 static int fc_disc_gpn_ft_parse(struct fc_disc
*disc
, void *buf
, size_t len
)
498 struct fc_lport
*lport
;
499 struct fc_gpn_ft_resp
*np
;
504 struct fc_rport_identifiers ids
;
505 struct fc_rport_priv
*rdata
;
510 * Handle partial name record left over from previous call.
514 np
= (struct fc_gpn_ft_resp
*)bp
;
515 tlen
= disc
->buf_len
;
517 WARN_ON(tlen
>= sizeof(*np
));
518 plen
= sizeof(*np
) - tlen
;
520 WARN_ON(plen
>= sizeof(*np
));
523 np
= &disc
->partial_buf
;
524 memcpy((char *)np
+ tlen
, bp
, plen
);
527 * Set bp so that the loop below will advance it to the
528 * first valid full name element.
533 disc
->buf_len
= (unsigned char) plen
;
534 if (plen
== sizeof(*np
))
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".
545 while (plen
>= sizeof(*np
)) {
546 ids
.port_id
= ntoh24(np
->fp_fid
);
547 ids
.port_name
= ntohll(np
->fp_wwpn
);
549 ids
.roles
= FC_RPORT_ROLE_UNKNOWN
;
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
);
555 rdata
->ops
= &fc_disc_rport_ops
;
556 lport
->tt
.rport_login(rdata
);
558 printk(KERN_WARNING
"libfc: Failed to allocate "
559 "memory for the newly discovered port "
560 "(%6x)\n", ids
.port_id
);
563 if (np
->fp_flags
& FC_NS_FID_LAST
) {
564 fc_disc_done(disc
, DISC_EV_SUCCESS
);
570 np
= (struct fc_gpn_ft_resp
*)bp
;
575 * Save any partial record at the end of the buffer for next time.
577 if (error
== 0 && len
> 0 && len
< sizeof(*np
)) {
578 if (np
!= &disc
->partial_buf
) {
579 FC_DISC_DBG(disc
, "Partial buffer remains "
581 memcpy(&disc
->partial_buf
, np
, len
);
583 disc
->buf_len
= (unsigned char) len
;
591 * fc_disc_timeout() - Retry handler for the disc component
592 * @work: Structure holding disc obj that needs retry discovery
594 * Handle retry of memory allocation for remote ports.
596 static void fc_disc_timeout(struct work_struct
*work
)
598 struct fc_disc
*disc
= container_of(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
);
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
613 * Locking Note: This function is called without disc mutex held, and
614 * should do all its processing with the mutex held
616 static void fc_disc_gpn_ft_resp(struct fc_seq
*sp
, struct fc_frame
*fp
,
619 struct fc_disc
*disc
= disc_arg
;
620 struct fc_ct_hdr
*cp
;
621 struct fc_frame_header
*fh
;
622 unsigned int seq_cnt
;
627 mutex_lock(&disc
->disc_mutex
);
628 FC_DISC_DBG(disc
, "Received a GPN_FT response\n");
631 fc_disc_error(disc
, fp
);
632 mutex_unlock(&disc
->disc_mutex
);
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
));
644 FC_DISC_DBG(disc
, "GPN_FT response too short, len %d\n",
646 } else if (ntohs(cp
->ct_cmd
) == FC_FS_ACC
) {
648 /* Accepted, parse the response. */
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
,
655 fc_disc_done(disc
, DISC_EV_FAILED
);
657 FC_DISC_DBG(disc
, "GPN_FT unexpected response code "
658 "%x\n", ntohs(cp
->ct_cmd
));
660 } else if (fr_sof(fp
) == FC_SOF_N3
&&
661 seq_cnt
== disc
->seq_count
) {
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
));
669 error
= fc_disc_gpn_ft_parse(disc
, buf
, len
);
671 fc_disc_error(disc
, fp
);
677 mutex_unlock(&disc
->disc_mutex
);
681 * fc_disc_single() - Discover the directory information for a single target
682 * @lport: FC local port
683 * @dp: The port to rediscover
685 * Locking Note: This function expects that the disc_mutex is locked
686 * before it is called.
688 static void fc_disc_single(struct fc_disc
*disc
, struct fc_disc_port
*dp
)
690 struct fc_lport
*lport
;
691 struct fc_rport_priv
*rdata
;
695 if (dp
->ids
.port_id
== fc_host_port_id(lport
->host
))
698 rdata
= lport
->tt
.rport_create(lport
, &dp
->ids
);
700 rdata
->ops
= &fc_disc_rport_ops
;
702 lport
->tt
.rport_login(rdata
);
710 * fc_disc_stop() - Stop discovery for a given lport
711 * @lport: The lport that discovery should stop for
713 void fc_disc_stop(struct fc_lport
*lport
)
715 struct fc_disc
*disc
= &lport
->disc
;
718 cancel_delayed_work_sync(&disc
->disc_work
);
719 fc_disc_stop_rports(disc
);
724 * fc_disc_stop_final() - Stop discovery for a given lport
725 * @lport: The lport that discovery should stop for
727 * This function will block until discovery has been
728 * completely stopped and all rports have been deleted.
730 void fc_disc_stop_final(struct fc_lport
*lport
)
733 lport
->tt
.rport_flush_queue();
737 * fc_disc_init() - Initialize the discovery block
738 * @lport: FC local port
740 int fc_disc_init(struct fc_lport
*lport
)
742 struct fc_disc
*disc
;
744 if (!lport
->tt
.disc_start
)
745 lport
->tt
.disc_start
= fc_disc_start
;
747 if (!lport
->tt
.disc_stop
)
748 lport
->tt
.disc_stop
= fc_disc_stop
;
750 if (!lport
->tt
.disc_stop_final
)
751 lport
->tt
.disc_stop_final
= fc_disc_stop_final
;
753 if (!lport
->tt
.disc_recv_req
)
754 lport
->tt
.disc_recv_req
= fc_disc_recv_req
;
756 if (!lport
->tt
.rport_lookup
)
757 lport
->tt
.rport_lookup
= fc_disc_lookup_rport
;
760 INIT_DELAYED_WORK(&disc
->disc_work
, fc_disc_timeout
);
761 mutex_init(&disc
->disc_mutex
);
762 INIT_LIST_HEAD(&disc
->rports
);
768 EXPORT_SYMBOL(fc_disc_init
);