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_stop_rports() - delete all the remote ports associated with the lport
57 * @disc: The discovery job to stop rports on
59 * Locking Note: This function expects that the lport mutex is locked before
62 void fc_disc_stop_rports(struct fc_disc *disc)
64 struct fc_lport *lport;
65 struct fc_rport_priv *rdata, *next;
69 mutex_lock(&disc->disc_mutex);
70 list_for_each_entry_safe(rdata, next, &disc->rports, peers)
71 lport->tt.rport_logoff(rdata);
72 mutex_unlock(&disc->disc_mutex);
76 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
77 * @sp: Current sequence of the RSCN exchange
79 * @lport: Fibre Channel host port instance
81 * Locking Note: This function expects that the disc_mutex is locked
82 * before it is called.
84 static void fc_disc_recv_rscn_req(struct fc_seq *sp, struct fc_frame *fp,
87 struct fc_lport *lport;
88 struct fc_rport_priv *rdata;
89 struct fc_els_rscn *rp;
90 struct fc_els_rscn_page *pp;
91 struct fc_seq_els_data rjt_data;
94 enum fc_els_rscn_ev_qual ev_qual;
95 enum fc_els_rscn_addr_fmt fmt;
96 LIST_HEAD(disc_ports);
97 struct fc_disc_port *dp, *next;
101 FC_DISC_DBG(disc, "Received an RSCN event\n");
103 /* make sure the frame contains an RSCN message */
104 rp = fc_frame_payload_get(fp, sizeof(*rp));
107 /* make sure the page length is as expected (4 bytes) */
108 if (rp->rscn_page_len != sizeof(*pp))
110 /* get the RSCN payload length */
111 len = ntohs(rp->rscn_plen);
112 if (len < sizeof(*rp))
114 /* make sure the frame contains the expected payload */
115 rp = fc_frame_payload_get(fp, len);
118 /* payload must be a multiple of the RSCN page size */
120 if (len % sizeof(*pp))
123 for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
124 ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
125 ev_qual &= ELS_RSCN_EV_QUAL_MASK;
126 fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
127 fmt &= ELS_RSCN_ADDR_FMT_MASK;
129 * if we get an address format other than port
130 * (area, domain, fabric), then do a full discovery
133 case ELS_ADDR_FMT_PORT:
134 FC_DISC_DBG(disc, "Port address format for port "
135 "(%6x)\n", ntoh24(pp->rscn_fid));
136 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
142 dp->ids.port_id = ntoh24(pp->rscn_fid);
143 dp->ids.port_name = -1;
144 dp->ids.node_name = -1;
145 dp->ids.roles = FC_RPORT_ROLE_UNKNOWN;
146 list_add_tail(&dp->peers, &disc_ports);
148 case ELS_ADDR_FMT_AREA:
149 case ELS_ADDR_FMT_DOM:
150 case ELS_ADDR_FMT_FAB:
152 FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
157 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
159 FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
160 fc_disc_restart(disc);
162 FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
163 "redisc %d state %d in_prog %d\n",
164 redisc, lport->state, disc->pending);
165 list_for_each_entry_safe(dp, next, &disc_ports, peers) {
166 list_del(&dp->peers);
167 rdata = lport->tt.rport_lookup(lport, dp->ids.port_id);
169 lport->tt.rport_logoff(rdata);
171 fc_disc_single(disc, dp);
177 FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
179 rjt_data.reason = ELS_RJT_LOGIC;
180 rjt_data.explan = ELS_EXPL_NONE;
181 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
186 * fc_disc_recv_req() - Handle incoming requests
187 * @sp: Current sequence of the request exchange
189 * @lport: The FC local port
191 * Locking Note: This function is called from the EM and will lock
192 * the disc_mutex before calling the handler for the
195 static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp,
196 struct fc_lport *lport)
199 struct fc_disc *disc = &lport->disc;
201 op = fc_frame_payload_op(fp);
204 mutex_lock(&disc->disc_mutex);
205 fc_disc_recv_rscn_req(sp, fp, disc);
206 mutex_unlock(&disc->disc_mutex);
209 FC_DISC_DBG(disc, "Received an unsupported request, "
210 "the opcode is (%x)\n", op);
216 * fc_disc_restart() - Restart discovery
217 * @lport: FC discovery context
219 * Locking Note: This function expects that the disc mutex
222 static void fc_disc_restart(struct fc_disc *disc)
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 fc_disc_gpn_ft_req(disc);
240 * fc_disc_start() - Fibre Channel Target discovery
241 * @lport: FC local port
243 * Returns non-zero if discovery cannot be started.
245 static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
247 struct fc_lport *lport)
249 struct fc_rport_priv *rdata;
250 struct fc_disc *disc = &lport->disc;
253 * At this point we may have a new disc job or an existing
254 * one. Either way, let's lock when we make changes to it
255 * and send the GPN_FT request.
257 mutex_lock(&disc->disc_mutex);
259 disc->disc_callback = disc_callback;
262 * If not ready, or already running discovery, just set request flag.
267 mutex_unlock(&disc->disc_mutex);
272 * Handle point-to-point mode as a simple discovery
273 * of the remote port. Yucky, yucky, yuck, yuck!
275 rdata = disc->lport->ptp_rp;
277 kref_get(&rdata->kref);
278 if (!fc_disc_new_target(disc, rdata, &rdata->ids)) {
279 fc_disc_done(disc, DISC_EV_SUCCESS);
281 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
283 disc->disc_id = (disc->disc_id + 2) | 1;
284 fc_disc_gpn_ft_req(disc); /* get ports by FC-4 type */
287 mutex_unlock(&disc->disc_mutex);
291 * fc_disc_new_target() - Handle new target found by discovery
292 * @lport: FC local port
293 * @rdata: The previous FC remote port priv (NULL if new remote port)
294 * @ids: Identifiers for the new FC remote port
296 * Locking Note: This function expects that the disc_mutex is locked
297 * before it is called.
299 static int fc_disc_new_target(struct fc_disc *disc,
300 struct fc_rport_priv *rdata,
301 struct fc_rport_identifiers *ids)
303 struct fc_lport *lport = disc->lport;
306 if (rdata && ids->port_name) {
307 if (rdata->ids.port_name == -1) {
309 * Set WWN and fall through to notify of create.
311 rdata->ids.port_name = ids->port_name;
312 rdata->ids.node_name = ids->node_name;
313 } else if (rdata->ids.port_name != ids->port_name) {
315 * This is a new port with the same FCID as
316 * a previously-discovered port. Presumably the old
317 * port logged out and a new port logged in and was
318 * assigned the same FCID. This should be rare.
319 * Delete the old one and fall thru to re-create.
321 lport->tt.rport_logoff(rdata);
325 if (((ids->port_name != -1) || (ids->port_id != -1)) &&
326 ids->port_id != fc_host_port_id(lport->host) &&
327 ids->port_name != lport->wwpn) {
329 rdata = lport->tt.rport_create(lport, ids);
334 lport->tt.rport_login(rdata);
340 * fc_disc_done() - Discovery has been completed
341 * @disc: FC discovery context
342 * @event: discovery completion status
344 * Locking Note: This function expects that the disc mutex is locked before
345 * it is called. The discovery callback is then made with the lock released,
346 * and the lock is re-taken before returning from this function
348 static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
350 struct fc_lport *lport = disc->lport;
351 struct fc_rport_priv *rdata;
353 FC_DISC_DBG(disc, "Discovery complete\n");
356 if (disc->requested) {
357 fc_disc_restart(disc);
362 * Go through all remote ports. If they were found in the latest
363 * discovery, reverify or log them in. Otherwise, log them out.
364 * Skip ports which were never discovered. These are the dNS port
365 * and ports which were created by PLOGI.
367 list_for_each_entry(rdata, &disc->rports, peers) {
370 if (rdata->disc_id == disc->disc_id)
371 lport->tt.rport_login(rdata);
373 lport->tt.rport_logoff(rdata);
376 mutex_unlock(&disc->disc_mutex);
377 disc->disc_callback(lport, event);
378 mutex_lock(&disc->disc_mutex);
382 * fc_disc_error() - Handle error on dNS request
383 * @disc: FC discovery context
384 * @fp: The frame pointer
386 static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
388 struct fc_lport *lport = disc->lport;
389 unsigned long delay = 0;
391 FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
392 PTR_ERR(fp), disc->retry_count,
393 FC_DISC_RETRY_LIMIT);
395 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
397 * Memory allocation failure, or the exchange timed out,
400 if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
401 /* go ahead and retry */
403 delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
405 delay = msecs_to_jiffies(lport->e_d_tov);
407 /* timeout faster first time */
408 if (!disc->retry_count)
412 schedule_delayed_work(&disc->disc_work, delay);
414 fc_disc_done(disc, DISC_EV_FAILED);
419 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
420 * @lport: FC discovery context
422 * Locking Note: This function expects that the disc_mutex is locked
423 * before it is called.
425 static void fc_disc_gpn_ft_req(struct fc_disc *disc)
428 struct fc_lport *lport = disc->lport;
430 WARN_ON(!fc_lport_test_ready(lport));
437 fp = fc_frame_alloc(lport,
438 sizeof(struct fc_ct_hdr) +
439 sizeof(struct fc_ns_gid_ft));
443 if (lport->tt.elsct_send(lport, 0, fp,
446 disc, lport->e_d_tov))
449 fc_disc_error(disc, fp);
453 * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
454 * @lport: Fibre Channel host port instance
455 * @buf: GPN_FT response buffer
456 * @len: size of response buffer
458 * Goes through the list of IDs and names resulting from a request.
460 static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
462 struct fc_lport *lport;
463 struct fc_gpn_ft_resp *np;
468 struct fc_rport_identifiers ids;
469 struct fc_rport_priv *rdata;
475 * Handle partial name record left over from previous call.
479 np = (struct fc_gpn_ft_resp *)bp;
480 tlen = disc->buf_len;
482 WARN_ON(tlen >= sizeof(*np));
483 plen = sizeof(*np) - tlen;
485 WARN_ON(plen >= sizeof(*np));
488 np = &disc->partial_buf;
489 memcpy((char *)np + tlen, bp, plen);
492 * Set bp so that the loop below will advance it to the
493 * first valid full name element.
498 disc->buf_len = (unsigned char) plen;
499 if (plen == sizeof(*np))
504 * Handle full name records, including the one filled from above.
505 * Normally, np == bp and plen == len, but from the partial case above,
506 * bp, len describe the overall buffer, and np, plen describe the
507 * partial buffer, which if would usually be full now.
508 * After the first time through the loop, things return to "normal".
510 while (plen >= sizeof(*np)) {
511 ids.port_id = ntoh24(np->fp_fid);
512 ids.port_name = ntohll(np->fp_wwpn);
514 ids.roles = FC_RPORT_ROLE_UNKNOWN;
516 if (ids.port_id != fc_host_port_id(lport->host) &&
517 ids.port_name != lport->wwpn) {
518 rdata = lport->tt.rport_create(lport, &ids);
520 rdata->disc_id = disc->disc_id;
522 printk(KERN_WARNING "libfc: Failed to allocate "
523 "memory for the newly discovered port "
524 "(%6x)\n", ids.port_id);
527 if (np->fp_flags & FC_NS_FID_LAST) {
528 fc_disc_done(disc, DISC_EV_SUCCESS);
534 np = (struct fc_gpn_ft_resp *)bp;
539 * Save any partial record at the end of the buffer for next time.
541 if (error == 0 && len > 0 && len < sizeof(*np)) {
542 if (np != &disc->partial_buf) {
543 FC_DISC_DBG(disc, "Partial buffer remains "
545 memcpy(&disc->partial_buf, np, len);
547 disc->buf_len = (unsigned char) len;
555 * fc_disc_timeout() - Retry handler for the disc component
556 * @work: Structure holding disc obj that needs retry discovery
558 * Handle retry of memory allocation for remote ports.
560 static void fc_disc_timeout(struct work_struct *work)
562 struct fc_disc *disc = container_of(work,
565 mutex_lock(&disc->disc_mutex);
566 if (disc->requested && !disc->pending)
567 fc_disc_gpn_ft_req(disc);
568 mutex_unlock(&disc->disc_mutex);
572 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
573 * @sp: Current sequence of GPN_FT exchange
574 * @fp: response frame
575 * @lp_arg: Fibre Channel host port instance
577 * Locking Note: This function is called without disc mutex held, and
578 * should do all its processing with the mutex held
580 static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
583 struct fc_disc *disc = disc_arg;
584 struct fc_ct_hdr *cp;
585 struct fc_frame_header *fh;
586 enum fc_disc_event event = DISC_EV_NONE;
587 unsigned int seq_cnt;
591 mutex_lock(&disc->disc_mutex);
592 FC_DISC_DBG(disc, "Received a GPN_FT response\n");
595 fc_disc_error(disc, fp);
596 mutex_unlock(&disc->disc_mutex);
600 WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
601 fh = fc_frame_header_get(fp);
602 len = fr_len(fp) - sizeof(*fh);
603 seq_cnt = ntohs(fh->fh_seq_cnt);
604 if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 && disc->seq_count == 0) {
605 cp = fc_frame_payload_get(fp, sizeof(*cp));
607 FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
609 event = DISC_EV_FAILED;
610 } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
612 /* Accepted, parse the response. */
614 error = fc_disc_gpn_ft_parse(disc, cp + 1, len);
615 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
616 FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
617 "(check zoning)\n", cp->ct_reason,
619 event = DISC_EV_FAILED;
621 FC_DISC_DBG(disc, "GPN_FT unexpected response code "
622 "%x\n", ntohs(cp->ct_cmd));
623 event = DISC_EV_FAILED;
625 } else if (fr_sof(fp) == FC_SOF_N3 && seq_cnt == disc->seq_count) {
626 error = fc_disc_gpn_ft_parse(disc, fh + 1, len);
628 FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
629 "seq_cnt %x expected %x sof %x eof %x\n",
630 seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
631 event = DISC_EV_FAILED;
634 fc_disc_error(disc, fp);
635 else if (event != DISC_EV_NONE)
636 fc_disc_done(disc, event);
638 mutex_unlock(&disc->disc_mutex);
642 * fc_disc_single() - Discover the directory information for a single target
643 * @lport: FC local port
644 * @dp: The port to rediscover
646 * Locking Note: This function expects that the disc_mutex is locked
647 * before it is called.
649 static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp)
651 struct fc_lport *lport;
652 struct fc_rport_priv *rdata;
656 if (dp->ids.port_id == fc_host_port_id(lport->host))
659 rdata = lport->tt.rport_create(lport, &dp->ids);
661 rdata->disc_id = disc->disc_id;
663 lport->tt.rport_login(rdata);
671 * fc_disc_stop() - Stop discovery for a given lport
672 * @lport: The lport that discovery should stop for
674 void fc_disc_stop(struct fc_lport *lport)
676 struct fc_disc *disc = &lport->disc;
679 cancel_delayed_work_sync(&disc->disc_work);
680 fc_disc_stop_rports(disc);
685 * fc_disc_stop_final() - Stop discovery for a given lport
686 * @lport: The lport that discovery should stop for
688 * This function will block until discovery has been
689 * completely stopped and all rports have been deleted.
691 void fc_disc_stop_final(struct fc_lport *lport)
694 lport->tt.rport_flush_queue();
698 * fc_disc_init() - Initialize the discovery block
699 * @lport: FC local port
701 int fc_disc_init(struct fc_lport *lport)
703 struct fc_disc *disc;
705 if (!lport->tt.disc_start)
706 lport->tt.disc_start = fc_disc_start;
708 if (!lport->tt.disc_stop)
709 lport->tt.disc_stop = fc_disc_stop;
711 if (!lport->tt.disc_stop_final)
712 lport->tt.disc_stop_final = fc_disc_stop_final;
714 if (!lport->tt.disc_recv_req)
715 lport->tt.disc_recv_req = fc_disc_recv_req;
718 INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
719 mutex_init(&disc->disc_mutex);
720 INIT_LIST_HEAD(&disc->rports);
726 EXPORT_SYMBOL(fc_disc_init);