2 * Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2009 Intel Corporation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 * Maintained at www.Open-FCoE.org
21 #include <linux/types.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/spinlock.h>
26 #include <linux/timer.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ethtool.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/netdevice.h>
33 #include <linux/errno.h>
34 #include <linux/bitops.h>
35 #include <net/rtnetlink.h>
37 #include <scsi/fc/fc_els.h>
38 #include <scsi/fc/fc_fs.h>
39 #include <scsi/fc/fc_fip.h>
40 #include <scsi/fc/fc_encaps.h>
41 #include <scsi/fc/fc_fcoe.h>
43 #include <scsi/libfc.h>
44 #include <scsi/libfcoe.h>
46 MODULE_AUTHOR("Open-FCoE.org");
47 MODULE_DESCRIPTION("FIP discovery protocol support for FCoE HBAs");
48 MODULE_LICENSE("GPL v2");
50 #define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
51 #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
53 static void fcoe_ctlr_timeout(unsigned long);
54 static void fcoe_ctlr_link_work(struct work_struct *);
55 static void fcoe_ctlr_recv_work(struct work_struct *);
57 static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
59 static u32 fcoe_ctlr_debug; /* 1 for basic, 2 for noisy debug */
61 #define FIP_DBG_LVL(level, fmt, args...) \
63 if (fcoe_ctlr_debug >= (level)) \
64 FC_DBG(fmt, ##args); \
67 #define FIP_DBG(fmt, args...) FIP_DBG_LVL(1, fmt, ##args)
70 * Return non-zero if FCF fcoe_size has been validated.
72 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
74 return (fcf->flags & FIP_FL_SOL) != 0;
78 * Return non-zero if the FCF is usable.
80 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
82 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
84 return (fcf->flags & flags) == flags;
88 * fcoe_ctlr_init() - Initialize the FCoE Controller instance.
89 * @fip: FCoE controller.
91 void fcoe_ctlr_init(struct fcoe_ctlr *fip)
93 fip->state = FIP_ST_LINK_WAIT;
94 INIT_LIST_HEAD(&fip->fcfs);
95 spin_lock_init(&fip->lock);
96 fip->flogi_oxid = FC_XID_UNKNOWN;
97 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
98 INIT_WORK(&fip->link_work, fcoe_ctlr_link_work);
99 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
100 skb_queue_head_init(&fip->fip_recv_list);
102 EXPORT_SYMBOL(fcoe_ctlr_init);
105 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller.
106 * @fip: FCoE controller.
108 * Called with &fcoe_ctlr lock held.
110 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
112 struct fcoe_fcf *fcf;
113 struct fcoe_fcf *next;
116 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
117 list_del(&fcf->list);
125 * fcoe_ctlr_destroy() - Disable and tear-down the FCoE controller.
126 * @fip: FCoE controller.
128 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
130 * The receive handler will have been deleted before this to guarantee
131 * that no more recv_work will be scheduled.
133 * The timer routine will simply return once we set FIP_ST_DISABLED.
134 * This guarantees that no further timeouts or work will be scheduled.
136 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
138 flush_work(&fip->recv_work);
139 spin_lock_bh(&fip->lock);
140 fip->state = FIP_ST_DISABLED;
141 fcoe_ctlr_reset_fcfs(fip);
142 spin_unlock_bh(&fip->lock);
143 del_timer_sync(&fip->timer);
144 flush_work(&fip->link_work);
146 EXPORT_SYMBOL(fcoe_ctlr_destroy);
149 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port.
150 * @fip: FCoE controller.
152 * Returns the maximum packet size including the FCoE header and trailer,
153 * but not including any Ethernet or VLAN headers.
155 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
158 * Determine the max FCoE frame size allowed, including
159 * FCoE header and trailer.
160 * Note: lp->mfs is currently the payload size, not the frame size.
162 return fip->lp->mfs + sizeof(struct fc_frame_header) +
163 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
167 * fcoe_ctlr_solicit() - Send a solicitation.
168 * @fip: FCoE controller.
169 * @fcf: Destination FCF. If NULL, a multicast solicitation is sent.
171 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
176 struct fip_header fip;
178 struct fip_mac_desc mac;
179 struct fip_wwn_desc wwnn;
180 struct fip_size_desc size;
181 } __attribute__((packed)) desc;
182 } __attribute__((packed)) *sol;
185 skb = dev_alloc_skb(sizeof(*sol));
189 sol = (struct fip_sol *)skb->data;
191 memset(sol, 0, sizeof(*sol));
192 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
193 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
194 sol->eth.h_proto = htons(ETH_P_FIP);
196 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
197 sol->fip.fip_op = htons(FIP_OP_DISC);
198 sol->fip.fip_subcode = FIP_SC_SOL;
199 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
200 sol->fip.fip_flags = htons(FIP_FL_FPMA);
202 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
204 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
205 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
206 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
208 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
209 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
210 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
212 fcoe_size = fcoe_ctlr_fcoe_size(fip);
213 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
214 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
215 sol->desc.size.fd_size = htons(fcoe_size);
217 skb_put(skb, sizeof(*sol));
218 skb->protocol = htons(ETH_P_802_3);
219 skb_reset_mac_header(skb);
220 skb_reset_network_header(skb);
224 fip->sol_time = jiffies;
228 * fcoe_ctlr_link_up() - Start FCoE controller.
229 * @fip: FCoE controller.
231 * Called from the LLD when the network link is ready.
233 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
235 spin_lock_bh(&fip->lock);
236 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
239 spin_unlock_bh(&fip->lock);
241 } else if (fip->state == FIP_ST_LINK_WAIT) {
242 fip->state = FIP_ST_AUTO;
245 spin_unlock_bh(&fip->lock);
246 FIP_DBG("%s", "setting AUTO mode.\n");
248 fcoe_ctlr_solicit(fip, NULL);
250 spin_unlock_bh(&fip->lock);
252 EXPORT_SYMBOL(fcoe_ctlr_link_up);
255 * fcoe_ctlr_reset() - Reset FIP.
256 * @fip: FCoE controller.
257 * @new_state: FIP state to be entered.
259 * Returns non-zero if the link was up and now isn't.
261 static int fcoe_ctlr_reset(struct fcoe_ctlr *fip, enum fip_state new_state)
263 struct fc_lport *lp = fip->lp;
266 spin_lock_bh(&fip->lock);
267 fcoe_ctlr_reset_fcfs(fip);
268 del_timer(&fip->timer);
269 fip->state = new_state;
270 fip->ctlr_ka_time = 0;
271 fip->port_ka_time = 0;
273 fip->flogi_oxid = FC_XID_UNKNOWN;
276 link_dropped = fip->link;
278 spin_unlock_bh(&fip->lock);
283 if (new_state == FIP_ST_ENABLED) {
284 fcoe_ctlr_solicit(fip, NULL);
292 * fcoe_ctlr_link_down() - Stop FCoE controller.
293 * @fip: FCoE controller.
295 * Returns non-zero if the link was up and now isn't.
297 * Called from the LLD when the network link is not ready.
298 * There may be multiple calls while the link is down.
300 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
302 return fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
304 EXPORT_SYMBOL(fcoe_ctlr_link_down);
307 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF.
308 * @fip: FCoE controller.
309 * @ports: 0 for controller keep-alive, 1 for port keep-alive.
310 * @sa: source MAC address.
312 * A controller keep-alive is sent every fka_period (typically 8 seconds).
313 * The source MAC is the native MAC address.
315 * A port keep-alive is sent every 90 seconds while logged in.
316 * The source MAC is the assigned mapped source address.
317 * The destination is the FCF's F-port.
319 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip, int ports, u8 *sa)
324 struct fip_header fip;
325 struct fip_mac_desc mac;
326 } __attribute__((packed)) *kal;
327 struct fip_vn_desc *vn;
330 struct fcoe_fcf *fcf;
334 if (!fcf || !fc_host_port_id(lp->host))
337 len = fcoe_ctlr_fcoe_size(fip) + sizeof(struct ethhdr);
338 BUG_ON(len < sizeof(*kal) + sizeof(*vn));
339 skb = dev_alloc_skb(len);
343 kal = (struct fip_kal *)skb->data;
345 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
346 memcpy(kal->eth.h_source, sa, ETH_ALEN);
347 kal->eth.h_proto = htons(ETH_P_FIP);
349 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
350 kal->fip.fip_op = htons(FIP_OP_CTRL);
351 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
352 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
353 ports * sizeof(*vn)) / FIP_BPW);
354 kal->fip.fip_flags = htons(FIP_FL_FPMA);
356 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
358 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
359 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
360 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
363 vn = (struct fip_vn_desc *)(kal + 1);
364 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
365 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
366 memcpy(vn->fd_mac, fip->data_src_addr, ETH_ALEN);
367 hton24(vn->fd_fc_id, fc_host_port_id(lp->host));
368 put_unaligned_be64(lp->wwpn, &vn->fd_wwpn);
372 skb->protocol = htons(ETH_P_802_3);
373 skb_reset_mac_header(skb);
374 skb_reset_network_header(skb);
379 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it.
380 * @fip: FCoE controller.
381 * @dtype: FIP descriptor type for the frame.
382 * @skb: FCoE ELS frame including FC header but no FCoE headers.
384 * Returns non-zero error code on failure.
386 * The caller must check that the length is a multiple of 4.
388 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
389 * Headroom includes the FIP encapsulation description, FIP header, and
390 * Ethernet header. The tailroom is for the FIP MAC descriptor.
392 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip,
393 u8 dtype, struct sk_buff *skb)
395 struct fip_encaps_head {
397 struct fip_header fip;
398 struct fip_encaps encaps;
399 } __attribute__((packed)) *cap;
400 struct fip_mac_desc *mac;
401 struct fcoe_fcf *fcf;
407 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
408 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
410 memset(cap, 0, sizeof(*cap));
411 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
412 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
413 cap->eth.h_proto = htons(ETH_P_FIP);
415 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
416 cap->fip.fip_op = htons(FIP_OP_LS);
417 cap->fip.fip_subcode = FIP_SC_REQ;
418 cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
419 cap->fip.fip_flags = htons(FIP_FL_FPMA);
421 cap->fip.fip_flags |= htons(FIP_FL_SPMA);
423 cap->encaps.fd_desc.fip_dtype = dtype;
424 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
426 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
427 memset(mac, 0, sizeof(mac));
428 mac->fd_desc.fip_dtype = FIP_DT_MAC;
429 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
430 if (dtype != FIP_DT_FLOGI)
431 memcpy(mac->fd_mac, fip->data_src_addr, ETH_ALEN);
433 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
435 skb->protocol = htons(ETH_P_802_3);
436 skb_reset_mac_header(skb);
437 skb_reset_network_header(skb);
442 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
443 * @fip: FCoE controller.
444 * @skb: FCoE ELS frame including FC header but no FCoE headers.
446 * Returns a non-zero error code if the frame should not be sent.
447 * Returns zero if the caller should send the frame with FCoE encapsulation.
449 * The caller must check that the length is a multiple of 4.
450 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
452 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
454 struct fc_frame_header *fh;
458 if (fip->state == FIP_ST_NON_FIP)
461 fh = (struct fc_frame_header *)skb->data;
462 op = *(u8 *)(fh + 1);
466 old_xid = fip->flogi_oxid;
467 fip->flogi_oxid = ntohs(fh->fh_ox_id);
468 if (fip->state == FIP_ST_AUTO) {
469 if (old_xid == FC_XID_UNKNOWN)
470 fip->flogi_count = 0;
472 if (fip->flogi_count < 3)
480 if (ntoh24(fh->fh_s_id))
485 if (fip->state != FIP_ST_ENABLED)
487 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
492 if (fip->flogi_oxid == FC_XID_UNKNOWN)
494 if (!ntoh24(fh->fh_s_id))
496 if (fip->state == FIP_ST_AUTO)
499 * Here we must've gotten an SID by accepting an FLOGI
500 * from a point-to-point connection. Switch to using
501 * the source mac based on the SID. The destination
502 * MAC in this case would have been set by receving the
505 fip->flogi_oxid = FC_XID_UNKNOWN;
506 fc_fcoe_set_mac(fip->data_src_addr, fh->fh_s_id);
509 if (fip->state != FIP_ST_ENABLED)
513 if (fcoe_ctlr_encaps(fip, op, skb))
521 EXPORT_SYMBOL(fcoe_ctlr_els_send);
524 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller.
525 * @fip: FCoE controller.
527 * Called with lock held.
529 * An FCF is considered old if we have missed three advertisements.
530 * That is, there have been no valid advertisement from it for three
531 * times its keep-alive period including fuzz.
533 * In addition, determine the time when an FCF selection can occur.
535 static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
537 struct fcoe_fcf *fcf;
538 struct fcoe_fcf *next;
539 unsigned long sel_time = 0;
541 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
542 if (time_after(jiffies, fcf->time + fcf->fka_period * 3 +
543 msecs_to_jiffies(FIP_FCF_FUZZ * 3))) {
544 if (fip->sel_fcf == fcf)
546 list_del(&fcf->list);
547 WARN_ON(!fip->fcf_count);
550 } else if (fcoe_ctlr_mtu_valid(fcf) &&
551 (!sel_time || time_before(sel_time, fcf->time))) {
552 sel_time = fcf->time;
556 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
557 fip->sel_time = sel_time;
558 if (time_before(sel_time, fip->timer.expires))
559 mod_timer(&fip->timer, sel_time);
566 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry.
567 * @skb: received FIP advertisement frame
568 * @fcf: resulting FCF entry.
570 * Returns zero on a valid parsed advertisement,
571 * otherwise returns non zero value.
573 static int fcoe_ctlr_parse_adv(struct sk_buff *skb, struct fcoe_fcf *fcf)
575 struct fip_header *fiph;
576 struct fip_desc *desc = NULL;
577 struct fip_wwn_desc *wwn;
578 struct fip_fab_desc *fab;
579 struct fip_fka_desc *fka;
584 memset(fcf, 0, sizeof(*fcf));
585 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
587 fiph = (struct fip_header *)skb->data;
588 fcf->flags = ntohs(fiph->fip_flags);
590 rlen = ntohs(fiph->fip_dl_len) * 4;
591 if (rlen + sizeof(*fiph) > skb->len)
594 desc = (struct fip_desc *)(fiph + 1);
596 dlen = desc->fip_dlen * FIP_BPW;
597 if (dlen < sizeof(*desc) || dlen > rlen)
599 switch (desc->fip_dtype) {
601 if (dlen != sizeof(struct fip_pri_desc))
603 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
606 if (dlen != sizeof(struct fip_mac_desc))
609 ((struct fip_mac_desc *)desc)->fd_mac,
611 if (!is_valid_ether_addr(fcf->fcf_mac)) {
612 FIP_DBG("invalid MAC addr in FIP adv\n");
617 if (dlen != sizeof(struct fip_wwn_desc))
619 wwn = (struct fip_wwn_desc *)desc;
620 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
623 if (dlen != sizeof(struct fip_fab_desc))
625 fab = (struct fip_fab_desc *)desc;
626 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
627 fcf->vfid = ntohs(fab->fd_vfid);
628 fcf->fc_map = ntoh24(fab->fd_map);
631 if (dlen != sizeof(struct fip_fka_desc))
633 fka = (struct fip_fka_desc *)desc;
634 t = ntohl(fka->fd_fka_period);
635 if (t >= FCOE_CTLR_MIN_FKA)
636 fcf->fka_period = msecs_to_jiffies(t);
639 case FIP_DT_FCOE_SIZE:
645 FIP_DBG("unexpected descriptor type %x in FIP adv\n",
647 /* standard says ignore unknown descriptors >= 128 */
648 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
652 desc = (struct fip_desc *)((char *)desc + dlen);
655 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
657 if (!fcf->switch_name || !fcf->fabric_name)
662 FIP_DBG("FIP length error in descriptor type %x len %zu\n",
663 desc->fip_dtype, dlen);
668 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement.
669 * @fip: FCoE controller.
670 * @skb: Received FIP packet.
672 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
674 struct fcoe_fcf *fcf;
676 struct fcoe_fcf *found;
677 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
681 if (fcoe_ctlr_parse_adv(skb, &new))
684 spin_lock_bh(&fip->lock);
685 first = list_empty(&fip->fcfs);
687 list_for_each_entry(fcf, &fip->fcfs, list) {
688 if (fcf->switch_name == new.switch_name &&
689 fcf->fabric_name == new.fabric_name &&
690 fcf->fc_map == new.fc_map &&
691 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
697 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
700 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
705 memcpy(fcf, &new, sizeof(new));
706 list_add(&fcf->list, &fip->fcfs);
709 * Flags in advertisements are ignored once the FCF is
710 * selected. Flags in unsolicited advertisements are
711 * ignored after a usable solicited advertisement
714 if (fcf == fip->sel_fcf) {
715 fip->ctlr_ka_time -= fcf->fka_period;
716 fip->ctlr_ka_time += new.fka_period;
717 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
718 mod_timer(&fip->timer, fip->ctlr_ka_time);
719 } else if (!fcoe_ctlr_fcf_usable(fcf))
720 fcf->flags = new.flags;
721 fcf->fka_period = new.fka_period;
722 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
724 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
726 FIP_DBG_LVL(found ? 2 : 1, "%s FCF for fab %llx map %x val %d\n",
727 found ? "old" : "new",
728 fcf->fabric_name, fcf->fc_map, mtu_valid);
731 * If this advertisement is not solicited and our max receive size
732 * hasn't been verified, send a solicited advertisement.
735 fcoe_ctlr_solicit(fip, fcf);
738 * If its been a while since we did a solicit, and this is
739 * the first advertisement we've received, do a multicast
740 * solicitation to gather as many advertisements as we can
741 * before selection occurs.
743 if (first && time_after(jiffies, fip->sol_time + sol_tov))
744 fcoe_ctlr_solicit(fip, NULL);
747 * If this is the first validated FCF, note the time and
748 * set a timer to trigger selection.
750 if (mtu_valid && !fip->sel_time && fcoe_ctlr_fcf_usable(fcf)) {
751 fip->sel_time = jiffies +
752 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
753 if (!timer_pending(&fip->timer) ||
754 time_before(fip->sel_time, fip->timer.expires))
755 mod_timer(&fip->timer, fip->sel_time);
758 spin_unlock_bh(&fip->lock);
762 * fcoe_ctlr_recv_els() - Handle an incoming FIP-encapsulated ELS frame.
763 * @fip: FCoE controller.
764 * @skb: Received FIP packet.
766 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
768 struct fc_lport *lp = fip->lp;
769 struct fip_header *fiph;
771 struct fc_frame_header *fh = NULL;
772 struct fip_desc *desc;
773 struct fip_encaps *els;
774 struct fcoe_dev_stats *stats;
775 enum fip_desc_type els_dtype = 0;
778 u8 granted_mac[ETH_ALEN] = { 0 };
783 fiph = (struct fip_header *)skb->data;
784 sub = fiph->fip_subcode;
785 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
788 rlen = ntohs(fiph->fip_dl_len) * 4;
789 if (rlen + sizeof(*fiph) > skb->len)
792 desc = (struct fip_desc *)(fiph + 1);
794 dlen = desc->fip_dlen * FIP_BPW;
795 if (dlen < sizeof(*desc) || dlen > rlen)
797 switch (desc->fip_dtype) {
799 if (dlen != sizeof(struct fip_mac_desc))
802 ((struct fip_mac_desc *)desc)->fd_mac,
804 if (!is_valid_ether_addr(granted_mac)) {
805 FIP_DBG("invalid MAC addrs in FIP ELS\n");
815 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
817 els_len = dlen - sizeof(*els);
818 els = (struct fip_encaps *)desc;
819 fh = (struct fc_frame_header *)(els + 1);
820 els_dtype = desc->fip_dtype;
823 FIP_DBG("unexpected descriptor type %x "
824 "in FIP adv\n", desc->fip_dtype);
825 /* standard says ignore unknown descriptors >= 128 */
826 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
830 desc = (struct fip_desc *)((char *)desc + dlen);
836 els_op = *(u8 *)(fh + 1);
838 if (els_dtype == FIP_DT_FLOGI && sub == FIP_SC_REP &&
839 fip->flogi_oxid == ntohs(fh->fh_ox_id) &&
840 els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac)) {
841 fip->flogi_oxid = FC_XID_UNKNOWN;
842 fip->update_mac(fip, fip->data_src_addr, granted_mac);
843 memcpy(fip->data_src_addr, granted_mac, ETH_ALEN);
847 * Convert skb into an fc_frame containing only the ELS.
849 skb_pull(skb, (u8 *)fh - skb->data);
850 skb_trim(skb, els_len);
851 fp = (struct fc_frame *)skb;
853 fr_sof(fp) = FC_SOF_I3;
854 fr_eof(fp) = FC_EOF_T;
857 stats = fc_lport_get_stats(lp);
859 stats->RxWords += skb->len / FIP_BPW;
861 fc_exch_recv(lp, lp->emp, fp);
865 FIP_DBG("FIP length error in descriptor type %x len %zu\n",
866 desc->fip_dtype, dlen);
872 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame.
873 * @fip: FCoE controller.
874 * @fh: Received FIP header.
876 * There may be multiple VN_Port descriptors.
877 * The overall length has already been checked.
879 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
880 struct fip_header *fh)
882 struct fip_desc *desc;
883 struct fip_mac_desc *mp;
884 struct fip_wwn_desc *wp;
885 struct fip_vn_desc *vp;
888 struct fcoe_fcf *fcf = fip->sel_fcf;
889 struct fc_lport *lp = fip->lp;
892 FIP_DBG("Clear Virtual Link received\n");
895 if (!fcf || !fc_host_port_id(lp->host))
899 * mask of required descriptors. Validating each one clears its bit.
901 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
903 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
904 desc = (struct fip_desc *)(fh + 1);
905 while (rlen >= sizeof(*desc)) {
906 dlen = desc->fip_dlen * FIP_BPW;
909 switch (desc->fip_dtype) {
911 mp = (struct fip_mac_desc *)desc;
912 if (dlen < sizeof(*mp))
914 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
916 desc_mask &= ~BIT(FIP_DT_MAC);
919 wp = (struct fip_wwn_desc *)desc;
920 if (dlen < sizeof(*wp))
922 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
924 desc_mask &= ~BIT(FIP_DT_NAME);
927 vp = (struct fip_vn_desc *)desc;
928 if (dlen < sizeof(*vp))
930 if (compare_ether_addr(vp->fd_mac,
931 fip->data_src_addr) == 0 &&
932 get_unaligned_be64(&vp->fd_wwpn) == lp->wwpn &&
933 ntoh24(vp->fd_fc_id) == fc_host_port_id(lp->host))
934 desc_mask &= ~BIT(FIP_DT_VN_ID);
937 /* standard says ignore unknown descriptors >= 128 */
938 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
942 desc = (struct fip_desc *)((char *)desc + dlen);
947 * reset only if all required descriptors were present and valid.
950 FIP_DBG("missing descriptors mask %x\n", desc_mask);
952 FIP_DBG("performing Clear Virtual Link\n");
953 fcoe_ctlr_reset(fip, FIP_ST_ENABLED);
958 * fcoe_ctlr_recv() - Receive a FIP frame.
959 * @fip: FCoE controller.
960 * @skb: Received FIP packet.
962 * This is called from NET_RX_SOFTIRQ.
964 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
966 spin_lock_bh(&fip->fip_recv_list.lock);
967 __skb_queue_tail(&fip->fip_recv_list, skb);
968 spin_unlock_bh(&fip->fip_recv_list.lock);
969 schedule_work(&fip->recv_work);
971 EXPORT_SYMBOL(fcoe_ctlr_recv);
974 * fcoe_ctlr_recv_handler() - Receive a FIP frame.
975 * @fip: FCoE controller.
976 * @skb: Received FIP packet.
978 * Returns non-zero if the frame is dropped.
980 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
982 struct fip_header *fiph;
984 enum fip_state state;
988 if (skb_linearize(skb))
990 if (skb->len < sizeof(*fiph))
993 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
994 compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS))
996 fiph = (struct fip_header *)skb->data;
997 op = ntohs(fiph->fip_op);
998 sub = fiph->fip_subcode;
1000 FIP_DBG_LVL(2, "ver %x op %x/%x dl %x fl %x\n",
1001 FIP_VER_DECAPS(fiph->fip_ver), op, sub,
1002 ntohs(fiph->fip_dl_len), ntohs(fiph->fip_flags));
1004 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1006 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1009 spin_lock_bh(&fip->lock);
1011 if (state == FIP_ST_AUTO) {
1013 fip->state = FIP_ST_ENABLED;
1014 state = FIP_ST_ENABLED;
1015 FIP_DBG("using FIP mode\n");
1017 spin_unlock_bh(&fip->lock);
1018 if (state != FIP_ST_ENABLED)
1021 if (op == FIP_OP_LS) {
1022 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1025 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1026 fcoe_ctlr_recv_adv(fip, skb);
1027 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1028 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1037 * fcoe_ctlr_select() - Select the best FCF, if possible.
1038 * @fip: FCoE controller.
1040 * If there are conflicting advertisements, no FCF can be chosen.
1042 * Called with lock held.
1044 static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1046 struct fcoe_fcf *fcf;
1047 struct fcoe_fcf *best = NULL;
1049 list_for_each_entry(fcf, &fip->fcfs, list) {
1050 FIP_DBG("consider FCF for fab %llx VFID %d map %x val %d\n",
1051 fcf->fabric_name, fcf->vfid,
1052 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
1053 if (!fcoe_ctlr_fcf_usable(fcf)) {
1054 FIP_DBG("FCF for fab %llx map %x %svalid %savailable\n",
1055 fcf->fabric_name, fcf->fc_map,
1056 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1057 (fcf->flags & FIP_FL_AVAIL) ? "" : "un");
1064 if (fcf->fabric_name != best->fabric_name ||
1065 fcf->vfid != best->vfid ||
1066 fcf->fc_map != best->fc_map) {
1067 FIP_DBG("conflicting fabric, VFID, or FC-MAP\n");
1070 if (fcf->pri < best->pri)
1073 fip->sel_fcf = best;
1077 * fcoe_ctlr_timeout() - FIP timer function.
1078 * @arg: &fcoe_ctlr pointer.
1080 * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
1082 static void fcoe_ctlr_timeout(unsigned long arg)
1084 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1085 struct fcoe_fcf *sel;
1086 struct fcoe_fcf *fcf;
1087 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
1088 DECLARE_MAC_BUF(buf);
1092 spin_lock_bh(&fip->lock);
1093 if (fip->state == FIP_ST_DISABLED) {
1094 spin_unlock_bh(&fip->lock);
1099 fcoe_ctlr_age_fcfs(fip);
1102 if (!sel && fip->sel_time && time_after_eq(jiffies, fip->sel_time)) {
1103 fcoe_ctlr_select(fip);
1109 fcf = sel; /* the old FCF may have been freed */
1111 printk(KERN_INFO "host%d: FIP selected "
1112 "Fibre-Channel Forwarder MAC %s\n",
1113 fip->lp->host->host_no,
1114 print_mac(buf, sel->fcf_mac));
1115 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1116 fip->port_ka_time = jiffies +
1117 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1118 fip->ctlr_ka_time = jiffies + sel->fka_period;
1121 printk(KERN_NOTICE "host%d: "
1122 "FIP Fibre-Channel Forwarder timed out. "
1123 "Starting FCF discovery.\n",
1124 fip->lp->host->host_no);
1127 schedule_work(&fip->link_work);
1133 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1134 fip->ctlr_ka_time = jiffies + sel->fka_period;
1137 if (time_after(next_timer, fip->ctlr_ka_time))
1138 next_timer = fip->ctlr_ka_time;
1140 if (time_after_eq(jiffies, fip->port_ka_time)) {
1141 fip->port_ka_time += jiffies +
1142 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1145 if (time_after(next_timer, fip->port_ka_time))
1146 next_timer = fip->port_ka_time;
1147 mod_timer(&fip->timer, next_timer);
1148 } else if (fip->sel_time) {
1149 next_timer = fip->sel_time +
1150 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1151 mod_timer(&fip->timer, next_timer);
1153 spin_unlock_bh(&fip->lock);
1156 fcoe_ctlr_send_keep_alive(fip, 0, fip->ctl_src_addr);
1158 fcoe_ctlr_send_keep_alive(fip, 1, fip->data_src_addr);
1162 * fcoe_ctlr_link_work() - worker thread function for link changes.
1163 * @work: pointer to link_work member inside &fcoe_ctlr.
1165 * See if the link status has changed and if so, report it.
1167 * This is here because fc_linkup() and fc_linkdown() must not
1168 * be called from the timer directly, since they use a mutex.
1170 static void fcoe_ctlr_link_work(struct work_struct *work)
1172 struct fcoe_ctlr *fip;
1176 fip = container_of(work, struct fcoe_ctlr, link_work);
1177 spin_lock_bh(&fip->lock);
1178 last_link = fip->last_link;
1180 fip->last_link = link;
1181 spin_unlock_bh(&fip->lock);
1183 if (last_link != link) {
1187 fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
1192 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames.
1193 * @recv_work: pointer to recv_work member inside &fcoe_ctlr.
1195 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1197 struct fcoe_ctlr *fip;
1198 struct sk_buff *skb;
1200 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1201 spin_lock_bh(&fip->fip_recv_list.lock);
1202 while ((skb = __skb_dequeue(&fip->fip_recv_list))) {
1203 spin_unlock_bh(&fip->fip_recv_list.lock);
1204 fcoe_ctlr_recv_handler(fip, skb);
1205 spin_lock_bh(&fip->fip_recv_list.lock);
1207 spin_unlock_bh(&fip->fip_recv_list.lock);
1211 * fcoe_ctlr_recv_flogi() - snoop Pre-FIP receipt of FLOGI response or request.
1212 * @fip: FCoE controller.
1214 * @sa: Ethernet source MAC address from received FCoE frame.
1216 * Snoop potential response to FLOGI or even incoming FLOGI.
1218 * The caller has checked that we are waiting for login as indicated
1219 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1221 * The caller is responsible for freeing the frame.
1223 * Return non-zero if the frame should not be delivered to libfc.
1225 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_frame *fp, u8 *sa)
1227 struct fc_frame_header *fh;
1231 fh = fc_frame_header_get(fp);
1232 if (fh->fh_type != FC_TYPE_ELS)
1235 op = fc_frame_payload_op(fp);
1236 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1237 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1239 spin_lock_bh(&fip->lock);
1240 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1241 spin_unlock_bh(&fip->lock);
1244 fip->state = FIP_ST_NON_FIP;
1245 FIP_DBG("received FLOGI LS_ACC using non-FIP mode\n");
1249 * If the src mac addr is FC_OUI-based, then we mark the
1250 * address_mode flag to use FC_OUI-based Ethernet DA.
1251 * Otherwise we use the FCoE gateway addr
1253 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1256 memcpy(fip->dest_addr, sa, ETH_ALEN);
1259 fip->flogi_oxid = FC_XID_UNKNOWN;
1260 memcpy(mac, fip->data_src_addr, ETH_ALEN);
1261 fc_fcoe_set_mac(fip->data_src_addr, fh->fh_d_id);
1262 spin_unlock_bh(&fip->lock);
1264 fip->update_mac(fip, mac, fip->data_src_addr);
1265 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1267 * Save source MAC for point-to-point responses.
1269 spin_lock_bh(&fip->lock);
1270 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1271 memcpy(fip->dest_addr, sa, ETH_ALEN);
1273 if (fip->state == FIP_ST_NON_FIP)
1274 FIP_DBG("received FLOGI REQ, "
1275 "using non-FIP mode\n");
1276 fip->state = FIP_ST_NON_FIP;
1278 spin_unlock_bh(&fip->lock);
1282 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1285 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
1287 * @scheme: check port
1288 * @port: port indicator for converting
1290 * Returns: u64 fc world wide name
1292 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1293 unsigned int scheme, unsigned int port)
1298 /* The MAC is in NO, so flip only the low 48 bits */
1299 host_mac = ((u64) mac[0] << 40) |
1300 ((u64) mac[1] << 32) |
1301 ((u64) mac[2] << 24) |
1302 ((u64) mac[3] << 16) |
1303 ((u64) mac[4] << 8) |
1306 WARN_ON(host_mac >= (1ULL << 48));
1307 wwn = host_mac | ((u64) scheme << 60);
1313 WARN_ON(port >= 0xfff);
1314 wwn |= (u64) port << 48;
1323 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1326 * fcoe_libfc_config() - sets up libfc related properties for lport
1327 * @lp: ptr to the fc_lport
1328 * @tt: libfc function template
1330 * Returns : 0 for success
1332 int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1334 /* Set the function pointers set by the LLDD */
1335 memcpy(&lp->tt, tt, sizeof(*tt));
1336 if (fc_fcp_init(lp))
1346 EXPORT_SYMBOL_GPL(fcoe_libfc_config);