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)
609 mutex_lock(&disc->disc_mutex);
610 fc_disc_restart(disc);
611 mutex_unlock(&disc->disc_mutex);
615 cp = fc_frame_payload_get(fp, sizeof(*cp));
618 if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
619 if (fr_len(fp) < sizeof(struct fc_frame_header) +
620 sizeof(*cp) + sizeof(*pn))
622 pn = (struct fc_ns_gid_pn *)(cp + 1);
623 port_name = get_unaligned_be64(&pn->fn_wwpn);
624 mutex_lock(&rdata->rp_mutex);
625 if (rdata->ids.port_name == -1)
626 rdata->ids.port_name = port_name;
627 else if (rdata->ids.port_name != port_name) {
628 FC_DISC_DBG(disc, "GPN_ID accepted. WWPN changed. "
629 "Port-id %6.6x wwpn %16.16llx\n",
630 rdata->ids.port_id, port_name);
631 mutex_unlock(&rdata->rp_mutex);
632 fc_rport_logoff(rdata);
633 mutex_lock(&lport->disc.disc_mutex);
634 new_rdata = fc_rport_create(lport, rdata->ids.port_id);
635 mutex_unlock(&lport->disc.disc_mutex);
637 new_rdata->disc_id = disc->disc_id;
638 fc_rport_login(new_rdata);
642 rdata->disc_id = disc->disc_id;
643 mutex_unlock(&rdata->rp_mutex);
644 fc_rport_login(rdata);
645 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
646 FC_DISC_DBG(disc, "GPN_ID rejected reason %x exp %x\n",
647 cp->ct_reason, cp->ct_explan);
648 fc_rport_logoff(rdata);
650 FC_DISC_DBG(disc, "GPN_ID unexpected response code %x\n",
653 mutex_lock(&disc->disc_mutex);
654 fc_disc_restart(disc);
655 mutex_unlock(&disc->disc_mutex);
660 kref_put(&rdata->kref, fc_rport_destroy);
664 * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request
665 * @lport: The local port to initiate discovery on
666 * @rdata: remote port private data
668 * Locking Note: This function expects that the disc_mutex is locked
669 * before it is called.
670 * On failure, an error code is returned.
672 static int fc_disc_gpn_id_req(struct fc_lport *lport,
673 struct fc_rport_priv *rdata)
677 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
678 sizeof(struct fc_ns_fid));
681 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID,
682 fc_disc_gpn_id_resp, rdata,
685 kref_get(&rdata->kref);
690 * fc_disc_single() - Discover the directory information for a single target
691 * @lport: The local port the remote port is associated with
692 * @dp: The port to rediscover
694 * Locking Note: This function expects that the disc_mutex is locked
695 * before it is called.
697 static int fc_disc_single(struct fc_lport *lport, struct fc_disc_port *dp)
699 struct fc_rport_priv *rdata;
701 rdata = fc_rport_create(lport, dp->port_id);
705 return fc_disc_gpn_id_req(lport, rdata);
709 * fc_disc_stop() - Stop discovery for a given lport
710 * @lport: The local port that discovery should stop on
712 static void fc_disc_stop(struct fc_lport *lport)
714 struct fc_disc *disc = &lport->disc;
717 cancel_delayed_work_sync(&disc->disc_work);
718 fc_disc_stop_rports(disc);
722 * fc_disc_stop_final() - Stop discovery for a given lport
723 * @lport: The lport that discovery should stop on
725 * This function will block until discovery has been
726 * completely stopped and all rports have been deleted.
728 static void fc_disc_stop_final(struct fc_lport *lport)
731 fc_rport_flush_queue();
735 * fc_disc_config() - Configure the discovery layer for a local port
736 * @lport: The local port that needs the discovery layer to be configured
737 * @priv: Private data structre for users of the discovery layer
739 void fc_disc_config(struct fc_lport *lport, void *priv)
741 struct fc_disc *disc = &lport->disc;
743 if (!lport->tt.disc_start)
744 lport->tt.disc_start = fc_disc_start;
746 if (!lport->tt.disc_stop)
747 lport->tt.disc_stop = fc_disc_stop;
749 if (!lport->tt.disc_stop_final)
750 lport->tt.disc_stop_final = fc_disc_stop_final;
752 if (!lport->tt.disc_recv_req)
753 lport->tt.disc_recv_req = fc_disc_recv_req;
759 EXPORT_SYMBOL(fc_disc_config);
762 * fc_disc_init() - Initialize the discovery layer for a local port
763 * @lport: The local port that needs the discovery layer to be initialized
765 void fc_disc_init(struct fc_lport *lport)
767 struct fc_disc *disc = &lport->disc;
769 INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
770 mutex_init(&disc->disc_mutex);
771 INIT_LIST_HEAD(&disc->rports);
773 EXPORT_SYMBOL(fc_disc_init);