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
;
420 dlen
= sizeof(struct fip_encaps
) + skb
->len
; /* len before push */
421 cap
= (struct fip_encaps_head
*)skb_push(skb
, sizeof(*cap
));
423 memset(cap
, 0, sizeof(*cap
));
424 memcpy(cap
->eth
.h_dest
, fcf
->fcf_mac
, ETH_ALEN
);
425 memcpy(cap
->eth
.h_source
, fip
->ctl_src_addr
, ETH_ALEN
);
426 cap
->eth
.h_proto
= htons(ETH_P_FIP
);
428 cap
->fip
.fip_ver
= FIP_VER_ENCAPS(FIP_VER
);
429 cap
->fip
.fip_op
= htons(FIP_OP_LS
);
430 cap
->fip
.fip_subcode
= FIP_SC_REQ
;
431 cap
->fip
.fip_dl_len
= htons((dlen
+ sizeof(*mac
)) / FIP_BPW
);
432 cap
->fip
.fip_flags
= htons(FIP_FL_FPMA
);
434 cap
->fip
.fip_flags
|= htons(FIP_FL_SPMA
);
436 cap
->encaps
.fd_desc
.fip_dtype
= dtype
;
437 cap
->encaps
.fd_desc
.fip_dlen
= dlen
/ FIP_BPW
;
439 mac
= (struct fip_mac_desc
*)skb_put(skb
, sizeof(*mac
));
440 memset(mac
, 0, sizeof(mac
));
441 mac
->fd_desc
.fip_dtype
= FIP_DT_MAC
;
442 mac
->fd_desc
.fip_dlen
= sizeof(*mac
) / FIP_BPW
;
443 if (dtype
!= FIP_DT_FLOGI
)
444 memcpy(mac
->fd_mac
, fip
->data_src_addr
, ETH_ALEN
);
446 memcpy(mac
->fd_mac
, fip
->ctl_src_addr
, ETH_ALEN
);
448 skb
->protocol
= htons(ETH_P_FIP
);
449 skb_reset_mac_header(skb
);
450 skb_reset_network_header(skb
);
455 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
456 * @fip: FCoE controller.
457 * @skb: FCoE ELS frame including FC header but no FCoE headers.
459 * Returns a non-zero error code if the frame should not be sent.
460 * Returns zero if the caller should send the frame with FCoE encapsulation.
462 * The caller must check that the length is a multiple of 4.
463 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
465 int fcoe_ctlr_els_send(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
467 struct fc_frame_header
*fh
;
471 fh
= (struct fc_frame_header
*)skb
->data
;
472 op
= *(u8
*)(fh
+ 1);
474 if (op
== ELS_FLOGI
) {
475 old_xid
= fip
->flogi_oxid
;
476 fip
->flogi_oxid
= ntohs(fh
->fh_ox_id
);
477 if (fip
->state
== FIP_ST_AUTO
) {
478 if (old_xid
== FC_XID_UNKNOWN
)
479 fip
->flogi_count
= 0;
481 if (fip
->flogi_count
< 3)
486 if (fip
->state
== FIP_ST_NON_FIP
)
490 if (fip
->state
== FIP_ST_NON_FIP
)
498 if (ntoh24(fh
->fh_s_id
))
503 if (fip
->state
!= FIP_ST_ENABLED
)
505 if (ntoh24(fh
->fh_d_id
) != FC_FID_FLOGI
)
510 if (fip
->flogi_oxid
== FC_XID_UNKNOWN
)
512 if (!ntoh24(fh
->fh_s_id
))
514 if (fip
->state
== FIP_ST_AUTO
)
517 * Here we must've gotten an SID by accepting an FLOGI
518 * from a point-to-point connection. Switch to using
519 * the source mac based on the SID. The destination
520 * MAC in this case would have been set by receving the
523 fip
->flogi_oxid
= FC_XID_UNKNOWN
;
524 fc_fcoe_set_mac(fip
->data_src_addr
, fh
->fh_s_id
);
527 if (fip
->state
!= FIP_ST_ENABLED
)
531 if (fcoe_ctlr_encaps(fip
, op
, skb
))
539 EXPORT_SYMBOL(fcoe_ctlr_els_send
);
542 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller.
543 * @fip: FCoE controller.
545 * Called with lock held.
547 * An FCF is considered old if we have missed three advertisements.
548 * That is, there have been no valid advertisement from it for three
549 * times its keep-alive period including fuzz.
551 * In addition, determine the time when an FCF selection can occur.
553 static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr
*fip
)
555 struct fcoe_fcf
*fcf
;
556 struct fcoe_fcf
*next
;
557 unsigned long sel_time
= 0;
559 list_for_each_entry_safe(fcf
, next
, &fip
->fcfs
, list
) {
560 if (time_after(jiffies
, fcf
->time
+ fcf
->fka_period
* 3 +
561 msecs_to_jiffies(FIP_FCF_FUZZ
* 3))) {
562 if (fip
->sel_fcf
== fcf
)
564 list_del(&fcf
->list
);
565 WARN_ON(!fip
->fcf_count
);
568 } else if (fcoe_ctlr_mtu_valid(fcf
) &&
569 (!sel_time
|| time_before(sel_time
, fcf
->time
))) {
570 sel_time
= fcf
->time
;
574 sel_time
+= msecs_to_jiffies(FCOE_CTLR_START_DELAY
);
575 fip
->sel_time
= sel_time
;
576 if (time_before(sel_time
, fip
->timer
.expires
))
577 mod_timer(&fip
->timer
, sel_time
);
584 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry.
585 * @skb: received FIP advertisement frame
586 * @fcf: resulting FCF entry.
588 * Returns zero on a valid parsed advertisement,
589 * otherwise returns non zero value.
591 static int fcoe_ctlr_parse_adv(struct sk_buff
*skb
, struct fcoe_fcf
*fcf
)
593 struct fip_header
*fiph
;
594 struct fip_desc
*desc
= NULL
;
595 struct fip_wwn_desc
*wwn
;
596 struct fip_fab_desc
*fab
;
597 struct fip_fka_desc
*fka
;
602 memset(fcf
, 0, sizeof(*fcf
));
603 fcf
->fka_period
= msecs_to_jiffies(FCOE_CTLR_DEF_FKA
);
605 fiph
= (struct fip_header
*)skb
->data
;
606 fcf
->flags
= ntohs(fiph
->fip_flags
);
608 rlen
= ntohs(fiph
->fip_dl_len
) * 4;
609 if (rlen
+ sizeof(*fiph
) > skb
->len
)
612 desc
= (struct fip_desc
*)(fiph
+ 1);
614 dlen
= desc
->fip_dlen
* FIP_BPW
;
615 if (dlen
< sizeof(*desc
) || dlen
> rlen
)
617 switch (desc
->fip_dtype
) {
619 if (dlen
!= sizeof(struct fip_pri_desc
))
621 fcf
->pri
= ((struct fip_pri_desc
*)desc
)->fd_pri
;
624 if (dlen
!= sizeof(struct fip_mac_desc
))
627 ((struct fip_mac_desc
*)desc
)->fd_mac
,
629 if (!is_valid_ether_addr(fcf
->fcf_mac
)) {
630 LIBFCOE_FIP_DBG("Invalid MAC address "
636 if (dlen
!= sizeof(struct fip_wwn_desc
))
638 wwn
= (struct fip_wwn_desc
*)desc
;
639 fcf
->switch_name
= get_unaligned_be64(&wwn
->fd_wwn
);
642 if (dlen
!= sizeof(struct fip_fab_desc
))
644 fab
= (struct fip_fab_desc
*)desc
;
645 fcf
->fabric_name
= get_unaligned_be64(&fab
->fd_wwn
);
646 fcf
->vfid
= ntohs(fab
->fd_vfid
);
647 fcf
->fc_map
= ntoh24(fab
->fd_map
);
650 if (dlen
!= sizeof(struct fip_fka_desc
))
652 fka
= (struct fip_fka_desc
*)desc
;
653 t
= ntohl(fka
->fd_fka_period
);
654 if (t
>= FCOE_CTLR_MIN_FKA
)
655 fcf
->fka_period
= msecs_to_jiffies(t
);
658 case FIP_DT_FCOE_SIZE
:
664 LIBFCOE_FIP_DBG("unexpected descriptor type %x "
665 "in FIP adv\n", desc
->fip_dtype
);
666 /* standard says ignore unknown descriptors >= 128 */
667 if (desc
->fip_dtype
< FIP_DT_VENDOR_BASE
)
671 desc
= (struct fip_desc
*)((char *)desc
+ dlen
);
674 if (!fcf
->fc_map
|| (fcf
->fc_map
& 0x10000))
676 if (!fcf
->switch_name
|| !fcf
->fabric_name
)
681 LIBFCOE_FIP_DBG("FIP length error in descriptor type %x len %zu\n",
682 desc
->fip_dtype
, dlen
);
687 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement.
688 * @fip: FCoE controller.
689 * @skb: Received FIP packet.
691 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
693 struct fcoe_fcf
*fcf
;
695 struct fcoe_fcf
*found
;
696 unsigned long sol_tov
= msecs_to_jiffies(FCOE_CTRL_SOL_TOV
);
700 if (fcoe_ctlr_parse_adv(skb
, &new))
703 spin_lock_bh(&fip
->lock
);
704 first
= list_empty(&fip
->fcfs
);
706 list_for_each_entry(fcf
, &fip
->fcfs
, list
) {
707 if (fcf
->switch_name
== new.switch_name
&&
708 fcf
->fabric_name
== new.fabric_name
&&
709 fcf
->fc_map
== new.fc_map
&&
710 compare_ether_addr(fcf
->fcf_mac
, new.fcf_mac
) == 0) {
716 if (fip
->fcf_count
>= FCOE_CTLR_FCF_LIMIT
)
719 fcf
= kmalloc(sizeof(*fcf
), GFP_ATOMIC
);
724 memcpy(fcf
, &new, sizeof(new));
725 list_add(&fcf
->list
, &fip
->fcfs
);
728 * Flags in advertisements are ignored once the FCF is
729 * selected. Flags in unsolicited advertisements are
730 * ignored after a usable solicited advertisement
733 if (fcf
== fip
->sel_fcf
) {
734 fip
->ctlr_ka_time
-= fcf
->fka_period
;
735 fip
->ctlr_ka_time
+= new.fka_period
;
736 if (time_before(fip
->ctlr_ka_time
, fip
->timer
.expires
))
737 mod_timer(&fip
->timer
, fip
->ctlr_ka_time
);
738 } else if (!fcoe_ctlr_fcf_usable(fcf
))
739 fcf
->flags
= new.flags
;
740 fcf
->fka_period
= new.fka_period
;
741 memcpy(fcf
->fcf_mac
, new.fcf_mac
, ETH_ALEN
);
743 mtu_valid
= fcoe_ctlr_mtu_valid(fcf
);
746 LIBFCOE_FIP_DBG("New FCF for fab %llx map %x val %d\n",
747 fcf
->fabric_name
, fcf
->fc_map
, mtu_valid
);
751 * If this advertisement is not solicited and our max receive size
752 * hasn't been verified, send a solicited advertisement.
755 fcoe_ctlr_solicit(fip
, fcf
);
758 * If its been a while since we did a solicit, and this is
759 * the first advertisement we've received, do a multicast
760 * solicitation to gather as many advertisements as we can
761 * before selection occurs.
763 if (first
&& time_after(jiffies
, fip
->sol_time
+ sol_tov
))
764 fcoe_ctlr_solicit(fip
, NULL
);
767 * If this is the first validated FCF, note the time and
768 * set a timer to trigger selection.
770 if (mtu_valid
&& !fip
->sel_time
&& fcoe_ctlr_fcf_usable(fcf
)) {
771 fip
->sel_time
= jiffies
+
772 msecs_to_jiffies(FCOE_CTLR_START_DELAY
);
773 if (!timer_pending(&fip
->timer
) ||
774 time_before(fip
->sel_time
, fip
->timer
.expires
))
775 mod_timer(&fip
->timer
, fip
->sel_time
);
778 spin_unlock_bh(&fip
->lock
);
782 * fcoe_ctlr_recv_els() - Handle an incoming FIP-encapsulated ELS frame.
783 * @fip: FCoE controller.
784 * @skb: Received FIP packet.
786 static void fcoe_ctlr_recv_els(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
788 struct fc_lport
*lp
= fip
->lp
;
789 struct fip_header
*fiph
;
791 struct fc_frame_header
*fh
= NULL
;
792 struct fip_desc
*desc
;
793 struct fip_encaps
*els
;
794 struct fcoe_dev_stats
*stats
;
795 enum fip_desc_type els_dtype
= 0;
798 u8 granted_mac
[ETH_ALEN
] = { 0 };
803 fiph
= (struct fip_header
*)skb
->data
;
804 sub
= fiph
->fip_subcode
;
805 if (sub
!= FIP_SC_REQ
&& sub
!= FIP_SC_REP
)
808 rlen
= ntohs(fiph
->fip_dl_len
) * 4;
809 if (rlen
+ sizeof(*fiph
) > skb
->len
)
812 desc
= (struct fip_desc
*)(fiph
+ 1);
814 dlen
= desc
->fip_dlen
* FIP_BPW
;
815 if (dlen
< sizeof(*desc
) || dlen
> rlen
)
817 switch (desc
->fip_dtype
) {
819 if (dlen
!= sizeof(struct fip_mac_desc
))
822 ((struct fip_mac_desc
*)desc
)->fd_mac
,
824 if (!is_valid_ether_addr(granted_mac
)) {
825 LIBFCOE_FIP_DBG("Invalid MAC address "
836 if (dlen
< sizeof(*els
) + sizeof(*fh
) + 1)
838 els_len
= dlen
- sizeof(*els
);
839 els
= (struct fip_encaps
*)desc
;
840 fh
= (struct fc_frame_header
*)(els
+ 1);
841 els_dtype
= desc
->fip_dtype
;
844 LIBFCOE_FIP_DBG("unexpected descriptor type %x "
845 "in FIP adv\n", desc
->fip_dtype
);
846 /* standard says ignore unknown descriptors >= 128 */
847 if (desc
->fip_dtype
< FIP_DT_VENDOR_BASE
)
851 desc
= (struct fip_desc
*)((char *)desc
+ dlen
);
857 els_op
= *(u8
*)(fh
+ 1);
859 if (els_dtype
== FIP_DT_FLOGI
&& sub
== FIP_SC_REP
&&
860 fip
->flogi_oxid
== ntohs(fh
->fh_ox_id
) &&
861 els_op
== ELS_LS_ACC
&& is_valid_ether_addr(granted_mac
)) {
862 fip
->flogi_oxid
= FC_XID_UNKNOWN
;
863 fip
->update_mac(fip
, fip
->data_src_addr
, granted_mac
);
864 memcpy(fip
->data_src_addr
, granted_mac
, ETH_ALEN
);
868 * Convert skb into an fc_frame containing only the ELS.
870 skb_pull(skb
, (u8
*)fh
- skb
->data
);
871 skb_trim(skb
, els_len
);
872 fp
= (struct fc_frame
*)skb
;
874 fr_sof(fp
) = FC_SOF_I3
;
875 fr_eof(fp
) = FC_EOF_T
;
878 stats
= fc_lport_get_stats(lp
);
880 stats
->RxWords
+= skb
->len
/ FIP_BPW
;
882 fc_exch_recv(lp
, lp
->emp
, fp
);
886 LIBFCOE_FIP_DBG("FIP length error in descriptor type %x len %zu\n",
887 desc
->fip_dtype
, dlen
);
893 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame.
894 * @fip: FCoE controller.
895 * @fh: Received FIP header.
897 * There may be multiple VN_Port descriptors.
898 * The overall length has already been checked.
900 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr
*fip
,
901 struct fip_header
*fh
)
903 struct fip_desc
*desc
;
904 struct fip_mac_desc
*mp
;
905 struct fip_wwn_desc
*wp
;
906 struct fip_vn_desc
*vp
;
909 struct fcoe_fcf
*fcf
= fip
->sel_fcf
;
910 struct fc_lport
*lp
= fip
->lp
;
913 LIBFCOE_FIP_DBG("Clear Virtual Link received\n");
916 if (!fcf
|| !fc_host_port_id(lp
->host
))
920 * mask of required descriptors. Validating each one clears its bit.
922 desc_mask
= BIT(FIP_DT_MAC
) | BIT(FIP_DT_NAME
) | BIT(FIP_DT_VN_ID
);
924 rlen
= ntohs(fh
->fip_dl_len
) * FIP_BPW
;
925 desc
= (struct fip_desc
*)(fh
+ 1);
926 while (rlen
>= sizeof(*desc
)) {
927 dlen
= desc
->fip_dlen
* FIP_BPW
;
930 switch (desc
->fip_dtype
) {
932 mp
= (struct fip_mac_desc
*)desc
;
933 if (dlen
< sizeof(*mp
))
935 if (compare_ether_addr(mp
->fd_mac
, fcf
->fcf_mac
))
937 desc_mask
&= ~BIT(FIP_DT_MAC
);
940 wp
= (struct fip_wwn_desc
*)desc
;
941 if (dlen
< sizeof(*wp
))
943 if (get_unaligned_be64(&wp
->fd_wwn
) != fcf
->switch_name
)
945 desc_mask
&= ~BIT(FIP_DT_NAME
);
948 vp
= (struct fip_vn_desc
*)desc
;
949 if (dlen
< sizeof(*vp
))
951 if (compare_ether_addr(vp
->fd_mac
,
952 fip
->data_src_addr
) == 0 &&
953 get_unaligned_be64(&vp
->fd_wwpn
) == lp
->wwpn
&&
954 ntoh24(vp
->fd_fc_id
) == fc_host_port_id(lp
->host
))
955 desc_mask
&= ~BIT(FIP_DT_VN_ID
);
958 /* standard says ignore unknown descriptors >= 128 */
959 if (desc
->fip_dtype
< FIP_DT_VENDOR_BASE
)
963 desc
= (struct fip_desc
*)((char *)desc
+ dlen
);
968 * reset only if all required descriptors were present and valid.
971 LIBFCOE_FIP_DBG("missing descriptors mask %x\n", desc_mask
);
973 LIBFCOE_FIP_DBG("performing Clear Virtual Link\n");
974 fcoe_ctlr_reset(fip
, FIP_ST_ENABLED
);
979 * fcoe_ctlr_recv() - Receive a FIP frame.
980 * @fip: FCoE controller.
981 * @skb: Received FIP packet.
983 * This is called from NET_RX_SOFTIRQ.
985 void fcoe_ctlr_recv(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
987 spin_lock_bh(&fip
->fip_recv_list
.lock
);
988 __skb_queue_tail(&fip
->fip_recv_list
, skb
);
989 spin_unlock_bh(&fip
->fip_recv_list
.lock
);
990 schedule_work(&fip
->recv_work
);
992 EXPORT_SYMBOL(fcoe_ctlr_recv
);
995 * fcoe_ctlr_recv_handler() - Receive a FIP frame.
996 * @fip: FCoE controller.
997 * @skb: Received FIP packet.
999 * Returns non-zero if the frame is dropped.
1001 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr
*fip
, struct sk_buff
*skb
)
1003 struct fip_header
*fiph
;
1005 enum fip_state state
;
1009 if (skb_linearize(skb
))
1011 if (skb
->len
< sizeof(*fiph
))
1014 if (compare_ether_addr(eh
->h_dest
, fip
->ctl_src_addr
) &&
1015 compare_ether_addr(eh
->h_dest
, FIP_ALL_ENODE_MACS
))
1017 fiph
= (struct fip_header
*)skb
->data
;
1018 op
= ntohs(fiph
->fip_op
);
1019 sub
= fiph
->fip_subcode
;
1021 if (FIP_VER_DECAPS(fiph
->fip_ver
) != FIP_VER
)
1023 if (ntohs(fiph
->fip_dl_len
) * FIP_BPW
+ sizeof(*fiph
) > skb
->len
)
1026 spin_lock_bh(&fip
->lock
);
1028 if (state
== FIP_ST_AUTO
) {
1030 fip
->state
= FIP_ST_ENABLED
;
1031 state
= FIP_ST_ENABLED
;
1032 LIBFCOE_FIP_DBG("Using FIP mode\n");
1034 spin_unlock_bh(&fip
->lock
);
1035 if (state
!= FIP_ST_ENABLED
)
1038 if (op
== FIP_OP_LS
) {
1039 fcoe_ctlr_recv_els(fip
, skb
); /* consumes skb */
1042 if (op
== FIP_OP_DISC
&& sub
== FIP_SC_ADV
)
1043 fcoe_ctlr_recv_adv(fip
, skb
);
1044 else if (op
== FIP_OP_CTRL
&& sub
== FIP_SC_CLR_VLINK
)
1045 fcoe_ctlr_recv_clr_vlink(fip
, fiph
);
1054 * fcoe_ctlr_select() - Select the best FCF, if possible.
1055 * @fip: FCoE controller.
1057 * If there are conflicting advertisements, no FCF can be chosen.
1059 * Called with lock held.
1061 static void fcoe_ctlr_select(struct fcoe_ctlr
*fip
)
1063 struct fcoe_fcf
*fcf
;
1064 struct fcoe_fcf
*best
= NULL
;
1066 list_for_each_entry(fcf
, &fip
->fcfs
, list
) {
1067 LIBFCOE_FIP_DBG("consider FCF for fab %llx VFID %d map %x "
1068 "val %d\n", fcf
->fabric_name
, fcf
->vfid
,
1069 fcf
->fc_map
, fcoe_ctlr_mtu_valid(fcf
));
1070 if (!fcoe_ctlr_fcf_usable(fcf
)) {
1071 LIBFCOE_FIP_DBG("FCF for fab %llx map %x %svalid "
1072 "%savailable\n", fcf
->fabric_name
,
1073 fcf
->fc_map
, (fcf
->flags
& FIP_FL_SOL
)
1074 ? "" : "in", (fcf
->flags
& FIP_FL_AVAIL
)
1082 if (fcf
->fabric_name
!= best
->fabric_name
||
1083 fcf
->vfid
!= best
->vfid
||
1084 fcf
->fc_map
!= best
->fc_map
) {
1085 LIBFCOE_FIP_DBG("Conflicting fabric, VFID, "
1089 if (fcf
->pri
< best
->pri
)
1092 fip
->sel_fcf
= best
;
1096 * fcoe_ctlr_timeout() - FIP timer function.
1097 * @arg: &fcoe_ctlr pointer.
1099 * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
1101 static void fcoe_ctlr_timeout(unsigned long arg
)
1103 struct fcoe_ctlr
*fip
= (struct fcoe_ctlr
*)arg
;
1104 struct fcoe_fcf
*sel
;
1105 struct fcoe_fcf
*fcf
;
1106 unsigned long next_timer
= jiffies
+ msecs_to_jiffies(FIP_VN_KA_PERIOD
);
1107 DECLARE_MAC_BUF(buf
);
1111 spin_lock_bh(&fip
->lock
);
1112 if (fip
->state
== FIP_ST_DISABLED
) {
1113 spin_unlock_bh(&fip
->lock
);
1118 fcoe_ctlr_age_fcfs(fip
);
1121 if (!sel
&& fip
->sel_time
&& time_after_eq(jiffies
, fip
->sel_time
)) {
1122 fcoe_ctlr_select(fip
);
1128 fcf
= sel
; /* the old FCF may have been freed */
1130 printk(KERN_INFO
"libfcoe: host%d: FIP selected "
1131 "Fibre-Channel Forwarder MAC %s\n",
1132 fip
->lp
->host
->host_no
,
1133 print_mac(buf
, sel
->fcf_mac
));
1134 memcpy(fip
->dest_addr
, sel
->fcf_mac
, ETH_ALEN
);
1135 fip
->port_ka_time
= jiffies
+
1136 msecs_to_jiffies(FIP_VN_KA_PERIOD
);
1137 fip
->ctlr_ka_time
= jiffies
+ sel
->fka_period
;
1140 printk(KERN_NOTICE
"libfcoe: host%d: "
1141 "FIP Fibre-Channel Forwarder timed out. "
1142 "Starting FCF discovery.\n",
1143 fip
->lp
->host
->host_no
);
1146 schedule_work(&fip
->link_work
);
1152 if (time_after_eq(jiffies
, fip
->ctlr_ka_time
)) {
1153 fip
->ctlr_ka_time
= jiffies
+ sel
->fka_period
;
1156 if (time_after(next_timer
, fip
->ctlr_ka_time
))
1157 next_timer
= fip
->ctlr_ka_time
;
1159 if (time_after_eq(jiffies
, fip
->port_ka_time
)) {
1160 fip
->port_ka_time
+= jiffies
+
1161 msecs_to_jiffies(FIP_VN_KA_PERIOD
);
1164 if (time_after(next_timer
, fip
->port_ka_time
))
1165 next_timer
= fip
->port_ka_time
;
1166 mod_timer(&fip
->timer
, next_timer
);
1167 } else if (fip
->sel_time
) {
1168 next_timer
= fip
->sel_time
+
1169 msecs_to_jiffies(FCOE_CTLR_START_DELAY
);
1170 mod_timer(&fip
->timer
, next_timer
);
1172 spin_unlock_bh(&fip
->lock
);
1175 fcoe_ctlr_send_keep_alive(fip
, 0, fip
->ctl_src_addr
);
1177 fcoe_ctlr_send_keep_alive(fip
, 1, fip
->data_src_addr
);
1181 * fcoe_ctlr_link_work() - worker thread function for link changes.
1182 * @work: pointer to link_work member inside &fcoe_ctlr.
1184 * See if the link status has changed and if so, report it.
1186 * This is here because fc_linkup() and fc_linkdown() must not
1187 * be called from the timer directly, since they use a mutex.
1189 static void fcoe_ctlr_link_work(struct work_struct
*work
)
1191 struct fcoe_ctlr
*fip
;
1195 fip
= container_of(work
, struct fcoe_ctlr
, link_work
);
1196 spin_lock_bh(&fip
->lock
);
1197 last_link
= fip
->last_link
;
1199 fip
->last_link
= link
;
1200 spin_unlock_bh(&fip
->lock
);
1202 if (last_link
!= link
) {
1206 fcoe_ctlr_reset(fip
, FIP_ST_LINK_WAIT
);
1211 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames.
1212 * @recv_work: pointer to recv_work member inside &fcoe_ctlr.
1214 static void fcoe_ctlr_recv_work(struct work_struct
*recv_work
)
1216 struct fcoe_ctlr
*fip
;
1217 struct sk_buff
*skb
;
1219 fip
= container_of(recv_work
, struct fcoe_ctlr
, recv_work
);
1220 spin_lock_bh(&fip
->fip_recv_list
.lock
);
1221 while ((skb
= __skb_dequeue(&fip
->fip_recv_list
))) {
1222 spin_unlock_bh(&fip
->fip_recv_list
.lock
);
1223 fcoe_ctlr_recv_handler(fip
, skb
);
1224 spin_lock_bh(&fip
->fip_recv_list
.lock
);
1226 spin_unlock_bh(&fip
->fip_recv_list
.lock
);
1230 * fcoe_ctlr_recv_flogi() - snoop Pre-FIP receipt of FLOGI response or request.
1231 * @fip: FCoE controller.
1233 * @sa: Ethernet source MAC address from received FCoE frame.
1235 * Snoop potential response to FLOGI or even incoming FLOGI.
1237 * The caller has checked that we are waiting for login as indicated
1238 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1240 * The caller is responsible for freeing the frame.
1242 * Return non-zero if the frame should not be delivered to libfc.
1244 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr
*fip
, struct fc_frame
*fp
, u8
*sa
)
1246 struct fc_frame_header
*fh
;
1250 fh
= fc_frame_header_get(fp
);
1251 if (fh
->fh_type
!= FC_TYPE_ELS
)
1254 op
= fc_frame_payload_op(fp
);
1255 if (op
== ELS_LS_ACC
&& fh
->fh_r_ctl
== FC_RCTL_ELS_REP
&&
1256 fip
->flogi_oxid
== ntohs(fh
->fh_ox_id
)) {
1258 spin_lock_bh(&fip
->lock
);
1259 if (fip
->state
!= FIP_ST_AUTO
&& fip
->state
!= FIP_ST_NON_FIP
) {
1260 spin_unlock_bh(&fip
->lock
);
1263 fip
->state
= FIP_ST_NON_FIP
;
1264 LIBFCOE_FIP_DBG("received FLOGI LS_ACC using non-FIP mode\n");
1268 * If the src mac addr is FC_OUI-based, then we mark the
1269 * address_mode flag to use FC_OUI-based Ethernet DA.
1270 * Otherwise we use the FCoE gateway addr
1272 if (!compare_ether_addr(sa
, (u8
[6])FC_FCOE_FLOGI_MAC
)) {
1275 memcpy(fip
->dest_addr
, sa
, ETH_ALEN
);
1278 fip
->flogi_oxid
= FC_XID_UNKNOWN
;
1279 memcpy(mac
, fip
->data_src_addr
, ETH_ALEN
);
1280 fc_fcoe_set_mac(fip
->data_src_addr
, fh
->fh_d_id
);
1281 spin_unlock_bh(&fip
->lock
);
1283 fip
->update_mac(fip
, mac
, fip
->data_src_addr
);
1284 } else if (op
== ELS_FLOGI
&& fh
->fh_r_ctl
== FC_RCTL_ELS_REQ
&& sa
) {
1286 * Save source MAC for point-to-point responses.
1288 spin_lock_bh(&fip
->lock
);
1289 if (fip
->state
== FIP_ST_AUTO
|| fip
->state
== FIP_ST_NON_FIP
) {
1290 memcpy(fip
->dest_addr
, sa
, ETH_ALEN
);
1292 if (fip
->state
== FIP_ST_NON_FIP
)
1293 LIBFCOE_FIP_DBG("received FLOGI REQ, "
1294 "using non-FIP mode\n");
1295 fip
->state
= FIP_ST_NON_FIP
;
1297 spin_unlock_bh(&fip
->lock
);
1301 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi
);
1304 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
1306 * @scheme: check port
1307 * @port: port indicator for converting
1309 * Returns: u64 fc world wide name
1311 u64
fcoe_wwn_from_mac(unsigned char mac
[MAX_ADDR_LEN
],
1312 unsigned int scheme
, unsigned int port
)
1317 /* The MAC is in NO, so flip only the low 48 bits */
1318 host_mac
= ((u64
) mac
[0] << 40) |
1319 ((u64
) mac
[1] << 32) |
1320 ((u64
) mac
[2] << 24) |
1321 ((u64
) mac
[3] << 16) |
1322 ((u64
) mac
[4] << 8) |
1325 WARN_ON(host_mac
>= (1ULL << 48));
1326 wwn
= host_mac
| ((u64
) scheme
<< 60);
1332 WARN_ON(port
>= 0xfff);
1333 wwn
|= (u64
) port
<< 48;
1342 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac
);
1345 * fcoe_libfc_config() - sets up libfc related properties for lport
1346 * @lp: ptr to the fc_lport
1347 * @tt: libfc function template
1349 * Returns : 0 for success
1351 int fcoe_libfc_config(struct fc_lport
*lp
, struct libfc_function_template
*tt
)
1353 /* Set the function pointers set by the LLDD */
1354 memcpy(&lp
->tt
, tt
, sizeof(*tt
));
1355 if (fc_fcp_init(lp
))
1365 EXPORT_SYMBOL_GPL(fcoe_libfc_config
);