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/errno.h>
33 #include <linux/bitops.h>
34 #include <linux/slab.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>
42 #include <scsi/fc/fc_fcp.h>
44 #include <scsi/libfc.h>
45 #include <scsi/libfcoe.h>
47 MODULE_AUTHOR("Open-FCoE.org");
48 MODULE_DESCRIPTION("FIP discovery protocol support for FCoE HBAs");
49 MODULE_LICENSE("GPL v2");
51 #define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
52 #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
54 static void fcoe_ctlr_timeout(unsigned long);
55 static void fcoe_ctlr_timer_work(struct work_struct *);
56 static void fcoe_ctlr_recv_work(struct work_struct *);
58 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
59 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
60 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
61 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
63 static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
64 static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
65 static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
66 static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
68 unsigned int libfcoe_debug_logging;
69 module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
70 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
72 #define LIBFCOE_LOGGING 0x01 /* General logging, not categorized */
73 #define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
75 #define LIBFCOE_CHECK_LOGGING(LEVEL, CMD) \
77 if (unlikely(libfcoe_debug_logging & LEVEL)) \
83 #define LIBFCOE_DBG(fmt, args...) \
84 LIBFCOE_CHECK_LOGGING(LIBFCOE_LOGGING, \
85 printk(KERN_INFO "libfcoe: " fmt, ##args);)
87 #define LIBFCOE_FIP_DBG(fip, fmt, args...) \
88 LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING, \
89 printk(KERN_INFO "host%d: fip: " fmt, \
90 (fip)->lp->host->host_no, ##args);)
92 static const char *fcoe_ctlr_states[] = {
93 [FIP_ST_DISABLED] = "DISABLED",
94 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
95 [FIP_ST_AUTO] = "AUTO",
96 [FIP_ST_NON_FIP] = "NON_FIP",
97 [FIP_ST_ENABLED] = "ENABLED",
98 [FIP_ST_VNMP_START] = "VNMP_START",
99 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
100 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
101 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
102 [FIP_ST_VNMP_UP] = "VNMP_UP",
105 static const char *fcoe_ctlr_state(enum fip_state state)
107 const char *cp = "unknown";
109 if (state < ARRAY_SIZE(fcoe_ctlr_states))
110 cp = fcoe_ctlr_states[state];
117 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
118 * @fip: The FCoE controller
119 * @state: The new state
121 static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
123 if (state == fip->state)
126 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
127 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
132 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
133 * @fcf: The FCF to check
135 * Return non-zero if FCF fcoe_size has been validated.
137 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
139 return (fcf->flags & FIP_FL_SOL) != 0;
143 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
144 * @fcf: The FCF to check
146 * Return non-zero if the FCF is usable.
148 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
150 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
152 return (fcf->flags & flags) == flags;
156 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
157 * @fip: The FCoE controller
159 static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
161 if (fip->mode == FIP_MODE_VN2VN)
162 hton24(fip->dest_addr, FIP_VN_FC_MAP);
164 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
165 hton24(fip->dest_addr + 3, 0);
170 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
171 * @fip: The FCoE controller to initialize
173 void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
175 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
177 INIT_LIST_HEAD(&fip->fcfs);
178 mutex_init(&fip->ctlr_mutex);
179 fip->flogi_oxid = FC_XID_UNKNOWN;
180 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
181 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
182 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
183 skb_queue_head_init(&fip->fip_recv_list);
185 EXPORT_SYMBOL(fcoe_ctlr_init);
188 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
189 * @fip: The FCoE controller whose FCFs are to be reset
191 * Called with &fcoe_ctlr lock held.
193 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
195 struct fcoe_fcf *fcf;
196 struct fcoe_fcf *next;
199 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
200 list_del(&fcf->list);
208 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
209 * @fip: The FCoE controller to tear down
211 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
213 * The receive handler will have been deleted before this to guarantee
214 * that no more recv_work will be scheduled.
216 * The timer routine will simply return once we set FIP_ST_DISABLED.
217 * This guarantees that no further timeouts or work will be scheduled.
219 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
221 cancel_work_sync(&fip->recv_work);
222 skb_queue_purge(&fip->fip_recv_list);
224 mutex_lock(&fip->ctlr_mutex);
225 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
226 fcoe_ctlr_reset_fcfs(fip);
227 mutex_unlock(&fip->ctlr_mutex);
228 del_timer_sync(&fip->timer);
229 cancel_work_sync(&fip->timer_work);
231 EXPORT_SYMBOL(fcoe_ctlr_destroy);
234 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
235 * @fip: The FCoE controller to get the maximum FCoE size from
237 * Returns the maximum packet size including the FCoE header and trailer,
238 * but not including any Ethernet or VLAN headers.
240 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
243 * Determine the max FCoE frame size allowed, including
244 * FCoE header and trailer.
245 * Note: lp->mfs is currently the payload size, not the frame size.
247 return fip->lp->mfs + sizeof(struct fc_frame_header) +
248 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
252 * fcoe_ctlr_solicit() - Send a FIP solicitation
253 * @fip: The FCoE controller to send the solicitation on
254 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
256 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
261 struct fip_header fip;
263 struct fip_mac_desc mac;
264 struct fip_wwn_desc wwnn;
265 struct fip_size_desc size;
266 } __attribute__((packed)) desc;
267 } __attribute__((packed)) *sol;
270 skb = dev_alloc_skb(sizeof(*sol));
274 sol = (struct fip_sol *)skb->data;
276 memset(sol, 0, sizeof(*sol));
277 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
278 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
279 sol->eth.h_proto = htons(ETH_P_FIP);
281 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
282 sol->fip.fip_op = htons(FIP_OP_DISC);
283 sol->fip.fip_subcode = FIP_SC_SOL;
284 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
285 sol->fip.fip_flags = htons(FIP_FL_FPMA);
287 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
289 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
290 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
291 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
293 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
294 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
295 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
297 fcoe_size = fcoe_ctlr_fcoe_size(fip);
298 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
299 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
300 sol->desc.size.fd_size = htons(fcoe_size);
302 skb_put(skb, sizeof(*sol));
303 skb->protocol = htons(ETH_P_FIP);
304 skb_reset_mac_header(skb);
305 skb_reset_network_header(skb);
309 fip->sol_time = jiffies;
313 * fcoe_ctlr_link_up() - Start FCoE controller
314 * @fip: The FCoE controller to start
316 * Called from the LLD when the network link is ready.
318 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
320 mutex_lock(&fip->ctlr_mutex);
321 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
322 mutex_unlock(&fip->ctlr_mutex);
324 } else if (fip->state == FIP_ST_LINK_WAIT) {
325 fcoe_ctlr_set_state(fip, fip->mode);
328 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
331 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
333 case FIP_MODE_FABRIC:
334 case FIP_MODE_NON_FIP:
335 mutex_unlock(&fip->ctlr_mutex);
337 fcoe_ctlr_solicit(fip, NULL);
340 fcoe_ctlr_vn_start(fip);
341 mutex_unlock(&fip->ctlr_mutex);
346 mutex_unlock(&fip->ctlr_mutex);
348 EXPORT_SYMBOL(fcoe_ctlr_link_up);
351 * fcoe_ctlr_reset() - Reset a FCoE controller
352 * @fip: The FCoE controller to reset
354 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
356 fcoe_ctlr_reset_fcfs(fip);
357 del_timer(&fip->timer);
358 fip->ctlr_ka_time = 0;
359 fip->port_ka_time = 0;
361 fip->flogi_oxid = FC_XID_UNKNOWN;
362 fcoe_ctlr_map_dest(fip);
366 * fcoe_ctlr_link_down() - Stop a FCoE controller
367 * @fip: The FCoE controller to be stopped
369 * Returns non-zero if the link was up and now isn't.
371 * Called from the LLD when the network link is not ready.
372 * There may be multiple calls while the link is down.
374 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
378 LIBFCOE_FIP_DBG(fip, "link down.\n");
379 mutex_lock(&fip->ctlr_mutex);
380 fcoe_ctlr_reset(fip);
381 link_dropped = fip->state != FIP_ST_LINK_WAIT;
382 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
383 mutex_unlock(&fip->ctlr_mutex);
386 fc_linkdown(fip->lp);
389 EXPORT_SYMBOL(fcoe_ctlr_link_down);
392 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
393 * @fip: The FCoE controller to send the FKA on
394 * @lport: libfc fc_lport to send from
395 * @ports: 0 for controller keep-alive, 1 for port keep-alive
396 * @sa: The source MAC address
398 * A controller keep-alive is sent every fka_period (typically 8 seconds).
399 * The source MAC is the native MAC address.
401 * A port keep-alive is sent every 90 seconds while logged in.
402 * The source MAC is the assigned mapped source address.
403 * The destination is the FCF's F-port.
405 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
406 struct fc_lport *lport,
412 struct fip_header fip;
413 struct fip_mac_desc mac;
414 } __attribute__((packed)) *kal;
415 struct fip_vn_desc *vn;
418 struct fcoe_fcf *fcf;
422 if (!fcf || (ports && !lp->port_id))
425 len = sizeof(*kal) + ports * sizeof(*vn);
426 skb = dev_alloc_skb(len);
430 kal = (struct fip_kal *)skb->data;
432 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
433 memcpy(kal->eth.h_source, sa, ETH_ALEN);
434 kal->eth.h_proto = htons(ETH_P_FIP);
436 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
437 kal->fip.fip_op = htons(FIP_OP_CTRL);
438 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
439 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
440 ports * sizeof(*vn)) / FIP_BPW);
441 kal->fip.fip_flags = htons(FIP_FL_FPMA);
443 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
445 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
446 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
447 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
449 vn = (struct fip_vn_desc *)(kal + 1);
450 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
451 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
452 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
453 hton24(vn->fd_fc_id, lport->port_id);
454 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
457 skb->protocol = htons(ETH_P_FIP);
458 skb_reset_mac_header(skb);
459 skb_reset_network_header(skb);
464 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
465 * @fip: The FCoE controller for the ELS frame
466 * @dtype: The FIP descriptor type for the frame
467 * @skb: The FCoE ELS frame including FC header but no FCoE headers
468 * @d_id: The destination port ID.
470 * Returns non-zero error code on failure.
472 * The caller must check that the length is a multiple of 4.
474 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
475 * Headroom includes the FIP encapsulation description, FIP header, and
476 * Ethernet header. The tailroom is for the FIP MAC descriptor.
478 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
479 u8 dtype, struct sk_buff *skb, u32 d_id)
481 struct fip_encaps_head {
483 struct fip_header fip;
484 struct fip_encaps encaps;
485 } __attribute__((packed)) *cap;
486 struct fc_frame_header *fh;
487 struct fip_mac_desc *mac;
488 struct fcoe_fcf *fcf;
493 fh = (struct fc_frame_header *)skb->data;
494 op = *(u8 *)(fh + 1);
495 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
496 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
497 memset(cap, 0, sizeof(*cap));
499 if (lport->point_to_multipoint) {
500 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
507 fip_flags = fcf->flags;
508 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
512 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
514 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
515 cap->eth.h_proto = htons(ETH_P_FIP);
517 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
518 cap->fip.fip_op = htons(FIP_OP_LS);
519 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
520 cap->fip.fip_subcode = FIP_SC_REP;
522 cap->fip.fip_subcode = FIP_SC_REQ;
523 cap->fip.fip_flags = htons(fip_flags);
525 cap->encaps.fd_desc.fip_dtype = dtype;
526 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
528 if (op != ELS_LS_RJT) {
529 dlen += sizeof(*mac);
530 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
531 memset(mac, 0, sizeof(*mac));
532 mac->fd_desc.fip_dtype = FIP_DT_MAC;
533 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
534 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
535 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
536 } else if (fip->mode == FIP_MODE_VN2VN) {
537 hton24(mac->fd_mac, FIP_VN_FC_MAP);
538 hton24(mac->fd_mac + 3, fip->port_id);
539 } else if (fip_flags & FIP_FL_SPMA) {
540 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
541 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
543 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
544 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
547 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
549 skb->protocol = htons(ETH_P_FIP);
550 skb_reset_mac_header(skb);
551 skb_reset_network_header(skb);
556 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
557 * @fip: FCoE controller.
558 * @lport: libfc fc_lport to send from
559 * @skb: FCoE ELS frame including FC header but no FCoE headers.
561 * Returns a non-zero error code if the frame should not be sent.
562 * Returns zero if the caller should send the frame with FCoE encapsulation.
564 * The caller must check that the length is a multiple of 4.
565 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
566 * The the skb must also be an fc_frame.
568 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
572 struct fc_frame_header *fh;
577 fp = container_of(skb, struct fc_frame, skb);
578 fh = (struct fc_frame_header *)skb->data;
579 op = *(u8 *)(fh + 1);
581 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
582 old_xid = fip->flogi_oxid;
583 fip->flogi_oxid = ntohs(fh->fh_ox_id);
584 if (fip->state == FIP_ST_AUTO) {
585 if (old_xid == FC_XID_UNKNOWN)
586 fip->flogi_count = 0;
588 if (fip->flogi_count < 3)
590 fcoe_ctlr_map_dest(fip);
593 if (fip->state == FIP_ST_NON_FIP)
594 fcoe_ctlr_map_dest(fip);
597 if (fip->state == FIP_ST_NON_FIP)
599 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
606 if (ntoh24(fh->fh_s_id))
611 if (fip->mode == FIP_MODE_VN2VN) {
612 if (fip->state != FIP_ST_VNMP_UP)
614 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
617 if (fip->state != FIP_ST_ENABLED)
619 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
626 * If non-FIP, we may have gotten an SID by accepting an FLOGI
627 * from a point-to-point connection. Switch to using
628 * the source mac based on the SID. The destination
629 * MAC in this case would have been set by receving the
632 if (fip->state == FIP_ST_NON_FIP) {
633 if (fip->flogi_oxid == FC_XID_UNKNOWN)
635 fip->flogi_oxid = FC_XID_UNKNOWN;
636 fc_fcoe_set_mac(mac, fh->fh_d_id);
637 fip->update_mac(lport, mac);
646 if (fip->state != FIP_ST_ENABLED &&
647 fip->state != FIP_ST_VNMP_UP)
651 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
652 op, ntoh24(fh->fh_d_id));
653 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
661 EXPORT_SYMBOL(fcoe_ctlr_els_send);
664 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
665 * @fip: The FCoE controller to free FCFs on
667 * Called with lock held and preemption disabled.
669 * An FCF is considered old if we have missed two advertisements.
670 * That is, there have been no valid advertisement from it for 2.5
671 * times its keep-alive period.
673 * In addition, determine the time when an FCF selection can occur.
675 * Also, increment the MissDiscAdvCount when no advertisement is received
676 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
678 * Returns the time in jiffies for the next call.
680 static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
682 struct fcoe_fcf *fcf;
683 struct fcoe_fcf *next;
684 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
685 unsigned long deadline;
686 unsigned long sel_time = 0;
687 struct fcoe_dev_stats *stats;
689 stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
691 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
692 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
693 if (fip->sel_fcf == fcf) {
694 if (time_after(jiffies, deadline)) {
695 stats->MissDiscAdvCount++;
696 printk(KERN_INFO "libfcoe: host%d: "
697 "Missing Discovery Advertisement "
698 "for fab %16.16llx count %lld\n",
699 fip->lp->host->host_no, fcf->fabric_name,
700 stats->MissDiscAdvCount);
701 } else if (time_after(next_timer, deadline))
702 next_timer = deadline;
705 deadline += fcf->fka_period;
706 if (time_after_eq(jiffies, deadline)) {
707 if (fip->sel_fcf == fcf)
709 list_del(&fcf->list);
710 WARN_ON(!fip->fcf_count);
713 stats->VLinkFailureCount++;
715 if (time_after(next_timer, deadline))
716 next_timer = deadline;
717 if (fcoe_ctlr_mtu_valid(fcf) &&
718 (!sel_time || time_before(sel_time, fcf->time)))
719 sel_time = fcf->time;
723 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
724 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
725 fip->sel_time = sel_time;
732 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
733 * @fip: The FCoE controller receiving the advertisement
734 * @skb: The received FIP advertisement frame
735 * @fcf: The resulting FCF entry
737 * Returns zero on a valid parsed advertisement,
738 * otherwise returns non zero value.
740 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
741 struct sk_buff *skb, struct fcoe_fcf *fcf)
743 struct fip_header *fiph;
744 struct fip_desc *desc = NULL;
745 struct fip_wwn_desc *wwn;
746 struct fip_fab_desc *fab;
747 struct fip_fka_desc *fka;
753 memset(fcf, 0, sizeof(*fcf));
754 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
756 fiph = (struct fip_header *)skb->data;
757 fcf->flags = ntohs(fiph->fip_flags);
760 * mask of required descriptors. validating each one clears its bit.
762 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
763 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
765 rlen = ntohs(fiph->fip_dl_len) * 4;
766 if (rlen + sizeof(*fiph) > skb->len)
769 desc = (struct fip_desc *)(fiph + 1);
771 dlen = desc->fip_dlen * FIP_BPW;
772 if (dlen < sizeof(*desc) || dlen > rlen)
774 /* Drop Adv if there are duplicate critical descriptors */
775 if ((desc->fip_dtype < 32) &&
776 !(desc_mask & 1U << desc->fip_dtype)) {
777 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
778 "Descriptors in FIP adv\n");
781 switch (desc->fip_dtype) {
783 if (dlen != sizeof(struct fip_pri_desc))
785 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
786 desc_mask &= ~BIT(FIP_DT_PRI);
789 if (dlen != sizeof(struct fip_mac_desc))
792 ((struct fip_mac_desc *)desc)->fd_mac,
794 if (!is_valid_ether_addr(fcf->fcf_mac)) {
796 "Invalid MAC addr %pM in FIP adv\n",
800 desc_mask &= ~BIT(FIP_DT_MAC);
803 if (dlen != sizeof(struct fip_wwn_desc))
805 wwn = (struct fip_wwn_desc *)desc;
806 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
807 desc_mask &= ~BIT(FIP_DT_NAME);
810 if (dlen != sizeof(struct fip_fab_desc))
812 fab = (struct fip_fab_desc *)desc;
813 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
814 fcf->vfid = ntohs(fab->fd_vfid);
815 fcf->fc_map = ntoh24(fab->fd_map);
816 desc_mask &= ~BIT(FIP_DT_FAB);
819 if (dlen != sizeof(struct fip_fka_desc))
821 fka = (struct fip_fka_desc *)desc;
822 if (fka->fd_flags & FIP_FKA_ADV_D)
824 t = ntohl(fka->fd_fka_period);
825 if (t >= FCOE_CTLR_MIN_FKA)
826 fcf->fka_period = msecs_to_jiffies(t);
827 desc_mask &= ~BIT(FIP_DT_FKA);
830 case FIP_DT_FCOE_SIZE:
836 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
837 "in FIP adv\n", desc->fip_dtype);
838 /* standard says ignore unknown descriptors >= 128 */
839 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
843 desc = (struct fip_desc *)((char *)desc + dlen);
846 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
848 if (!fcf->switch_name)
851 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
858 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
859 desc->fip_dtype, dlen);
864 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
865 * @fip: The FCoE controller receiving the advertisement
866 * @skb: The received FIP packet
868 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
870 struct fcoe_fcf *fcf;
872 struct fcoe_fcf *found;
873 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
877 if (fcoe_ctlr_parse_adv(fip, skb, &new))
880 mutex_lock(&fip->ctlr_mutex);
881 first = list_empty(&fip->fcfs);
883 list_for_each_entry(fcf, &fip->fcfs, list) {
884 if (fcf->switch_name == new.switch_name &&
885 fcf->fabric_name == new.fabric_name &&
886 fcf->fc_map == new.fc_map &&
887 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
893 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
896 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
901 memcpy(fcf, &new, sizeof(new));
902 list_add(&fcf->list, &fip->fcfs);
905 * Update the FCF's keep-alive descriptor flags.
906 * Other flag changes from new advertisements are
907 * ignored after a solicited advertisement is
908 * received and the FCF is selectable (usable).
910 fcf->fd_flags = new.fd_flags;
911 if (!fcoe_ctlr_fcf_usable(fcf))
912 fcf->flags = new.flags;
914 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
915 fip->ctlr_ka_time -= fcf->fka_period;
916 fip->ctlr_ka_time += new.fka_period;
917 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
918 mod_timer(&fip->timer, fip->ctlr_ka_time);
920 fcf->fka_period = new.fka_period;
921 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
923 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
926 LIBFCOE_FIP_DBG(fip, "New FCF for fab %16.16llx "
928 fcf->fabric_name, fcf->fc_map, mtu_valid);
932 * If this advertisement is not solicited and our max receive size
933 * hasn't been verified, send a solicited advertisement.
936 fcoe_ctlr_solicit(fip, fcf);
939 * If its been a while since we did a solicit, and this is
940 * the first advertisement we've received, do a multicast
941 * solicitation to gather as many advertisements as we can
942 * before selection occurs.
944 if (first && time_after(jiffies, fip->sol_time + sol_tov))
945 fcoe_ctlr_solicit(fip, NULL);
948 * If this is the first validated FCF, note the time and
949 * set a timer to trigger selection.
951 if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
952 fip->sel_time = jiffies +
953 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
954 if (!timer_pending(&fip->timer) ||
955 time_before(fip->sel_time, fip->timer.expires))
956 mod_timer(&fip->timer, fip->sel_time);
959 mutex_unlock(&fip->ctlr_mutex);
963 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
964 * @fip: The FCoE controller which received the packet
965 * @skb: The received FIP packet
967 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
969 struct fc_lport *lport = fip->lp;
970 struct fip_header *fiph;
971 struct fc_frame *fp = (struct fc_frame *)skb;
972 struct fc_frame_header *fh = NULL;
973 struct fip_desc *desc;
974 struct fip_encaps *els;
975 struct fcoe_dev_stats *stats;
976 enum fip_desc_type els_dtype = 0;
979 u8 granted_mac[ETH_ALEN] = { 0 };
986 fiph = (struct fip_header *)skb->data;
987 sub = fiph->fip_subcode;
988 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
991 rlen = ntohs(fiph->fip_dl_len) * 4;
992 if (rlen + sizeof(*fiph) > skb->len)
995 desc = (struct fip_desc *)(fiph + 1);
998 dlen = desc->fip_dlen * FIP_BPW;
999 if (dlen < sizeof(*desc) || dlen > rlen)
1001 /* Drop ELS if there are duplicate critical descriptors */
1002 if (desc->fip_dtype < 32) {
1003 if (desc_mask & 1U << desc->fip_dtype) {
1004 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1005 "Descriptors in FIP ELS\n");
1008 desc_mask |= (1 << desc->fip_dtype);
1010 switch (desc->fip_dtype) {
1012 if (desc_cnt == 1) {
1013 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1014 "received out of order\n");
1018 if (dlen != sizeof(struct fip_mac_desc))
1021 ((struct fip_mac_desc *)desc)->fd_mac,
1028 if (desc_cnt != 1) {
1029 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1030 "received out of order\n");
1035 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1037 els_len = dlen - sizeof(*els);
1038 els = (struct fip_encaps *)desc;
1039 fh = (struct fc_frame_header *)(els + 1);
1040 els_dtype = desc->fip_dtype;
1043 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1044 "in FIP adv\n", desc->fip_dtype);
1045 /* standard says ignore unknown descriptors >= 128 */
1046 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1048 if (desc_cnt <= 2) {
1049 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1050 "received out of order\n");
1055 desc = (struct fip_desc *)((char *)desc + dlen);
1061 els_op = *(u8 *)(fh + 1);
1063 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1064 sub == FIP_SC_REP && els_op == ELS_LS_ACC &&
1065 fip->mode != FIP_MODE_VN2VN) {
1066 if (!is_valid_ether_addr(granted_mac)) {
1067 LIBFCOE_FIP_DBG(fip,
1068 "Invalid MAC address %pM in FIP ELS\n",
1072 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1074 if (fip->flogi_oxid == ntohs(fh->fh_ox_id))
1075 fip->flogi_oxid = FC_XID_UNKNOWN;
1078 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1079 (!(1U << FIP_DT_MAC & desc_mask)))) {
1080 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1086 * Convert skb into an fc_frame containing only the ELS.
1088 skb_pull(skb, (u8 *)fh - skb->data);
1089 skb_trim(skb, els_len);
1090 fp = (struct fc_frame *)skb;
1092 fr_sof(fp) = FC_SOF_I3;
1093 fr_eof(fp) = FC_EOF_T;
1095 fr_encaps(fp) = els_dtype;
1097 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1099 stats->RxWords += skb->len / FIP_BPW;
1102 fc_exch_recv(lport, fp);
1106 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1107 desc->fip_dtype, dlen);
1113 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1114 * @fip: The FCoE controller that received the frame
1115 * @fh: The received FIP header
1117 * There may be multiple VN_Port descriptors.
1118 * The overall length has already been checked.
1120 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1121 struct fip_header *fh)
1123 struct fip_desc *desc;
1124 struct fip_mac_desc *mp;
1125 struct fip_wwn_desc *wp;
1126 struct fip_vn_desc *vp;
1129 struct fcoe_fcf *fcf = fip->sel_fcf;
1130 struct fc_lport *lport = fip->lp;
1131 struct fc_lport *vn_port = NULL;
1135 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1137 if (!fcf || !lport->port_id)
1141 * mask of required descriptors. Validating each one clears its bit.
1143 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
1145 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1146 desc = (struct fip_desc *)(fh + 1);
1147 while (rlen >= sizeof(*desc)) {
1148 dlen = desc->fip_dlen * FIP_BPW;
1151 /* Drop CVL if there are duplicate critical descriptors */
1152 if ((desc->fip_dtype < 32) &&
1153 !(desc_mask & 1U << desc->fip_dtype)) {
1154 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1155 "Descriptors in FIP CVL\n");
1158 switch (desc->fip_dtype) {
1160 mp = (struct fip_mac_desc *)desc;
1161 if (dlen < sizeof(*mp))
1163 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
1165 desc_mask &= ~BIT(FIP_DT_MAC);
1168 wp = (struct fip_wwn_desc *)desc;
1169 if (dlen < sizeof(*wp))
1171 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1173 desc_mask &= ~BIT(FIP_DT_NAME);
1176 vp = (struct fip_vn_desc *)desc;
1177 if (dlen < sizeof(*vp))
1179 if (compare_ether_addr(vp->fd_mac,
1180 fip->get_src_addr(lport)) == 0 &&
1181 get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
1182 ntoh24(vp->fd_fc_id) == lport->port_id) {
1183 desc_mask &= ~BIT(FIP_DT_VN_ID);
1186 /* check if clr_vlink is for NPIV port */
1187 mutex_lock(&lport->lp_mutex);
1188 list_for_each_entry(vn_port, &lport->vports, list) {
1189 if (compare_ether_addr(vp->fd_mac,
1190 fip->get_src_addr(vn_port)) == 0 &&
1191 (get_unaligned_be64(&vp->fd_wwpn)
1192 == vn_port->wwpn) &&
1193 (ntoh24(vp->fd_fc_id) ==
1194 fc_host_port_id(vn_port->host))) {
1195 desc_mask &= ~BIT(FIP_DT_VN_ID);
1200 mutex_unlock(&lport->lp_mutex);
1204 /* standard says ignore unknown descriptors >= 128 */
1205 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1209 desc = (struct fip_desc *)((char *)desc + dlen);
1214 * reset only if all required descriptors were present and valid.
1217 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1220 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1223 fc_lport_reset(vn_port);
1225 mutex_lock(&fip->ctlr_mutex);
1226 per_cpu_ptr(lport->dev_stats,
1227 get_cpu())->VLinkFailureCount++;
1229 fcoe_ctlr_reset(fip);
1230 mutex_unlock(&fip->ctlr_mutex);
1232 fc_lport_reset(fip->lp);
1233 fcoe_ctlr_solicit(fip, NULL);
1239 * fcoe_ctlr_recv() - Receive a FIP packet
1240 * @fip: The FCoE controller that received the packet
1241 * @skb: The received FIP packet
1243 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1245 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1247 skb_queue_tail(&fip->fip_recv_list, skb);
1248 schedule_work(&fip->recv_work);
1250 EXPORT_SYMBOL(fcoe_ctlr_recv);
1253 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1254 * @fip: The FCoE controller that received the frame
1255 * @skb: The received FIP frame
1257 * Returns non-zero if the frame is dropped.
1259 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1261 struct fip_header *fiph;
1263 enum fip_state state;
1267 if (skb_linearize(skb))
1269 if (skb->len < sizeof(*fiph))
1272 if (fip->mode == FIP_MODE_VN2VN) {
1273 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1274 compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1275 compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1277 } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1278 compare_ether_addr(eh->h_dest, fcoe_all_enode))
1280 fiph = (struct fip_header *)skb->data;
1281 op = ntohs(fiph->fip_op);
1282 sub = fiph->fip_subcode;
1284 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1286 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1289 mutex_lock(&fip->ctlr_mutex);
1291 if (state == FIP_ST_AUTO) {
1293 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1294 state = FIP_ST_ENABLED;
1295 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1297 mutex_unlock(&fip->ctlr_mutex);
1299 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1300 return fcoe_ctlr_vn_recv(fip, skb);
1302 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1303 state != FIP_ST_VNMP_CLAIM)
1306 if (op == FIP_OP_LS) {
1307 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1311 if (state != FIP_ST_ENABLED)
1314 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1315 fcoe_ctlr_recv_adv(fip, skb);
1316 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1317 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1326 * fcoe_ctlr_select() - Select the best FCF (if possible)
1327 * @fip: The FCoE controller
1329 * If there are conflicting advertisements, no FCF can be chosen.
1331 * Called with lock held.
1333 static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1335 struct fcoe_fcf *fcf;
1336 struct fcoe_fcf *best = NULL;
1338 list_for_each_entry(fcf, &fip->fcfs, list) {
1339 LIBFCOE_FIP_DBG(fip, "consider FCF for fab %16.16llx "
1340 "VFID %d map %x val %d\n",
1341 fcf->fabric_name, fcf->vfid,
1342 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
1343 if (!fcoe_ctlr_fcf_usable(fcf)) {
1344 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1345 "map %x %svalid %savailable\n",
1346 fcf->fabric_name, fcf->fc_map,
1347 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1348 (fcf->flags & FIP_FL_AVAIL) ?
1356 if (fcf->fabric_name != best->fabric_name ||
1357 fcf->vfid != best->vfid ||
1358 fcf->fc_map != best->fc_map) {
1359 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1363 if (fcf->pri < best->pri)
1366 fip->sel_fcf = best;
1370 * fcoe_ctlr_timeout() - FIP timeout handler
1371 * @arg: The FCoE controller that timed out
1373 static void fcoe_ctlr_timeout(unsigned long arg)
1375 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1377 schedule_work(&fip->timer_work);
1381 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1382 * @work: Handle to a FCoE controller
1384 * Ages FCFs. Triggers FCF selection if possible.
1385 * Sends keep-alives and resets.
1387 static void fcoe_ctlr_timer_work(struct work_struct *work)
1389 struct fcoe_ctlr *fip;
1390 struct fc_lport *vport;
1393 u8 send_ctlr_ka = 0;
1394 u8 send_port_ka = 0;
1395 struct fcoe_fcf *sel;
1396 struct fcoe_fcf *fcf;
1397 unsigned long next_timer;
1399 fip = container_of(work, struct fcoe_ctlr, timer_work);
1400 if (fip->mode == FIP_MODE_VN2VN)
1401 return fcoe_ctlr_vn_timeout(fip);
1402 mutex_lock(&fip->ctlr_mutex);
1403 if (fip->state == FIP_ST_DISABLED) {
1404 mutex_unlock(&fip->ctlr_mutex);
1409 next_timer = fcoe_ctlr_age_fcfs(fip);
1412 if (!sel && fip->sel_time) {
1413 if (time_after_eq(jiffies, fip->sel_time)) {
1414 fcoe_ctlr_select(fip);
1417 } else if (time_after(next_timer, fip->sel_time))
1418 next_timer = fip->sel_time;
1422 fcf = sel; /* the old FCF may have been freed */
1424 printk(KERN_INFO "libfcoe: host%d: FIP selected "
1425 "Fibre-Channel Forwarder MAC %pM\n",
1426 fip->lp->host->host_no, sel->fcf_mac);
1427 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1429 fip->port_ka_time = jiffies +
1430 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1431 fip->ctlr_ka_time = jiffies + sel->fka_period;
1432 if (time_after(next_timer, fip->ctlr_ka_time))
1433 next_timer = fip->ctlr_ka_time;
1435 printk(KERN_NOTICE "libfcoe: host%d: "
1436 "FIP Fibre-Channel Forwarder timed out. "
1437 "Starting FCF discovery.\n",
1438 fip->lp->host->host_no);
1443 if (sel && !sel->fd_flags) {
1444 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1445 fip->ctlr_ka_time = jiffies + sel->fka_period;
1448 if (time_after(next_timer, fip->ctlr_ka_time))
1449 next_timer = fip->ctlr_ka_time;
1451 if (time_after_eq(jiffies, fip->port_ka_time)) {
1452 fip->port_ka_time = jiffies +
1453 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1456 if (time_after(next_timer, fip->port_ka_time))
1457 next_timer = fip->port_ka_time;
1459 if (!list_empty(&fip->fcfs))
1460 mod_timer(&fip->timer, next_timer);
1461 mutex_unlock(&fip->ctlr_mutex);
1464 fc_lport_reset(fip->lp);
1465 /* restart things with a solicitation */
1466 fcoe_ctlr_solicit(fip, NULL);
1470 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1473 mutex_lock(&fip->lp->lp_mutex);
1474 mac = fip->get_src_addr(fip->lp);
1475 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1476 list_for_each_entry(vport, &fip->lp->vports, list) {
1477 mac = fip->get_src_addr(vport);
1478 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1480 mutex_unlock(&fip->lp->lp_mutex);
1485 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1486 * @recv_work: Handle to a FCoE controller
1488 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1490 struct fcoe_ctlr *fip;
1491 struct sk_buff *skb;
1493 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1494 while ((skb = skb_dequeue(&fip->fip_recv_list)))
1495 fcoe_ctlr_recv_handler(fip, skb);
1499 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1500 * @fip: The FCoE controller
1501 * @fp: The FC frame to snoop
1503 * Snoop potential response to FLOGI or even incoming FLOGI.
1505 * The caller has checked that we are waiting for login as indicated
1506 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1508 * The caller is responsible for freeing the frame.
1509 * Fill in the granted_mac address.
1511 * Return non-zero if the frame should not be delivered to libfc.
1513 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1514 struct fc_frame *fp)
1516 struct fc_frame_header *fh;
1520 sa = eth_hdr(&fp->skb)->h_source;
1521 fh = fc_frame_header_get(fp);
1522 if (fh->fh_type != FC_TYPE_ELS)
1525 op = fc_frame_payload_op(fp);
1526 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1527 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1529 mutex_lock(&fip->ctlr_mutex);
1530 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1531 mutex_unlock(&fip->ctlr_mutex);
1534 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1535 LIBFCOE_FIP_DBG(fip,
1536 "received FLOGI LS_ACC using non-FIP mode\n");
1540 * If the src mac addr is FC_OUI-based, then we mark the
1541 * address_mode flag to use FC_OUI-based Ethernet DA.
1542 * Otherwise we use the FCoE gateway addr
1544 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1545 fcoe_ctlr_map_dest(fip);
1547 memcpy(fip->dest_addr, sa, ETH_ALEN);
1550 fip->flogi_oxid = FC_XID_UNKNOWN;
1551 mutex_unlock(&fip->ctlr_mutex);
1552 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1553 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1555 * Save source MAC for point-to-point responses.
1557 mutex_lock(&fip->ctlr_mutex);
1558 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1559 memcpy(fip->dest_addr, sa, ETH_ALEN);
1561 if (fip->state == FIP_ST_AUTO)
1562 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1563 "Setting non-FIP mode\n");
1564 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1566 mutex_unlock(&fip->ctlr_mutex);
1570 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1573 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1574 * @mac: The MAC address to convert
1575 * @scheme: The scheme to use when converting
1576 * @port: The port indicator for converting
1578 * Returns: u64 fc world wide name
1580 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1581 unsigned int scheme, unsigned int port)
1586 /* The MAC is in NO, so flip only the low 48 bits */
1587 host_mac = ((u64) mac[0] << 40) |
1588 ((u64) mac[1] << 32) |
1589 ((u64) mac[2] << 24) |
1590 ((u64) mac[3] << 16) |
1591 ((u64) mac[4] << 8) |
1594 WARN_ON(host_mac >= (1ULL << 48));
1595 wwn = host_mac | ((u64) scheme << 60);
1601 WARN_ON(port >= 0xfff);
1602 wwn |= (u64) port << 48;
1611 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1614 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1615 * @rdata: libfc remote port
1617 static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1619 return (struct fcoe_rport *)(rdata + 1);
1623 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1624 * @fip: The FCoE controller
1625 * @sub: sub-opcode for probe request, reply, or advertisement.
1626 * @dest: The destination Ethernet MAC address
1627 * @min_len: minimum size of the Ethernet payload to be sent
1629 static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1630 enum fip_vn2vn_subcode sub,
1631 const u8 *dest, size_t min_len)
1633 struct sk_buff *skb;
1636 struct fip_header fip;
1637 struct fip_mac_desc mac;
1638 struct fip_wwn_desc wwnn;
1639 struct fip_vn_desc vn;
1640 } __attribute__((packed)) *frame;
1641 struct fip_fc4_feat *ff;
1642 struct fip_size_desc *size;
1647 len = sizeof(*frame);
1649 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1650 dlen = sizeof(struct fip_fc4_feat) +
1651 sizeof(struct fip_size_desc);
1654 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1655 len = max(len, min_len + sizeof(struct ethhdr));
1657 skb = dev_alloc_skb(len);
1661 frame = (struct fip_frame *)skb->data;
1662 memset(frame, 0, len);
1663 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1664 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1665 frame->eth.h_proto = htons(ETH_P_FIP);
1667 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1668 frame->fip.fip_op = htons(FIP_OP_VN2VN);
1669 frame->fip.fip_subcode = sub;
1670 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1672 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1673 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1674 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1676 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1677 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1678 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1680 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
1681 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
1682 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
1683 hton24(frame->vn.fd_mac + 3, fip->port_id);
1684 hton24(frame->vn.fd_fc_id, fip->port_id);
1685 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
1688 * For claims, add FC-4 features.
1689 * TBD: Add interface to get fc-4 types and features from libfc.
1691 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1692 ff = (struct fip_fc4_feat *)(frame + 1);
1693 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
1694 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
1695 ff->fd_fts = fip->lp->fcts;
1698 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
1699 fcp_feat |= FCP_FEAT_INIT;
1700 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
1701 fcp_feat |= FCP_FEAT_TARG;
1702 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
1703 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
1705 size = (struct fip_size_desc *)(ff + 1);
1706 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
1707 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
1708 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
1712 skb->protocol = htons(ETH_P_FIP);
1713 skb_reset_mac_header(skb);
1714 skb_reset_network_header(skb);
1716 fip->send(fip, skb);
1720 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
1721 * @lport: The lport which is receiving the event
1722 * @rdata: remote port private data
1723 * @event: The event that occured
1725 * Locking Note: The rport lock must not be held when calling this function.
1727 static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
1728 struct fc_rport_priv *rdata,
1729 enum fc_rport_event event)
1731 struct fcoe_ctlr *fip = lport->disc.priv;
1732 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1734 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
1735 rdata->ids.port_id, event);
1737 mutex_lock(&fip->ctlr_mutex);
1739 case RPORT_EV_READY:
1740 frport->login_count = 0;
1743 case RPORT_EV_FAILED:
1745 frport->login_count++;
1746 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
1747 LIBFCOE_FIP_DBG(fip,
1748 "rport FLOGI limited port_id %6.6x\n",
1749 rdata->ids.port_id);
1750 lport->tt.rport_logoff(rdata);
1756 mutex_unlock(&fip->ctlr_mutex);
1759 static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
1760 .event_callback = fcoe_ctlr_vn_rport_callback,
1764 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
1765 * @fip: The FCoE controller
1767 * Called with ctlr_mutex held.
1769 static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
1771 mutex_lock(&lport->disc.disc_mutex);
1772 lport->disc.disc_callback = NULL;
1773 mutex_unlock(&lport->disc.disc_mutex);
1777 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
1778 * @fip: The FCoE controller
1780 * Called through the local port template for discovery.
1781 * Called without the ctlr_mutex held.
1783 static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
1785 struct fcoe_ctlr *fip = lport->disc.priv;
1787 mutex_lock(&fip->ctlr_mutex);
1788 fcoe_ctlr_disc_stop_locked(lport);
1789 mutex_unlock(&fip->ctlr_mutex);
1793 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
1794 * @fip: The FCoE controller
1796 * Called through the local port template for discovery.
1797 * Called without the ctlr_mutex held.
1799 static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
1801 fcoe_ctlr_disc_stop(lport);
1802 lport->tt.rport_flush_queue();
1807 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
1808 * @fip: The FCoE controller
1810 * Called with fcoe_ctlr lock held.
1812 static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
1817 fcoe_ctlr_disc_stop_locked(fip->lp);
1820 * Get proposed port ID.
1821 * If this is the first try after link up, use any previous port_id.
1822 * If there was none, use the low bits of the port_name.
1823 * On subsequent tries, get the next random one.
1824 * Don't use reserved IDs, use another non-zero value, just as random.
1826 port_id = fip->port_id;
1827 if (fip->probe_tries)
1828 port_id = prandom32(&fip->rnd_state) & 0xffff;
1830 port_id = fip->lp->wwpn & 0xffff;
1831 if (!port_id || port_id == 0xffff)
1833 fip->port_id = port_id;
1835 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
1837 wait = random32() % FIP_VN_PROBE_WAIT;
1839 wait = FIP_VN_RLIM_INT;
1840 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
1841 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
1845 * fcoe_ctlr_vn_start() - Start in VN2VN mode
1846 * @fip: The FCoE controller
1848 * Called with fcoe_ctlr lock held.
1850 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
1852 fip->probe_tries = 0;
1853 prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
1854 fcoe_ctlr_vn_restart(fip);
1858 * fcoe_ctlr_vn_parse - parse probe request or response
1859 * @fip: The FCoE controller
1860 * @skb: incoming packet
1861 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
1863 * Returns non-zero error number on error.
1864 * Does not consume the packet.
1866 static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
1867 struct sk_buff *skb,
1868 struct fc_rport_priv *rdata)
1870 struct fip_header *fiph;
1871 struct fip_desc *desc = NULL;
1872 struct fip_mac_desc *macd = NULL;
1873 struct fip_wwn_desc *wwn = NULL;
1874 struct fip_vn_desc *vn = NULL;
1875 struct fip_size_desc *size = NULL;
1876 struct fcoe_rport *frport;
1883 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
1884 frport = fcoe_ctlr_rport(rdata);
1886 fiph = (struct fip_header *)skb->data;
1887 frport->flags = ntohs(fiph->fip_flags);
1889 sub = fiph->fip_subcode;
1891 case FIP_SC_VN_PROBE_REQ:
1892 case FIP_SC_VN_PROBE_REP:
1893 case FIP_SC_VN_BEACON:
1894 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1897 case FIP_SC_VN_CLAIM_NOTIFY:
1898 case FIP_SC_VN_CLAIM_REP:
1899 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1900 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
1901 BIT(FIP_DT_FCOE_SIZE);
1904 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
1908 rlen = ntohs(fiph->fip_dl_len) * 4;
1909 if (rlen + sizeof(*fiph) > skb->len)
1912 desc = (struct fip_desc *)(fiph + 1);
1914 dlen = desc->fip_dlen * FIP_BPW;
1915 if (dlen < sizeof(*desc) || dlen > rlen)
1918 dtype = desc->fip_dtype;
1920 if (!(desc_mask & BIT(dtype))) {
1921 LIBFCOE_FIP_DBG(fip,
1922 "unexpected or duplicated desc "
1924 "FIP VN2VN subtype %u\n",
1928 desc_mask &= ~BIT(dtype);
1933 if (dlen != sizeof(struct fip_mac_desc))
1935 macd = (struct fip_mac_desc *)desc;
1936 if (!is_valid_ether_addr(macd->fd_mac)) {
1937 LIBFCOE_FIP_DBG(fip,
1938 "Invalid MAC addr %pM in FIP VN2VN\n",
1942 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
1945 if (dlen != sizeof(struct fip_wwn_desc))
1947 wwn = (struct fip_wwn_desc *)desc;
1948 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
1951 if (dlen != sizeof(struct fip_vn_desc))
1953 vn = (struct fip_vn_desc *)desc;
1954 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
1955 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
1956 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
1959 if (dlen != sizeof(struct fip_fc4_feat))
1962 case FIP_DT_FCOE_SIZE:
1963 if (dlen != sizeof(struct fip_size_desc))
1965 size = (struct fip_size_desc *)desc;
1966 frport->fcoe_len = ntohs(size->fd_size);
1969 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1970 "in FIP probe\n", dtype);
1971 /* standard says ignore unknown descriptors >= 128 */
1972 if (dtype < FIP_DT_VENDOR_BASE)
1976 desc = (struct fip_desc *)((char *)desc + dlen);
1982 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1988 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
1989 * @fip: The FCoE controller
1991 * Called with ctlr_mutex held.
1993 static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
1995 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
1996 fip->sol_time = jiffies;
2000 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2001 * @fip: The FCoE controller
2002 * @rdata: parsed remote port with frport from the probe request
2004 * Called with ctlr_mutex held.
2006 static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2007 struct fc_rport_priv *rdata)
2009 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2011 if (rdata->ids.port_id != fip->port_id)
2014 switch (fip->state) {
2015 case FIP_ST_VNMP_CLAIM:
2016 case FIP_ST_VNMP_UP:
2017 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2018 frport->enode_mac, 0);
2020 case FIP_ST_VNMP_PROBE1:
2021 case FIP_ST_VNMP_PROBE2:
2023 * Decide whether to reply to the Probe.
2024 * Our selected address is never a "recorded" one, so
2025 * only reply if our WWPN is greater and the
2026 * Probe's REC bit is not set.
2027 * If we don't reply, we will change our address.
2029 if (fip->lp->wwpn > rdata->ids.port_name &&
2030 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2031 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2032 frport->enode_mac, 0);
2036 case FIP_ST_VNMP_START:
2037 fcoe_ctlr_vn_restart(fip);
2045 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2046 * @fip: The FCoE controller
2047 * @rdata: parsed remote port with frport from the probe request
2049 * Called with ctlr_mutex held.
2051 static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2052 struct fc_rport_priv *rdata)
2054 if (rdata->ids.port_id != fip->port_id)
2056 switch (fip->state) {
2057 case FIP_ST_VNMP_START:
2058 case FIP_ST_VNMP_PROBE1:
2059 case FIP_ST_VNMP_PROBE2:
2060 case FIP_ST_VNMP_CLAIM:
2061 fcoe_ctlr_vn_restart(fip);
2063 case FIP_ST_VNMP_UP:
2064 fcoe_ctlr_vn_send_claim(fip);
2072 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2073 * @fip: The FCoE controller
2074 * @new: newly-parsed remote port with frport as a template for new rdata
2076 * Called with ctlr_mutex held.
2078 static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2080 struct fc_lport *lport = fip->lp;
2081 struct fc_rport_priv *rdata;
2082 struct fc_rport_identifiers *ids;
2083 struct fcoe_rport *frport;
2086 port_id = new->ids.port_id;
2087 if (port_id == fip->port_id)
2090 mutex_lock(&lport->disc.disc_mutex);
2091 rdata = lport->tt.rport_create(lport, port_id);
2093 mutex_unlock(&lport->disc.disc_mutex);
2097 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2098 rdata->disc_id = lport->disc.disc_id;
2101 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2102 (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2103 lport->tt.rport_logoff(rdata);
2104 ids->port_name = new->ids.port_name;
2105 ids->node_name = new->ids.node_name;
2106 mutex_unlock(&lport->disc.disc_mutex);
2108 frport = fcoe_ctlr_rport(rdata);
2109 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2110 port_id, frport->fcoe_len ? "old" : "new");
2111 *frport = *fcoe_ctlr_rport(new);
2116 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2117 * @fip: The FCoE controller
2118 * @port_id: The port_id of the remote VN_node
2119 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2121 * Returns non-zero error if no remote port found.
2123 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2125 struct fc_lport *lport = fip->lp;
2126 struct fc_rport_priv *rdata;
2127 struct fcoe_rport *frport;
2131 rdata = lport->tt.rport_lookup(lport, port_id);
2133 frport = fcoe_ctlr_rport(rdata);
2134 memcpy(mac, frport->enode_mac, ETH_ALEN);
2142 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2143 * @fip: The FCoE controller
2144 * @new: newly-parsed remote port with frport as a template for new rdata
2146 * Called with ctlr_mutex held.
2148 static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2149 struct fc_rport_priv *new)
2151 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2153 if (frport->flags & FIP_FL_REC_OR_P2P) {
2154 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2157 switch (fip->state) {
2158 case FIP_ST_VNMP_START:
2159 case FIP_ST_VNMP_PROBE1:
2160 case FIP_ST_VNMP_PROBE2:
2161 if (new->ids.port_id == fip->port_id)
2162 fcoe_ctlr_vn_restart(fip);
2164 case FIP_ST_VNMP_CLAIM:
2165 case FIP_ST_VNMP_UP:
2166 if (new->ids.port_id == fip->port_id) {
2167 if (new->ids.port_name > fip->lp->wwpn) {
2168 fcoe_ctlr_vn_restart(fip);
2171 fcoe_ctlr_vn_send_claim(fip);
2174 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2175 min((u32)frport->fcoe_len,
2176 fcoe_ctlr_fcoe_size(fip)));
2177 fcoe_ctlr_vn_add(fip, new);
2185 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2186 * @fip: The FCoE controller that received the frame
2187 * @new: newly-parsed remote port with frport from the Claim Response
2189 * Called with ctlr_mutex held.
2191 static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2192 struct fc_rport_priv *new)
2194 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2195 new->ids.port_id, fcoe_ctlr_state(fip->state));
2196 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2197 fcoe_ctlr_vn_add(fip, new);
2201 * fcoe_ctlr_vn_beacon() - handle received beacon.
2202 * @fip: The FCoE controller that received the frame
2203 * @new: newly-parsed remote port with frport from the Beacon
2205 * Called with ctlr_mutex held.
2207 static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2208 struct fc_rport_priv *new)
2210 struct fc_lport *lport = fip->lp;
2211 struct fc_rport_priv *rdata;
2212 struct fcoe_rport *frport;
2214 frport = fcoe_ctlr_rport(new);
2215 if (frport->flags & FIP_FL_REC_OR_P2P) {
2216 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2219 mutex_lock(&lport->disc.disc_mutex);
2220 rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2222 kref_get(&rdata->kref);
2223 mutex_unlock(&lport->disc.disc_mutex);
2225 if (rdata->ids.node_name == new->ids.node_name &&
2226 rdata->ids.port_name == new->ids.port_name) {
2227 frport = fcoe_ctlr_rport(rdata);
2228 if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2229 lport->tt.rport_login(rdata);
2230 frport->time = jiffies;
2232 kref_put(&rdata->kref, lport->tt.rport_destroy);
2235 if (fip->state != FIP_ST_VNMP_UP)
2239 * Beacon from a new neighbor.
2240 * Send a claim notify if one hasn't been sent recently.
2241 * Don't add the neighbor yet.
2243 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2245 if (time_after(jiffies,
2246 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2247 fcoe_ctlr_vn_send_claim(fip);
2251 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2252 * @fip: The FCoE controller
2254 * Called with ctlr_mutex held.
2255 * Called only in state FIP_ST_VNMP_UP.
2256 * Returns the soonest time for next age-out or a time far in the future.
2258 static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2260 struct fc_lport *lport = fip->lp;
2261 struct fc_rport_priv *rdata;
2262 struct fcoe_rport *frport;
2263 unsigned long next_time;
2264 unsigned long deadline;
2266 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2267 mutex_lock(&lport->disc.disc_mutex);
2268 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2269 frport = fcoe_ctlr_rport(rdata);
2272 deadline = frport->time +
2273 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2274 if (time_after_eq(jiffies, deadline)) {
2276 LIBFCOE_FIP_DBG(fip,
2277 "port %16.16llx fc_id %6.6x beacon expired\n",
2278 rdata->ids.port_name, rdata->ids.port_id);
2279 lport->tt.rport_logoff(rdata);
2280 } else if (time_before(deadline, next_time))
2281 next_time = deadline;
2283 mutex_unlock(&lport->disc.disc_mutex);
2288 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2289 * @fip: The FCoE controller that received the frame
2290 * @skb: The received FIP frame
2292 * Returns non-zero if the frame is dropped.
2293 * Always consumes the frame.
2295 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2297 struct fip_header *fiph;
2298 enum fip_vn2vn_subcode sub;
2300 struct fc_rport_priv rdata;
2301 struct fcoe_rport frport;
2305 fiph = (struct fip_header *)skb->data;
2306 sub = fiph->fip_subcode;
2308 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2310 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2314 mutex_lock(&fip->ctlr_mutex);
2316 case FIP_SC_VN_PROBE_REQ:
2317 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2319 case FIP_SC_VN_PROBE_REP:
2320 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2322 case FIP_SC_VN_CLAIM_NOTIFY:
2323 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2325 case FIP_SC_VN_CLAIM_REP:
2326 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2328 case FIP_SC_VN_BEACON:
2329 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2332 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2336 mutex_unlock(&fip->ctlr_mutex);
2343 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
2344 * @lport: The local port
2345 * @fp: The received frame
2347 * This should never be called since we don't see RSCNs or other
2348 * fabric-generated ELSes.
2350 static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
2352 struct fc_seq_els_data rjt_data;
2354 rjt_data.reason = ELS_RJT_UNSUP;
2355 rjt_data.explan = ELS_EXPL_NONE;
2356 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
2361 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2362 * @fip: The FCoE controller
2364 * This sets a flag indicating that remote ports should be created
2365 * and started for the peers we discover. We use the disc_callback
2366 * pointer as that flag. Peers already discovered are created here.
2368 * The lport lock is held during this call. The callback must be done
2369 * later, without holding either the lport or discovery locks.
2370 * The fcoe_ctlr lock may also be held during this call.
2372 static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2373 enum fc_disc_event),
2374 struct fc_lport *lport)
2376 struct fc_disc *disc = &lport->disc;
2377 struct fcoe_ctlr *fip = disc->priv;
2379 mutex_lock(&disc->disc_mutex);
2380 disc->disc_callback = callback;
2381 disc->disc_id = (disc->disc_id + 2) | 1;
2383 schedule_work(&fip->timer_work);
2384 mutex_unlock(&disc->disc_mutex);
2388 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2389 * @fip: The FCoE controller
2391 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2392 * we've received at least one beacon.
2393 * Performs the discovery complete callback.
2395 static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2397 struct fc_lport *lport = fip->lp;
2398 struct fc_disc *disc = &lport->disc;
2399 struct fc_rport_priv *rdata;
2400 struct fcoe_rport *frport;
2401 void (*callback)(struct fc_lport *, enum fc_disc_event);
2403 mutex_lock(&disc->disc_mutex);
2404 callback = disc->pending ? disc->disc_callback : NULL;
2406 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2407 frport = fcoe_ctlr_rport(rdata);
2409 lport->tt.rport_login(rdata);
2411 mutex_unlock(&disc->disc_mutex);
2413 callback(lport, DISC_EV_SUCCESS);
2417 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2418 * @fip: The FCoE controller
2420 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2422 unsigned long next_time;
2424 u32 new_port_id = 0;
2426 mutex_lock(&fip->ctlr_mutex);
2427 switch (fip->state) {
2428 case FIP_ST_VNMP_START:
2429 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2430 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2431 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2433 case FIP_ST_VNMP_PROBE1:
2434 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2435 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2436 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2438 case FIP_ST_VNMP_PROBE2:
2439 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2440 new_port_id = fip->port_id;
2441 hton24(mac, FIP_VN_FC_MAP);
2442 hton24(mac + 3, new_port_id);
2443 fcoe_ctlr_map_dest(fip);
2444 fip->update_mac(fip->lp, mac);
2445 fcoe_ctlr_vn_send_claim(fip);
2446 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2448 case FIP_ST_VNMP_CLAIM:
2450 * This may be invoked either by starting discovery so don't
2451 * go to the next state unless it's been long enough.
2453 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2454 if (time_after_eq(jiffies, next_time)) {
2455 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2456 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2458 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2459 fip->port_ka_time = next_time;
2461 fcoe_ctlr_vn_disc(fip);
2463 case FIP_ST_VNMP_UP:
2464 next_time = fcoe_ctlr_vn_age(fip);
2465 if (time_after_eq(jiffies, fip->port_ka_time)) {
2466 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2468 fip->port_ka_time = jiffies +
2469 msecs_to_jiffies(FIP_VN_BEACON_INT +
2470 (random32() % FIP_VN_BEACON_FUZZ));
2472 if (time_before(fip->port_ka_time, next_time))
2473 next_time = fip->port_ka_time;
2475 case FIP_ST_LINK_WAIT:
2478 WARN(1, "unexpected state %d", fip->state);
2481 mod_timer(&fip->timer, next_time);
2483 mutex_unlock(&fip->ctlr_mutex);
2485 /* If port ID is new, notify local port after dropping ctlr_mutex */
2487 fc_lport_set_local_id(fip->lp, new_port_id);
2491 * fcoe_libfc_config() - Sets up libfc related properties for local port
2492 * @lp: The local port to configure libfc for
2493 * @fip: The FCoE controller in use by the local port
2494 * @tt: The libfc function template
2495 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
2497 * Returns : 0 for success
2499 int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2500 const struct libfc_function_template *tt, int init_fcp)
2502 /* Set the function pointers set by the LLDD */
2503 memcpy(&lport->tt, tt, sizeof(*tt));
2504 if (init_fcp && fc_fcp_init(lport))
2506 fc_exch_init(lport);
2507 fc_elsct_init(lport);
2508 fc_lport_init(lport);
2509 if (fip->mode == FIP_MODE_VN2VN)
2510 lport->rport_priv_size = sizeof(struct fcoe_rport);
2511 fc_rport_init(lport);
2512 if (fip->mode == FIP_MODE_VN2VN) {
2513 lport->point_to_multipoint = 1;
2514 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2515 lport->tt.disc_start = fcoe_ctlr_disc_start;
2516 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2517 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2518 mutex_init(&lport->disc.disc_mutex);
2519 INIT_LIST_HEAD(&lport->disc.rports);
2520 lport->disc.priv = fip;
2522 fc_disc_init(lport);
2526 EXPORT_SYMBOL_GPL(fcoe_libfc_config);