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 cancel_work_sync(&fip
->recv_work
);
152 spin_lock_bh(&fip
->fip_recv_list
.lock
);
153 __skb_queue_purge(&fip
->fip_recv_list
);
154 spin_unlock_bh(&fip
->fip_recv_list
.lock
);
156 spin_lock_bh(&fip
->lock
);
157 fip
->state
= FIP_ST_DISABLED
;
158 fcoe_ctlr_reset_fcfs(fip
);
159 spin_unlock_bh(&fip
->lock
);
160 del_timer_sync(&fip
->timer
);
161 cancel_work_sync(&fip
->link_work
);
163 EXPORT_SYMBOL(fcoe_ctlr_destroy
);
166 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port.
167 * @fip: FCoE controller.
169 * Returns the maximum packet size including the FCoE header and trailer,
170 * but not including any Ethernet or VLAN headers.
172 static inline u32
fcoe_ctlr_fcoe_size(struct fcoe_ctlr
*fip
)
175 * Determine the max FCoE frame size allowed, including
176 * FCoE header and trailer.
177 * Note: lp->mfs is currently the payload size, not the frame size.
179 return fip
->lp
->mfs
+ sizeof(struct fc_frame_header
) +
180 sizeof(struct fcoe_hdr
) + sizeof(struct fcoe_crc_eof
);
184 * fcoe_ctlr_solicit() - Send a solicitation.
185 * @fip: FCoE controller.
186 * @fcf: Destination FCF. If NULL, a multicast solicitation is sent.
188 static void fcoe_ctlr_solicit(struct fcoe_ctlr
*fip
, struct fcoe_fcf
*fcf
)
193 struct fip_header fip
;
195 struct fip_mac_desc mac
;
196 struct fip_wwn_desc wwnn
;
197 struct fip_size_desc size
;
198 } __attribute__((packed
)) desc
;
199 } __attribute__((packed
)) *sol
;
202 skb
= dev_alloc_skb(sizeof(*sol
));
206 sol
= (struct fip_sol
*)skb
->data
;
208 memset(sol
, 0, sizeof(*sol
));
209 memcpy(sol
->eth
.h_dest
, fcf
? fcf
->fcf_mac
: fcoe_all_fcfs
, ETH_ALEN
);
210 memcpy(sol
->eth
.h_source
, fip
->ctl_src_addr
, ETH_ALEN
);
211 sol
->eth
.h_proto
= htons(ETH_P_FIP
);
213 sol
->fip
.fip_ver
= FIP_VER_ENCAPS(FIP_VER
);
214 sol
->fip
.fip_op
= htons(FIP_OP_DISC
);
215 sol
->fip
.fip_subcode
= FIP_SC_SOL
;
216 sol
->fip
.fip_dl_len
= htons(sizeof(sol
->desc
) / FIP_BPW
);
217 sol
->fip
.fip_flags
= htons(FIP_FL_FPMA
);
219 sol
->fip
.fip_flags
|= htons(FIP_FL_SPMA
);
221 sol
->desc
.mac
.fd_desc
.fip_dtype
= FIP_DT_MAC
;
222 sol
->desc
.mac
.fd_desc
.fip_dlen
= sizeof(sol
->desc
.mac
) / FIP_BPW
;
223 memcpy(sol
->desc
.mac
.fd_mac
, fip
->ctl_src_addr
, ETH_ALEN
);
225 sol
->desc
.wwnn
.fd_desc
.fip_dtype
= FIP_DT_NAME
;
226 sol
->desc
.wwnn
.fd_desc
.fip_dlen
= sizeof(sol
->desc
.wwnn
) / FIP_BPW
;
227 put_unaligned_be64(fip
->lp
->wwnn
, &sol
->desc
.wwnn
.fd_wwn
);
229 fcoe_size
= fcoe_ctlr_fcoe_size(fip
);
230 sol
->desc
.size
.fd_desc
.fip_dtype
= FIP_DT_FCOE_SIZE
;
231 sol
->desc
.size
.fd_desc
.fip_dlen
= sizeof(sol
->desc
.size
) / FIP_BPW
;
232 sol
->desc
.size
.fd_size
= htons(fcoe_size
);
234 skb_put(skb
, sizeof(*sol
));
235 skb
->protocol
= htons(ETH_P_FIP
);
236 skb_reset_mac_header(skb
);
237 skb_reset_network_header(skb
);
241 fip
->sol_time
= jiffies
;
245 * fcoe_ctlr_link_up() - Start FCoE controller.
246 * @fip: FCoE controller.
248 * Called from the LLD when the network link is ready.
250 void fcoe_ctlr_link_up(struct fcoe_ctlr
*fip
)
252 spin_lock_bh(&fip
->lock
);
253 if (fip
->state
== FIP_ST_NON_FIP
|| fip
->state
== FIP_ST_AUTO
) {
256 spin_unlock_bh(&fip
->lock
);
258 } else if (fip
->state
== FIP_ST_LINK_WAIT
) {
259 fip
->state
= FIP_ST_AUTO
;
262 spin_unlock_bh(&fip
->lock
);
263 LIBFCOE_FIP_DBG("%s", "setting AUTO mode.\n");
265 fcoe_ctlr_solicit(fip
, NULL
);
267 spin_unlock_bh(&fip
->lock
);
269 EXPORT_SYMBOL(fcoe_ctlr_link_up
);
272 * fcoe_ctlr_reset() - Reset FIP.
273 * @fip: FCoE controller.
274 * @new_state: FIP state to be entered.
276 * Returns non-zero if the link was up and now isn't.
278 static int fcoe_ctlr_reset(struct fcoe_ctlr
*fip
, enum fip_state new_state
)
280 struct fc_lport
*lp
= fip
->lp
;
283 spin_lock_bh(&fip
->lock
);
284 fcoe_ctlr_reset_fcfs(fip
);
285 del_timer(&fip
->timer
);
286 fip
->state
= new_state
;
287 fip
->ctlr_ka_time
= 0;
288 fip
->port_ka_time
= 0;
290 fip
->flogi_oxid
= FC_XID_UNKNOWN
;
293 link_dropped
= fip
->link
;
295 spin_unlock_bh(&fip
->lock
);
300 if (new_state
== FIP_ST_ENABLED
) {
301 fcoe_ctlr_solicit(fip
, NULL
);
309 * fcoe_ctlr_link_down() - Stop FCoE controller.
310 * @fip: FCoE controller.
312 * Returns non-zero if the link was up and now isn't.
314 * Called from the LLD when the network link is not ready.
315 * There may be multiple calls while the link is down.
317 int fcoe_ctlr_link_down(struct fcoe_ctlr
*fip
)
319 return fcoe_ctlr_reset(fip
, FIP_ST_LINK_WAIT
);
321 EXPORT_SYMBOL(fcoe_ctlr_link_down
);
324 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF.
325 * @fip: FCoE controller.
326 * @ports: 0 for controller keep-alive, 1 for port keep-alive.
327 * @sa: source MAC address.
329 * A controller keep-alive is sent every fka_period (typically 8 seconds).
330 * The source MAC is the native MAC address.
332 * A port keep-alive is sent every 90 seconds while logged in.
333 * The source MAC is the assigned mapped source address.
334 * The destination is the FCF's F-port.
336 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr
*fip
, int ports
, u8
*sa
)
341 struct fip_header fip
;
342 struct fip_mac_desc mac
;
343 } __attribute__((packed
)) *kal
;
344 struct fip_vn_desc
*vn
;
347 struct fcoe_fcf
*fcf
;
351 if (!fcf
|| !fc_host_port_id(lp
->host
))
354 len
= fcoe_ctlr_fcoe_size(fip
) + sizeof(struct ethhdr
);
355 BUG_ON(len
< sizeof(*kal
) + sizeof(*vn
));
356 skb
= dev_alloc_skb(len
);
360 kal
= (struct fip_kal
*)skb
->data
;
362 memcpy(kal
->eth
.h_dest
, fcf
->fcf_mac
, ETH_ALEN
);
363 memcpy(kal
->eth
.h_source
, sa
, ETH_ALEN
);
364 kal
->eth
.h_proto
= htons(ETH_P_FIP
);
366 kal
->fip
.fip_ver
= FIP_VER_ENCAPS(FIP_VER
);
367 kal
->fip
.fip_op
= htons(FIP_OP_CTRL
);
368 kal
->fip
.fip_subcode
= FIP_SC_KEEP_ALIVE
;
369 kal
->fip
.fip_dl_len
= htons((sizeof(kal
->mac
) +
370 ports
* sizeof(*vn
)) / FIP_BPW
);
371 kal
->fip
.fip_flags
= htons(FIP_FL_FPMA
);
373 kal
->fip
.fip_flags
|= htons(FIP_FL_SPMA
);
375 kal
->mac
.fd_desc
.fip_dtype
= FIP_DT_MAC
;
376 kal
->mac
.fd_desc
.fip_dlen
= sizeof(kal
->mac
) / FIP_BPW
;
377 memcpy(kal
->mac
.fd_mac
, fip
->ctl_src_addr
, ETH_ALEN
);
380 vn
= (struct fip_vn_desc
*)(kal
+ 1);
381 vn
->fd_desc
.fip_dtype
= FIP_DT_VN_ID
;
382 vn
->fd_desc
.fip_dlen
= sizeof(*vn
) / FIP_BPW
;
383 memcpy(vn
->fd_mac
, fip
->data_src_addr
, ETH_ALEN
);
384 hton24(vn
->fd_fc_id
, fc_host_port_id(lp
->host
));
385 put_unaligned_be64(lp
->wwpn
, &vn
->fd_wwpn
);
389 skb
->protocol
= htons(ETH_P_FIP
);
390 skb_reset_mac_header(skb
);
391 skb_reset_network_header(skb
);
396 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it.
397 * @fip: FCoE controller.
398 * @dtype: FIP descriptor type for the frame.
399 * @skb: FCoE ELS frame including FC header but no FCoE headers.
401 * Returns non-zero error code on failure.
403 * The caller must check that the length is a multiple of 4.
405 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
406 * Headroom includes the FIP encapsulation description, FIP header, and
407 * Ethernet header. The tailroom is for the FIP MAC descriptor.
409 static int fcoe_ctlr_encaps(struct fcoe_ctlr
*fip
,
410 u8 dtype
, struct sk_buff
*skb
)
412 struct fip_encaps_head
{
414 struct fip_header fip
;
415 struct fip_encaps encaps
;
416 } __attribute__((packed
)) *cap
;
417 struct fip_mac_desc
*mac
;
418 struct fcoe_fcf
*fcf
;
426 /* set flags according to both FCF and lport's capability on SPMA */
427 fip_flags
= fcf
->flags
;
428 fip_flags
&= fip
->spma
? FIP_FL_SPMA
| FIP_FL_FPMA
: FIP_FL_FPMA
;
432 dlen
= sizeof(struct fip_encaps
) + skb
->len
; /* len before push */
433 cap
= (struct fip_encaps_head
*)skb_push(skb
, sizeof(*cap
));
435 memset(cap
, 0, sizeof(*cap
));
436 memcpy(cap
->eth
.h_dest
, fcf
->fcf_mac
, ETH_ALEN
);
437 memcpy(cap
->eth
.h_source
, fip
->ctl_src_addr
, ETH_ALEN
);
438 cap
->eth
.h_proto
= htons(ETH_P_FIP
);
440 cap
->fip
.fip_ver
= FIP_VER_ENCAPS(FIP_VER
);
441 cap
->fip
.fip_op
= htons(FIP_OP_LS
);
442 cap
->fip
.fip_subcode
= FIP_SC_REQ
;
443 cap
->fip
.fip_dl_len
= htons((dlen
+ sizeof(*mac
)) / FIP_BPW
);
444 cap
->fip
.fip_flags
= htons(fip_flags
);
446 cap
->encaps
.fd_desc
.fip_dtype
= dtype
;
447 cap
->encaps
.fd_desc
.fip_dlen
= dlen
/ FIP_BPW
;
449 mac
= (struct fip_mac_desc
*)skb_put(skb
, sizeof(*mac
));
450 memset(mac
, 0, sizeof(mac
));
451 mac
->fd_desc
.fip_dtype
= FIP_DT_MAC
;
452 mac
->fd_desc
.fip_dlen
= sizeof(*mac
) / FIP_BPW
;
453 if (dtype
!= FIP_DT_FLOGI
)
454 memcpy(mac
->fd_mac
, fip
->data_src_addr
, ETH_ALEN
);
456 memcpy(mac
->fd_mac
, fip
->ctl_src_addr
, ETH_ALEN
);
458 skb
->protocol
= htons(ETH_P_FIP
);
459 skb_reset_mac_header(skb
);
460 skb_reset_network_header(skb
);
465 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
466 * @fip: FCoE controller.
467 * @skb: FCoE ELS frame including FC header but no FCoE headers.
469 * Returns a non-zero error code if the frame should not be sent.
470 * Returns zero if the caller should send the frame with FCoE encapsulation.
472 * The caller must check that the length is a multiple of 4.
473 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
475 int fcoe_ctlr_els_send(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
477 struct fc_frame_header
*fh
;
481 fh
= (struct fc_frame_header
*)skb
->data
;
482 op
= *(u8
*)(fh
+ 1);
484 if (op
== ELS_FLOGI
) {
485 old_xid
= fip
->flogi_oxid
;
486 fip
->flogi_oxid
= ntohs(fh
->fh_ox_id
);
487 if (fip
->state
== FIP_ST_AUTO
) {
488 if (old_xid
== FC_XID_UNKNOWN
)
489 fip
->flogi_count
= 0;
491 if (fip
->flogi_count
< 3)
496 if (fip
->state
== FIP_ST_NON_FIP
)
500 if (fip
->state
== FIP_ST_NON_FIP
)
508 if (ntoh24(fh
->fh_s_id
))
513 if (fip
->state
!= FIP_ST_ENABLED
)
515 if (ntoh24(fh
->fh_d_id
) != FC_FID_FLOGI
)
520 if (fip
->flogi_oxid
== FC_XID_UNKNOWN
)
522 if (!ntoh24(fh
->fh_s_id
))
524 if (fip
->state
== FIP_ST_AUTO
)
527 * Here we must've gotten an SID by accepting an FLOGI
528 * from a point-to-point connection. Switch to using
529 * the source mac based on the SID. The destination
530 * MAC in this case would have been set by receving the
533 fip
->flogi_oxid
= FC_XID_UNKNOWN
;
534 fc_fcoe_set_mac(fip
->data_src_addr
, fh
->fh_s_id
);
537 if (fip
->state
!= FIP_ST_ENABLED
)
541 if (fcoe_ctlr_encaps(fip
, op
, skb
))
549 EXPORT_SYMBOL(fcoe_ctlr_els_send
);
552 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller.
553 * @fip: FCoE controller.
555 * Called with lock held.
557 * An FCF is considered old if we have missed three advertisements.
558 * That is, there have been no valid advertisement from it for three
559 * times its keep-alive period including fuzz.
561 * In addition, determine the time when an FCF selection can occur.
563 static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr
*fip
)
565 struct fcoe_fcf
*fcf
;
566 struct fcoe_fcf
*next
;
567 unsigned long sel_time
= 0;
569 list_for_each_entry_safe(fcf
, next
, &fip
->fcfs
, list
) {
570 if (time_after(jiffies
, fcf
->time
+ fcf
->fka_period
* 3 +
571 msecs_to_jiffies(FIP_FCF_FUZZ
* 3))) {
572 if (fip
->sel_fcf
== fcf
)
574 list_del(&fcf
->list
);
575 WARN_ON(!fip
->fcf_count
);
578 } else if (fcoe_ctlr_mtu_valid(fcf
) &&
579 (!sel_time
|| time_before(sel_time
, fcf
->time
))) {
580 sel_time
= fcf
->time
;
584 sel_time
+= msecs_to_jiffies(FCOE_CTLR_START_DELAY
);
585 fip
->sel_time
= sel_time
;
586 if (time_before(sel_time
, fip
->timer
.expires
))
587 mod_timer(&fip
->timer
, sel_time
);
594 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry.
595 * @skb: received FIP advertisement frame
596 * @fcf: resulting FCF entry.
598 * Returns zero on a valid parsed advertisement,
599 * otherwise returns non zero value.
601 static int fcoe_ctlr_parse_adv(struct sk_buff
*skb
, struct fcoe_fcf
*fcf
)
603 struct fip_header
*fiph
;
604 struct fip_desc
*desc
= NULL
;
605 struct fip_wwn_desc
*wwn
;
606 struct fip_fab_desc
*fab
;
607 struct fip_fka_desc
*fka
;
612 memset(fcf
, 0, sizeof(*fcf
));
613 fcf
->fka_period
= msecs_to_jiffies(FCOE_CTLR_DEF_FKA
);
615 fiph
= (struct fip_header
*)skb
->data
;
616 fcf
->flags
= ntohs(fiph
->fip_flags
);
618 rlen
= ntohs(fiph
->fip_dl_len
) * 4;
619 if (rlen
+ sizeof(*fiph
) > skb
->len
)
622 desc
= (struct fip_desc
*)(fiph
+ 1);
624 dlen
= desc
->fip_dlen
* FIP_BPW
;
625 if (dlen
< sizeof(*desc
) || dlen
> rlen
)
627 switch (desc
->fip_dtype
) {
629 if (dlen
!= sizeof(struct fip_pri_desc
))
631 fcf
->pri
= ((struct fip_pri_desc
*)desc
)->fd_pri
;
634 if (dlen
!= sizeof(struct fip_mac_desc
))
637 ((struct fip_mac_desc
*)desc
)->fd_mac
,
639 if (!is_valid_ether_addr(fcf
->fcf_mac
)) {
640 LIBFCOE_FIP_DBG("Invalid MAC address "
646 if (dlen
!= sizeof(struct fip_wwn_desc
))
648 wwn
= (struct fip_wwn_desc
*)desc
;
649 fcf
->switch_name
= get_unaligned_be64(&wwn
->fd_wwn
);
652 if (dlen
!= sizeof(struct fip_fab_desc
))
654 fab
= (struct fip_fab_desc
*)desc
;
655 fcf
->fabric_name
= get_unaligned_be64(&fab
->fd_wwn
);
656 fcf
->vfid
= ntohs(fab
->fd_vfid
);
657 fcf
->fc_map
= ntoh24(fab
->fd_map
);
660 if (dlen
!= sizeof(struct fip_fka_desc
))
662 fka
= (struct fip_fka_desc
*)desc
;
663 t
= ntohl(fka
->fd_fka_period
);
664 if (t
>= FCOE_CTLR_MIN_FKA
)
665 fcf
->fka_period
= msecs_to_jiffies(t
);
668 case FIP_DT_FCOE_SIZE
:
674 LIBFCOE_FIP_DBG("unexpected descriptor type %x "
675 "in FIP adv\n", desc
->fip_dtype
);
676 /* standard says ignore unknown descriptors >= 128 */
677 if (desc
->fip_dtype
< FIP_DT_VENDOR_BASE
)
681 desc
= (struct fip_desc
*)((char *)desc
+ dlen
);
684 if (!fcf
->fc_map
|| (fcf
->fc_map
& 0x10000))
686 if (!fcf
->switch_name
|| !fcf
->fabric_name
)
691 LIBFCOE_FIP_DBG("FIP length error in descriptor type %x len %zu\n",
692 desc
->fip_dtype
, dlen
);
697 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement.
698 * @fip: FCoE controller.
699 * @skb: Received FIP packet.
701 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
703 struct fcoe_fcf
*fcf
;
705 struct fcoe_fcf
*found
;
706 unsigned long sol_tov
= msecs_to_jiffies(FCOE_CTRL_SOL_TOV
);
710 if (fcoe_ctlr_parse_adv(skb
, &new))
713 spin_lock_bh(&fip
->lock
);
714 first
= list_empty(&fip
->fcfs
);
716 list_for_each_entry(fcf
, &fip
->fcfs
, list
) {
717 if (fcf
->switch_name
== new.switch_name
&&
718 fcf
->fabric_name
== new.fabric_name
&&
719 fcf
->fc_map
== new.fc_map
&&
720 compare_ether_addr(fcf
->fcf_mac
, new.fcf_mac
) == 0) {
726 if (fip
->fcf_count
>= FCOE_CTLR_FCF_LIMIT
)
729 fcf
= kmalloc(sizeof(*fcf
), GFP_ATOMIC
);
734 memcpy(fcf
, &new, sizeof(new));
735 list_add(&fcf
->list
, &fip
->fcfs
);
738 * Flags in advertisements are ignored once the FCF is
739 * selected. Flags in unsolicited advertisements are
740 * ignored after a usable solicited advertisement
743 if (fcf
== fip
->sel_fcf
) {
744 fip
->ctlr_ka_time
-= fcf
->fka_period
;
745 fip
->ctlr_ka_time
+= new.fka_period
;
746 if (time_before(fip
->ctlr_ka_time
, fip
->timer
.expires
))
747 mod_timer(&fip
->timer
, fip
->ctlr_ka_time
);
748 } else if (!fcoe_ctlr_fcf_usable(fcf
))
749 fcf
->flags
= new.flags
;
750 fcf
->fka_period
= new.fka_period
;
751 memcpy(fcf
->fcf_mac
, new.fcf_mac
, ETH_ALEN
);
753 mtu_valid
= fcoe_ctlr_mtu_valid(fcf
);
756 LIBFCOE_FIP_DBG("New FCF for fab %llx map %x val %d\n",
757 fcf
->fabric_name
, fcf
->fc_map
, mtu_valid
);
761 * If this advertisement is not solicited and our max receive size
762 * hasn't been verified, send a solicited advertisement.
765 fcoe_ctlr_solicit(fip
, fcf
);
768 * If its been a while since we did a solicit, and this is
769 * the first advertisement we've received, do a multicast
770 * solicitation to gather as many advertisements as we can
771 * before selection occurs.
773 if (first
&& time_after(jiffies
, fip
->sol_time
+ sol_tov
))
774 fcoe_ctlr_solicit(fip
, NULL
);
777 * If this is the first validated FCF, note the time and
778 * set a timer to trigger selection.
780 if (mtu_valid
&& !fip
->sel_time
&& fcoe_ctlr_fcf_usable(fcf
)) {
781 fip
->sel_time
= jiffies
+
782 msecs_to_jiffies(FCOE_CTLR_START_DELAY
);
783 if (!timer_pending(&fip
->timer
) ||
784 time_before(fip
->sel_time
, fip
->timer
.expires
))
785 mod_timer(&fip
->timer
, fip
->sel_time
);
788 spin_unlock_bh(&fip
->lock
);
792 * fcoe_ctlr_recv_els() - Handle an incoming FIP-encapsulated ELS frame.
793 * @fip: FCoE controller.
794 * @skb: Received FIP packet.
796 static void fcoe_ctlr_recv_els(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
798 struct fc_lport
*lp
= fip
->lp
;
799 struct fip_header
*fiph
;
801 struct fc_frame_header
*fh
= NULL
;
802 struct fip_desc
*desc
;
803 struct fip_encaps
*els
;
804 struct fcoe_dev_stats
*stats
;
805 enum fip_desc_type els_dtype
= 0;
808 u8 granted_mac
[ETH_ALEN
] = { 0 };
813 fiph
= (struct fip_header
*)skb
->data
;
814 sub
= fiph
->fip_subcode
;
815 if (sub
!= FIP_SC_REQ
&& sub
!= FIP_SC_REP
)
818 rlen
= ntohs(fiph
->fip_dl_len
) * 4;
819 if (rlen
+ sizeof(*fiph
) > skb
->len
)
822 desc
= (struct fip_desc
*)(fiph
+ 1);
824 dlen
= desc
->fip_dlen
* FIP_BPW
;
825 if (dlen
< sizeof(*desc
) || dlen
> rlen
)
827 switch (desc
->fip_dtype
) {
829 if (dlen
!= sizeof(struct fip_mac_desc
))
832 ((struct fip_mac_desc
*)desc
)->fd_mac
,
834 if (!is_valid_ether_addr(granted_mac
)) {
835 LIBFCOE_FIP_DBG("Invalid MAC address "
846 if (dlen
< sizeof(*els
) + sizeof(*fh
) + 1)
848 els_len
= dlen
- sizeof(*els
);
849 els
= (struct fip_encaps
*)desc
;
850 fh
= (struct fc_frame_header
*)(els
+ 1);
851 els_dtype
= desc
->fip_dtype
;
854 LIBFCOE_FIP_DBG("unexpected descriptor type %x "
855 "in FIP adv\n", desc
->fip_dtype
);
856 /* standard says ignore unknown descriptors >= 128 */
857 if (desc
->fip_dtype
< FIP_DT_VENDOR_BASE
)
861 desc
= (struct fip_desc
*)((char *)desc
+ dlen
);
867 els_op
= *(u8
*)(fh
+ 1);
869 if (els_dtype
== FIP_DT_FLOGI
&& sub
== FIP_SC_REP
&&
870 fip
->flogi_oxid
== ntohs(fh
->fh_ox_id
) &&
871 els_op
== ELS_LS_ACC
&& is_valid_ether_addr(granted_mac
)) {
872 fip
->flogi_oxid
= FC_XID_UNKNOWN
;
873 fip
->update_mac(fip
, fip
->data_src_addr
, granted_mac
);
874 memcpy(fip
->data_src_addr
, granted_mac
, ETH_ALEN
);
878 * Convert skb into an fc_frame containing only the ELS.
880 skb_pull(skb
, (u8
*)fh
- skb
->data
);
881 skb_trim(skb
, els_len
);
882 fp
= (struct fc_frame
*)skb
;
884 fr_sof(fp
) = FC_SOF_I3
;
885 fr_eof(fp
) = FC_EOF_T
;
888 stats
= fc_lport_get_stats(lp
);
890 stats
->RxWords
+= skb
->len
/ FIP_BPW
;
892 fc_exch_recv(lp
, fp
);
896 LIBFCOE_FIP_DBG("FIP length error in descriptor type %x len %zu\n",
897 desc
->fip_dtype
, dlen
);
903 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame.
904 * @fip: FCoE controller.
905 * @fh: Received FIP header.
907 * There may be multiple VN_Port descriptors.
908 * The overall length has already been checked.
910 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr
*fip
,
911 struct fip_header
*fh
)
913 struct fip_desc
*desc
;
914 struct fip_mac_desc
*mp
;
915 struct fip_wwn_desc
*wp
;
916 struct fip_vn_desc
*vp
;
919 struct fcoe_fcf
*fcf
= fip
->sel_fcf
;
920 struct fc_lport
*lp
= fip
->lp
;
923 LIBFCOE_FIP_DBG("Clear Virtual Link received\n");
926 if (!fcf
|| !fc_host_port_id(lp
->host
))
930 * mask of required descriptors. Validating each one clears its bit.
932 desc_mask
= BIT(FIP_DT_MAC
) | BIT(FIP_DT_NAME
) | BIT(FIP_DT_VN_ID
);
934 rlen
= ntohs(fh
->fip_dl_len
) * FIP_BPW
;
935 desc
= (struct fip_desc
*)(fh
+ 1);
936 while (rlen
>= sizeof(*desc
)) {
937 dlen
= desc
->fip_dlen
* FIP_BPW
;
940 switch (desc
->fip_dtype
) {
942 mp
= (struct fip_mac_desc
*)desc
;
943 if (dlen
< sizeof(*mp
))
945 if (compare_ether_addr(mp
->fd_mac
, fcf
->fcf_mac
))
947 desc_mask
&= ~BIT(FIP_DT_MAC
);
950 wp
= (struct fip_wwn_desc
*)desc
;
951 if (dlen
< sizeof(*wp
))
953 if (get_unaligned_be64(&wp
->fd_wwn
) != fcf
->switch_name
)
955 desc_mask
&= ~BIT(FIP_DT_NAME
);
958 vp
= (struct fip_vn_desc
*)desc
;
959 if (dlen
< sizeof(*vp
))
961 if (compare_ether_addr(vp
->fd_mac
,
962 fip
->data_src_addr
) == 0 &&
963 get_unaligned_be64(&vp
->fd_wwpn
) == lp
->wwpn
&&
964 ntoh24(vp
->fd_fc_id
) == fc_host_port_id(lp
->host
))
965 desc_mask
&= ~BIT(FIP_DT_VN_ID
);
968 /* standard says ignore unknown descriptors >= 128 */
969 if (desc
->fip_dtype
< FIP_DT_VENDOR_BASE
)
973 desc
= (struct fip_desc
*)((char *)desc
+ dlen
);
978 * reset only if all required descriptors were present and valid.
981 LIBFCOE_FIP_DBG("missing descriptors mask %x\n", desc_mask
);
983 LIBFCOE_FIP_DBG("performing Clear Virtual Link\n");
984 fcoe_ctlr_reset(fip
, FIP_ST_ENABLED
);
989 * fcoe_ctlr_recv() - Receive a FIP frame.
990 * @fip: FCoE controller.
991 * @skb: Received FIP packet.
993 * This is called from NET_RX_SOFTIRQ.
995 void fcoe_ctlr_recv(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
997 spin_lock_bh(&fip
->fip_recv_list
.lock
);
998 __skb_queue_tail(&fip
->fip_recv_list
, skb
);
999 spin_unlock_bh(&fip
->fip_recv_list
.lock
);
1000 schedule_work(&fip
->recv_work
);
1002 EXPORT_SYMBOL(fcoe_ctlr_recv
);
1005 * fcoe_ctlr_recv_handler() - Receive a FIP frame.
1006 * @fip: FCoE controller.
1007 * @skb: Received FIP packet.
1009 * Returns non-zero if the frame is dropped.
1011 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
1013 struct fip_header
*fiph
;
1015 enum fip_state state
;
1019 if (skb_linearize(skb
))
1021 if (skb
->len
< sizeof(*fiph
))
1024 if (compare_ether_addr(eh
->h_dest
, fip
->ctl_src_addr
) &&
1025 compare_ether_addr(eh
->h_dest
, FIP_ALL_ENODE_MACS
))
1027 fiph
= (struct fip_header
*)skb
->data
;
1028 op
= ntohs(fiph
->fip_op
);
1029 sub
= fiph
->fip_subcode
;
1031 if (FIP_VER_DECAPS(fiph
->fip_ver
) != FIP_VER
)
1033 if (ntohs(fiph
->fip_dl_len
) * FIP_BPW
+ sizeof(*fiph
) > skb
->len
)
1036 spin_lock_bh(&fip
->lock
);
1038 if (state
== FIP_ST_AUTO
) {
1040 fip
->state
= FIP_ST_ENABLED
;
1041 state
= FIP_ST_ENABLED
;
1042 LIBFCOE_FIP_DBG("Using FIP mode\n");
1044 spin_unlock_bh(&fip
->lock
);
1045 if (state
!= FIP_ST_ENABLED
)
1048 if (op
== FIP_OP_LS
) {
1049 fcoe_ctlr_recv_els(fip
, skb
); /* consumes skb */
1052 if (op
== FIP_OP_DISC
&& sub
== FIP_SC_ADV
)
1053 fcoe_ctlr_recv_adv(fip
, skb
);
1054 else if (op
== FIP_OP_CTRL
&& sub
== FIP_SC_CLR_VLINK
)
1055 fcoe_ctlr_recv_clr_vlink(fip
, fiph
);
1064 * fcoe_ctlr_select() - Select the best FCF, if possible.
1065 * @fip: FCoE controller.
1067 * If there are conflicting advertisements, no FCF can be chosen.
1069 * Called with lock held.
1071 static void fcoe_ctlr_select(struct fcoe_ctlr
*fip
)
1073 struct fcoe_fcf
*fcf
;
1074 struct fcoe_fcf
*best
= NULL
;
1076 list_for_each_entry(fcf
, &fip
->fcfs
, list
) {
1077 LIBFCOE_FIP_DBG("consider FCF for fab %llx VFID %d map %x "
1078 "val %d\n", fcf
->fabric_name
, fcf
->vfid
,
1079 fcf
->fc_map
, fcoe_ctlr_mtu_valid(fcf
));
1080 if (!fcoe_ctlr_fcf_usable(fcf
)) {
1081 LIBFCOE_FIP_DBG("FCF for fab %llx map %x %svalid "
1082 "%savailable\n", fcf
->fabric_name
,
1083 fcf
->fc_map
, (fcf
->flags
& FIP_FL_SOL
)
1084 ? "" : "in", (fcf
->flags
& FIP_FL_AVAIL
)
1092 if (fcf
->fabric_name
!= best
->fabric_name
||
1093 fcf
->vfid
!= best
->vfid
||
1094 fcf
->fc_map
!= best
->fc_map
) {
1095 LIBFCOE_FIP_DBG("Conflicting fabric, VFID, "
1099 if (fcf
->pri
< best
->pri
)
1102 fip
->sel_fcf
= best
;
1106 * fcoe_ctlr_timeout() - FIP timer function.
1107 * @arg: &fcoe_ctlr pointer.
1109 * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
1111 static void fcoe_ctlr_timeout(unsigned long arg
)
1113 struct fcoe_ctlr
*fip
= (struct fcoe_ctlr
*)arg
;
1114 struct fcoe_fcf
*sel
;
1115 struct fcoe_fcf
*fcf
;
1116 unsigned long next_timer
= jiffies
+ msecs_to_jiffies(FIP_VN_KA_PERIOD
);
1120 spin_lock_bh(&fip
->lock
);
1121 if (fip
->state
== FIP_ST_DISABLED
) {
1122 spin_unlock_bh(&fip
->lock
);
1127 fcoe_ctlr_age_fcfs(fip
);
1130 if (!sel
&& fip
->sel_time
&& time_after_eq(jiffies
, fip
->sel_time
)) {
1131 fcoe_ctlr_select(fip
);
1137 fcf
= sel
; /* the old FCF may have been freed */
1139 printk(KERN_INFO
"libfcoe: host%d: FIP selected "
1140 "Fibre-Channel Forwarder MAC %pM\n",
1141 fip
->lp
->host
->host_no
, sel
->fcf_mac
);
1142 memcpy(fip
->dest_addr
, sel
->fcf_mac
, ETH_ALEN
);
1143 fip
->port_ka_time
= jiffies
+
1144 msecs_to_jiffies(FIP_VN_KA_PERIOD
);
1145 fip
->ctlr_ka_time
= jiffies
+ sel
->fka_period
;
1148 printk(KERN_NOTICE
"libfcoe: host%d: "
1149 "FIP Fibre-Channel Forwarder timed out. "
1150 "Starting FCF discovery.\n",
1151 fip
->lp
->host
->host_no
);
1154 schedule_work(&fip
->link_work
);
1160 if (time_after_eq(jiffies
, fip
->ctlr_ka_time
)) {
1161 fip
->ctlr_ka_time
= jiffies
+ sel
->fka_period
;
1164 if (time_after(next_timer
, fip
->ctlr_ka_time
))
1165 next_timer
= fip
->ctlr_ka_time
;
1167 if (time_after_eq(jiffies
, fip
->port_ka_time
)) {
1168 fip
->port_ka_time
+= jiffies
+
1169 msecs_to_jiffies(FIP_VN_KA_PERIOD
);
1172 if (time_after(next_timer
, fip
->port_ka_time
))
1173 next_timer
= fip
->port_ka_time
;
1174 mod_timer(&fip
->timer
, next_timer
);
1175 } else if (fip
->sel_time
) {
1176 next_timer
= fip
->sel_time
+
1177 msecs_to_jiffies(FCOE_CTLR_START_DELAY
);
1178 mod_timer(&fip
->timer
, next_timer
);
1180 spin_unlock_bh(&fip
->lock
);
1183 fcoe_ctlr_send_keep_alive(fip
, 0, fip
->ctl_src_addr
);
1185 fcoe_ctlr_send_keep_alive(fip
, 1, fip
->data_src_addr
);
1189 * fcoe_ctlr_link_work() - worker thread function for link changes.
1190 * @work: pointer to link_work member inside &fcoe_ctlr.
1192 * See if the link status has changed and if so, report it.
1194 * This is here because fc_linkup() and fc_linkdown() must not
1195 * be called from the timer directly, since they use a mutex.
1197 static void fcoe_ctlr_link_work(struct work_struct
*work
)
1199 struct fcoe_ctlr
*fip
;
1203 fip
= container_of(work
, struct fcoe_ctlr
, link_work
);
1204 spin_lock_bh(&fip
->lock
);
1205 last_link
= fip
->last_link
;
1207 fip
->last_link
= link
;
1208 spin_unlock_bh(&fip
->lock
);
1210 if (last_link
!= link
) {
1214 fcoe_ctlr_reset(fip
, FIP_ST_LINK_WAIT
);
1219 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames.
1220 * @recv_work: pointer to recv_work member inside &fcoe_ctlr.
1222 static void fcoe_ctlr_recv_work(struct work_struct
*recv_work
)
1224 struct fcoe_ctlr
*fip
;
1225 struct sk_buff
*skb
;
1227 fip
= container_of(recv_work
, struct fcoe_ctlr
, recv_work
);
1228 spin_lock_bh(&fip
->fip_recv_list
.lock
);
1229 while ((skb
= __skb_dequeue(&fip
->fip_recv_list
))) {
1230 spin_unlock_bh(&fip
->fip_recv_list
.lock
);
1231 fcoe_ctlr_recv_handler(fip
, skb
);
1232 spin_lock_bh(&fip
->fip_recv_list
.lock
);
1234 spin_unlock_bh(&fip
->fip_recv_list
.lock
);
1238 * fcoe_ctlr_recv_flogi() - snoop Pre-FIP receipt of FLOGI response or request.
1239 * @fip: FCoE controller.
1241 * @sa: Ethernet source MAC address from received FCoE frame.
1243 * Snoop potential response to FLOGI or even incoming FLOGI.
1245 * The caller has checked that we are waiting for login as indicated
1246 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1248 * The caller is responsible for freeing the frame.
1250 * Return non-zero if the frame should not be delivered to libfc.
1252 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr
*fip
, struct fc_frame
*fp
, u8
*sa
)
1254 struct fc_frame_header
*fh
;
1258 fh
= fc_frame_header_get(fp
);
1259 if (fh
->fh_type
!= FC_TYPE_ELS
)
1262 op
= fc_frame_payload_op(fp
);
1263 if (op
== ELS_LS_ACC
&& fh
->fh_r_ctl
== FC_RCTL_ELS_REP
&&
1264 fip
->flogi_oxid
== ntohs(fh
->fh_ox_id
)) {
1266 spin_lock_bh(&fip
->lock
);
1267 if (fip
->state
!= FIP_ST_AUTO
&& fip
->state
!= FIP_ST_NON_FIP
) {
1268 spin_unlock_bh(&fip
->lock
);
1271 fip
->state
= FIP_ST_NON_FIP
;
1272 LIBFCOE_FIP_DBG("received FLOGI LS_ACC using non-FIP mode\n");
1276 * If the src mac addr is FC_OUI-based, then we mark the
1277 * address_mode flag to use FC_OUI-based Ethernet DA.
1278 * Otherwise we use the FCoE gateway addr
1280 if (!compare_ether_addr(sa
, (u8
[6])FC_FCOE_FLOGI_MAC
)) {
1283 memcpy(fip
->dest_addr
, sa
, ETH_ALEN
);
1286 fip
->flogi_oxid
= FC_XID_UNKNOWN
;
1287 memcpy(mac
, fip
->data_src_addr
, ETH_ALEN
);
1288 fc_fcoe_set_mac(fip
->data_src_addr
, fh
->fh_d_id
);
1289 spin_unlock_bh(&fip
->lock
);
1291 fip
->update_mac(fip
, mac
, fip
->data_src_addr
);
1292 } else if (op
== ELS_FLOGI
&& fh
->fh_r_ctl
== FC_RCTL_ELS_REQ
&& sa
) {
1294 * Save source MAC for point-to-point responses.
1296 spin_lock_bh(&fip
->lock
);
1297 if (fip
->state
== FIP_ST_AUTO
|| fip
->state
== FIP_ST_NON_FIP
) {
1298 memcpy(fip
->dest_addr
, sa
, ETH_ALEN
);
1300 if (fip
->state
== FIP_ST_NON_FIP
)
1301 LIBFCOE_FIP_DBG("received FLOGI REQ, "
1302 "using non-FIP mode\n");
1303 fip
->state
= FIP_ST_NON_FIP
;
1305 spin_unlock_bh(&fip
->lock
);
1309 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi
);
1312 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
1314 * @scheme: check port
1315 * @port: port indicator for converting
1317 * Returns: u64 fc world wide name
1319 u64
fcoe_wwn_from_mac(unsigned char mac
[MAX_ADDR_LEN
],
1320 unsigned int scheme
, unsigned int port
)
1325 /* The MAC is in NO, so flip only the low 48 bits */
1326 host_mac
= ((u64
) mac
[0] << 40) |
1327 ((u64
) mac
[1] << 32) |
1328 ((u64
) mac
[2] << 24) |
1329 ((u64
) mac
[3] << 16) |
1330 ((u64
) mac
[4] << 8) |
1333 WARN_ON(host_mac
>= (1ULL << 48));
1334 wwn
= host_mac
| ((u64
) scheme
<< 60);
1340 WARN_ON(port
>= 0xfff);
1341 wwn
|= (u64
) port
<< 48;
1350 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac
);
1353 * fcoe_libfc_config() - sets up libfc related properties for lport
1354 * @lp: ptr to the fc_lport
1355 * @tt: libfc function template
1357 * Returns : 0 for success
1359 int fcoe_libfc_config(struct fc_lport
*lp
, struct libfc_function_template
*tt
)
1361 /* Set the function pointers set by the LLDD */
1362 memcpy(&lp
->tt
, tt
, sizeof(*tt
));
1363 if (fc_fcp_init(lp
))
1373 EXPORT_SYMBOL_GPL(fcoe_libfc_config
);