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 void fc_disc_gpn_ft_req(struct fc_disc *);
49 static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
50 static int fc_disc_new_target(struct fc_disc *, struct fc_rport *,
51 struct fc_rport_identifiers *);
52 static void fc_disc_del_target(struct fc_disc *, struct fc_rport *);
53 static void fc_disc_done(struct fc_disc *);
54 static void fc_disc_timeout(struct work_struct *);
55 static void fc_disc_single(struct fc_disc *, struct fc_disc_port *);
56 static void fc_disc_restart(struct fc_disc *);
59 * fc_disc_lookup_rport() - lookup a remote port by port_id
60 * @lport: Fibre Channel host port instance
61 * @port_id: remote port port_id to match
63 struct fc_rport *fc_disc_lookup_rport(const struct fc_lport *lport,
66 const struct fc_disc *disc = &lport->disc;
67 struct fc_rport *rport, *found = NULL;
68 struct fc_rport_libfc_priv *rdata;
71 list_for_each_entry(rdata, &disc->rports, peers) {
72 rport = PRIV_TO_RPORT(rdata);
73 if (rport->port_id == port_id) {
87 * fc_disc_stop_rports() - delete all the remote ports associated with the lport
88 * @disc: The discovery job to stop rports on
90 * Locking Note: This function expects that the lport mutex is locked before
93 void fc_disc_stop_rports(struct fc_disc *disc)
95 struct fc_lport *lport;
96 struct fc_rport *rport;
97 struct fc_rport_libfc_priv *rdata, *next;
101 mutex_lock(&disc->disc_mutex);
102 list_for_each_entry_safe(rdata, next, &disc->rports, peers) {
103 rport = PRIV_TO_RPORT(rdata);
104 list_del(&rdata->peers);
105 lport->tt.rport_logoff(rport);
108 list_for_each_entry_safe(rdata, next, &disc->rogue_rports, peers) {
109 rport = PRIV_TO_RPORT(rdata);
110 lport->tt.rport_logoff(rport);
113 mutex_unlock(&disc->disc_mutex);
117 * fc_disc_rport_callback() - Event handler for rport events
118 * @lport: The lport which is receiving the event
119 * @rport: The rport which the event has occured on
120 * @event: The event that occured
122 * Locking Note: The rport lock should not be held when calling
125 static void fc_disc_rport_callback(struct fc_lport *lport,
126 struct fc_rport *rport,
127 enum fc_rport_event event)
129 struct fc_rport_libfc_priv *rdata = rport->dd_data;
130 struct fc_disc *disc = &lport->disc;
132 FC_DISC_DBG(disc, "Received a %d event for port (%6x)\n", event,
136 case RPORT_EV_CREATED:
138 mutex_lock(&disc->disc_mutex);
139 list_add_tail(&rdata->peers, &disc->rports);
140 mutex_unlock(&disc->disc_mutex);
144 case RPORT_EV_FAILED:
146 mutex_lock(&disc->disc_mutex);
147 mutex_lock(&rdata->rp_mutex);
148 if (rdata->trans_state == FC_PORTSTATE_ROGUE)
149 list_del(&rdata->peers);
150 mutex_unlock(&rdata->rp_mutex);
151 mutex_unlock(&disc->disc_mutex);
160 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
161 * @sp: Current sequence of the RSCN exchange
163 * @lport: Fibre Channel host port instance
165 * Locking Note: This function expects that the disc_mutex is locked
166 * before it is called.
168 static void fc_disc_recv_rscn_req(struct fc_seq *sp, struct fc_frame *fp,
169 struct fc_disc *disc)
171 struct fc_lport *lport;
172 struct fc_rport *rport;
173 struct fc_rport_libfc_priv *rdata;
174 struct fc_els_rscn *rp;
175 struct fc_els_rscn_page *pp;
176 struct fc_seq_els_data rjt_data;
179 enum fc_els_rscn_ev_qual ev_qual;
180 enum fc_els_rscn_addr_fmt fmt;
181 LIST_HEAD(disc_ports);
182 struct fc_disc_port *dp, *next;
186 FC_DISC_DBG(disc, "Received an RSCN event\n");
188 /* make sure the frame contains an RSCN message */
189 rp = fc_frame_payload_get(fp, sizeof(*rp));
192 /* make sure the page length is as expected (4 bytes) */
193 if (rp->rscn_page_len != sizeof(*pp))
195 /* get the RSCN payload length */
196 len = ntohs(rp->rscn_plen);
197 if (len < sizeof(*rp))
199 /* make sure the frame contains the expected payload */
200 rp = fc_frame_payload_get(fp, len);
203 /* payload must be a multiple of the RSCN page size */
205 if (len % sizeof(*pp))
208 for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
209 ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
210 ev_qual &= ELS_RSCN_EV_QUAL_MASK;
211 fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
212 fmt &= ELS_RSCN_ADDR_FMT_MASK;
214 * if we get an address format other than port
215 * (area, domain, fabric), then do a full discovery
218 case ELS_ADDR_FMT_PORT:
219 FC_DISC_DBG(disc, "Port address format for port "
220 "(%6x)\n", ntoh24(pp->rscn_fid));
221 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
227 dp->ids.port_id = ntoh24(pp->rscn_fid);
228 dp->ids.port_name = -1;
229 dp->ids.node_name = -1;
230 dp->ids.roles = FC_RPORT_ROLE_UNKNOWN;
231 list_add_tail(&dp->peers, &disc_ports);
233 case ELS_ADDR_FMT_AREA:
234 case ELS_ADDR_FMT_DOM:
235 case ELS_ADDR_FMT_FAB:
237 FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
242 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
244 FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
245 fc_disc_restart(disc);
247 FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
248 "redisc %d state %d in_prog %d\n",
249 redisc, lport->state, disc->pending);
250 list_for_each_entry_safe(dp, next, &disc_ports, peers) {
251 list_del(&dp->peers);
252 rport = lport->tt.rport_lookup(lport, dp->ids.port_id);
254 rdata = rport->dd_data;
255 list_del(&rdata->peers);
256 lport->tt.rport_logoff(rport);
258 fc_disc_single(disc, dp);
264 FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
266 rjt_data.reason = ELS_RJT_LOGIC;
267 rjt_data.explan = ELS_EXPL_NONE;
268 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
273 * fc_disc_recv_req() - Handle incoming requests
274 * @sp: Current sequence of the request exchange
276 * @lport: The FC local port
278 * Locking Note: This function is called from the EM and will lock
279 * the disc_mutex before calling the handler for the
282 static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp,
283 struct fc_lport *lport)
286 struct fc_disc *disc = &lport->disc;
288 op = fc_frame_payload_op(fp);
291 mutex_lock(&disc->disc_mutex);
292 fc_disc_recv_rscn_req(sp, fp, disc);
293 mutex_unlock(&disc->disc_mutex);
296 FC_DISC_DBG(disc, "Received an unsupported request, "
297 "the opcode is (%x)\n", op);
303 * fc_disc_restart() - Restart discovery
304 * @lport: FC discovery context
306 * Locking Note: This function expects that the disc mutex
309 static void fc_disc_restart(struct fc_disc *disc)
311 struct fc_rport *rport;
312 struct fc_rport_libfc_priv *rdata, *next;
313 struct fc_lport *lport = disc->lport;
315 FC_DISC_DBG(disc, "Restarting discovery\n");
317 list_for_each_entry_safe(rdata, next, &disc->rports, peers) {
318 rport = PRIV_TO_RPORT(rdata);
319 list_del(&rdata->peers);
320 lport->tt.rport_logoff(rport);
325 fc_disc_gpn_ft_req(disc);
329 * fc_disc_start() - Fibre Channel Target discovery
330 * @lport: FC local port
332 * Returns non-zero if discovery cannot be started.
334 static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
336 struct fc_lport *lport)
338 struct fc_rport *rport;
339 struct fc_rport_identifiers ids;
340 struct fc_disc *disc = &lport->disc;
343 * At this point we may have a new disc job or an existing
344 * one. Either way, let's lock when we make changes to it
345 * and send the GPN_FT request.
347 mutex_lock(&disc->disc_mutex);
349 disc->disc_callback = disc_callback;
352 * If not ready, or already running discovery, just set request flag.
357 mutex_unlock(&disc->disc_mutex);
362 * Handle point-to-point mode as a simple discovery
363 * of the remote port. Yucky, yucky, yuck, yuck!
365 rport = disc->lport->ptp_rp;
367 ids.port_id = rport->port_id;
368 ids.port_name = rport->port_name;
369 ids.node_name = rport->node_name;
370 ids.roles = FC_RPORT_ROLE_UNKNOWN;
371 get_device(&rport->dev);
373 if (!fc_disc_new_target(disc, rport, &ids)) {
374 disc->event = DISC_EV_SUCCESS;
377 put_device(&rport->dev);
379 fc_disc_gpn_ft_req(disc); /* get ports by FC-4 type */
382 mutex_unlock(&disc->disc_mutex);
385 static struct fc_rport_operations fc_disc_rport_ops = {
386 .event_callback = fc_disc_rport_callback,
390 * fc_disc_new_target() - Handle new target found by discovery
391 * @lport: FC local port
392 * @rport: The previous FC remote port (NULL if new remote port)
393 * @ids: Identifiers for the new FC remote port
395 * Locking Note: This function expects that the disc_mutex is locked
396 * before it is called.
398 static int fc_disc_new_target(struct fc_disc *disc,
399 struct fc_rport *rport,
400 struct fc_rport_identifiers *ids)
402 struct fc_lport *lport = disc->lport;
403 struct fc_rport_libfc_priv *rdata;
406 if (rport && ids->port_name) {
407 if (rport->port_name == -1) {
409 * Set WWN and fall through to notify of create.
411 fc_rport_set_name(rport, ids->port_name,
413 } else if (rport->port_name != ids->port_name) {
415 * This is a new port with the same FCID as
416 * a previously-discovered port. Presumably the old
417 * port logged out and a new port logged in and was
418 * assigned the same FCID. This should be rare.
419 * Delete the old one and fall thru to re-create.
421 fc_disc_del_target(disc, rport);
425 if (((ids->port_name != -1) || (ids->port_id != -1)) &&
426 ids->port_id != fc_host_port_id(lport->host) &&
427 ids->port_name != lport->wwpn) {
429 rport = lport->tt.rport_lookup(lport, ids->port_id);
431 struct fc_disc_port dp;
433 dp.ids.port_id = ids->port_id;
434 dp.ids.port_name = ids->port_name;
435 dp.ids.node_name = ids->node_name;
436 dp.ids.roles = ids->roles;
437 rport = lport->tt.rport_create(&dp);
443 rdata = rport->dd_data;
444 rdata->ops = &fc_disc_rport_ops;
445 rdata->rp_state = RPORT_ST_INIT;
446 list_add_tail(&rdata->peers, &disc->rogue_rports);
447 lport->tt.rport_login(rport);
454 * fc_disc_del_target() - Delete a target
455 * @disc: FC discovery context
456 * @rport: The remote port to be removed
458 static void fc_disc_del_target(struct fc_disc *disc, struct fc_rport *rport)
460 struct fc_lport *lport = disc->lport;
461 struct fc_rport_libfc_priv *rdata = rport->dd_data;
462 list_del(&rdata->peers);
463 lport->tt.rport_logoff(rport);
467 * fc_disc_done() - Discovery has been completed
468 * @disc: FC discovery context
469 * Locking Note: This function expects that the disc mutex is locked before
470 * it is called. The discovery callback is then made with the lock released,
471 * and the lock is re-taken before returning from this function
473 static void fc_disc_done(struct fc_disc *disc)
475 struct fc_lport *lport = disc->lport;
476 enum fc_disc_event event;
478 FC_DISC_DBG(disc, "Discovery complete\n");
481 disc->event = DISC_EV_NONE;
484 fc_disc_gpn_ft_req(disc);
488 mutex_unlock(&disc->disc_mutex);
489 disc->disc_callback(lport, event);
490 mutex_lock(&disc->disc_mutex);
494 * fc_disc_error() - Handle error on dNS request
495 * @disc: FC discovery context
496 * @fp: The frame pointer
498 static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
500 struct fc_lport *lport = disc->lport;
501 unsigned long delay = 0;
503 FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
504 PTR_ERR(fp), disc->retry_count,
505 FC_DISC_RETRY_LIMIT);
507 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
509 * Memory allocation failure, or the exchange timed out,
512 if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
513 /* go ahead and retry */
515 delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
517 delay = msecs_to_jiffies(lport->e_d_tov);
519 /* timeout faster first time */
520 if (!disc->retry_count)
524 schedule_delayed_work(&disc->disc_work, delay);
526 /* exceeded retries */
527 disc->event = DISC_EV_FAILED;
534 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
535 * @lport: FC discovery context
537 * Locking Note: This function expects that the disc_mutex is locked
538 * before it is called.
540 static void fc_disc_gpn_ft_req(struct fc_disc *disc)
543 struct fc_lport *lport = disc->lport;
545 WARN_ON(!fc_lport_test_ready(lport));
552 fp = fc_frame_alloc(lport,
553 sizeof(struct fc_ct_hdr) +
554 sizeof(struct fc_ns_gid_ft));
558 if (lport->tt.elsct_send(lport, NULL, fp,
561 disc, lport->e_d_tov))
564 fc_disc_error(disc, fp);
568 * fc_disc_gpn_ft_parse() - Parse the list of IDs and names resulting from a request
569 * @lport: Fibre Channel host port instance
570 * @buf: GPN_FT response buffer
571 * @len: size of response buffer
573 static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
575 struct fc_lport *lport;
576 struct fc_gpn_ft_resp *np;
581 struct fc_disc_port dp;
582 struct fc_rport *rport;
583 struct fc_rport_libfc_priv *rdata;
588 * Handle partial name record left over from previous call.
592 np = (struct fc_gpn_ft_resp *)bp;
593 tlen = disc->buf_len;
595 WARN_ON(tlen >= sizeof(*np));
596 plen = sizeof(*np) - tlen;
598 WARN_ON(plen >= sizeof(*np));
601 np = &disc->partial_buf;
602 memcpy((char *)np + tlen, bp, plen);
605 * Set bp so that the loop below will advance it to the
606 * first valid full name element.
611 disc->buf_len = (unsigned char) plen;
612 if (plen == sizeof(*np))
617 * Handle full name records, including the one filled from above.
618 * Normally, np == bp and plen == len, but from the partial case above,
619 * bp, len describe the overall buffer, and np, plen describe the
620 * partial buffer, which if would usually be full now.
621 * After the first time through the loop, things return to "normal".
623 while (plen >= sizeof(*np)) {
625 dp.ids.port_id = ntoh24(np->fp_fid);
626 dp.ids.port_name = ntohll(np->fp_wwpn);
627 dp.ids.node_name = -1;
628 dp.ids.roles = FC_RPORT_ROLE_UNKNOWN;
630 if ((dp.ids.port_id != fc_host_port_id(lport->host)) &&
631 (dp.ids.port_name != lport->wwpn)) {
632 rport = lport->tt.rport_create(&dp);
634 rdata = rport->dd_data;
635 rdata->ops = &fc_disc_rport_ops;
636 rdata->local_port = lport;
637 list_add_tail(&rdata->peers,
638 &disc->rogue_rports);
639 lport->tt.rport_login(rport);
641 printk(KERN_WARNING "libfc: Failed to allocate "
642 "memory for the newly discovered port "
643 "(%6x)\n", dp.ids.port_id);
646 if (np->fp_flags & FC_NS_FID_LAST) {
647 disc->event = DISC_EV_SUCCESS;
654 np = (struct fc_gpn_ft_resp *)bp;
659 * Save any partial record at the end of the buffer for next time.
661 if (error == 0 && len > 0 && len < sizeof(*np)) {
662 if (np != &disc->partial_buf) {
663 FC_DISC_DBG(disc, "Partial buffer remains "
665 memcpy(&disc->partial_buf, np, len);
667 disc->buf_len = (unsigned char) len;
675 * fc_disc_timeout() - Retry handler for the disc component
676 * @work: Structure holding disc obj that needs retry discovery
678 * Handle retry of memory allocation for remote ports.
680 static void fc_disc_timeout(struct work_struct *work)
682 struct fc_disc *disc = container_of(work,
685 mutex_lock(&disc->disc_mutex);
686 if (disc->requested && !disc->pending)
687 fc_disc_gpn_ft_req(disc);
688 mutex_unlock(&disc->disc_mutex);
692 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
693 * @sp: Current sequence of GPN_FT exchange
694 * @fp: response frame
695 * @lp_arg: Fibre Channel host port instance
697 * Locking Note: This function is called without disc mutex held, and
698 * should do all its processing with the mutex held
700 static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
703 struct fc_disc *disc = disc_arg;
704 struct fc_ct_hdr *cp;
705 struct fc_frame_header *fh;
706 unsigned int seq_cnt;
711 mutex_lock(&disc->disc_mutex);
712 FC_DISC_DBG(disc, "Received a GPN_FT response\n");
715 fc_disc_error(disc, fp);
716 mutex_unlock(&disc->disc_mutex);
720 WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
721 fh = fc_frame_header_get(fp);
722 len = fr_len(fp) - sizeof(*fh);
723 seq_cnt = ntohs(fh->fh_seq_cnt);
724 if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 &&
725 disc->seq_count == 0) {
726 cp = fc_frame_payload_get(fp, sizeof(*cp));
728 FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
730 } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
732 /* Accepted, parse the response. */
735 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
736 FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
737 "(check zoning)\n", cp->ct_reason,
739 disc->event = DISC_EV_FAILED;
742 FC_DISC_DBG(disc, "GPN_FT unexpected response code "
743 "%x\n", ntohs(cp->ct_cmd));
745 } else if (fr_sof(fp) == FC_SOF_N3 &&
746 seq_cnt == disc->seq_count) {
749 FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
750 "seq_cnt %x expected %x sof %x eof %x\n",
751 seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
754 error = fc_disc_gpn_ft_parse(disc, buf, len);
756 fc_disc_error(disc, fp);
762 mutex_unlock(&disc->disc_mutex);
766 * fc_disc_single() - Discover the directory information for a single target
767 * @lport: FC local port
768 * @dp: The port to rediscover
770 * Locking Note: This function expects that the disc_mutex is locked
771 * before it is called.
773 static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp)
775 struct fc_lport *lport;
776 struct fc_rport *new_rport;
777 struct fc_rport_libfc_priv *rdata;
781 if (dp->ids.port_id == fc_host_port_id(lport->host))
784 new_rport = lport->tt.rport_create(dp);
786 rdata = new_rport->dd_data;
787 rdata->ops = &fc_disc_rport_ops;
789 list_add_tail(&rdata->peers, &disc->rogue_rports);
790 lport->tt.rport_login(new_rport);
798 * fc_disc_stop() - Stop discovery for a given lport
799 * @lport: The lport that discovery should stop for
801 void fc_disc_stop(struct fc_lport *lport)
803 struct fc_disc *disc = &lport->disc;
806 cancel_delayed_work_sync(&disc->disc_work);
807 fc_disc_stop_rports(disc);
812 * fc_disc_stop_final() - Stop discovery for a given lport
813 * @lport: The lport that discovery should stop for
815 * This function will block until discovery has been
816 * completely stopped and all rports have been deleted.
818 void fc_disc_stop_final(struct fc_lport *lport)
821 lport->tt.rport_flush_queue();
825 * fc_disc_init() - Initialize the discovery block
826 * @lport: FC local port
828 int fc_disc_init(struct fc_lport *lport)
830 struct fc_disc *disc;
832 if (!lport->tt.disc_start)
833 lport->tt.disc_start = fc_disc_start;
835 if (!lport->tt.disc_stop)
836 lport->tt.disc_stop = fc_disc_stop;
838 if (!lport->tt.disc_stop_final)
839 lport->tt.disc_stop_final = fc_disc_stop_final;
841 if (!lport->tt.disc_recv_req)
842 lport->tt.disc_recv_req = fc_disc_recv_req;
844 if (!lport->tt.rport_lookup)
845 lport->tt.rport_lookup = fc_disc_lookup_rport;
848 INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
849 mutex_init(&disc->disc_mutex);
850 INIT_LIST_HEAD(&disc->rports);
851 INIT_LIST_HEAD(&disc->rogue_rports);
854 disc->delay = FC_DISC_DELAY;
855 disc->event = DISC_EV_NONE;
859 EXPORT_SYMBOL(fc_disc_init);