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 #define FC_DISC_DELAY 3
48 static int fc_disc_debug;
50 #define FC_DEBUG_DISC(fmt...) \
56 static void fc_disc_gpn_ft_req(struct fc_disc *);
57 static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
58 static int fc_disc_new_target(struct fc_disc *, struct fc_rport *,
59 struct fc_rport_identifiers *);
60 static void fc_disc_del_target(struct fc_disc *, struct fc_rport *);
61 static void fc_disc_done(struct fc_disc *);
62 static void fc_disc_timeout(struct work_struct *);
63 static void fc_disc_single(struct fc_disc *, struct fc_disc_port *);
64 static void fc_disc_restart(struct fc_disc *);
67 * fc_disc_lookup_rport() - lookup a remote port by port_id
68 * @lport: Fibre Channel host port instance
69 * @port_id: remote port port_id to match
71 struct fc_rport *fc_disc_lookup_rport(const struct fc_lport *lport,
74 const struct fc_disc *disc = &lport->disc;
75 struct fc_rport *rport, *found = NULL;
76 struct fc_rport_libfc_priv *rdata;
79 list_for_each_entry(rdata, &disc->rports, peers) {
80 rport = PRIV_TO_RPORT(rdata);
81 if (rport->port_id == port_id) {
95 * fc_disc_stop_rports() - delete all the remote ports associated with the lport
96 * @disc: The discovery job to stop rports on
98 * Locking Note: This function expects that the lport mutex is locked before
101 void fc_disc_stop_rports(struct fc_disc *disc)
103 struct fc_lport *lport;
104 struct fc_rport *rport;
105 struct fc_rport_libfc_priv *rdata, *next;
109 mutex_lock(&disc->disc_mutex);
110 list_for_each_entry_safe(rdata, next, &disc->rports, peers) {
111 rport = PRIV_TO_RPORT(rdata);
112 list_del(&rdata->peers);
113 lport->tt.rport_logoff(rport);
116 list_for_each_entry_safe(rdata, next, &disc->rogue_rports, peers) {
117 rport = PRIV_TO_RPORT(rdata);
118 lport->tt.rport_logoff(rport);
121 mutex_unlock(&disc->disc_mutex);
125 * fc_disc_rport_callback() - Event handler for rport events
126 * @lport: The lport which is receiving the event
127 * @rport: The rport which the event has occured on
128 * @event: The event that occured
130 * Locking Note: The rport lock should not be held when calling
133 static void fc_disc_rport_callback(struct fc_lport *lport,
134 struct fc_rport *rport,
135 enum fc_rport_event event)
137 struct fc_rport_libfc_priv *rdata = rport->dd_data;
138 struct fc_disc *disc = &lport->disc;
140 FC_DEBUG_DISC("Received a %d event for port (%6x)\n", event,
144 case RPORT_EV_CREATED:
146 mutex_lock(&disc->disc_mutex);
147 list_add_tail(&rdata->peers, &disc->rports);
148 mutex_unlock(&disc->disc_mutex);
152 case RPORT_EV_FAILED:
154 mutex_lock(&disc->disc_mutex);
155 mutex_lock(&rdata->rp_mutex);
156 if (rdata->trans_state == FC_PORTSTATE_ROGUE)
157 list_del(&rdata->peers);
158 mutex_unlock(&rdata->rp_mutex);
159 mutex_unlock(&disc->disc_mutex);
168 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
169 * @sp: Current sequence of the RSCN exchange
171 * @lport: Fibre Channel host port instance
173 * Locking Note: This function expects that the disc_mutex is locked
174 * before it is called.
176 static void fc_disc_recv_rscn_req(struct fc_seq *sp, struct fc_frame *fp,
177 struct fc_disc *disc)
179 struct fc_lport *lport;
180 struct fc_rport *rport;
181 struct fc_rport_libfc_priv *rdata;
182 struct fc_els_rscn *rp;
183 struct fc_els_rscn_page *pp;
184 struct fc_seq_els_data rjt_data;
187 enum fc_els_rscn_ev_qual ev_qual;
188 enum fc_els_rscn_addr_fmt fmt;
189 LIST_HEAD(disc_ports);
190 struct fc_disc_port *dp, *next;
194 FC_DEBUG_DISC("Received an RSCN event on port (%6x)\n",
195 fc_host_port_id(lport->host));
197 /* make sure the frame contains an RSCN message */
198 rp = fc_frame_payload_get(fp, sizeof(*rp));
201 /* make sure the page length is as expected (4 bytes) */
202 if (rp->rscn_page_len != sizeof(*pp))
204 /* get the RSCN payload length */
205 len = ntohs(rp->rscn_plen);
206 if (len < sizeof(*rp))
208 /* make sure the frame contains the expected payload */
209 rp = fc_frame_payload_get(fp, len);
212 /* payload must be a multiple of the RSCN page size */
214 if (len % sizeof(*pp))
217 for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
218 ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
219 ev_qual &= ELS_RSCN_EV_QUAL_MASK;
220 fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
221 fmt &= ELS_RSCN_ADDR_FMT_MASK;
223 * if we get an address format other than port
224 * (area, domain, fabric), then do a full discovery
227 case ELS_ADDR_FMT_PORT:
228 FC_DEBUG_DISC("Port address format for port (%6x)\n",
229 ntoh24(pp->rscn_fid));
230 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
236 dp->ids.port_id = ntoh24(pp->rscn_fid);
237 dp->ids.port_name = -1;
238 dp->ids.node_name = -1;
239 dp->ids.roles = FC_RPORT_ROLE_UNKNOWN;
240 list_add_tail(&dp->peers, &disc_ports);
242 case ELS_ADDR_FMT_AREA:
243 case ELS_ADDR_FMT_DOM:
244 case ELS_ADDR_FMT_FAB:
246 FC_DEBUG_DISC("Address format is (%d)\n", fmt);
251 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
253 FC_DEBUG_DISC("RSCN received: rediscovering\n");
254 fc_disc_restart(disc);
256 FC_DEBUG_DISC("RSCN received: not rediscovering. "
257 "redisc %d state %d in_prog %d\n",
258 redisc, lport->state, disc->pending);
259 list_for_each_entry_safe(dp, next, &disc_ports, peers) {
260 list_del(&dp->peers);
261 rport = lport->tt.rport_lookup(lport, dp->ids.port_id);
263 rdata = rport->dd_data;
264 list_del(&rdata->peers);
265 lport->tt.rport_logoff(rport);
267 fc_disc_single(disc, dp);
273 FC_DEBUG_DISC("Received a bad RSCN frame\n");
275 rjt_data.reason = ELS_RJT_LOGIC;
276 rjt_data.explan = ELS_EXPL_NONE;
277 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
282 * fc_disc_recv_req() - Handle incoming requests
283 * @sp: Current sequence of the request exchange
285 * @lport: The FC local port
287 * Locking Note: This function is called from the EM and will lock
288 * the disc_mutex before calling the handler for the
291 static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp,
292 struct fc_lport *lport)
295 struct fc_disc *disc = &lport->disc;
297 op = fc_frame_payload_op(fp);
300 mutex_lock(&disc->disc_mutex);
301 fc_disc_recv_rscn_req(sp, fp, disc);
302 mutex_unlock(&disc->disc_mutex);
305 FC_DBG("Received an unsupported request. opcode (%x)\n", op);
311 * fc_disc_restart() - Restart discovery
312 * @lport: FC discovery context
314 * Locking Note: This function expects that the disc mutex
317 static void fc_disc_restart(struct fc_disc *disc)
319 struct fc_rport *rport;
320 struct fc_rport_libfc_priv *rdata, *next;
321 struct fc_lport *lport = disc->lport;
323 FC_DEBUG_DISC("Restarting discovery for port (%6x)\n",
324 fc_host_port_id(lport->host));
326 list_for_each_entry_safe(rdata, next, &disc->rports, peers) {
327 rport = PRIV_TO_RPORT(rdata);
328 FC_DEBUG_DISC("list_del(%6x)\n", rport->port_id);
329 list_del(&rdata->peers);
330 lport->tt.rport_logoff(rport);
335 fc_disc_gpn_ft_req(disc);
339 * fc_disc_start() - Fibre Channel Target discovery
340 * @lport: FC local port
342 * Returns non-zero if discovery cannot be started.
344 static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
346 struct fc_lport *lport)
348 struct fc_rport *rport;
349 struct fc_rport_identifiers ids;
350 struct fc_disc *disc = &lport->disc;
353 * At this point we may have a new disc job or an existing
354 * one. Either way, let's lock when we make changes to it
355 * and send the GPN_FT request.
357 mutex_lock(&disc->disc_mutex);
359 disc->disc_callback = disc_callback;
362 * If not ready, or already running discovery, just set request flag.
367 mutex_unlock(&disc->disc_mutex);
372 * Handle point-to-point mode as a simple discovery
373 * of the remote port. Yucky, yucky, yuck, yuck!
375 rport = disc->lport->ptp_rp;
377 ids.port_id = rport->port_id;
378 ids.port_name = rport->port_name;
379 ids.node_name = rport->node_name;
380 ids.roles = FC_RPORT_ROLE_UNKNOWN;
381 get_device(&rport->dev);
383 if (!fc_disc_new_target(disc, rport, &ids)) {
384 disc->event = DISC_EV_SUCCESS;
387 put_device(&rport->dev);
389 fc_disc_gpn_ft_req(disc); /* get ports by FC-4 type */
392 mutex_unlock(&disc->disc_mutex);
395 static struct fc_rport_operations fc_disc_rport_ops = {
396 .event_callback = fc_disc_rport_callback,
400 * fc_disc_new_target() - Handle new target found by discovery
401 * @lport: FC local port
402 * @rport: The previous FC remote port (NULL if new remote port)
403 * @ids: Identifiers for the new FC remote port
405 * Locking Note: This function expects that the disc_mutex is locked
406 * before it is called.
408 static int fc_disc_new_target(struct fc_disc *disc,
409 struct fc_rport *rport,
410 struct fc_rport_identifiers *ids)
412 struct fc_lport *lport = disc->lport;
413 struct fc_rport_libfc_priv *rdata;
416 if (rport && ids->port_name) {
417 if (rport->port_name == -1) {
419 * Set WWN and fall through to notify of create.
421 fc_rport_set_name(rport, ids->port_name,
423 } else if (rport->port_name != ids->port_name) {
425 * This is a new port with the same FCID as
426 * a previously-discovered port. Presumably the old
427 * port logged out and a new port logged in and was
428 * assigned the same FCID. This should be rare.
429 * Delete the old one and fall thru to re-create.
431 fc_disc_del_target(disc, rport);
435 if (((ids->port_name != -1) || (ids->port_id != -1)) &&
436 ids->port_id != fc_host_port_id(lport->host) &&
437 ids->port_name != lport->wwpn) {
439 rport = lport->tt.rport_lookup(lport, ids->port_id);
441 struct fc_disc_port dp;
443 dp.ids.port_id = ids->port_id;
444 dp.ids.port_name = ids->port_name;
445 dp.ids.node_name = ids->node_name;
446 dp.ids.roles = ids->roles;
447 rport = lport->tt.rport_create(&dp);
453 rdata = rport->dd_data;
454 rdata->ops = &fc_disc_rport_ops;
455 rdata->rp_state = RPORT_ST_INIT;
456 list_add_tail(&rdata->peers, &disc->rogue_rports);
457 lport->tt.rport_login(rport);
464 * fc_disc_del_target() - Delete a target
465 * @disc: FC discovery context
466 * @rport: The remote port to be removed
468 static void fc_disc_del_target(struct fc_disc *disc, struct fc_rport *rport)
470 struct fc_lport *lport = disc->lport;
471 struct fc_rport_libfc_priv *rdata = rport->dd_data;
472 list_del(&rdata->peers);
473 lport->tt.rport_logoff(rport);
477 * fc_disc_done() - Discovery has been completed
478 * @disc: FC discovery context
479 * Locking Note: This function expects that the disc mutex is locked before
480 * it is called. The discovery callback is then made with the lock released,
481 * and the lock is re-taken before returning from this function
483 static void fc_disc_done(struct fc_disc *disc)
485 struct fc_lport *lport = disc->lport;
486 enum fc_disc_event event;
488 FC_DEBUG_DISC("Discovery complete for port (%6x)\n",
489 fc_host_port_id(lport->host));
492 disc->event = DISC_EV_NONE;
495 fc_disc_gpn_ft_req(disc);
499 mutex_unlock(&disc->disc_mutex);
500 disc->disc_callback(lport, event);
501 mutex_lock(&disc->disc_mutex);
505 * fc_disc_error() - Handle error on dNS request
506 * @disc: FC discovery context
507 * @fp: The frame pointer
509 static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
511 struct fc_lport *lport = disc->lport;
512 unsigned long delay = 0;
514 FC_DBG("Error %ld, retries %d/%d\n",
515 PTR_ERR(fp), disc->retry_count,
516 FC_DISC_RETRY_LIMIT);
518 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
520 * Memory allocation failure, or the exchange timed out,
523 if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
524 /* go ahead and retry */
526 delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
528 delay = msecs_to_jiffies(lport->e_d_tov);
530 /* timeout faster first time */
531 if (!disc->retry_count)
535 schedule_delayed_work(&disc->disc_work, delay);
537 /* exceeded retries */
538 disc->event = DISC_EV_FAILED;
545 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
546 * @lport: FC discovery context
548 * Locking Note: This function expects that the disc_mutex is locked
549 * before it is called.
551 static void fc_disc_gpn_ft_req(struct fc_disc *disc)
554 struct fc_lport *lport = disc->lport;
556 WARN_ON(!fc_lport_test_ready(lport));
563 fp = fc_frame_alloc(lport,
564 sizeof(struct fc_ct_hdr) +
565 sizeof(struct fc_ns_gid_ft));
569 if (lport->tt.elsct_send(lport, NULL, fp,
572 disc, lport->e_d_tov))
575 fc_disc_error(disc, fp);
579 * fc_disc_gpn_ft_parse() - Parse the list of IDs and names resulting from a request
580 * @lport: Fibre Channel host port instance
581 * @buf: GPN_FT response buffer
582 * @len: size of response buffer
584 static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
586 struct fc_lport *lport;
587 struct fc_gpn_ft_resp *np;
592 struct fc_disc_port dp;
593 struct fc_rport *rport;
594 struct fc_rport_libfc_priv *rdata;
599 * Handle partial name record left over from previous call.
603 np = (struct fc_gpn_ft_resp *)bp;
604 tlen = disc->buf_len;
606 WARN_ON(tlen >= sizeof(*np));
607 plen = sizeof(*np) - tlen;
609 WARN_ON(plen >= sizeof(*np));
612 np = &disc->partial_buf;
613 memcpy((char *)np + tlen, bp, plen);
616 * Set bp so that the loop below will advance it to the
617 * first valid full name element.
622 disc->buf_len = (unsigned char) plen;
623 if (plen == sizeof(*np))
628 * Handle full name records, including the one filled from above.
629 * Normally, np == bp and plen == len, but from the partial case above,
630 * bp, len describe the overall buffer, and np, plen describe the
631 * partial buffer, which if would usually be full now.
632 * After the first time through the loop, things return to "normal".
634 while (plen >= sizeof(*np)) {
636 dp.ids.port_id = ntoh24(np->fp_fid);
637 dp.ids.port_name = ntohll(np->fp_wwpn);
638 dp.ids.node_name = -1;
639 dp.ids.roles = FC_RPORT_ROLE_UNKNOWN;
641 if ((dp.ids.port_id != fc_host_port_id(lport->host)) &&
642 (dp.ids.port_name != lport->wwpn)) {
643 rport = lport->tt.rport_create(&dp);
645 rdata = rport->dd_data;
646 rdata->ops = &fc_disc_rport_ops;
647 rdata->local_port = lport;
648 list_add_tail(&rdata->peers,
649 &disc->rogue_rports);
650 lport->tt.rport_login(rport);
652 FC_DBG("Failed to allocate memory for "
653 "the newly discovered port (%6x)\n",
657 if (np->fp_flags & FC_NS_FID_LAST) {
658 disc->event = DISC_EV_SUCCESS;
665 np = (struct fc_gpn_ft_resp *)bp;
670 * Save any partial record at the end of the buffer for next time.
672 if (error == 0 && len > 0 && len < sizeof(*np)) {
673 if (np != &disc->partial_buf) {
674 FC_DEBUG_DISC("Partial buffer remains "
675 "for discovery by (%6x)\n",
676 fc_host_port_id(lport->host));
677 memcpy(&disc->partial_buf, np, len);
679 disc->buf_len = (unsigned char) len;
687 * fc_disc_timeout() - Retry handler for the disc component
688 * @work: Structure holding disc obj that needs retry discovery
690 * Handle retry of memory allocation for remote ports.
692 static void fc_disc_timeout(struct work_struct *work)
694 struct fc_disc *disc = container_of(work,
697 mutex_lock(&disc->disc_mutex);
698 if (disc->requested && !disc->pending)
699 fc_disc_gpn_ft_req(disc);
700 mutex_unlock(&disc->disc_mutex);
704 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
705 * @sp: Current sequence of GPN_FT exchange
706 * @fp: response frame
707 * @lp_arg: Fibre Channel host port instance
709 * Locking Note: This function is called without disc mutex held, and
710 * should do all its processing with the mutex held
712 static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
715 struct fc_disc *disc = disc_arg;
716 struct fc_ct_hdr *cp;
717 struct fc_frame_header *fh;
718 unsigned int seq_cnt;
723 mutex_lock(&disc->disc_mutex);
724 FC_DEBUG_DISC("Received a GPN_FT response on port (%6x)\n",
725 fc_host_port_id(disc->lport->host));
728 fc_disc_error(disc, fp);
729 mutex_unlock(&disc->disc_mutex);
733 WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
734 fh = fc_frame_header_get(fp);
735 len = fr_len(fp) - sizeof(*fh);
736 seq_cnt = ntohs(fh->fh_seq_cnt);
737 if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 &&
738 disc->seq_count == 0) {
739 cp = fc_frame_payload_get(fp, sizeof(*cp));
741 FC_DBG("GPN_FT response too short, len %d\n",
743 } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
745 /* Accepted, parse the response. */
748 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
749 FC_DBG("GPN_FT rejected reason %x exp %x "
750 "(check zoning)\n", cp->ct_reason,
752 disc->event = DISC_EV_FAILED;
755 FC_DBG("GPN_FT unexpected response code %x\n",
758 } else if (fr_sof(fp) == FC_SOF_N3 &&
759 seq_cnt == disc->seq_count) {
762 FC_DBG("GPN_FT unexpected frame - out of sequence? "
763 "seq_cnt %x expected %x sof %x eof %x\n",
764 seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
767 error = fc_disc_gpn_ft_parse(disc, buf, len);
769 fc_disc_error(disc, fp);
775 mutex_unlock(&disc->disc_mutex);
779 * fc_disc_single() - Discover the directory information for a single target
780 * @lport: FC local port
781 * @dp: The port to rediscover
783 * Locking Note: This function expects that the disc_mutex is locked
784 * before it is called.
786 static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp)
788 struct fc_lport *lport;
789 struct fc_rport *new_rport;
790 struct fc_rport_libfc_priv *rdata;
794 if (dp->ids.port_id == fc_host_port_id(lport->host))
797 new_rport = lport->tt.rport_create(dp);
799 rdata = new_rport->dd_data;
800 rdata->ops = &fc_disc_rport_ops;
802 list_add_tail(&rdata->peers, &disc->rogue_rports);
803 lport->tt.rport_login(new_rport);
811 * fc_disc_stop() - Stop discovery for a given lport
812 * @lport: The lport that discovery should stop for
814 void fc_disc_stop(struct fc_lport *lport)
816 struct fc_disc *disc = &lport->disc;
819 cancel_delayed_work_sync(&disc->disc_work);
820 fc_disc_stop_rports(disc);
825 * fc_disc_stop_final() - Stop discovery for a given lport
826 * @lport: The lport that discovery should stop for
828 * This function will block until discovery has been
829 * completely stopped and all rports have been deleted.
831 void fc_disc_stop_final(struct fc_lport *lport)
834 lport->tt.rport_flush_queue();
838 * fc_disc_init() - Initialize the discovery block
839 * @lport: FC local port
841 int fc_disc_init(struct fc_lport *lport)
843 struct fc_disc *disc;
845 if (!lport->tt.disc_start)
846 lport->tt.disc_start = fc_disc_start;
848 if (!lport->tt.disc_stop)
849 lport->tt.disc_stop = fc_disc_stop;
851 if (!lport->tt.disc_stop_final)
852 lport->tt.disc_stop_final = fc_disc_stop_final;
854 if (!lport->tt.disc_recv_req)
855 lport->tt.disc_recv_req = fc_disc_recv_req;
857 if (!lport->tt.rport_lookup)
858 lport->tt.rport_lookup = fc_disc_lookup_rport;
861 INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
862 mutex_init(&disc->disc_mutex);
863 INIT_LIST_HEAD(&disc->rports);
864 INIT_LIST_HEAD(&disc->rogue_rports);
867 disc->delay = FC_DISC_DELAY;
868 disc->event = DISC_EV_NONE;
872 EXPORT_SYMBOL(fc_disc_init);