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>
49 #define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
50 #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
52 static void fcoe_ctlr_timeout(struct timer_list *);
53 static void fcoe_ctlr_timer_work(struct work_struct *);
54 static void fcoe_ctlr_recv_work(struct work_struct *);
55 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
57 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
58 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
59 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
60 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
62 static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *, struct sk_buff *);
64 static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
65 static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
66 static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
67 static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
69 static const char * const fcoe_ctlr_states[] = {
70 [FIP_ST_DISABLED] = "DISABLED",
71 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
72 [FIP_ST_AUTO] = "AUTO",
73 [FIP_ST_NON_FIP] = "NON_FIP",
74 [FIP_ST_ENABLED] = "ENABLED",
75 [FIP_ST_VNMP_START] = "VNMP_START",
76 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
77 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
78 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
79 [FIP_ST_VNMP_UP] = "VNMP_UP",
82 static const char *fcoe_ctlr_state(enum fip_state state)
84 const char *cp = "unknown";
86 if (state < ARRAY_SIZE(fcoe_ctlr_states))
87 cp = fcoe_ctlr_states[state];
94 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
95 * @fip: The FCoE controller
96 * @state: The new state
98 static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
100 if (state == fip->state)
103 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
104 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
109 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
110 * @fcf: The FCF to check
112 * Return non-zero if FCF fcoe_size has been validated.
114 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
116 return (fcf->flags & FIP_FL_SOL) != 0;
120 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
121 * @fcf: The FCF to check
123 * Return non-zero if the FCF is usable.
125 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
127 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
129 return (fcf->flags & flags) == flags;
133 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
134 * @fip: The FCoE controller
136 static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
138 if (fip->mode == FIP_MODE_VN2VN)
139 hton24(fip->dest_addr, FIP_VN_FC_MAP);
141 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
142 hton24(fip->dest_addr + 3, 0);
147 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
148 * @fip: The FCoE controller to initialize
150 void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
152 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
154 fip->fip_resp = false;
155 INIT_LIST_HEAD(&fip->fcfs);
156 mutex_init(&fip->ctlr_mutex);
157 spin_lock_init(&fip->ctlr_lock);
158 fip->flogi_oxid = FC_XID_UNKNOWN;
159 timer_setup(&fip->timer, fcoe_ctlr_timeout, 0);
160 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
161 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
162 skb_queue_head_init(&fip->fip_recv_list);
164 EXPORT_SYMBOL(fcoe_ctlr_init);
167 * fcoe_sysfs_fcf_add() - Add a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
168 * @new: The newly discovered FCF
170 * Called with fip->ctlr_mutex held
172 static int fcoe_sysfs_fcf_add(struct fcoe_fcf *new)
174 struct fcoe_ctlr *fip = new->fip;
175 struct fcoe_ctlr_device *ctlr_dev;
176 struct fcoe_fcf_device *temp, *fcf_dev;
179 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
180 new->fabric_name, new->fcf_mac);
182 temp = kzalloc(sizeof(*temp), GFP_KERNEL);
186 temp->fabric_name = new->fabric_name;
187 temp->switch_name = new->switch_name;
188 temp->fc_map = new->fc_map;
189 temp->vfid = new->vfid;
190 memcpy(temp->mac, new->fcf_mac, ETH_ALEN);
191 temp->priority = new->pri;
192 temp->fka_period = new->fka_period;
193 temp->selected = 0; /* default to unselected */
196 * If ctlr_dev doesn't exist then it means we're a libfcoe user
197 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device.
198 * fnic would be an example of a driver with this behavior. In this
199 * case we want to add the fcoe_fcf to the fcoe_ctlr list, but we
200 * don't want to make sysfs changes.
203 ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
205 mutex_lock(&ctlr_dev->lock);
206 fcf_dev = fcoe_fcf_device_add(ctlr_dev, temp);
207 if (unlikely(!fcf_dev)) {
209 mutex_unlock(&ctlr_dev->lock);
214 * The fcoe_sysfs layer can return a CONNECTED fcf that
215 * has a priv (fcf was never deleted) or a CONNECTED fcf
216 * that doesn't have a priv (fcf was deleted). However,
217 * libfcoe will always delete FCFs before trying to add
218 * them. This is ensured because both recv_adv and
219 * age_fcfs are protected by the the fcoe_ctlr's mutex.
220 * This means that we should never get a FCF with a
221 * non-NULL priv pointer.
223 BUG_ON(fcf_dev->priv);
226 new->fcf_dev = fcf_dev;
227 mutex_unlock(&ctlr_dev->lock);
230 list_add(&new->list, &fip->fcfs);
240 * fcoe_sysfs_fcf_del() - Remove a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
241 * @new: The FCF to be removed
243 * Called with fip->ctlr_mutex held
245 static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new)
247 struct fcoe_ctlr *fip = new->fip;
248 struct fcoe_ctlr_device *cdev;
249 struct fcoe_fcf_device *fcf_dev;
251 list_del(&new->list);
255 * If ctlr_dev doesn't exist then it means we're a libfcoe user
256 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device
257 * or a fcoe_fcf_device.
259 * fnic would be an example of a driver with this behavior. In this
260 * case we want to remove the fcoe_fcf from the fcoe_ctlr list (above),
261 * but we don't want to make sysfs changes.
263 cdev = fcoe_ctlr_to_ctlr_dev(fip);
265 mutex_lock(&cdev->lock);
266 fcf_dev = fcoe_fcf_to_fcf_dev(new);
269 fcoe_fcf_device_delete(fcf_dev);
271 mutex_unlock(&cdev->lock);
276 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
277 * @fip: The FCoE controller whose FCFs are to be reset
279 * Called with &fcoe_ctlr lock held.
281 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
283 struct fcoe_fcf *fcf;
284 struct fcoe_fcf *next;
287 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
288 fcoe_sysfs_fcf_del(fcf);
290 WARN_ON(fip->fcf_count);
296 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
297 * @fip: The FCoE controller to tear down
299 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
301 * The receive handler will have been deleted before this to guarantee
302 * that no more recv_work will be scheduled.
304 * The timer routine will simply return once we set FIP_ST_DISABLED.
305 * This guarantees that no further timeouts or work will be scheduled.
307 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
309 cancel_work_sync(&fip->recv_work);
310 skb_queue_purge(&fip->fip_recv_list);
312 mutex_lock(&fip->ctlr_mutex);
313 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
314 fcoe_ctlr_reset_fcfs(fip);
315 mutex_unlock(&fip->ctlr_mutex);
316 del_timer_sync(&fip->timer);
317 cancel_work_sync(&fip->timer_work);
319 EXPORT_SYMBOL(fcoe_ctlr_destroy);
322 * fcoe_ctlr_announce() - announce new FCF selection
323 * @fip: The FCoE controller
325 * Also sets the destination MAC for FCoE and control packets
327 * Called with neither ctlr_mutex nor ctlr_lock held.
329 static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
331 struct fcoe_fcf *sel;
332 struct fcoe_fcf *fcf;
334 mutex_lock(&fip->ctlr_mutex);
335 spin_lock_bh(&fip->ctlr_lock);
337 kfree_skb(fip->flogi_req);
338 fip->flogi_req = NULL;
339 list_for_each_entry(fcf, &fip->fcfs, list)
342 spin_unlock_bh(&fip->ctlr_lock);
345 if (sel && ether_addr_equal(sel->fcf_mac, fip->dest_addr))
347 if (!is_zero_ether_addr(fip->dest_addr)) {
348 printk(KERN_NOTICE "libfcoe: host%d: "
349 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
350 fip->lp->host->host_no, fip->dest_addr);
351 memset(fip->dest_addr, 0, ETH_ALEN);
354 printk(KERN_INFO "libfcoe: host%d: FIP selected "
355 "Fibre-Channel Forwarder MAC %pM\n",
356 fip->lp->host->host_no, sel->fcf_mac);
357 memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
361 mutex_unlock(&fip->ctlr_mutex);
365 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
366 * @fip: The FCoE controller to get the maximum FCoE size from
368 * Returns the maximum packet size including the FCoE header and trailer,
369 * but not including any Ethernet or VLAN headers.
371 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
374 * Determine the max FCoE frame size allowed, including
375 * FCoE header and trailer.
376 * Note: lp->mfs is currently the payload size, not the frame size.
378 return fip->lp->mfs + sizeof(struct fc_frame_header) +
379 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
383 * fcoe_ctlr_solicit() - Send a FIP solicitation
384 * @fip: The FCoE controller to send the solicitation on
385 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
387 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
392 struct fip_header fip;
394 struct fip_mac_desc mac;
395 struct fip_wwn_desc wwnn;
396 struct fip_size_desc size;
401 skb = dev_alloc_skb(sizeof(*sol));
405 sol = (struct fip_sol *)skb->data;
407 memset(sol, 0, sizeof(*sol));
408 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
409 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
410 sol->eth.h_proto = htons(ETH_P_FIP);
412 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
413 sol->fip.fip_op = htons(FIP_OP_DISC);
414 sol->fip.fip_subcode = FIP_SC_SOL;
415 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
416 sol->fip.fip_flags = htons(FIP_FL_FPMA);
418 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
420 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
421 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
422 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
424 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
425 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
426 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
428 fcoe_size = fcoe_ctlr_fcoe_size(fip);
429 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
430 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
431 sol->desc.size.fd_size = htons(fcoe_size);
433 skb_put(skb, sizeof(*sol));
434 skb->protocol = htons(ETH_P_FIP);
435 skb->priority = fip->priority;
436 skb_reset_mac_header(skb);
437 skb_reset_network_header(skb);
441 fip->sol_time = jiffies;
445 * fcoe_ctlr_link_up() - Start FCoE controller
446 * @fip: The FCoE controller to start
448 * Called from the LLD when the network link is ready.
450 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
452 mutex_lock(&fip->ctlr_mutex);
453 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
454 mutex_unlock(&fip->ctlr_mutex);
456 } else if (fip->state == FIP_ST_LINK_WAIT) {
457 fcoe_ctlr_set_state(fip, fip->mode);
460 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
463 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
465 case FIP_MODE_FABRIC:
466 case FIP_MODE_NON_FIP:
467 mutex_unlock(&fip->ctlr_mutex);
469 fcoe_ctlr_solicit(fip, NULL);
472 fcoe_ctlr_vn_start(fip);
473 mutex_unlock(&fip->ctlr_mutex);
478 mutex_unlock(&fip->ctlr_mutex);
480 EXPORT_SYMBOL(fcoe_ctlr_link_up);
483 * fcoe_ctlr_reset() - Reset a FCoE controller
484 * @fip: The FCoE controller to reset
486 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
488 fcoe_ctlr_reset_fcfs(fip);
489 del_timer(&fip->timer);
490 fip->ctlr_ka_time = 0;
491 fip->port_ka_time = 0;
493 fip->flogi_oxid = FC_XID_UNKNOWN;
494 fcoe_ctlr_map_dest(fip);
498 * fcoe_ctlr_link_down() - Stop a FCoE controller
499 * @fip: The FCoE controller to be stopped
501 * Returns non-zero if the link was up and now isn't.
503 * Called from the LLD when the network link is not ready.
504 * There may be multiple calls while the link is down.
506 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
510 LIBFCOE_FIP_DBG(fip, "link down.\n");
511 mutex_lock(&fip->ctlr_mutex);
512 fcoe_ctlr_reset(fip);
513 link_dropped = fip->state != FIP_ST_LINK_WAIT;
514 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
515 mutex_unlock(&fip->ctlr_mutex);
518 fc_linkdown(fip->lp);
521 EXPORT_SYMBOL(fcoe_ctlr_link_down);
524 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
525 * @fip: The FCoE controller to send the FKA on
526 * @lport: libfc fc_lport to send from
527 * @ports: 0 for controller keep-alive, 1 for port keep-alive
528 * @sa: The source MAC address
530 * A controller keep-alive is sent every fka_period (typically 8 seconds).
531 * The source MAC is the native MAC address.
533 * A port keep-alive is sent every 90 seconds while logged in.
534 * The source MAC is the assigned mapped source address.
535 * The destination is the FCF's F-port.
537 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
538 struct fc_lport *lport,
544 struct fip_header fip;
545 struct fip_mac_desc mac;
547 struct fip_vn_desc *vn;
550 struct fcoe_fcf *fcf;
554 if (!fcf || (ports && !lp->port_id))
557 len = sizeof(*kal) + ports * sizeof(*vn);
558 skb = dev_alloc_skb(len);
562 kal = (struct fip_kal *)skb->data;
564 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
565 memcpy(kal->eth.h_source, sa, ETH_ALEN);
566 kal->eth.h_proto = htons(ETH_P_FIP);
568 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
569 kal->fip.fip_op = htons(FIP_OP_CTRL);
570 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
571 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
572 ports * sizeof(*vn)) / FIP_BPW);
573 kal->fip.fip_flags = htons(FIP_FL_FPMA);
575 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
577 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
578 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
579 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
581 vn = (struct fip_vn_desc *)(kal + 1);
582 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
583 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
584 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
585 hton24(vn->fd_fc_id, lport->port_id);
586 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
589 skb->protocol = htons(ETH_P_FIP);
590 skb->priority = fip->priority;
591 skb_reset_mac_header(skb);
592 skb_reset_network_header(skb);
597 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
598 * @fip: The FCoE controller for the ELS frame
599 * @dtype: The FIP descriptor type for the frame
600 * @skb: The FCoE ELS frame including FC header but no FCoE headers
601 * @d_id: The destination port ID.
603 * Returns non-zero error code on failure.
605 * The caller must check that the length is a multiple of 4.
607 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
608 * Headroom includes the FIP encapsulation description, FIP header, and
609 * Ethernet header. The tailroom is for the FIP MAC descriptor.
611 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
612 u8 dtype, struct sk_buff *skb, u32 d_id)
614 struct fip_encaps_head {
616 struct fip_header fip;
617 struct fip_encaps encaps;
619 struct fc_frame_header *fh;
620 struct fip_mac_desc *mac;
621 struct fcoe_fcf *fcf;
626 fh = (struct fc_frame_header *)skb->data;
627 op = *(u8 *)(fh + 1);
628 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
629 cap = skb_push(skb, sizeof(*cap));
630 memset(cap, 0, sizeof(*cap));
632 if (lport->point_to_multipoint) {
633 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
640 fip_flags = fcf->flags;
641 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
645 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
647 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
648 cap->eth.h_proto = htons(ETH_P_FIP);
650 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
651 cap->fip.fip_op = htons(FIP_OP_LS);
652 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
653 cap->fip.fip_subcode = FIP_SC_REP;
655 cap->fip.fip_subcode = FIP_SC_REQ;
656 cap->fip.fip_flags = htons(fip_flags);
658 cap->encaps.fd_desc.fip_dtype = dtype;
659 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
661 if (op != ELS_LS_RJT) {
662 dlen += sizeof(*mac);
663 mac = skb_put_zero(skb, sizeof(*mac));
664 mac->fd_desc.fip_dtype = FIP_DT_MAC;
665 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
666 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
667 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
668 } else if (fip->mode == FIP_MODE_VN2VN) {
669 hton24(mac->fd_mac, FIP_VN_FC_MAP);
670 hton24(mac->fd_mac + 3, fip->port_id);
671 } else if (fip_flags & FIP_FL_SPMA) {
672 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
673 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
675 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
676 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
679 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
681 skb->protocol = htons(ETH_P_FIP);
682 skb->priority = fip->priority;
683 skb_reset_mac_header(skb);
684 skb_reset_network_header(skb);
689 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
690 * @fip: FCoE controller.
691 * @lport: libfc fc_lport to send from
692 * @skb: FCoE ELS frame including FC header but no FCoE headers.
694 * Returns a non-zero error code if the frame should not be sent.
695 * Returns zero if the caller should send the frame with FCoE encapsulation.
697 * The caller must check that the length is a multiple of 4.
698 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
699 * The the skb must also be an fc_frame.
701 * This is called from the lower-level driver with spinlocks held,
702 * so we must not take a mutex here.
704 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
708 struct fc_frame_header *fh;
713 fp = container_of(skb, struct fc_frame, skb);
714 fh = (struct fc_frame_header *)skb->data;
715 op = *(u8 *)(fh + 1);
717 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
718 old_xid = fip->flogi_oxid;
719 fip->flogi_oxid = ntohs(fh->fh_ox_id);
720 if (fip->state == FIP_ST_AUTO) {
721 if (old_xid == FC_XID_UNKNOWN)
722 fip->flogi_count = 0;
724 if (fip->flogi_count < 3)
726 fcoe_ctlr_map_dest(fip);
729 if (fip->state == FIP_ST_NON_FIP)
730 fcoe_ctlr_map_dest(fip);
733 if (fip->state == FIP_ST_NON_FIP)
735 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
740 if (fip->mode == FIP_MODE_VN2VN)
742 spin_lock_bh(&fip->ctlr_lock);
743 kfree_skb(fip->flogi_req);
744 fip->flogi_req = skb;
745 fip->flogi_req_send = 1;
746 spin_unlock_bh(&fip->ctlr_lock);
747 schedule_work(&fip->timer_work);
750 if (ntoh24(fh->fh_s_id))
755 if (fip->mode == FIP_MODE_VN2VN) {
756 if (fip->state != FIP_ST_VNMP_UP)
758 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
761 if (fip->state != FIP_ST_ENABLED)
763 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
770 * If non-FIP, we may have gotten an SID by accepting an FLOGI
771 * from a point-to-point connection. Switch to using
772 * the source mac based on the SID. The destination
773 * MAC in this case would have been set by receiving the
776 if (fip->state == FIP_ST_NON_FIP) {
777 if (fip->flogi_oxid == FC_XID_UNKNOWN)
779 fip->flogi_oxid = FC_XID_UNKNOWN;
780 fc_fcoe_set_mac(mac, fh->fh_d_id);
781 fip->update_mac(lport, mac);
790 if (fip->state != FIP_ST_ENABLED &&
791 fip->state != FIP_ST_VNMP_UP)
795 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
796 op, ntoh24(fh->fh_d_id));
797 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
802 LIBFCOE_FIP_DBG(fip, "drop els_send op %u d_id %x\n",
803 op, ntoh24(fh->fh_d_id));
807 EXPORT_SYMBOL(fcoe_ctlr_els_send);
810 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
811 * @fip: The FCoE controller to free FCFs on
813 * Called with lock held and preemption disabled.
815 * An FCF is considered old if we have missed two advertisements.
816 * That is, there have been no valid advertisement from it for 2.5
817 * times its keep-alive period.
819 * In addition, determine the time when an FCF selection can occur.
821 * Also, increment the MissDiscAdvCount when no advertisement is received
822 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
824 * Returns the time in jiffies for the next call.
826 static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
828 struct fcoe_fcf *fcf;
829 struct fcoe_fcf *next;
830 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
831 unsigned long deadline;
832 unsigned long sel_time = 0;
833 struct list_head del_list;
834 struct fc_stats *stats;
836 INIT_LIST_HEAD(&del_list);
838 stats = per_cpu_ptr(fip->lp->stats, get_cpu());
840 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
841 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
842 if (fip->sel_fcf == fcf) {
843 if (time_after(jiffies, deadline)) {
844 stats->MissDiscAdvCount++;
845 printk(KERN_INFO "libfcoe: host%d: "
846 "Missing Discovery Advertisement "
847 "for fab %16.16llx count %lld\n",
848 fip->lp->host->host_no, fcf->fabric_name,
849 stats->MissDiscAdvCount);
850 } else if (time_after(next_timer, deadline))
851 next_timer = deadline;
854 deadline += fcf->fka_period;
855 if (time_after_eq(jiffies, deadline)) {
856 if (fip->sel_fcf == fcf)
859 * Move to delete list so we can call
860 * fcoe_sysfs_fcf_del (which can sleep)
861 * after the put_cpu().
863 list_del(&fcf->list);
864 list_add(&fcf->list, &del_list);
865 stats->VLinkFailureCount++;
867 if (time_after(next_timer, deadline))
868 next_timer = deadline;
869 if (fcoe_ctlr_mtu_valid(fcf) &&
870 (!sel_time || time_before(sel_time, fcf->time)))
871 sel_time = fcf->time;
876 list_for_each_entry_safe(fcf, next, &del_list, list) {
877 /* Removes fcf from current list */
878 fcoe_sysfs_fcf_del(fcf);
881 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
882 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
883 fip->sel_time = sel_time;
890 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
891 * @fip: The FCoE controller receiving the advertisement
892 * @skb: The received FIP advertisement frame
893 * @fcf: The resulting FCF entry
895 * Returns zero on a valid parsed advertisement,
896 * otherwise returns non zero value.
898 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
899 struct sk_buff *skb, struct fcoe_fcf *fcf)
901 struct fip_header *fiph;
902 struct fip_desc *desc = NULL;
903 struct fip_wwn_desc *wwn;
904 struct fip_fab_desc *fab;
905 struct fip_fka_desc *fka;
911 memset(fcf, 0, sizeof(*fcf));
912 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
914 fiph = (struct fip_header *)skb->data;
915 fcf->flags = ntohs(fiph->fip_flags);
918 * mask of required descriptors. validating each one clears its bit.
920 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
921 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
923 rlen = ntohs(fiph->fip_dl_len) * 4;
924 if (rlen + sizeof(*fiph) > skb->len)
927 desc = (struct fip_desc *)(fiph + 1);
929 dlen = desc->fip_dlen * FIP_BPW;
930 if (dlen < sizeof(*desc) || dlen > rlen)
932 /* Drop Adv if there are duplicate critical descriptors */
933 if ((desc->fip_dtype < 32) &&
934 !(desc_mask & 1U << desc->fip_dtype)) {
935 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
936 "Descriptors in FIP adv\n");
939 switch (desc->fip_dtype) {
941 if (dlen != sizeof(struct fip_pri_desc))
943 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
944 desc_mask &= ~BIT(FIP_DT_PRI);
947 if (dlen != sizeof(struct fip_mac_desc))
950 ((struct fip_mac_desc *)desc)->fd_mac,
952 memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
953 if (!is_valid_ether_addr(fcf->fcf_mac)) {
955 "Invalid MAC addr %pM in FIP adv\n",
959 desc_mask &= ~BIT(FIP_DT_MAC);
962 if (dlen != sizeof(struct fip_wwn_desc))
964 wwn = (struct fip_wwn_desc *)desc;
965 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
966 desc_mask &= ~BIT(FIP_DT_NAME);
969 if (dlen != sizeof(struct fip_fab_desc))
971 fab = (struct fip_fab_desc *)desc;
972 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
973 fcf->vfid = ntohs(fab->fd_vfid);
974 fcf->fc_map = ntoh24(fab->fd_map);
975 desc_mask &= ~BIT(FIP_DT_FAB);
978 if (dlen != sizeof(struct fip_fka_desc))
980 fka = (struct fip_fka_desc *)desc;
981 if (fka->fd_flags & FIP_FKA_ADV_D)
983 t = ntohl(fka->fd_fka_period);
984 if (t >= FCOE_CTLR_MIN_FKA)
985 fcf->fka_period = msecs_to_jiffies(t);
986 desc_mask &= ~BIT(FIP_DT_FKA);
989 case FIP_DT_FCOE_SIZE:
995 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
996 "in FIP adv\n", desc->fip_dtype);
997 /* standard says ignore unknown descriptors >= 128 */
998 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1002 desc = (struct fip_desc *)((char *)desc + dlen);
1005 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
1007 if (!fcf->switch_name)
1010 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
1017 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1018 desc->fip_dtype, dlen);
1023 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
1024 * @fip: The FCoE controller receiving the advertisement
1025 * @skb: The received FIP packet
1027 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1029 struct fcoe_fcf *fcf;
1030 struct fcoe_fcf new;
1031 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
1037 if (fcoe_ctlr_parse_adv(fip, skb, &new))
1040 mutex_lock(&fip->ctlr_mutex);
1041 first = list_empty(&fip->fcfs);
1042 list_for_each_entry(fcf, &fip->fcfs, list) {
1043 if (fcf->switch_name == new.switch_name &&
1044 fcf->fabric_name == new.fabric_name &&
1045 fcf->fc_map == new.fc_map &&
1046 ether_addr_equal(fcf->fcf_mac, new.fcf_mac)) {
1052 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
1055 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
1059 memcpy(fcf, &new, sizeof(new));
1061 rc = fcoe_sysfs_fcf_add(fcf);
1063 printk(KERN_ERR "Failed to allocate sysfs instance "
1064 "for FCF, fab %16.16llx mac %pM\n",
1065 new.fabric_name, new.fcf_mac);
1071 * Update the FCF's keep-alive descriptor flags.
1072 * Other flag changes from new advertisements are
1073 * ignored after a solicited advertisement is
1074 * received and the FCF is selectable (usable).
1076 fcf->fd_flags = new.fd_flags;
1077 if (!fcoe_ctlr_fcf_usable(fcf))
1078 fcf->flags = new.flags;
1080 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
1081 fip->ctlr_ka_time -= fcf->fka_period;
1082 fip->ctlr_ka_time += new.fka_period;
1083 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1084 mod_timer(&fip->timer, fip->ctlr_ka_time);
1086 fcf->fka_period = new.fka_period;
1087 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
1090 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
1091 fcf->time = jiffies;
1093 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
1094 fcf->fabric_name, fcf->fcf_mac);
1097 * If this advertisement is not solicited and our max receive size
1098 * hasn't been verified, send a solicited advertisement.
1101 fcoe_ctlr_solicit(fip, fcf);
1104 * If its been a while since we did a solicit, and this is
1105 * the first advertisement we've received, do a multicast
1106 * solicitation to gather as many advertisements as we can
1107 * before selection occurs.
1109 if (first && time_after(jiffies, fip->sol_time + sol_tov))
1110 fcoe_ctlr_solicit(fip, NULL);
1113 * Put this FCF at the head of the list for priority among equals.
1114 * This helps in the case of an NPV switch which insists we use
1115 * the FCF that answers multicast solicitations, not the others that
1116 * are sending periodic multicast advertisements.
1119 list_move(&fcf->list, &fip->fcfs);
1122 * If this is the first validated FCF, note the time and
1123 * set a timer to trigger selection.
1125 if (mtu_valid && !fip->sel_fcf && !fip->sel_time &&
1126 fcoe_ctlr_fcf_usable(fcf)) {
1127 fip->sel_time = jiffies +
1128 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1129 if (!timer_pending(&fip->timer) ||
1130 time_before(fip->sel_time, fip->timer.expires))
1131 mod_timer(&fip->timer, fip->sel_time);
1135 mutex_unlock(&fip->ctlr_mutex);
1139 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1140 * @fip: The FCoE controller which received the packet
1141 * @skb: The received FIP packet
1143 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1145 struct fc_lport *lport = fip->lp;
1146 struct fip_header *fiph;
1147 struct fc_frame *fp = (struct fc_frame *)skb;
1148 struct fc_frame_header *fh = NULL;
1149 struct fip_desc *desc;
1150 struct fip_encaps *els;
1151 struct fcoe_fcf *sel;
1152 struct fc_stats *stats;
1153 enum fip_desc_type els_dtype = 0;
1156 u8 granted_mac[ETH_ALEN] = { 0 };
1163 fiph = (struct fip_header *)skb->data;
1164 sub = fiph->fip_subcode;
1165 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1168 rlen = ntohs(fiph->fip_dl_len) * 4;
1169 if (rlen + sizeof(*fiph) > skb->len)
1172 desc = (struct fip_desc *)(fiph + 1);
1175 dlen = desc->fip_dlen * FIP_BPW;
1176 if (dlen < sizeof(*desc) || dlen > rlen)
1178 /* Drop ELS if there are duplicate critical descriptors */
1179 if (desc->fip_dtype < 32) {
1180 if ((desc->fip_dtype != FIP_DT_MAC) &&
1181 (desc_mask & 1U << desc->fip_dtype)) {
1182 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1183 "Descriptors in FIP ELS\n");
1186 desc_mask |= (1 << desc->fip_dtype);
1188 switch (desc->fip_dtype) {
1191 if (desc_cnt == 1) {
1192 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1193 "received out of order\n");
1197 * Some switch implementations send two MAC descriptors,
1198 * with first MAC(granted_mac) being the FPMA, and the
1199 * second one(fcoe_mac) is used as destination address
1200 * for sending/receiving FCoE packets. FIP traffic is
1201 * sent using fip_mac. For regular switches, both
1202 * fip_mac and fcoe_mac would be the same.
1206 ((struct fip_mac_desc *)desc)->fd_mac,
1209 if (dlen != sizeof(struct fip_mac_desc))
1212 if ((desc_cnt == 3) && (sel))
1213 memcpy(sel->fcoe_mac,
1214 ((struct fip_mac_desc *)desc)->fd_mac,
1221 if (desc_cnt != 1) {
1222 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1223 "received out of order\n");
1228 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1230 els_len = dlen - sizeof(*els);
1231 els = (struct fip_encaps *)desc;
1232 fh = (struct fc_frame_header *)(els + 1);
1233 els_dtype = desc->fip_dtype;
1236 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1237 "in FIP adv\n", desc->fip_dtype);
1238 /* standard says ignore unknown descriptors >= 128 */
1239 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1241 if (desc_cnt <= 2) {
1242 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1243 "received out of order\n");
1248 desc = (struct fip_desc *)((char *)desc + dlen);
1254 els_op = *(u8 *)(fh + 1);
1256 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1257 sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1258 if (els_op == ELS_LS_ACC) {
1259 if (!is_valid_ether_addr(granted_mac)) {
1260 LIBFCOE_FIP_DBG(fip,
1261 "Invalid MAC address %pM in FIP ELS\n",
1265 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1267 if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1268 fip->flogi_oxid = FC_XID_UNKNOWN;
1269 if (els_dtype == FIP_DT_FLOGI)
1270 fcoe_ctlr_announce(fip);
1272 } else if (els_dtype == FIP_DT_FLOGI &&
1273 !fcoe_ctlr_flogi_retry(fip))
1274 goto drop; /* retrying FLOGI so drop reject */
1277 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1278 (!(1U << FIP_DT_MAC & desc_mask)))) {
1279 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1285 * Convert skb into an fc_frame containing only the ELS.
1287 skb_pull(skb, (u8 *)fh - skb->data);
1288 skb_trim(skb, els_len);
1289 fp = (struct fc_frame *)skb;
1291 fr_sof(fp) = FC_SOF_I3;
1292 fr_eof(fp) = FC_EOF_T;
1294 fr_encaps(fp) = els_dtype;
1296 stats = per_cpu_ptr(lport->stats, get_cpu());
1298 stats->RxWords += skb->len / FIP_BPW;
1301 fc_exch_recv(lport, fp);
1305 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1306 desc->fip_dtype, dlen);
1312 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1313 * @fip: The FCoE controller that received the frame
1314 * @fh: The received FIP header
1316 * There may be multiple VN_Port descriptors.
1317 * The overall length has already been checked.
1319 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1320 struct sk_buff *skb)
1322 struct fip_desc *desc;
1323 struct fip_mac_desc *mp;
1324 struct fip_wwn_desc *wp;
1325 struct fip_vn_desc *vp;
1328 struct fcoe_fcf *fcf = fip->sel_fcf;
1329 struct fc_lport *lport = fip->lp;
1330 struct fc_lport *vn_port = NULL;
1333 int reset_phys_port = 0;
1334 struct fip_vn_desc **vlink_desc_arr = NULL;
1335 struct fip_header *fh = (struct fip_header *)skb->data;
1336 struct ethhdr *eh = eth_hdr(skb);
1338 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1342 * We are yet to select best FCF, but we got CVL in the
1343 * meantime. reset the ctlr and let it rediscover the FCF
1345 LIBFCOE_FIP_DBG(fip, "Resetting fcoe_ctlr as FCF has not been "
1347 mutex_lock(&fip->ctlr_mutex);
1348 fcoe_ctlr_reset(fip);
1349 mutex_unlock(&fip->ctlr_mutex);
1354 * If we've selected an FCF check that the CVL is from there to avoid
1355 * processing CVLs from an unexpected source. If it is from an
1356 * unexpected source drop it on the floor.
1358 if (!ether_addr_equal(eh->h_source, fcf->fcf_mac)) {
1359 LIBFCOE_FIP_DBG(fip, "Dropping CVL due to source address "
1360 "mismatch with FCF src=%pM\n", eh->h_source);
1365 * If we haven't logged into the fabric but receive a CVL we should
1366 * reset everything and go back to solicitation.
1368 if (!lport->port_id) {
1369 LIBFCOE_FIP_DBG(fip, "lport not logged in, resoliciting\n");
1370 mutex_lock(&fip->ctlr_mutex);
1371 fcoe_ctlr_reset(fip);
1372 mutex_unlock(&fip->ctlr_mutex);
1373 fc_lport_reset(fip->lp);
1374 fcoe_ctlr_solicit(fip, NULL);
1379 * mask of required descriptors. Validating each one clears its bit.
1381 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
1383 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1384 desc = (struct fip_desc *)(fh + 1);
1387 * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1388 * before determining max Vx_Port descriptor but a buggy FCF could have
1389 * omitted either or both MAC Address and Name Identifier descriptors
1391 num_vlink_desc = rlen / sizeof(*vp);
1393 vlink_desc_arr = kmalloc_array(num_vlink_desc, sizeof(vp),
1395 if (!vlink_desc_arr)
1399 while (rlen >= sizeof(*desc)) {
1400 dlen = desc->fip_dlen * FIP_BPW;
1403 /* Drop CVL if there are duplicate critical descriptors */
1404 if ((desc->fip_dtype < 32) &&
1405 (desc->fip_dtype != FIP_DT_VN_ID) &&
1406 !(desc_mask & 1U << desc->fip_dtype)) {
1407 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1408 "Descriptors in FIP CVL\n");
1411 switch (desc->fip_dtype) {
1413 mp = (struct fip_mac_desc *)desc;
1414 if (dlen < sizeof(*mp))
1416 if (!ether_addr_equal(mp->fd_mac, fcf->fcf_mac))
1418 desc_mask &= ~BIT(FIP_DT_MAC);
1421 wp = (struct fip_wwn_desc *)desc;
1422 if (dlen < sizeof(*wp))
1424 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1426 desc_mask &= ~BIT(FIP_DT_NAME);
1429 vp = (struct fip_vn_desc *)desc;
1430 if (dlen < sizeof(*vp))
1432 vlink_desc_arr[num_vlink_desc++] = vp;
1433 vn_port = fc_vport_id_lookup(lport,
1434 ntoh24(vp->fd_fc_id));
1435 if (vn_port && (vn_port == lport)) {
1436 mutex_lock(&fip->ctlr_mutex);
1437 per_cpu_ptr(lport->stats,
1438 get_cpu())->VLinkFailureCount++;
1440 fcoe_ctlr_reset(fip);
1441 mutex_unlock(&fip->ctlr_mutex);
1445 /* standard says ignore unknown descriptors >= 128 */
1446 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1450 desc = (struct fip_desc *)((char *)desc + dlen);
1455 * reset only if all required descriptors were present and valid.
1458 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1460 else if (!num_vlink_desc) {
1461 LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1463 * No Vx_Port description. Clear all NPIV ports,
1464 * followed by physical port
1466 mutex_lock(&fip->ctlr_mutex);
1467 per_cpu_ptr(lport->stats, get_cpu())->VLinkFailureCount++;
1469 fcoe_ctlr_reset(fip);
1470 mutex_unlock(&fip->ctlr_mutex);
1472 mutex_lock(&lport->lp_mutex);
1473 list_for_each_entry(vn_port, &lport->vports, list)
1474 fc_lport_reset(vn_port);
1475 mutex_unlock(&lport->lp_mutex);
1477 fc_lport_reset(fip->lp);
1478 fcoe_ctlr_solicit(fip, NULL);
1482 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1483 for (i = 0; i < num_vlink_desc; i++) {
1484 vp = vlink_desc_arr[i];
1485 vn_port = fc_vport_id_lookup(lport,
1486 ntoh24(vp->fd_fc_id));
1491 * 'port_id' is already validated, check MAC address and
1494 if (!ether_addr_equal(fip->get_src_addr(vn_port),
1496 get_unaligned_be64(&vp->fd_wwpn) !=
1500 if (vn_port == lport)
1502 * Physical port, defer processing till all
1503 * listed NPIV ports are cleared
1505 reset_phys_port = 1;
1506 else /* NPIV port */
1507 fc_lport_reset(vn_port);
1510 if (reset_phys_port) {
1511 fc_lport_reset(fip->lp);
1512 fcoe_ctlr_solicit(fip, NULL);
1517 kfree(vlink_desc_arr);
1521 * fcoe_ctlr_recv() - Receive a FIP packet
1522 * @fip: The FCoE controller that received the packet
1523 * @skb: The received FIP packet
1525 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1527 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1529 skb = skb_share_check(skb, GFP_ATOMIC);
1532 skb_queue_tail(&fip->fip_recv_list, skb);
1533 schedule_work(&fip->recv_work);
1535 EXPORT_SYMBOL(fcoe_ctlr_recv);
1538 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1539 * @fip: The FCoE controller that received the frame
1540 * @skb: The received FIP frame
1542 * Returns non-zero if the frame is dropped.
1544 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1546 struct fip_header *fiph;
1548 enum fip_state state;
1549 bool fip_vlan_resp = false;
1553 if (skb_linearize(skb))
1555 if (skb->len < sizeof(*fiph))
1558 if (fip->mode == FIP_MODE_VN2VN) {
1559 if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1560 !ether_addr_equal(eh->h_dest, fcoe_all_vn2vn) &&
1561 !ether_addr_equal(eh->h_dest, fcoe_all_p2p))
1563 } else if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1564 !ether_addr_equal(eh->h_dest, fcoe_all_enode))
1566 fiph = (struct fip_header *)skb->data;
1567 op = ntohs(fiph->fip_op);
1568 sub = fiph->fip_subcode;
1570 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1572 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1575 mutex_lock(&fip->ctlr_mutex);
1577 if (state == FIP_ST_AUTO) {
1579 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1580 state = FIP_ST_ENABLED;
1581 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1583 fip_vlan_resp = fip->fip_resp;
1584 mutex_unlock(&fip->ctlr_mutex);
1586 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1587 return fcoe_ctlr_vn_recv(fip, skb);
1589 if (fip_vlan_resp && op == FIP_OP_VLAN) {
1590 LIBFCOE_FIP_DBG(fip, "fip vlan discovery\n");
1591 return fcoe_ctlr_vlan_recv(fip, skb);
1594 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1595 state != FIP_ST_VNMP_CLAIM)
1598 if (op == FIP_OP_LS) {
1599 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1603 if (state != FIP_ST_ENABLED)
1606 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1607 fcoe_ctlr_recv_adv(fip, skb);
1608 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1609 fcoe_ctlr_recv_clr_vlink(fip, skb);
1618 * fcoe_ctlr_select() - Select the best FCF (if possible)
1619 * @fip: The FCoE controller
1621 * Returns the selected FCF, or NULL if none are usable.
1623 * If there are conflicting advertisements, no FCF can be chosen.
1625 * If there is already a selected FCF, this will choose a better one or
1626 * an equivalent one that hasn't already been sent a FLOGI.
1628 * Called with lock held.
1630 static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
1632 struct fcoe_fcf *fcf;
1633 struct fcoe_fcf *best = fip->sel_fcf;
1635 list_for_each_entry(fcf, &fip->fcfs, list) {
1636 LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1637 "VFID %d mac %pM map %x val %d "
1639 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1640 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1641 fcf->flogi_sent, fcf->pri);
1642 if (!fcoe_ctlr_fcf_usable(fcf)) {
1643 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1644 "map %x %svalid %savailable\n",
1645 fcf->fabric_name, fcf->fc_map,
1646 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1647 (fcf->flags & FIP_FL_AVAIL) ?
1651 if (!best || fcf->pri < best->pri || best->flogi_sent)
1653 if (fcf->fabric_name != best->fabric_name ||
1654 fcf->vfid != best->vfid ||
1655 fcf->fc_map != best->fc_map) {
1656 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1661 fip->sel_fcf = best;
1663 LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
1664 fip->port_ka_time = jiffies +
1665 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1666 fip->ctlr_ka_time = jiffies + best->fka_period;
1667 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1668 mod_timer(&fip->timer, fip->ctlr_ka_time);
1674 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1675 * @fip: The FCoE controller
1677 * Returns non-zero error if it could not be sent.
1679 * Called with ctlr_mutex and ctlr_lock held.
1680 * Caller must verify that fip->sel_fcf is not NULL.
1682 static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1684 struct sk_buff *skb;
1685 struct sk_buff *skb_orig;
1686 struct fc_frame_header *fh;
1689 skb_orig = fip->flogi_req;
1694 * Clone and send the FLOGI request. If clone fails, use original.
1696 skb = skb_clone(skb_orig, GFP_ATOMIC);
1699 fip->flogi_req = NULL;
1701 fh = (struct fc_frame_header *)skb->data;
1702 error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1703 ntoh24(fh->fh_d_id));
1708 fip->send(fip, skb);
1709 fip->sel_fcf->flogi_sent = 1;
1714 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1715 * @fip: The FCoE controller
1717 * Returns non-zero error code if there's no FLOGI request to retry or
1718 * no alternate FCF available.
1720 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1722 struct fcoe_fcf *fcf;
1725 mutex_lock(&fip->ctlr_mutex);
1726 spin_lock_bh(&fip->ctlr_lock);
1727 LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1728 fcf = fcoe_ctlr_select(fip);
1729 if (!fcf || fcf->flogi_sent) {
1730 kfree_skb(fip->flogi_req);
1731 fip->flogi_req = NULL;
1734 fcoe_ctlr_solicit(fip, NULL);
1735 error = fcoe_ctlr_flogi_send_locked(fip);
1737 spin_unlock_bh(&fip->ctlr_lock);
1738 mutex_unlock(&fip->ctlr_mutex);
1744 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1745 * @fip: The FCoE controller that timed out
1747 * Done here because fcoe_ctlr_els_send() can't get mutex.
1749 * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
1751 static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1753 struct fcoe_fcf *fcf;
1755 spin_lock_bh(&fip->ctlr_lock);
1757 if (!fcf || !fip->flogi_req_send)
1760 LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1763 * If this FLOGI is being sent due to a timeout retry
1764 * to the same FCF as before, select a different FCF if possible.
1766 if (fcf->flogi_sent) {
1767 LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1768 fcf = fcoe_ctlr_select(fip);
1769 if (!fcf || fcf->flogi_sent) {
1770 LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1771 list_for_each_entry(fcf, &fip->fcfs, list)
1772 fcf->flogi_sent = 0;
1773 fcf = fcoe_ctlr_select(fip);
1777 fcoe_ctlr_flogi_send_locked(fip);
1778 fip->flogi_req_send = 0;
1780 LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1782 spin_unlock_bh(&fip->ctlr_lock);
1786 * fcoe_ctlr_timeout() - FIP timeout handler
1787 * @arg: The FCoE controller that timed out
1789 static void fcoe_ctlr_timeout(struct timer_list *t)
1791 struct fcoe_ctlr *fip = from_timer(fip, t, timer);
1793 schedule_work(&fip->timer_work);
1797 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1798 * @work: Handle to a FCoE controller
1800 * Ages FCFs. Triggers FCF selection if possible.
1801 * Sends keep-alives and resets.
1803 static void fcoe_ctlr_timer_work(struct work_struct *work)
1805 struct fcoe_ctlr *fip;
1806 struct fc_lport *vport;
1809 u8 send_ctlr_ka = 0;
1810 u8 send_port_ka = 0;
1811 struct fcoe_fcf *sel;
1812 struct fcoe_fcf *fcf;
1813 unsigned long next_timer;
1815 fip = container_of(work, struct fcoe_ctlr, timer_work);
1816 if (fip->mode == FIP_MODE_VN2VN)
1817 return fcoe_ctlr_vn_timeout(fip);
1818 mutex_lock(&fip->ctlr_mutex);
1819 if (fip->state == FIP_ST_DISABLED) {
1820 mutex_unlock(&fip->ctlr_mutex);
1825 next_timer = fcoe_ctlr_age_fcfs(fip);
1828 if (!sel && fip->sel_time) {
1829 if (time_after_eq(jiffies, fip->sel_time)) {
1830 sel = fcoe_ctlr_select(fip);
1832 } else if (time_after(next_timer, fip->sel_time))
1833 next_timer = fip->sel_time;
1836 if (sel && fip->flogi_req_send)
1837 fcoe_ctlr_flogi_send(fip);
1838 else if (!sel && fcf)
1841 if (sel && !sel->fd_flags) {
1842 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1843 fip->ctlr_ka_time = jiffies + sel->fka_period;
1846 if (time_after(next_timer, fip->ctlr_ka_time))
1847 next_timer = fip->ctlr_ka_time;
1849 if (time_after_eq(jiffies, fip->port_ka_time)) {
1850 fip->port_ka_time = jiffies +
1851 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1854 if (time_after(next_timer, fip->port_ka_time))
1855 next_timer = fip->port_ka_time;
1857 if (!list_empty(&fip->fcfs))
1858 mod_timer(&fip->timer, next_timer);
1859 mutex_unlock(&fip->ctlr_mutex);
1862 fc_lport_reset(fip->lp);
1863 /* restart things with a solicitation */
1864 fcoe_ctlr_solicit(fip, NULL);
1868 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1871 mutex_lock(&fip->lp->lp_mutex);
1872 mac = fip->get_src_addr(fip->lp);
1873 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1874 list_for_each_entry(vport, &fip->lp->vports, list) {
1875 mac = fip->get_src_addr(vport);
1876 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1878 mutex_unlock(&fip->lp->lp_mutex);
1883 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1884 * @recv_work: Handle to a FCoE controller
1886 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1888 struct fcoe_ctlr *fip;
1889 struct sk_buff *skb;
1891 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1892 while ((skb = skb_dequeue(&fip->fip_recv_list)))
1893 fcoe_ctlr_recv_handler(fip, skb);
1897 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1898 * @fip: The FCoE controller
1899 * @fp: The FC frame to snoop
1901 * Snoop potential response to FLOGI or even incoming FLOGI.
1903 * The caller has checked that we are waiting for login as indicated
1904 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1906 * The caller is responsible for freeing the frame.
1907 * Fill in the granted_mac address.
1909 * Return non-zero if the frame should not be delivered to libfc.
1911 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1912 struct fc_frame *fp)
1914 struct fc_frame_header *fh;
1918 sa = eth_hdr(&fp->skb)->h_source;
1919 fh = fc_frame_header_get(fp);
1920 if (fh->fh_type != FC_TYPE_ELS)
1923 op = fc_frame_payload_op(fp);
1924 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1925 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1927 mutex_lock(&fip->ctlr_mutex);
1928 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1929 mutex_unlock(&fip->ctlr_mutex);
1932 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1933 LIBFCOE_FIP_DBG(fip,
1934 "received FLOGI LS_ACC using non-FIP mode\n");
1938 * If the src mac addr is FC_OUI-based, then we mark the
1939 * address_mode flag to use FC_OUI-based Ethernet DA.
1940 * Otherwise we use the FCoE gateway addr
1942 if (ether_addr_equal(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1943 fcoe_ctlr_map_dest(fip);
1945 memcpy(fip->dest_addr, sa, ETH_ALEN);
1948 fip->flogi_oxid = FC_XID_UNKNOWN;
1949 mutex_unlock(&fip->ctlr_mutex);
1950 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1951 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1953 * Save source MAC for point-to-point responses.
1955 mutex_lock(&fip->ctlr_mutex);
1956 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1957 memcpy(fip->dest_addr, sa, ETH_ALEN);
1959 if (fip->state == FIP_ST_AUTO)
1960 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1961 "Setting non-FIP mode\n");
1962 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1964 mutex_unlock(&fip->ctlr_mutex);
1968 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1971 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1972 * @mac: The MAC address to convert
1973 * @scheme: The scheme to use when converting
1974 * @port: The port indicator for converting
1976 * Returns: u64 fc world wide name
1978 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1979 unsigned int scheme, unsigned int port)
1984 /* The MAC is in NO, so flip only the low 48 bits */
1985 host_mac = ((u64) mac[0] << 40) |
1986 ((u64) mac[1] << 32) |
1987 ((u64) mac[2] << 24) |
1988 ((u64) mac[3] << 16) |
1989 ((u64) mac[4] << 8) |
1992 WARN_ON(host_mac >= (1ULL << 48));
1993 wwn = host_mac | ((u64) scheme << 60);
1999 WARN_ON(port >= 0xfff);
2000 wwn |= (u64) port << 48;
2009 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
2012 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
2013 * @rdata: libfc remote port
2015 static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
2017 return (struct fcoe_rport *)(rdata + 1);
2021 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
2022 * @fip: The FCoE controller
2023 * @sub: sub-opcode for probe request, reply, or advertisement.
2024 * @dest: The destination Ethernet MAC address
2025 * @min_len: minimum size of the Ethernet payload to be sent
2027 static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
2028 enum fip_vn2vn_subcode sub,
2029 const u8 *dest, size_t min_len)
2031 struct sk_buff *skb;
2032 struct fip_vn2vn_probe_frame {
2034 struct fip_header fip;
2035 struct fip_mac_desc mac;
2036 struct fip_wwn_desc wwnn;
2037 struct fip_vn_desc vn;
2039 struct fip_fc4_feat *ff;
2040 struct fip_size_desc *size;
2045 len = sizeof(*frame);
2047 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2048 dlen = sizeof(struct fip_fc4_feat) +
2049 sizeof(struct fip_size_desc);
2052 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
2053 len = max(len, min_len + sizeof(struct ethhdr));
2055 skb = dev_alloc_skb(len);
2059 frame = (struct fip_vn2vn_probe_frame *)skb->data;
2060 memset(frame, 0, len);
2061 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2063 if (sub == FIP_SC_VN_BEACON) {
2064 hton24(frame->eth.h_source, FIP_VN_FC_MAP);
2065 hton24(frame->eth.h_source + 3, fip->port_id);
2067 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2069 frame->eth.h_proto = htons(ETH_P_FIP);
2071 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2072 frame->fip.fip_op = htons(FIP_OP_VN2VN);
2073 frame->fip.fip_subcode = sub;
2074 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2076 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2077 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2078 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2080 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
2081 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
2082 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
2084 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
2085 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
2086 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
2087 hton24(frame->vn.fd_mac + 3, fip->port_id);
2088 hton24(frame->vn.fd_fc_id, fip->port_id);
2089 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
2092 * For claims, add FC-4 features.
2093 * TBD: Add interface to get fc-4 types and features from libfc.
2095 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2096 ff = (struct fip_fc4_feat *)(frame + 1);
2097 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
2098 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
2099 ff->fd_fts = fip->lp->fcts;
2102 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
2103 fcp_feat |= FCP_FEAT_INIT;
2104 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
2105 fcp_feat |= FCP_FEAT_TARG;
2106 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
2107 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
2109 size = (struct fip_size_desc *)(ff + 1);
2110 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
2111 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
2112 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
2116 skb->protocol = htons(ETH_P_FIP);
2117 skb->priority = fip->priority;
2118 skb_reset_mac_header(skb);
2119 skb_reset_network_header(skb);
2121 fip->send(fip, skb);
2125 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
2126 * @lport: The lport which is receiving the event
2127 * @rdata: remote port private data
2128 * @event: The event that occurred
2130 * Locking Note: The rport lock must not be held when calling this function.
2132 static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
2133 struct fc_rport_priv *rdata,
2134 enum fc_rport_event event)
2136 struct fcoe_ctlr *fip = lport->disc.priv;
2137 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2139 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
2140 rdata->ids.port_id, event);
2142 mutex_lock(&fip->ctlr_mutex);
2144 case RPORT_EV_READY:
2145 frport->login_count = 0;
2148 case RPORT_EV_FAILED:
2150 frport->login_count++;
2151 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
2152 LIBFCOE_FIP_DBG(fip,
2153 "rport FLOGI limited port_id %6.6x\n",
2154 rdata->ids.port_id);
2155 fc_rport_logoff(rdata);
2161 mutex_unlock(&fip->ctlr_mutex);
2164 static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
2165 .event_callback = fcoe_ctlr_vn_rport_callback,
2169 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
2170 * @fip: The FCoE controller
2172 * Called with ctlr_mutex held.
2174 static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
2176 struct fc_rport_priv *rdata;
2178 mutex_lock(&lport->disc.disc_mutex);
2179 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2180 if (kref_get_unless_zero(&rdata->kref)) {
2181 fc_rport_logoff(rdata);
2182 kref_put(&rdata->kref, fc_rport_destroy);
2185 lport->disc.disc_callback = NULL;
2186 mutex_unlock(&lport->disc.disc_mutex);
2190 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2191 * @fip: The FCoE controller
2193 * Called through the local port template for discovery.
2194 * Called without the ctlr_mutex held.
2196 static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2198 struct fcoe_ctlr *fip = lport->disc.priv;
2200 mutex_lock(&fip->ctlr_mutex);
2201 fcoe_ctlr_disc_stop_locked(lport);
2202 mutex_unlock(&fip->ctlr_mutex);
2206 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2207 * @fip: The FCoE controller
2209 * Called through the local port template for discovery.
2210 * Called without the ctlr_mutex held.
2212 static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2214 fcoe_ctlr_disc_stop(lport);
2215 fc_rport_flush_queue();
2220 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2221 * @fip: The FCoE controller
2223 * Called with fcoe_ctlr lock held.
2225 static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2230 fcoe_ctlr_disc_stop_locked(fip->lp);
2233 * Get proposed port ID.
2234 * If this is the first try after link up, use any previous port_id.
2235 * If there was none, use the low bits of the port_name.
2236 * On subsequent tries, get the next random one.
2237 * Don't use reserved IDs, use another non-zero value, just as random.
2239 port_id = fip->port_id;
2240 if (fip->probe_tries)
2241 port_id = prandom_u32_state(&fip->rnd_state) & 0xffff;
2243 port_id = fip->lp->wwpn & 0xffff;
2244 if (!port_id || port_id == 0xffff)
2246 fip->port_id = port_id;
2248 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2250 wait = prandom_u32() % FIP_VN_PROBE_WAIT;
2252 wait = FIP_VN_RLIM_INT;
2253 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2254 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2258 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2259 * @fip: The FCoE controller
2261 * Called with fcoe_ctlr lock held.
2263 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2265 fip->probe_tries = 0;
2266 prandom_seed_state(&fip->rnd_state, fip->lp->wwpn);
2267 fcoe_ctlr_vn_restart(fip);
2271 * fcoe_ctlr_vn_parse - parse probe request or response
2272 * @fip: The FCoE controller
2273 * @skb: incoming packet
2274 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2276 * Returns non-zero error number on error.
2277 * Does not consume the packet.
2279 static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2280 struct sk_buff *skb,
2281 struct fc_rport_priv *rdata)
2283 struct fip_header *fiph;
2284 struct fip_desc *desc = NULL;
2285 struct fip_mac_desc *macd = NULL;
2286 struct fip_wwn_desc *wwn = NULL;
2287 struct fip_vn_desc *vn = NULL;
2288 struct fip_size_desc *size = NULL;
2289 struct fcoe_rport *frport;
2296 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2297 frport = fcoe_ctlr_rport(rdata);
2299 fiph = (struct fip_header *)skb->data;
2300 frport->flags = ntohs(fiph->fip_flags);
2302 sub = fiph->fip_subcode;
2304 case FIP_SC_VN_PROBE_REQ:
2305 case FIP_SC_VN_PROBE_REP:
2306 case FIP_SC_VN_BEACON:
2307 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2310 case FIP_SC_VN_CLAIM_NOTIFY:
2311 case FIP_SC_VN_CLAIM_REP:
2312 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2313 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2314 BIT(FIP_DT_FCOE_SIZE);
2317 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2321 rlen = ntohs(fiph->fip_dl_len) * 4;
2322 if (rlen + sizeof(*fiph) > skb->len)
2325 desc = (struct fip_desc *)(fiph + 1);
2327 dlen = desc->fip_dlen * FIP_BPW;
2328 if (dlen < sizeof(*desc) || dlen > rlen)
2331 dtype = desc->fip_dtype;
2333 if (!(desc_mask & BIT(dtype))) {
2334 LIBFCOE_FIP_DBG(fip,
2335 "unexpected or duplicated desc "
2337 "FIP VN2VN subtype %u\n",
2341 desc_mask &= ~BIT(dtype);
2346 if (dlen != sizeof(struct fip_mac_desc))
2348 macd = (struct fip_mac_desc *)desc;
2349 if (!is_valid_ether_addr(macd->fd_mac)) {
2350 LIBFCOE_FIP_DBG(fip,
2351 "Invalid MAC addr %pM in FIP VN2VN\n",
2355 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2358 if (dlen != sizeof(struct fip_wwn_desc))
2360 wwn = (struct fip_wwn_desc *)desc;
2361 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2364 if (dlen != sizeof(struct fip_vn_desc))
2366 vn = (struct fip_vn_desc *)desc;
2367 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2368 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
2369 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
2372 if (dlen != sizeof(struct fip_fc4_feat))
2375 case FIP_DT_FCOE_SIZE:
2376 if (dlen != sizeof(struct fip_size_desc))
2378 size = (struct fip_size_desc *)desc;
2379 frport->fcoe_len = ntohs(size->fd_size);
2382 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2383 "in FIP probe\n", dtype);
2384 /* standard says ignore unknown descriptors >= 128 */
2385 if (dtype < FIP_DT_NON_CRITICAL)
2389 desc = (struct fip_desc *)((char *)desc + dlen);
2395 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2401 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2402 * @fip: The FCoE controller
2404 * Called with ctlr_mutex held.
2406 static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2408 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2409 fip->sol_time = jiffies;
2413 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2414 * @fip: The FCoE controller
2415 * @rdata: parsed remote port with frport from the probe request
2417 * Called with ctlr_mutex held.
2419 static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2420 struct fc_rport_priv *rdata)
2422 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2424 if (rdata->ids.port_id != fip->port_id)
2427 switch (fip->state) {
2428 case FIP_ST_VNMP_CLAIM:
2429 case FIP_ST_VNMP_UP:
2430 LIBFCOE_FIP_DBG(fip, "vn_probe_req: send reply, state %x\n",
2432 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2433 frport->enode_mac, 0);
2435 case FIP_ST_VNMP_PROBE1:
2436 case FIP_ST_VNMP_PROBE2:
2438 * Decide whether to reply to the Probe.
2439 * Our selected address is never a "recorded" one, so
2440 * only reply if our WWPN is greater and the
2441 * Probe's REC bit is not set.
2442 * If we don't reply, we will change our address.
2444 if (fip->lp->wwpn > rdata->ids.port_name &&
2445 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2446 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2447 "port_id collision\n");
2448 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2449 frport->enode_mac, 0);
2453 case FIP_ST_VNMP_START:
2454 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2455 "restart VN2VN negotiation\n");
2456 fcoe_ctlr_vn_restart(fip);
2459 LIBFCOE_FIP_DBG(fip, "vn_probe_req: ignore state %x\n",
2466 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2467 * @fip: The FCoE controller
2468 * @rdata: parsed remote port with frport from the probe request
2470 * Called with ctlr_mutex held.
2472 static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2473 struct fc_rport_priv *rdata)
2475 if (rdata->ids.port_id != fip->port_id)
2477 switch (fip->state) {
2478 case FIP_ST_VNMP_START:
2479 case FIP_ST_VNMP_PROBE1:
2480 case FIP_ST_VNMP_PROBE2:
2481 case FIP_ST_VNMP_CLAIM:
2482 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: restart state %x\n",
2484 fcoe_ctlr_vn_restart(fip);
2486 case FIP_ST_VNMP_UP:
2487 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: send claim notify\n");
2488 fcoe_ctlr_vn_send_claim(fip);
2496 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2497 * @fip: The FCoE controller
2498 * @new: newly-parsed remote port with frport as a template for new rdata
2500 * Called with ctlr_mutex held.
2502 static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2504 struct fc_lport *lport = fip->lp;
2505 struct fc_rport_priv *rdata;
2506 struct fc_rport_identifiers *ids;
2507 struct fcoe_rport *frport;
2510 port_id = new->ids.port_id;
2511 if (port_id == fip->port_id)
2514 mutex_lock(&lport->disc.disc_mutex);
2515 rdata = fc_rport_create(lport, port_id);
2517 mutex_unlock(&lport->disc.disc_mutex);
2520 mutex_lock(&rdata->rp_mutex);
2521 mutex_unlock(&lport->disc.disc_mutex);
2523 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2524 rdata->disc_id = lport->disc.disc_id;
2527 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2528 (ids->node_name != -1 && ids->node_name != new->ids.node_name)) {
2529 mutex_unlock(&rdata->rp_mutex);
2530 LIBFCOE_FIP_DBG(fip, "vn_add rport logoff %6.6x\n", port_id);
2531 fc_rport_logoff(rdata);
2532 mutex_lock(&rdata->rp_mutex);
2534 ids->port_name = new->ids.port_name;
2535 ids->node_name = new->ids.node_name;
2536 mutex_unlock(&rdata->rp_mutex);
2538 frport = fcoe_ctlr_rport(rdata);
2539 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s state %d\n",
2540 port_id, frport->fcoe_len ? "old" : "new",
2542 *frport = *fcoe_ctlr_rport(new);
2547 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2548 * @fip: The FCoE controller
2549 * @port_id: The port_id of the remote VN_node
2550 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2552 * Returns non-zero error if no remote port found.
2554 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2556 struct fc_lport *lport = fip->lp;
2557 struct fc_rport_priv *rdata;
2558 struct fcoe_rport *frport;
2561 rdata = fc_rport_lookup(lport, port_id);
2563 frport = fcoe_ctlr_rport(rdata);
2564 memcpy(mac, frport->enode_mac, ETH_ALEN);
2566 kref_put(&rdata->kref, fc_rport_destroy);
2572 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2573 * @fip: The FCoE controller
2574 * @new: newly-parsed remote port with frport as a template for new rdata
2576 * Called with ctlr_mutex held.
2578 static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2579 struct fc_rport_priv *new)
2581 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2583 if (frport->flags & FIP_FL_REC_OR_P2P) {
2584 LIBFCOE_FIP_DBG(fip, "send probe req for P2P/REC\n");
2585 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2588 switch (fip->state) {
2589 case FIP_ST_VNMP_START:
2590 case FIP_ST_VNMP_PROBE1:
2591 case FIP_ST_VNMP_PROBE2:
2592 if (new->ids.port_id == fip->port_id) {
2593 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2594 "restart, state %d\n",
2596 fcoe_ctlr_vn_restart(fip);
2599 case FIP_ST_VNMP_CLAIM:
2600 case FIP_ST_VNMP_UP:
2601 if (new->ids.port_id == fip->port_id) {
2602 if (new->ids.port_name > fip->lp->wwpn) {
2603 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2604 "restart, port_id collision\n");
2605 fcoe_ctlr_vn_restart(fip);
2608 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2609 "send claim notify\n");
2610 fcoe_ctlr_vn_send_claim(fip);
2613 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: send reply to %x\n",
2615 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2616 min((u32)frport->fcoe_len,
2617 fcoe_ctlr_fcoe_size(fip)));
2618 fcoe_ctlr_vn_add(fip, new);
2621 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2622 "ignoring claim from %x\n", new->ids.port_id);
2628 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2629 * @fip: The FCoE controller that received the frame
2630 * @new: newly-parsed remote port with frport from the Claim Response
2632 * Called with ctlr_mutex held.
2634 static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2635 struct fc_rport_priv *new)
2637 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2638 new->ids.port_id, fcoe_ctlr_state(fip->state));
2639 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2640 fcoe_ctlr_vn_add(fip, new);
2644 * fcoe_ctlr_vn_beacon() - handle received beacon.
2645 * @fip: The FCoE controller that received the frame
2646 * @new: newly-parsed remote port with frport from the Beacon
2648 * Called with ctlr_mutex held.
2650 static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2651 struct fc_rport_priv *new)
2653 struct fc_lport *lport = fip->lp;
2654 struct fc_rport_priv *rdata;
2655 struct fcoe_rport *frport;
2657 frport = fcoe_ctlr_rport(new);
2658 if (frport->flags & FIP_FL_REC_OR_P2P) {
2659 LIBFCOE_FIP_DBG(fip, "p2p beacon while in vn2vn mode\n");
2660 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2663 rdata = fc_rport_lookup(lport, new->ids.port_id);
2665 if (rdata->ids.node_name == new->ids.node_name &&
2666 rdata->ids.port_name == new->ids.port_name) {
2667 frport = fcoe_ctlr_rport(rdata);
2668 LIBFCOE_FIP_DBG(fip, "beacon from rport %x\n",
2669 rdata->ids.port_id);
2670 if (!frport->time && fip->state == FIP_ST_VNMP_UP) {
2671 LIBFCOE_FIP_DBG(fip, "beacon expired "
2673 rdata->ids.port_id);
2674 fc_rport_login(rdata);
2676 frport->time = jiffies;
2678 kref_put(&rdata->kref, fc_rport_destroy);
2681 if (fip->state != FIP_ST_VNMP_UP)
2685 * Beacon from a new neighbor.
2686 * Send a claim notify if one hasn't been sent recently.
2687 * Don't add the neighbor yet.
2689 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2691 if (time_after(jiffies,
2692 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2693 fcoe_ctlr_vn_send_claim(fip);
2697 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2698 * @fip: The FCoE controller
2700 * Called with ctlr_mutex held.
2701 * Called only in state FIP_ST_VNMP_UP.
2702 * Returns the soonest time for next age-out or a time far in the future.
2704 static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2706 struct fc_lport *lport = fip->lp;
2707 struct fc_rport_priv *rdata;
2708 struct fcoe_rport *frport;
2709 unsigned long next_time;
2710 unsigned long deadline;
2712 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2713 mutex_lock(&lport->disc.disc_mutex);
2714 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2715 if (!kref_get_unless_zero(&rdata->kref))
2717 frport = fcoe_ctlr_rport(rdata);
2718 if (!frport->time) {
2719 kref_put(&rdata->kref, fc_rport_destroy);
2722 deadline = frport->time +
2723 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2724 if (time_after_eq(jiffies, deadline)) {
2726 LIBFCOE_FIP_DBG(fip,
2727 "port %16.16llx fc_id %6.6x beacon expired\n",
2728 rdata->ids.port_name, rdata->ids.port_id);
2729 fc_rport_logoff(rdata);
2730 } else if (time_before(deadline, next_time))
2731 next_time = deadline;
2732 kref_put(&rdata->kref, fc_rport_destroy);
2734 mutex_unlock(&lport->disc.disc_mutex);
2739 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2740 * @fip: The FCoE controller that received the frame
2741 * @skb: The received FIP frame
2743 * Returns non-zero if the frame is dropped.
2744 * Always consumes the frame.
2746 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2748 struct fip_header *fiph;
2749 enum fip_vn2vn_subcode sub;
2751 struct fc_rport_priv rdata;
2752 struct fcoe_rport frport;
2754 int rc, vlan_id = 0;
2756 fiph = (struct fip_header *)skb->data;
2757 sub = fiph->fip_subcode;
2760 vlan_id = skb_vlan_tag_get_id(skb);
2762 if (vlan_id && vlan_id != fip->lp->vlan) {
2763 LIBFCOE_FIP_DBG(fip, "vn_recv drop frame sub %x vlan %d\n",
2769 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2771 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2775 mutex_lock(&fip->ctlr_mutex);
2777 case FIP_SC_VN_PROBE_REQ:
2778 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2780 case FIP_SC_VN_PROBE_REP:
2781 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2783 case FIP_SC_VN_CLAIM_NOTIFY:
2784 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2786 case FIP_SC_VN_CLAIM_REP:
2787 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2789 case FIP_SC_VN_BEACON:
2790 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2793 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2797 mutex_unlock(&fip->ctlr_mutex);
2804 * fcoe_ctlr_vlan_parse - parse vlan discovery request or response
2805 * @fip: The FCoE controller
2806 * @skb: incoming packet
2807 * @rdata: buffer for resulting parsed VLAN entry plus fcoe_rport
2809 * Returns non-zero error number on error.
2810 * Does not consume the packet.
2812 static int fcoe_ctlr_vlan_parse(struct fcoe_ctlr *fip,
2813 struct sk_buff *skb,
2814 struct fc_rport_priv *rdata)
2816 struct fip_header *fiph;
2817 struct fip_desc *desc = NULL;
2818 struct fip_mac_desc *macd = NULL;
2819 struct fip_wwn_desc *wwn = NULL;
2820 struct fcoe_rport *frport;
2827 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2828 frport = fcoe_ctlr_rport(rdata);
2830 fiph = (struct fip_header *)skb->data;
2831 frport->flags = ntohs(fiph->fip_flags);
2833 sub = fiph->fip_subcode;
2836 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
2839 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2843 rlen = ntohs(fiph->fip_dl_len) * 4;
2844 if (rlen + sizeof(*fiph) > skb->len)
2847 desc = (struct fip_desc *)(fiph + 1);
2849 dlen = desc->fip_dlen * FIP_BPW;
2850 if (dlen < sizeof(*desc) || dlen > rlen)
2853 dtype = desc->fip_dtype;
2855 if (!(desc_mask & BIT(dtype))) {
2856 LIBFCOE_FIP_DBG(fip,
2857 "unexpected or duplicated desc "
2859 "FIP VN2VN subtype %u\n",
2863 desc_mask &= ~BIT(dtype);
2868 if (dlen != sizeof(struct fip_mac_desc))
2870 macd = (struct fip_mac_desc *)desc;
2871 if (!is_valid_ether_addr(macd->fd_mac)) {
2872 LIBFCOE_FIP_DBG(fip,
2873 "Invalid MAC addr %pM in FIP VN2VN\n",
2877 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2880 if (dlen != sizeof(struct fip_wwn_desc))
2882 wwn = (struct fip_wwn_desc *)desc;
2883 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2886 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2887 "in FIP probe\n", dtype);
2888 /* standard says ignore unknown descriptors >= 128 */
2889 if (dtype < FIP_DT_NON_CRITICAL)
2893 desc = (struct fip_desc *)((char *)desc + dlen);
2899 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2905 * fcoe_ctlr_vlan_send() - Send a FIP VLAN Notification
2906 * @fip: The FCoE controller
2907 * @sub: sub-opcode for vlan notification or vn2vn vlan notification
2908 * @dest: The destination Ethernet MAC address
2909 * @min_len: minimum size of the Ethernet payload to be sent
2911 static void fcoe_ctlr_vlan_send(struct fcoe_ctlr *fip,
2912 enum fip_vlan_subcode sub,
2915 struct sk_buff *skb;
2916 struct fip_vlan_notify_frame {
2918 struct fip_header fip;
2919 struct fip_mac_desc mac;
2920 struct fip_vlan_desc vlan;
2925 len = sizeof(*frame);
2926 dlen = sizeof(frame->mac) + sizeof(frame->vlan);
2927 len = max(len, sizeof(struct ethhdr));
2929 skb = dev_alloc_skb(len);
2933 LIBFCOE_FIP_DBG(fip, "fip %s vlan notification, vlan %d\n",
2934 fip->mode == FIP_MODE_VN2VN ? "vn2vn" : "fcf",
2937 frame = (struct fip_vlan_notify_frame *)skb->data;
2938 memset(frame, 0, len);
2939 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2941 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2942 frame->eth.h_proto = htons(ETH_P_FIP);
2944 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2945 frame->fip.fip_op = htons(FIP_OP_VLAN);
2946 frame->fip.fip_subcode = sub;
2947 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2949 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2950 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2951 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2953 frame->vlan.fd_desc.fip_dtype = FIP_DT_VLAN;
2954 frame->vlan.fd_desc.fip_dlen = sizeof(frame->vlan) / FIP_BPW;
2955 put_unaligned_be16(fip->lp->vlan, &frame->vlan.fd_vlan);
2958 skb->protocol = htons(ETH_P_FIP);
2959 skb->priority = fip->priority;
2960 skb_reset_mac_header(skb);
2961 skb_reset_network_header(skb);
2963 fip->send(fip, skb);
2967 * fcoe_ctlr_vlan_disk_reply() - send FIP VLAN Discovery Notification.
2968 * @fip: The FCoE controller
2970 * Called with ctlr_mutex held.
2972 static void fcoe_ctlr_vlan_disc_reply(struct fcoe_ctlr *fip,
2973 struct fc_rport_priv *rdata)
2975 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2976 enum fip_vlan_subcode sub = FIP_SC_VL_NOTE;
2978 if (fip->mode == FIP_MODE_VN2VN)
2979 sub = FIP_SC_VL_VN2VN_NOTE;
2981 fcoe_ctlr_vlan_send(fip, sub, frport->enode_mac);
2985 * fcoe_ctlr_vlan_recv - vlan request receive handler for VN2VN mode.
2986 * @lport: The local port
2987 * @fp: The received frame
2990 static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2992 struct fip_header *fiph;
2993 enum fip_vlan_subcode sub;
2995 struct fc_rport_priv rdata;
2996 struct fcoe_rport frport;
3000 fiph = (struct fip_header *)skb->data;
3001 sub = fiph->fip_subcode;
3002 rc = fcoe_ctlr_vlan_parse(fip, skb, &buf.rdata);
3004 LIBFCOE_FIP_DBG(fip, "vlan_recv vlan_parse error %d\n", rc);
3007 mutex_lock(&fip->ctlr_mutex);
3008 if (sub == FIP_SC_VL_REQ)
3009 fcoe_ctlr_vlan_disc_reply(fip, &buf.rdata);
3010 mutex_unlock(&fip->ctlr_mutex);
3018 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
3019 * @lport: The local port
3020 * @fp: The received frame
3022 * This should never be called since we don't see RSCNs or other
3023 * fabric-generated ELSes.
3025 static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
3027 struct fc_seq_els_data rjt_data;
3029 rjt_data.reason = ELS_RJT_UNSUP;
3030 rjt_data.explan = ELS_EXPL_NONE;
3031 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
3036 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
3037 * @fip: The FCoE controller
3039 * This sets a flag indicating that remote ports should be created
3040 * and started for the peers we discover. We use the disc_callback
3041 * pointer as that flag. Peers already discovered are created here.
3043 * The lport lock is held during this call. The callback must be done
3044 * later, without holding either the lport or discovery locks.
3045 * The fcoe_ctlr lock may also be held during this call.
3047 static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
3048 enum fc_disc_event),
3049 struct fc_lport *lport)
3051 struct fc_disc *disc = &lport->disc;
3052 struct fcoe_ctlr *fip = disc->priv;
3054 mutex_lock(&disc->disc_mutex);
3055 disc->disc_callback = callback;
3056 disc->disc_id = (disc->disc_id + 2) | 1;
3058 schedule_work(&fip->timer_work);
3059 mutex_unlock(&disc->disc_mutex);
3063 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
3064 * @fip: The FCoE controller
3066 * Starts the FLOGI and PLOGI login process to each discovered rport for which
3067 * we've received at least one beacon.
3068 * Performs the discovery complete callback.
3070 static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
3072 struct fc_lport *lport = fip->lp;
3073 struct fc_disc *disc = &lport->disc;
3074 struct fc_rport_priv *rdata;
3075 struct fcoe_rport *frport;
3076 void (*callback)(struct fc_lport *, enum fc_disc_event);
3078 mutex_lock(&disc->disc_mutex);
3079 callback = disc->pending ? disc->disc_callback : NULL;
3081 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
3082 if (!kref_get_unless_zero(&rdata->kref))
3084 frport = fcoe_ctlr_rport(rdata);
3086 fc_rport_login(rdata);
3087 kref_put(&rdata->kref, fc_rport_destroy);
3089 mutex_unlock(&disc->disc_mutex);
3091 callback(lport, DISC_EV_SUCCESS);
3095 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
3096 * @fip: The FCoE controller
3098 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
3100 unsigned long next_time;
3102 u32 new_port_id = 0;
3104 mutex_lock(&fip->ctlr_mutex);
3105 switch (fip->state) {
3106 case FIP_ST_VNMP_START:
3107 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
3108 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 1st probe request\n");
3109 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3110 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
3112 case FIP_ST_VNMP_PROBE1:
3113 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
3114 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 2nd probe request\n");
3115 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3116 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3118 case FIP_ST_VNMP_PROBE2:
3119 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
3120 new_port_id = fip->port_id;
3121 hton24(mac, FIP_VN_FC_MAP);
3122 hton24(mac + 3, new_port_id);
3123 fcoe_ctlr_map_dest(fip);
3124 fip->update_mac(fip->lp, mac);
3125 LIBFCOE_FIP_DBG(fip, "vn_timeout: send claim notify\n");
3126 fcoe_ctlr_vn_send_claim(fip);
3127 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3129 case FIP_ST_VNMP_CLAIM:
3131 * This may be invoked either by starting discovery so don't
3132 * go to the next state unless it's been long enough.
3134 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3135 if (time_after_eq(jiffies, next_time)) {
3136 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
3137 LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3138 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3140 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3141 fip->port_ka_time = next_time;
3143 fcoe_ctlr_vn_disc(fip);
3145 case FIP_ST_VNMP_UP:
3146 next_time = fcoe_ctlr_vn_age(fip);
3147 if (time_after_eq(jiffies, fip->port_ka_time)) {
3148 LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3149 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3151 fip->port_ka_time = jiffies +
3152 msecs_to_jiffies(FIP_VN_BEACON_INT +
3153 (prandom_u32() % FIP_VN_BEACON_FUZZ));
3155 if (time_before(fip->port_ka_time, next_time))
3156 next_time = fip->port_ka_time;
3158 case FIP_ST_LINK_WAIT:
3161 WARN(1, "unexpected state %d\n", fip->state);
3164 mod_timer(&fip->timer, next_time);
3166 mutex_unlock(&fip->ctlr_mutex);
3168 /* If port ID is new, notify local port after dropping ctlr_mutex */
3170 fc_lport_set_local_id(fip->lp, new_port_id);
3174 * fcoe_ctlr_mode_set() - Set or reset the ctlr's mode
3175 * @lport: The local port to be (re)configured
3176 * @fip: The FCoE controller whose mode is changing
3177 * @fip_mode: The new fip mode
3179 * Note that the we shouldn't be changing the libfc discovery settings
3180 * (fc_disc_config) while an lport is going through the libfc state
3181 * machine. The mode can only be changed when a fcoe_ctlr device is
3182 * disabled, so that should ensure that this routine is only called
3183 * when nothing is happening.
3185 static void fcoe_ctlr_mode_set(struct fc_lport *lport, struct fcoe_ctlr *fip,
3186 enum fip_mode fip_mode)
3190 WARN_ON(lport->state != LPORT_ST_RESET &&
3191 lport->state != LPORT_ST_DISABLED);
3193 if (fip_mode == FIP_MODE_VN2VN) {
3194 lport->rport_priv_size = sizeof(struct fcoe_rport);
3195 lport->point_to_multipoint = 1;
3196 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
3197 lport->tt.disc_start = fcoe_ctlr_disc_start;
3198 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
3199 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
3202 lport->rport_priv_size = 0;
3203 lport->point_to_multipoint = 0;
3204 lport->tt.disc_recv_req = NULL;
3205 lport->tt.disc_start = NULL;
3206 lport->tt.disc_stop = NULL;
3207 lport->tt.disc_stop_final = NULL;
3211 fc_disc_config(lport, priv);
3215 * fcoe_libfc_config() - Sets up libfc related properties for local port
3216 * @lport: The local port to configure libfc for
3217 * @fip: The FCoE controller in use by the local port
3218 * @tt: The libfc function template
3219 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
3221 * Returns : 0 for success
3223 int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
3224 const struct libfc_function_template *tt, int init_fcp)
3226 /* Set the function pointers set by the LLDD */
3227 memcpy(&lport->tt, tt, sizeof(*tt));
3228 if (init_fcp && fc_fcp_init(lport))
3230 fc_exch_init(lport);
3231 fc_elsct_init(lport);
3232 fc_lport_init(lport);
3233 fc_disc_init(lport);
3234 fcoe_ctlr_mode_set(lport, fip, fip->mode);
3237 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
3239 void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
3241 struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
3242 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
3243 struct fcoe_fcf *fcf;
3245 mutex_lock(&fip->ctlr_mutex);
3246 mutex_lock(&ctlr_dev->lock);
3248 fcf = fcoe_fcf_device_priv(fcf_dev);
3250 fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
3252 fcf_dev->selected = 0;
3254 mutex_unlock(&ctlr_dev->lock);
3255 mutex_unlock(&fip->ctlr_mutex);
3257 EXPORT_SYMBOL(fcoe_fcf_get_selected);
3259 void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
3261 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
3262 struct fc_lport *lport = ctlr->lp;
3264 mutex_lock(&ctlr->ctlr_mutex);
3265 switch (ctlr_dev->mode) {
3266 case FIP_CONN_TYPE_VN2VN:
3267 ctlr->mode = FIP_MODE_VN2VN;
3269 case FIP_CONN_TYPE_FABRIC:
3271 ctlr->mode = FIP_MODE_FABRIC;
3275 mutex_unlock(&ctlr->ctlr_mutex);
3277 fcoe_ctlr_mode_set(lport, ctlr, ctlr->mode);
3279 EXPORT_SYMBOL(fcoe_ctlr_set_fip_mode);