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 unsigned int libfcoe_debug_logging;
60 module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
61 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
63 #define LIBFCOE_LOGGING 0x01 /* General logging, not categorized */
64 #define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
66 #define LIBFCOE_CHECK_LOGGING(LEVEL, CMD) \
68 if (unlikely(libfcoe_debug_logging & LEVEL)) \
74 #define LIBFCOE_DBG(fmt, args...) \
75 LIBFCOE_CHECK_LOGGING(LIBFCOE_LOGGING, \
76 printk(KERN_INFO "libfcoe: " fmt, ##args);)
78 #define LIBFCOE_FIP_DBG(fmt, args...) \
79 LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING, \
80 printk(KERN_INFO "fip: " fmt, ##args);)
83 * Return non-zero if FCF fcoe_size has been validated.
85 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
87 return (fcf->flags & FIP_FL_SOL) != 0;
91 * Return non-zero if the FCF is usable.
93 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
95 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
97 return (fcf->flags & flags) == flags;
101 * fcoe_ctlr_init() - Initialize the FCoE Controller instance.
102 * @fip: FCoE controller.
104 void fcoe_ctlr_init(struct fcoe_ctlr *fip)
106 fip->state = FIP_ST_LINK_WAIT;
107 INIT_LIST_HEAD(&fip->fcfs);
108 spin_lock_init(&fip->lock);
109 fip->flogi_oxid = FC_XID_UNKNOWN;
110 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
111 INIT_WORK(&fip->link_work, fcoe_ctlr_link_work);
112 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
113 skb_queue_head_init(&fip->fip_recv_list);
115 EXPORT_SYMBOL(fcoe_ctlr_init);
118 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller.
119 * @fip: FCoE controller.
121 * Called with &fcoe_ctlr lock held.
123 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
125 struct fcoe_fcf *fcf;
126 struct fcoe_fcf *next;
129 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
130 list_del(&fcf->list);
138 * fcoe_ctlr_destroy() - Disable and tear-down the FCoE controller.
139 * @fip: FCoE controller.
141 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
143 * The receive handler will have been deleted before this to guarantee
144 * that no more recv_work will be scheduled.
146 * The timer routine will simply return once we set FIP_ST_DISABLED.
147 * This guarantees that no further timeouts or work will be scheduled.
149 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
151 flush_work(&fip->recv_work);
152 spin_lock_bh(&fip->lock);
153 fip->state = FIP_ST_DISABLED;
154 fcoe_ctlr_reset_fcfs(fip);
155 spin_unlock_bh(&fip->lock);
156 del_timer_sync(&fip->timer);
157 flush_work(&fip->link_work);
159 EXPORT_SYMBOL(fcoe_ctlr_destroy);
162 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port.
163 * @fip: FCoE controller.
165 * Returns the maximum packet size including the FCoE header and trailer,
166 * but not including any Ethernet or VLAN headers.
168 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
171 * Determine the max FCoE frame size allowed, including
172 * FCoE header and trailer.
173 * Note: lp->mfs is currently the payload size, not the frame size.
175 return fip->lp->mfs + sizeof(struct fc_frame_header) +
176 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
180 * fcoe_ctlr_solicit() - Send a solicitation.
181 * @fip: FCoE controller.
182 * @fcf: Destination FCF. If NULL, a multicast solicitation is sent.
184 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
189 struct fip_header fip;
191 struct fip_mac_desc mac;
192 struct fip_wwn_desc wwnn;
193 struct fip_size_desc size;
194 } __attribute__((packed)) desc;
195 } __attribute__((packed)) *sol;
198 skb = dev_alloc_skb(sizeof(*sol));
202 sol = (struct fip_sol *)skb->data;
204 memset(sol, 0, sizeof(*sol));
205 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
206 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
207 sol->eth.h_proto = htons(ETH_P_FIP);
209 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
210 sol->fip.fip_op = htons(FIP_OP_DISC);
211 sol->fip.fip_subcode = FIP_SC_SOL;
212 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
213 sol->fip.fip_flags = htons(FIP_FL_FPMA);
215 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
217 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
218 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
219 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
221 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
222 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
223 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
225 fcoe_size = fcoe_ctlr_fcoe_size(fip);
226 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
227 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
228 sol->desc.size.fd_size = htons(fcoe_size);
230 skb_put(skb, sizeof(*sol));
231 skb->protocol = htons(ETH_P_FIP);
232 skb_reset_mac_header(skb);
233 skb_reset_network_header(skb);
237 fip->sol_time = jiffies;
241 * fcoe_ctlr_link_up() - Start FCoE controller.
242 * @fip: FCoE controller.
244 * Called from the LLD when the network link is ready.
246 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
248 spin_lock_bh(&fip->lock);
249 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
252 spin_unlock_bh(&fip->lock);
254 } else if (fip->state == FIP_ST_LINK_WAIT) {
255 fip->state = FIP_ST_AUTO;
258 spin_unlock_bh(&fip->lock);
259 LIBFCOE_FIP_DBG("%s", "setting AUTO mode.\n");
261 fcoe_ctlr_solicit(fip, NULL);
263 spin_unlock_bh(&fip->lock);
265 EXPORT_SYMBOL(fcoe_ctlr_link_up);
268 * fcoe_ctlr_reset() - Reset FIP.
269 * @fip: FCoE controller.
270 * @new_state: FIP state to be entered.
272 * Returns non-zero if the link was up and now isn't.
274 static int fcoe_ctlr_reset(struct fcoe_ctlr *fip, enum fip_state new_state)
276 struct fc_lport *lp = fip->lp;
279 spin_lock_bh(&fip->lock);
280 fcoe_ctlr_reset_fcfs(fip);
281 del_timer(&fip->timer);
282 fip->state = new_state;
283 fip->ctlr_ka_time = 0;
284 fip->port_ka_time = 0;
286 fip->flogi_oxid = FC_XID_UNKNOWN;
289 link_dropped = fip->link;
291 spin_unlock_bh(&fip->lock);
296 if (new_state == FIP_ST_ENABLED) {
297 fcoe_ctlr_solicit(fip, NULL);
305 * fcoe_ctlr_link_down() - Stop FCoE controller.
306 * @fip: FCoE controller.
308 * Returns non-zero if the link was up and now isn't.
310 * Called from the LLD when the network link is not ready.
311 * There may be multiple calls while the link is down.
313 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
315 return fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
317 EXPORT_SYMBOL(fcoe_ctlr_link_down);
320 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF.
321 * @fip: FCoE controller.
322 * @ports: 0 for controller keep-alive, 1 for port keep-alive.
323 * @sa: source MAC address.
325 * A controller keep-alive is sent every fka_period (typically 8 seconds).
326 * The source MAC is the native MAC address.
328 * A port keep-alive is sent every 90 seconds while logged in.
329 * The source MAC is the assigned mapped source address.
330 * The destination is the FCF's F-port.
332 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip, int ports, u8 *sa)
337 struct fip_header fip;
338 struct fip_mac_desc mac;
339 } __attribute__((packed)) *kal;
340 struct fip_vn_desc *vn;
343 struct fcoe_fcf *fcf;
347 if (!fcf || !fc_host_port_id(lp->host))
350 len = fcoe_ctlr_fcoe_size(fip) + sizeof(struct ethhdr);
351 BUG_ON(len < sizeof(*kal) + sizeof(*vn));
352 skb = dev_alloc_skb(len);
356 kal = (struct fip_kal *)skb->data;
358 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
359 memcpy(kal->eth.h_source, sa, ETH_ALEN);
360 kal->eth.h_proto = htons(ETH_P_FIP);
362 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
363 kal->fip.fip_op = htons(FIP_OP_CTRL);
364 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
365 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
366 ports * sizeof(*vn)) / FIP_BPW);
367 kal->fip.fip_flags = htons(FIP_FL_FPMA);
369 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
371 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
372 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
373 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
376 vn = (struct fip_vn_desc *)(kal + 1);
377 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
378 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
379 memcpy(vn->fd_mac, fip->data_src_addr, ETH_ALEN);
380 hton24(vn->fd_fc_id, fc_host_port_id(lp->host));
381 put_unaligned_be64(lp->wwpn, &vn->fd_wwpn);
385 skb->protocol = htons(ETH_P_FIP);
386 skb_reset_mac_header(skb);
387 skb_reset_network_header(skb);
392 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it.
393 * @fip: FCoE controller.
394 * @dtype: FIP descriptor type for the frame.
395 * @skb: FCoE ELS frame including FC header but no FCoE headers.
397 * Returns non-zero error code on failure.
399 * The caller must check that the length is a multiple of 4.
401 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
402 * Headroom includes the FIP encapsulation description, FIP header, and
403 * Ethernet header. The tailroom is for the FIP MAC descriptor.
405 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip,
406 u8 dtype, struct sk_buff *skb)
408 struct fip_encaps_head {
410 struct fip_header fip;
411 struct fip_encaps encaps;
412 } __attribute__((packed)) *cap;
413 struct fip_mac_desc *mac;
414 struct fcoe_fcf *fcf;
422 /* set flags according to both FCF and lport's capability on SPMA */
423 fip_flags = fcf->flags;
424 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA : FIP_FL_FPMA;
428 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
429 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
431 memset(cap, 0, sizeof(*cap));
432 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
433 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
434 cap->eth.h_proto = htons(ETH_P_FIP);
436 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
437 cap->fip.fip_op = htons(FIP_OP_LS);
438 cap->fip.fip_subcode = FIP_SC_REQ;
439 cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
440 cap->fip.fip_flags = htons(fip_flags);
442 cap->encaps.fd_desc.fip_dtype = dtype;
443 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
445 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
446 memset(mac, 0, sizeof(mac));
447 mac->fd_desc.fip_dtype = FIP_DT_MAC;
448 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
449 if (dtype != FIP_DT_FLOGI)
450 memcpy(mac->fd_mac, fip->data_src_addr, ETH_ALEN);
452 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
454 skb->protocol = htons(ETH_P_FIP);
455 skb_reset_mac_header(skb);
456 skb_reset_network_header(skb);
461 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
462 * @fip: FCoE controller.
463 * @skb: FCoE ELS frame including FC header but no FCoE headers.
465 * Returns a non-zero error code if the frame should not be sent.
466 * Returns zero if the caller should send the frame with FCoE encapsulation.
468 * The caller must check that the length is a multiple of 4.
469 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
471 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
473 struct fc_frame_header *fh;
477 fh = (struct fc_frame_header *)skb->data;
478 op = *(u8 *)(fh + 1);
480 if (op == ELS_FLOGI) {
481 old_xid = fip->flogi_oxid;
482 fip->flogi_oxid = ntohs(fh->fh_ox_id);
483 if (fip->state == FIP_ST_AUTO) {
484 if (old_xid == FC_XID_UNKNOWN)
485 fip->flogi_count = 0;
487 if (fip->flogi_count < 3)
492 if (fip->state == FIP_ST_NON_FIP)
496 if (fip->state == FIP_ST_NON_FIP)
504 if (ntoh24(fh->fh_s_id))
509 if (fip->state != FIP_ST_ENABLED)
511 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
516 if (fip->flogi_oxid == FC_XID_UNKNOWN)
518 if (!ntoh24(fh->fh_s_id))
520 if (fip->state == FIP_ST_AUTO)
523 * Here we must've gotten an SID by accepting an FLOGI
524 * from a point-to-point connection. Switch to using
525 * the source mac based on the SID. The destination
526 * MAC in this case would have been set by receving the
529 fip->flogi_oxid = FC_XID_UNKNOWN;
530 fc_fcoe_set_mac(fip->data_src_addr, fh->fh_s_id);
533 if (fip->state != FIP_ST_ENABLED)
537 if (fcoe_ctlr_encaps(fip, op, skb))
545 EXPORT_SYMBOL(fcoe_ctlr_els_send);
548 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller.
549 * @fip: FCoE controller.
551 * Called with lock held.
553 * An FCF is considered old if we have missed three advertisements.
554 * That is, there have been no valid advertisement from it for three
555 * times its keep-alive period including fuzz.
557 * In addition, determine the time when an FCF selection can occur.
559 static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
561 struct fcoe_fcf *fcf;
562 struct fcoe_fcf *next;
563 unsigned long sel_time = 0;
565 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
566 if (time_after(jiffies, fcf->time + fcf->fka_period * 3 +
567 msecs_to_jiffies(FIP_FCF_FUZZ * 3))) {
568 if (fip->sel_fcf == fcf)
570 list_del(&fcf->list);
571 WARN_ON(!fip->fcf_count);
574 } else if (fcoe_ctlr_mtu_valid(fcf) &&
575 (!sel_time || time_before(sel_time, fcf->time))) {
576 sel_time = fcf->time;
580 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
581 fip->sel_time = sel_time;
582 if (time_before(sel_time, fip->timer.expires))
583 mod_timer(&fip->timer, sel_time);
590 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry.
591 * @skb: received FIP advertisement frame
592 * @fcf: resulting FCF entry.
594 * Returns zero on a valid parsed advertisement,
595 * otherwise returns non zero value.
597 static int fcoe_ctlr_parse_adv(struct sk_buff *skb, struct fcoe_fcf *fcf)
599 struct fip_header *fiph;
600 struct fip_desc *desc = NULL;
601 struct fip_wwn_desc *wwn;
602 struct fip_fab_desc *fab;
603 struct fip_fka_desc *fka;
608 memset(fcf, 0, sizeof(*fcf));
609 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
611 fiph = (struct fip_header *)skb->data;
612 fcf->flags = ntohs(fiph->fip_flags);
614 rlen = ntohs(fiph->fip_dl_len) * 4;
615 if (rlen + sizeof(*fiph) > skb->len)
618 desc = (struct fip_desc *)(fiph + 1);
620 dlen = desc->fip_dlen * FIP_BPW;
621 if (dlen < sizeof(*desc) || dlen > rlen)
623 switch (desc->fip_dtype) {
625 if (dlen != sizeof(struct fip_pri_desc))
627 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
630 if (dlen != sizeof(struct fip_mac_desc))
633 ((struct fip_mac_desc *)desc)->fd_mac,
635 if (!is_valid_ether_addr(fcf->fcf_mac)) {
636 LIBFCOE_FIP_DBG("Invalid MAC address "
642 if (dlen != sizeof(struct fip_wwn_desc))
644 wwn = (struct fip_wwn_desc *)desc;
645 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
648 if (dlen != sizeof(struct fip_fab_desc))
650 fab = (struct fip_fab_desc *)desc;
651 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
652 fcf->vfid = ntohs(fab->fd_vfid);
653 fcf->fc_map = ntoh24(fab->fd_map);
656 if (dlen != sizeof(struct fip_fka_desc))
658 fka = (struct fip_fka_desc *)desc;
659 t = ntohl(fka->fd_fka_period);
660 if (t >= FCOE_CTLR_MIN_FKA)
661 fcf->fka_period = msecs_to_jiffies(t);
664 case FIP_DT_FCOE_SIZE:
670 LIBFCOE_FIP_DBG("unexpected descriptor type %x "
671 "in FIP adv\n", desc->fip_dtype);
672 /* standard says ignore unknown descriptors >= 128 */
673 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
677 desc = (struct fip_desc *)((char *)desc + dlen);
680 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
682 if (!fcf->switch_name || !fcf->fabric_name)
687 LIBFCOE_FIP_DBG("FIP length error in descriptor type %x len %zu\n",
688 desc->fip_dtype, dlen);
693 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement.
694 * @fip: FCoE controller.
695 * @skb: Received FIP packet.
697 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
699 struct fcoe_fcf *fcf;
701 struct fcoe_fcf *found;
702 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
706 if (fcoe_ctlr_parse_adv(skb, &new))
709 spin_lock_bh(&fip->lock);
710 first = list_empty(&fip->fcfs);
712 list_for_each_entry(fcf, &fip->fcfs, list) {
713 if (fcf->switch_name == new.switch_name &&
714 fcf->fabric_name == new.fabric_name &&
715 fcf->fc_map == new.fc_map &&
716 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
722 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
725 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
730 memcpy(fcf, &new, sizeof(new));
731 list_add(&fcf->list, &fip->fcfs);
734 * Flags in advertisements are ignored once the FCF is
735 * selected. Flags in unsolicited advertisements are
736 * ignored after a usable solicited advertisement
739 if (fcf == fip->sel_fcf) {
740 fip->ctlr_ka_time -= fcf->fka_period;
741 fip->ctlr_ka_time += new.fka_period;
742 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
743 mod_timer(&fip->timer, fip->ctlr_ka_time);
744 } else if (!fcoe_ctlr_fcf_usable(fcf))
745 fcf->flags = new.flags;
746 fcf->fka_period = new.fka_period;
747 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
749 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
752 LIBFCOE_FIP_DBG("New FCF for fab %llx map %x val %d\n",
753 fcf->fabric_name, fcf->fc_map, mtu_valid);
757 * If this advertisement is not solicited and our max receive size
758 * hasn't been verified, send a solicited advertisement.
761 fcoe_ctlr_solicit(fip, fcf);
764 * If its been a while since we did a solicit, and this is
765 * the first advertisement we've received, do a multicast
766 * solicitation to gather as many advertisements as we can
767 * before selection occurs.
769 if (first && time_after(jiffies, fip->sol_time + sol_tov))
770 fcoe_ctlr_solicit(fip, NULL);
773 * If this is the first validated FCF, note the time and
774 * set a timer to trigger selection.
776 if (mtu_valid && !fip->sel_time && fcoe_ctlr_fcf_usable(fcf)) {
777 fip->sel_time = jiffies +
778 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
779 if (!timer_pending(&fip->timer) ||
780 time_before(fip->sel_time, fip->timer.expires))
781 mod_timer(&fip->timer, fip->sel_time);
784 spin_unlock_bh(&fip->lock);
788 * fcoe_ctlr_recv_els() - Handle an incoming FIP-encapsulated ELS frame.
789 * @fip: FCoE controller.
790 * @skb: Received FIP packet.
792 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
794 struct fc_lport *lp = fip->lp;
795 struct fip_header *fiph;
797 struct fc_frame_header *fh = NULL;
798 struct fip_desc *desc;
799 struct fip_encaps *els;
800 struct fcoe_dev_stats *stats;
801 enum fip_desc_type els_dtype = 0;
804 u8 granted_mac[ETH_ALEN] = { 0 };
809 fiph = (struct fip_header *)skb->data;
810 sub = fiph->fip_subcode;
811 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
814 rlen = ntohs(fiph->fip_dl_len) * 4;
815 if (rlen + sizeof(*fiph) > skb->len)
818 desc = (struct fip_desc *)(fiph + 1);
820 dlen = desc->fip_dlen * FIP_BPW;
821 if (dlen < sizeof(*desc) || dlen > rlen)
823 switch (desc->fip_dtype) {
825 if (dlen != sizeof(struct fip_mac_desc))
828 ((struct fip_mac_desc *)desc)->fd_mac,
830 if (!is_valid_ether_addr(granted_mac)) {
831 LIBFCOE_FIP_DBG("Invalid MAC address "
842 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
844 els_len = dlen - sizeof(*els);
845 els = (struct fip_encaps *)desc;
846 fh = (struct fc_frame_header *)(els + 1);
847 els_dtype = desc->fip_dtype;
850 LIBFCOE_FIP_DBG("unexpected descriptor type %x "
851 "in FIP adv\n", desc->fip_dtype);
852 /* standard says ignore unknown descriptors >= 128 */
853 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
857 desc = (struct fip_desc *)((char *)desc + dlen);
863 els_op = *(u8 *)(fh + 1);
865 if (els_dtype == FIP_DT_FLOGI && sub == FIP_SC_REP &&
866 fip->flogi_oxid == ntohs(fh->fh_ox_id) &&
867 els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac)) {
868 fip->flogi_oxid = FC_XID_UNKNOWN;
869 fip->update_mac(fip, fip->data_src_addr, granted_mac);
870 memcpy(fip->data_src_addr, granted_mac, ETH_ALEN);
874 * Convert skb into an fc_frame containing only the ELS.
876 skb_pull(skb, (u8 *)fh - skb->data);
877 skb_trim(skb, els_len);
878 fp = (struct fc_frame *)skb;
880 fr_sof(fp) = FC_SOF_I3;
881 fr_eof(fp) = FC_EOF_T;
884 stats = fc_lport_get_stats(lp);
886 stats->RxWords += skb->len / FIP_BPW;
888 fc_exch_recv(lp, fp);
892 LIBFCOE_FIP_DBG("FIP length error in descriptor type %x len %zu\n",
893 desc->fip_dtype, dlen);
899 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame.
900 * @fip: FCoE controller.
901 * @fh: Received FIP header.
903 * There may be multiple VN_Port descriptors.
904 * The overall length has already been checked.
906 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
907 struct fip_header *fh)
909 struct fip_desc *desc;
910 struct fip_mac_desc *mp;
911 struct fip_wwn_desc *wp;
912 struct fip_vn_desc *vp;
915 struct fcoe_fcf *fcf = fip->sel_fcf;
916 struct fc_lport *lp = fip->lp;
919 LIBFCOE_FIP_DBG("Clear Virtual Link received\n");
922 if (!fcf || !fc_host_port_id(lp->host))
926 * mask of required descriptors. Validating each one clears its bit.
928 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
930 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
931 desc = (struct fip_desc *)(fh + 1);
932 while (rlen >= sizeof(*desc)) {
933 dlen = desc->fip_dlen * FIP_BPW;
936 switch (desc->fip_dtype) {
938 mp = (struct fip_mac_desc *)desc;
939 if (dlen < sizeof(*mp))
941 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
943 desc_mask &= ~BIT(FIP_DT_MAC);
946 wp = (struct fip_wwn_desc *)desc;
947 if (dlen < sizeof(*wp))
949 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
951 desc_mask &= ~BIT(FIP_DT_NAME);
954 vp = (struct fip_vn_desc *)desc;
955 if (dlen < sizeof(*vp))
957 if (compare_ether_addr(vp->fd_mac,
958 fip->data_src_addr) == 0 &&
959 get_unaligned_be64(&vp->fd_wwpn) == lp->wwpn &&
960 ntoh24(vp->fd_fc_id) == fc_host_port_id(lp->host))
961 desc_mask &= ~BIT(FIP_DT_VN_ID);
964 /* standard says ignore unknown descriptors >= 128 */
965 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
969 desc = (struct fip_desc *)((char *)desc + dlen);
974 * reset only if all required descriptors were present and valid.
977 LIBFCOE_FIP_DBG("missing descriptors mask %x\n", desc_mask);
979 LIBFCOE_FIP_DBG("performing Clear Virtual Link\n");
980 fcoe_ctlr_reset(fip, FIP_ST_ENABLED);
985 * fcoe_ctlr_recv() - Receive a FIP frame.
986 * @fip: FCoE controller.
987 * @skb: Received FIP packet.
989 * This is called from NET_RX_SOFTIRQ.
991 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
993 spin_lock_bh(&fip->fip_recv_list.lock);
994 __skb_queue_tail(&fip->fip_recv_list, skb);
995 spin_unlock_bh(&fip->fip_recv_list.lock);
996 schedule_work(&fip->recv_work);
998 EXPORT_SYMBOL(fcoe_ctlr_recv);
1001 * fcoe_ctlr_recv_handler() - Receive a FIP frame.
1002 * @fip: FCoE controller.
1003 * @skb: Received FIP packet.
1005 * Returns non-zero if the frame is dropped.
1007 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1009 struct fip_header *fiph;
1011 enum fip_state state;
1015 if (skb_linearize(skb))
1017 if (skb->len < sizeof(*fiph))
1020 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1021 compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS))
1023 fiph = (struct fip_header *)skb->data;
1024 op = ntohs(fiph->fip_op);
1025 sub = fiph->fip_subcode;
1027 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1029 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1032 spin_lock_bh(&fip->lock);
1034 if (state == FIP_ST_AUTO) {
1036 fip->state = FIP_ST_ENABLED;
1037 state = FIP_ST_ENABLED;
1038 LIBFCOE_FIP_DBG("Using FIP mode\n");
1040 spin_unlock_bh(&fip->lock);
1041 if (state != FIP_ST_ENABLED)
1044 if (op == FIP_OP_LS) {
1045 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1048 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1049 fcoe_ctlr_recv_adv(fip, skb);
1050 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1051 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1060 * fcoe_ctlr_select() - Select the best FCF, if possible.
1061 * @fip: FCoE controller.
1063 * If there are conflicting advertisements, no FCF can be chosen.
1065 * Called with lock held.
1067 static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1069 struct fcoe_fcf *fcf;
1070 struct fcoe_fcf *best = NULL;
1072 list_for_each_entry(fcf, &fip->fcfs, list) {
1073 LIBFCOE_FIP_DBG("consider FCF for fab %llx VFID %d map %x "
1074 "val %d\n", fcf->fabric_name, fcf->vfid,
1075 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
1076 if (!fcoe_ctlr_fcf_usable(fcf)) {
1077 LIBFCOE_FIP_DBG("FCF for fab %llx map %x %svalid "
1078 "%savailable\n", fcf->fabric_name,
1079 fcf->fc_map, (fcf->flags & FIP_FL_SOL)
1080 ? "" : "in", (fcf->flags & FIP_FL_AVAIL)
1088 if (fcf->fabric_name != best->fabric_name ||
1089 fcf->vfid != best->vfid ||
1090 fcf->fc_map != best->fc_map) {
1091 LIBFCOE_FIP_DBG("Conflicting fabric, VFID, "
1095 if (fcf->pri < best->pri)
1098 fip->sel_fcf = best;
1102 * fcoe_ctlr_timeout() - FIP timer function.
1103 * @arg: &fcoe_ctlr pointer.
1105 * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
1107 static void fcoe_ctlr_timeout(unsigned long arg)
1109 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1110 struct fcoe_fcf *sel;
1111 struct fcoe_fcf *fcf;
1112 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
1116 spin_lock_bh(&fip->lock);
1117 if (fip->state == FIP_ST_DISABLED) {
1118 spin_unlock_bh(&fip->lock);
1123 fcoe_ctlr_age_fcfs(fip);
1126 if (!sel && fip->sel_time && time_after_eq(jiffies, fip->sel_time)) {
1127 fcoe_ctlr_select(fip);
1133 fcf = sel; /* the old FCF may have been freed */
1135 printk(KERN_INFO "libfcoe: host%d: FIP selected "
1136 "Fibre-Channel Forwarder MAC %pM\n",
1137 fip->lp->host->host_no, sel->fcf_mac);
1138 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1139 fip->port_ka_time = jiffies +
1140 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1141 fip->ctlr_ka_time = jiffies + sel->fka_period;
1144 printk(KERN_NOTICE "libfcoe: host%d: "
1145 "FIP Fibre-Channel Forwarder timed out. "
1146 "Starting FCF discovery.\n",
1147 fip->lp->host->host_no);
1150 schedule_work(&fip->link_work);
1156 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1157 fip->ctlr_ka_time = jiffies + sel->fka_period;
1160 if (time_after(next_timer, fip->ctlr_ka_time))
1161 next_timer = fip->ctlr_ka_time;
1163 if (time_after_eq(jiffies, fip->port_ka_time)) {
1164 fip->port_ka_time += jiffies +
1165 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1168 if (time_after(next_timer, fip->port_ka_time))
1169 next_timer = fip->port_ka_time;
1170 mod_timer(&fip->timer, next_timer);
1171 } else if (fip->sel_time) {
1172 next_timer = fip->sel_time +
1173 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1174 mod_timer(&fip->timer, next_timer);
1176 spin_unlock_bh(&fip->lock);
1179 fcoe_ctlr_send_keep_alive(fip, 0, fip->ctl_src_addr);
1181 fcoe_ctlr_send_keep_alive(fip, 1, fip->data_src_addr);
1185 * fcoe_ctlr_link_work() - worker thread function for link changes.
1186 * @work: pointer to link_work member inside &fcoe_ctlr.
1188 * See if the link status has changed and if so, report it.
1190 * This is here because fc_linkup() and fc_linkdown() must not
1191 * be called from the timer directly, since they use a mutex.
1193 static void fcoe_ctlr_link_work(struct work_struct *work)
1195 struct fcoe_ctlr *fip;
1199 fip = container_of(work, struct fcoe_ctlr, link_work);
1200 spin_lock_bh(&fip->lock);
1201 last_link = fip->last_link;
1203 fip->last_link = link;
1204 spin_unlock_bh(&fip->lock);
1206 if (last_link != link) {
1210 fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
1215 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames.
1216 * @recv_work: pointer to recv_work member inside &fcoe_ctlr.
1218 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1220 struct fcoe_ctlr *fip;
1221 struct sk_buff *skb;
1223 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1224 spin_lock_bh(&fip->fip_recv_list.lock);
1225 while ((skb = __skb_dequeue(&fip->fip_recv_list))) {
1226 spin_unlock_bh(&fip->fip_recv_list.lock);
1227 fcoe_ctlr_recv_handler(fip, skb);
1228 spin_lock_bh(&fip->fip_recv_list.lock);
1230 spin_unlock_bh(&fip->fip_recv_list.lock);
1234 * fcoe_ctlr_recv_flogi() - snoop Pre-FIP receipt of FLOGI response or request.
1235 * @fip: FCoE controller.
1237 * @sa: Ethernet source MAC address from received FCoE frame.
1239 * Snoop potential response to FLOGI or even incoming FLOGI.
1241 * The caller has checked that we are waiting for login as indicated
1242 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1244 * The caller is responsible for freeing the frame.
1246 * Return non-zero if the frame should not be delivered to libfc.
1248 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_frame *fp, u8 *sa)
1250 struct fc_frame_header *fh;
1254 fh = fc_frame_header_get(fp);
1255 if (fh->fh_type != FC_TYPE_ELS)
1258 op = fc_frame_payload_op(fp);
1259 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1260 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1262 spin_lock_bh(&fip->lock);
1263 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1264 spin_unlock_bh(&fip->lock);
1267 fip->state = FIP_ST_NON_FIP;
1268 LIBFCOE_FIP_DBG("received FLOGI LS_ACC using non-FIP mode\n");
1272 * If the src mac addr is FC_OUI-based, then we mark the
1273 * address_mode flag to use FC_OUI-based Ethernet DA.
1274 * Otherwise we use the FCoE gateway addr
1276 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1279 memcpy(fip->dest_addr, sa, ETH_ALEN);
1282 fip->flogi_oxid = FC_XID_UNKNOWN;
1283 memcpy(mac, fip->data_src_addr, ETH_ALEN);
1284 fc_fcoe_set_mac(fip->data_src_addr, fh->fh_d_id);
1285 spin_unlock_bh(&fip->lock);
1287 fip->update_mac(fip, mac, fip->data_src_addr);
1288 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1290 * Save source MAC for point-to-point responses.
1292 spin_lock_bh(&fip->lock);
1293 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1294 memcpy(fip->dest_addr, sa, ETH_ALEN);
1296 if (fip->state == FIP_ST_NON_FIP)
1297 LIBFCOE_FIP_DBG("received FLOGI REQ, "
1298 "using non-FIP mode\n");
1299 fip->state = FIP_ST_NON_FIP;
1301 spin_unlock_bh(&fip->lock);
1305 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1308 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
1310 * @scheme: check port
1311 * @port: port indicator for converting
1313 * Returns: u64 fc world wide name
1315 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1316 unsigned int scheme, unsigned int port)
1321 /* The MAC is in NO, so flip only the low 48 bits */
1322 host_mac = ((u64) mac[0] << 40) |
1323 ((u64) mac[1] << 32) |
1324 ((u64) mac[2] << 24) |
1325 ((u64) mac[3] << 16) |
1326 ((u64) mac[4] << 8) |
1329 WARN_ON(host_mac >= (1ULL << 48));
1330 wwn = host_mac | ((u64) scheme << 60);
1336 WARN_ON(port >= 0xfff);
1337 wwn |= (u64) port << 48;
1346 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1349 * fcoe_libfc_config() - sets up libfc related properties for lport
1350 * @lp: ptr to the fc_lport
1351 * @tt: libfc function template
1353 * Returns : 0 for success
1355 int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1357 /* Set the function pointers set by the LLDD */
1358 memcpy(&lp->tt, tt, sizeof(*tt));
1359 if (fc_fcp_init(lp))
1369 EXPORT_SYMBOL_GPL(fcoe_libfc_config);