1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
4 * Copyright (c) 2009 Intel Corporation. All rights reserved.
6 * Maintained at www.Open-FCoE.org
9 #include <linux/types.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/spinlock.h>
14 #include <linux/timer.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/ethtool.h>
18 #include <linux/if_ether.h>
19 #include <linux/if_vlan.h>
20 #include <linux/errno.h>
21 #include <linux/bitops.h>
22 #include <linux/slab.h>
23 #include <net/rtnetlink.h>
25 #include <scsi/fc/fc_els.h>
26 #include <scsi/fc/fc_fs.h>
27 #include <scsi/fc/fc_fip.h>
28 #include <scsi/fc/fc_encaps.h>
29 #include <scsi/fc/fc_fcoe.h>
30 #include <scsi/fc/fc_fcp.h>
32 #include <scsi/libfc.h>
33 #include <scsi/libfcoe.h>
37 #define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
38 #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
40 static void fcoe_ctlr_timeout(struct timer_list *);
41 static void fcoe_ctlr_timer_work(struct work_struct *);
42 static void fcoe_ctlr_recv_work(struct work_struct *);
43 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
45 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
46 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
47 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
48 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
50 static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *, struct sk_buff *);
52 static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
53 static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
54 static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
55 static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
57 static const char * const fcoe_ctlr_states[] = {
58 [FIP_ST_DISABLED] = "DISABLED",
59 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
60 [FIP_ST_AUTO] = "AUTO",
61 [FIP_ST_NON_FIP] = "NON_FIP",
62 [FIP_ST_ENABLED] = "ENABLED",
63 [FIP_ST_VNMP_START] = "VNMP_START",
64 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
65 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
66 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
67 [FIP_ST_VNMP_UP] = "VNMP_UP",
70 static const char *fcoe_ctlr_state(enum fip_state state)
72 const char *cp = "unknown";
74 if (state < ARRAY_SIZE(fcoe_ctlr_states))
75 cp = fcoe_ctlr_states[state];
82 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
83 * @fip: The FCoE controller
84 * @state: The new state
86 static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
88 if (state == fip->state)
91 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
92 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
97 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
98 * @fcf: The FCF to check
100 * Return non-zero if FCF fcoe_size has been validated.
102 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
104 return (fcf->flags & FIP_FL_SOL) != 0;
108 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
109 * @fcf: The FCF to check
111 * Return non-zero if the FCF is usable.
113 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
115 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
117 return (fcf->flags & flags) == flags;
121 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
122 * @fip: The FCoE controller
124 static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
126 if (fip->mode == FIP_MODE_VN2VN)
127 hton24(fip->dest_addr, FIP_VN_FC_MAP);
129 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
130 hton24(fip->dest_addr + 3, 0);
135 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
136 * @fip: The FCoE controller to initialize
138 void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_mode mode)
140 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
142 fip->fip_resp = false;
143 INIT_LIST_HEAD(&fip->fcfs);
144 mutex_init(&fip->ctlr_mutex);
145 spin_lock_init(&fip->ctlr_lock);
146 fip->flogi_oxid = FC_XID_UNKNOWN;
147 timer_setup(&fip->timer, fcoe_ctlr_timeout, 0);
148 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
149 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
150 skb_queue_head_init(&fip->fip_recv_list);
152 EXPORT_SYMBOL(fcoe_ctlr_init);
155 * fcoe_sysfs_fcf_add() - Add a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
156 * @new: The newly discovered FCF
158 * Called with fip->ctlr_mutex held
160 static int fcoe_sysfs_fcf_add(struct fcoe_fcf *new)
162 struct fcoe_ctlr *fip = new->fip;
163 struct fcoe_ctlr_device *ctlr_dev;
164 struct fcoe_fcf_device *temp, *fcf_dev;
167 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
168 new->fabric_name, new->fcf_mac);
170 temp = kzalloc(sizeof(*temp), GFP_KERNEL);
174 temp->fabric_name = new->fabric_name;
175 temp->switch_name = new->switch_name;
176 temp->fc_map = new->fc_map;
177 temp->vfid = new->vfid;
178 memcpy(temp->mac, new->fcf_mac, ETH_ALEN);
179 temp->priority = new->pri;
180 temp->fka_period = new->fka_period;
181 temp->selected = 0; /* default to unselected */
184 * If ctlr_dev doesn't exist then it means we're a libfcoe user
185 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device.
186 * fnic would be an example of a driver with this behavior. In this
187 * case we want to add the fcoe_fcf to the fcoe_ctlr list, but we
188 * don't want to make sysfs changes.
191 ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
193 mutex_lock(&ctlr_dev->lock);
194 fcf_dev = fcoe_fcf_device_add(ctlr_dev, temp);
195 if (unlikely(!fcf_dev)) {
197 mutex_unlock(&ctlr_dev->lock);
202 * The fcoe_sysfs layer can return a CONNECTED fcf that
203 * has a priv (fcf was never deleted) or a CONNECTED fcf
204 * that doesn't have a priv (fcf was deleted). However,
205 * libfcoe will always delete FCFs before trying to add
206 * them. This is ensured because both recv_adv and
207 * age_fcfs are protected by the the fcoe_ctlr's mutex.
208 * This means that we should never get a FCF with a
209 * non-NULL priv pointer.
211 BUG_ON(fcf_dev->priv);
214 new->fcf_dev = fcf_dev;
215 mutex_unlock(&ctlr_dev->lock);
218 list_add(&new->list, &fip->fcfs);
228 * fcoe_sysfs_fcf_del() - Remove a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
229 * @new: The FCF to be removed
231 * Called with fip->ctlr_mutex held
233 static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new)
235 struct fcoe_ctlr *fip = new->fip;
236 struct fcoe_ctlr_device *cdev;
237 struct fcoe_fcf_device *fcf_dev;
239 list_del(&new->list);
243 * If ctlr_dev doesn't exist then it means we're a libfcoe user
244 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device
245 * or a fcoe_fcf_device.
247 * fnic would be an example of a driver with this behavior. In this
248 * case we want to remove the fcoe_fcf from the fcoe_ctlr list (above),
249 * but we don't want to make sysfs changes.
251 cdev = fcoe_ctlr_to_ctlr_dev(fip);
253 mutex_lock(&cdev->lock);
254 fcf_dev = fcoe_fcf_to_fcf_dev(new);
257 fcoe_fcf_device_delete(fcf_dev);
258 mutex_unlock(&cdev->lock);
264 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
265 * @fip: The FCoE controller whose FCFs are to be reset
267 * Called with &fcoe_ctlr lock held.
269 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
271 struct fcoe_fcf *fcf;
272 struct fcoe_fcf *next;
275 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
276 fcoe_sysfs_fcf_del(fcf);
278 WARN_ON(fip->fcf_count);
284 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
285 * @fip: The FCoE controller to tear down
287 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
289 * The receive handler will have been deleted before this to guarantee
290 * that no more recv_work will be scheduled.
292 * The timer routine will simply return once we set FIP_ST_DISABLED.
293 * This guarantees that no further timeouts or work will be scheduled.
295 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
297 cancel_work_sync(&fip->recv_work);
298 skb_queue_purge(&fip->fip_recv_list);
300 mutex_lock(&fip->ctlr_mutex);
301 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
302 fcoe_ctlr_reset_fcfs(fip);
303 mutex_unlock(&fip->ctlr_mutex);
304 del_timer_sync(&fip->timer);
305 cancel_work_sync(&fip->timer_work);
307 EXPORT_SYMBOL(fcoe_ctlr_destroy);
310 * fcoe_ctlr_announce() - announce new FCF selection
311 * @fip: The FCoE controller
313 * Also sets the destination MAC for FCoE and control packets
315 * Called with neither ctlr_mutex nor ctlr_lock held.
317 static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
319 struct fcoe_fcf *sel;
320 struct fcoe_fcf *fcf;
322 mutex_lock(&fip->ctlr_mutex);
323 spin_lock_bh(&fip->ctlr_lock);
325 kfree_skb(fip->flogi_req);
326 fip->flogi_req = NULL;
327 list_for_each_entry(fcf, &fip->fcfs, list)
330 spin_unlock_bh(&fip->ctlr_lock);
333 if (sel && ether_addr_equal(sel->fcf_mac, fip->dest_addr))
335 if (!is_zero_ether_addr(fip->dest_addr)) {
336 printk(KERN_NOTICE "libfcoe: host%d: "
337 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
338 fip->lp->host->host_no, fip->dest_addr);
339 memset(fip->dest_addr, 0, ETH_ALEN);
342 printk(KERN_INFO "libfcoe: host%d: FIP selected "
343 "Fibre-Channel Forwarder MAC %pM\n",
344 fip->lp->host->host_no, sel->fcf_mac);
345 memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
349 mutex_unlock(&fip->ctlr_mutex);
353 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
354 * @fip: The FCoE controller to get the maximum FCoE size from
356 * Returns the maximum packet size including the FCoE header and trailer,
357 * but not including any Ethernet or VLAN headers.
359 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
362 * Determine the max FCoE frame size allowed, including
363 * FCoE header and trailer.
364 * Note: lp->mfs is currently the payload size, not the frame size.
366 return fip->lp->mfs + sizeof(struct fc_frame_header) +
367 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
371 * fcoe_ctlr_solicit() - Send a FIP solicitation
372 * @fip: The FCoE controller to send the solicitation on
373 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
375 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
380 struct fip_header fip;
382 struct fip_mac_desc mac;
383 struct fip_wwn_desc wwnn;
384 struct fip_size_desc size;
389 skb = dev_alloc_skb(sizeof(*sol));
393 sol = (struct fip_sol *)skb->data;
395 memset(sol, 0, sizeof(*sol));
396 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
397 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
398 sol->eth.h_proto = htons(ETH_P_FIP);
400 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
401 sol->fip.fip_op = htons(FIP_OP_DISC);
402 sol->fip.fip_subcode = FIP_SC_SOL;
403 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
404 sol->fip.fip_flags = htons(FIP_FL_FPMA);
406 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
408 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
409 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
410 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
412 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
413 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
414 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
416 fcoe_size = fcoe_ctlr_fcoe_size(fip);
417 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
418 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
419 sol->desc.size.fd_size = htons(fcoe_size);
421 skb_put(skb, sizeof(*sol));
422 skb->protocol = htons(ETH_P_FIP);
423 skb->priority = fip->priority;
424 skb_reset_mac_header(skb);
425 skb_reset_network_header(skb);
429 fip->sol_time = jiffies;
433 * fcoe_ctlr_link_up() - Start FCoE controller
434 * @fip: The FCoE controller to start
436 * Called from the LLD when the network link is ready.
438 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
440 mutex_lock(&fip->ctlr_mutex);
441 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
442 mutex_unlock(&fip->ctlr_mutex);
444 } else if (fip->state == FIP_ST_LINK_WAIT) {
445 if (fip->mode == FIP_MODE_NON_FIP)
446 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
448 fcoe_ctlr_set_state(fip, FIP_ST_AUTO);
451 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
454 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
456 case FIP_MODE_FABRIC:
457 case FIP_MODE_NON_FIP:
458 mutex_unlock(&fip->ctlr_mutex);
460 fcoe_ctlr_solicit(fip, NULL);
463 fcoe_ctlr_vn_start(fip);
464 mutex_unlock(&fip->ctlr_mutex);
469 mutex_unlock(&fip->ctlr_mutex);
471 EXPORT_SYMBOL(fcoe_ctlr_link_up);
474 * fcoe_ctlr_reset() - Reset a FCoE controller
475 * @fip: The FCoE controller to reset
477 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
479 fcoe_ctlr_reset_fcfs(fip);
480 del_timer(&fip->timer);
481 fip->ctlr_ka_time = 0;
482 fip->port_ka_time = 0;
484 fip->flogi_oxid = FC_XID_UNKNOWN;
485 fcoe_ctlr_map_dest(fip);
489 * fcoe_ctlr_link_down() - Stop a FCoE controller
490 * @fip: The FCoE controller to be stopped
492 * Returns non-zero if the link was up and now isn't.
494 * Called from the LLD when the network link is not ready.
495 * There may be multiple calls while the link is down.
497 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
501 LIBFCOE_FIP_DBG(fip, "link down.\n");
502 mutex_lock(&fip->ctlr_mutex);
503 fcoe_ctlr_reset(fip);
504 link_dropped = fip->state != FIP_ST_LINK_WAIT;
505 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
506 mutex_unlock(&fip->ctlr_mutex);
509 fc_linkdown(fip->lp);
512 EXPORT_SYMBOL(fcoe_ctlr_link_down);
515 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
516 * @fip: The FCoE controller to send the FKA on
517 * @lport: libfc fc_lport to send from
518 * @ports: 0 for controller keep-alive, 1 for port keep-alive
519 * @sa: The source MAC address
521 * A controller keep-alive is sent every fka_period (typically 8 seconds).
522 * The source MAC is the native MAC address.
524 * A port keep-alive is sent every 90 seconds while logged in.
525 * The source MAC is the assigned mapped source address.
526 * The destination is the FCF's F-port.
528 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
529 struct fc_lport *lport,
535 struct fip_header fip;
536 struct fip_mac_desc mac;
538 struct fip_vn_desc *vn;
541 struct fcoe_fcf *fcf;
545 if (!fcf || (ports && !lp->port_id))
548 len = sizeof(*kal) + ports * sizeof(*vn);
549 skb = dev_alloc_skb(len);
553 kal = (struct fip_kal *)skb->data;
555 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
556 memcpy(kal->eth.h_source, sa, ETH_ALEN);
557 kal->eth.h_proto = htons(ETH_P_FIP);
559 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
560 kal->fip.fip_op = htons(FIP_OP_CTRL);
561 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
562 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
563 ports * sizeof(*vn)) / FIP_BPW);
564 kal->fip.fip_flags = htons(FIP_FL_FPMA);
566 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
568 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
569 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
570 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
572 vn = (struct fip_vn_desc *)(kal + 1);
573 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
574 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
575 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
576 hton24(vn->fd_fc_id, lport->port_id);
577 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
580 skb->protocol = htons(ETH_P_FIP);
581 skb->priority = fip->priority;
582 skb_reset_mac_header(skb);
583 skb_reset_network_header(skb);
588 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
589 * @fip: The FCoE controller for the ELS frame
590 * @dtype: The FIP descriptor type for the frame
591 * @skb: The FCoE ELS frame including FC header but no FCoE headers
592 * @d_id: The destination port ID.
594 * Returns non-zero error code on failure.
596 * The caller must check that the length is a multiple of 4.
598 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
599 * Headroom includes the FIP encapsulation description, FIP header, and
600 * Ethernet header. The tailroom is for the FIP MAC descriptor.
602 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
603 u8 dtype, struct sk_buff *skb, u32 d_id)
605 struct fip_encaps_head {
607 struct fip_header fip;
608 struct fip_encaps encaps;
610 struct fc_frame_header *fh;
611 struct fip_mac_desc *mac;
612 struct fcoe_fcf *fcf;
617 fh = (struct fc_frame_header *)skb->data;
618 op = *(u8 *)(fh + 1);
619 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
620 cap = skb_push(skb, sizeof(*cap));
621 memset(cap, 0, sizeof(*cap));
623 if (lport->point_to_multipoint) {
624 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
631 fip_flags = fcf->flags;
632 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
636 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
638 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
639 cap->eth.h_proto = htons(ETH_P_FIP);
641 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
642 cap->fip.fip_op = htons(FIP_OP_LS);
643 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
644 cap->fip.fip_subcode = FIP_SC_REP;
646 cap->fip.fip_subcode = FIP_SC_REQ;
647 cap->fip.fip_flags = htons(fip_flags);
649 cap->encaps.fd_desc.fip_dtype = dtype;
650 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
652 if (op != ELS_LS_RJT) {
653 dlen += sizeof(*mac);
654 mac = skb_put_zero(skb, sizeof(*mac));
655 mac->fd_desc.fip_dtype = FIP_DT_MAC;
656 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
657 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
658 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
659 } else if (fip->mode == FIP_MODE_VN2VN) {
660 hton24(mac->fd_mac, FIP_VN_FC_MAP);
661 hton24(mac->fd_mac + 3, fip->port_id);
662 } else if (fip_flags & FIP_FL_SPMA) {
663 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
664 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
666 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
667 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
670 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
672 skb->protocol = htons(ETH_P_FIP);
673 skb->priority = fip->priority;
674 skb_reset_mac_header(skb);
675 skb_reset_network_header(skb);
680 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
681 * @fip: FCoE controller.
682 * @lport: libfc fc_lport to send from
683 * @skb: FCoE ELS frame including FC header but no FCoE headers.
685 * Returns a non-zero error code if the frame should not be sent.
686 * Returns zero if the caller should send the frame with FCoE encapsulation.
688 * The caller must check that the length is a multiple of 4.
689 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
690 * The the skb must also be an fc_frame.
692 * This is called from the lower-level driver with spinlocks held,
693 * so we must not take a mutex here.
695 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
699 struct fc_frame_header *fh;
704 fp = container_of(skb, struct fc_frame, skb);
705 fh = (struct fc_frame_header *)skb->data;
706 op = *(u8 *)(fh + 1);
708 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
709 old_xid = fip->flogi_oxid;
710 fip->flogi_oxid = ntohs(fh->fh_ox_id);
711 if (fip->state == FIP_ST_AUTO) {
712 if (old_xid == FC_XID_UNKNOWN)
713 fip->flogi_count = 0;
715 if (fip->flogi_count < 3)
717 fcoe_ctlr_map_dest(fip);
720 if (fip->state == FIP_ST_NON_FIP)
721 fcoe_ctlr_map_dest(fip);
724 if (fip->state == FIP_ST_NON_FIP)
726 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
731 if (fip->mode == FIP_MODE_VN2VN)
733 spin_lock_bh(&fip->ctlr_lock);
734 kfree_skb(fip->flogi_req);
735 fip->flogi_req = skb;
736 fip->flogi_req_send = 1;
737 spin_unlock_bh(&fip->ctlr_lock);
738 schedule_work(&fip->timer_work);
741 if (ntoh24(fh->fh_s_id))
746 if (fip->mode == FIP_MODE_VN2VN) {
747 if (fip->state != FIP_ST_VNMP_UP)
749 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
752 if (fip->state != FIP_ST_ENABLED)
754 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
761 * If non-FIP, we may have gotten an SID by accepting an FLOGI
762 * from a point-to-point connection. Switch to using
763 * the source mac based on the SID. The destination
764 * MAC in this case would have been set by receiving the
767 if (fip->state == FIP_ST_NON_FIP) {
768 if (fip->flogi_oxid == FC_XID_UNKNOWN)
770 fip->flogi_oxid = FC_XID_UNKNOWN;
771 fc_fcoe_set_mac(mac, fh->fh_d_id);
772 fip->update_mac(lport, mac);
781 if (fip->state != FIP_ST_ENABLED &&
782 fip->state != FIP_ST_VNMP_UP)
786 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
787 op, ntoh24(fh->fh_d_id));
788 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
793 LIBFCOE_FIP_DBG(fip, "drop els_send op %u d_id %x\n",
794 op, ntoh24(fh->fh_d_id));
798 EXPORT_SYMBOL(fcoe_ctlr_els_send);
801 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
802 * @fip: The FCoE controller to free FCFs on
804 * Called with lock held and preemption disabled.
806 * An FCF is considered old if we have missed two advertisements.
807 * That is, there have been no valid advertisement from it for 2.5
808 * times its keep-alive period.
810 * In addition, determine the time when an FCF selection can occur.
812 * Also, increment the MissDiscAdvCount when no advertisement is received
813 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
815 * Returns the time in jiffies for the next call.
817 static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
819 struct fcoe_fcf *fcf;
820 struct fcoe_fcf *next;
821 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
822 unsigned long deadline;
823 unsigned long sel_time = 0;
824 struct list_head del_list;
825 struct fc_stats *stats;
827 INIT_LIST_HEAD(&del_list);
829 stats = per_cpu_ptr(fip->lp->stats, get_cpu());
831 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
832 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
833 if (fip->sel_fcf == fcf) {
834 if (time_after(jiffies, deadline)) {
835 stats->MissDiscAdvCount++;
836 printk(KERN_INFO "libfcoe: host%d: "
837 "Missing Discovery Advertisement "
838 "for fab %16.16llx count %lld\n",
839 fip->lp->host->host_no, fcf->fabric_name,
840 stats->MissDiscAdvCount);
841 } else if (time_after(next_timer, deadline))
842 next_timer = deadline;
845 deadline += fcf->fka_period;
846 if (time_after_eq(jiffies, deadline)) {
847 if (fip->sel_fcf == fcf)
850 * Move to delete list so we can call
851 * fcoe_sysfs_fcf_del (which can sleep)
852 * after the put_cpu().
854 list_del(&fcf->list);
855 list_add(&fcf->list, &del_list);
856 stats->VLinkFailureCount++;
858 if (time_after(next_timer, deadline))
859 next_timer = deadline;
860 if (fcoe_ctlr_mtu_valid(fcf) &&
861 (!sel_time || time_before(sel_time, fcf->time)))
862 sel_time = fcf->time;
867 list_for_each_entry_safe(fcf, next, &del_list, list) {
868 /* Removes fcf from current list */
869 fcoe_sysfs_fcf_del(fcf);
872 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
873 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
874 fip->sel_time = sel_time;
881 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
882 * @fip: The FCoE controller receiving the advertisement
883 * @skb: The received FIP advertisement frame
884 * @fcf: The resulting FCF entry
886 * Returns zero on a valid parsed advertisement,
887 * otherwise returns non zero value.
889 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
890 struct sk_buff *skb, struct fcoe_fcf *fcf)
892 struct fip_header *fiph;
893 struct fip_desc *desc = NULL;
894 struct fip_wwn_desc *wwn;
895 struct fip_fab_desc *fab;
896 struct fip_fka_desc *fka;
902 memset(fcf, 0, sizeof(*fcf));
903 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
905 fiph = (struct fip_header *)skb->data;
906 fcf->flags = ntohs(fiph->fip_flags);
909 * mask of required descriptors. validating each one clears its bit.
911 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
912 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
914 rlen = ntohs(fiph->fip_dl_len) * 4;
915 if (rlen + sizeof(*fiph) > skb->len)
918 desc = (struct fip_desc *)(fiph + 1);
920 dlen = desc->fip_dlen * FIP_BPW;
921 if (dlen < sizeof(*desc) || dlen > rlen)
923 /* Drop Adv if there are duplicate critical descriptors */
924 if ((desc->fip_dtype < 32) &&
925 !(desc_mask & 1U << desc->fip_dtype)) {
926 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
927 "Descriptors in FIP adv\n");
930 switch (desc->fip_dtype) {
932 if (dlen != sizeof(struct fip_pri_desc))
934 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
935 desc_mask &= ~BIT(FIP_DT_PRI);
938 if (dlen != sizeof(struct fip_mac_desc))
941 ((struct fip_mac_desc *)desc)->fd_mac,
943 memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
944 if (!is_valid_ether_addr(fcf->fcf_mac)) {
946 "Invalid MAC addr %pM in FIP adv\n",
950 desc_mask &= ~BIT(FIP_DT_MAC);
953 if (dlen != sizeof(struct fip_wwn_desc))
955 wwn = (struct fip_wwn_desc *)desc;
956 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
957 desc_mask &= ~BIT(FIP_DT_NAME);
960 if (dlen != sizeof(struct fip_fab_desc))
962 fab = (struct fip_fab_desc *)desc;
963 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
964 fcf->vfid = ntohs(fab->fd_vfid);
965 fcf->fc_map = ntoh24(fab->fd_map);
966 desc_mask &= ~BIT(FIP_DT_FAB);
969 if (dlen != sizeof(struct fip_fka_desc))
971 fka = (struct fip_fka_desc *)desc;
972 if (fka->fd_flags & FIP_FKA_ADV_D)
974 t = ntohl(fka->fd_fka_period);
975 if (t >= FCOE_CTLR_MIN_FKA)
976 fcf->fka_period = msecs_to_jiffies(t);
977 desc_mask &= ~BIT(FIP_DT_FKA);
980 case FIP_DT_FCOE_SIZE:
986 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
987 "in FIP adv\n", desc->fip_dtype);
988 /* standard says ignore unknown descriptors >= 128 */
989 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
993 desc = (struct fip_desc *)((char *)desc + dlen);
996 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
998 if (!fcf->switch_name)
1001 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
1008 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1009 desc->fip_dtype, dlen);
1014 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
1015 * @fip: The FCoE controller receiving the advertisement
1016 * @skb: The received FIP packet
1018 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1020 struct fcoe_fcf *fcf;
1021 struct fcoe_fcf new;
1022 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTLR_SOL_TOV);
1028 if (fcoe_ctlr_parse_adv(fip, skb, &new))
1031 mutex_lock(&fip->ctlr_mutex);
1032 first = list_empty(&fip->fcfs);
1033 list_for_each_entry(fcf, &fip->fcfs, list) {
1034 if (fcf->switch_name == new.switch_name &&
1035 fcf->fabric_name == new.fabric_name &&
1036 fcf->fc_map == new.fc_map &&
1037 ether_addr_equal(fcf->fcf_mac, new.fcf_mac)) {
1043 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
1046 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
1050 memcpy(fcf, &new, sizeof(new));
1052 rc = fcoe_sysfs_fcf_add(fcf);
1054 printk(KERN_ERR "Failed to allocate sysfs instance "
1055 "for FCF, fab %16.16llx mac %pM\n",
1056 new.fabric_name, new.fcf_mac);
1062 * Update the FCF's keep-alive descriptor flags.
1063 * Other flag changes from new advertisements are
1064 * ignored after a solicited advertisement is
1065 * received and the FCF is selectable (usable).
1067 fcf->fd_flags = new.fd_flags;
1068 if (!fcoe_ctlr_fcf_usable(fcf))
1069 fcf->flags = new.flags;
1071 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
1072 fip->ctlr_ka_time -= fcf->fka_period;
1073 fip->ctlr_ka_time += new.fka_period;
1074 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1075 mod_timer(&fip->timer, fip->ctlr_ka_time);
1077 fcf->fka_period = new.fka_period;
1078 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
1081 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
1082 fcf->time = jiffies;
1084 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
1085 fcf->fabric_name, fcf->fcf_mac);
1088 * If this advertisement is not solicited and our max receive size
1089 * hasn't been verified, send a solicited advertisement.
1092 fcoe_ctlr_solicit(fip, fcf);
1095 * If its been a while since we did a solicit, and this is
1096 * the first advertisement we've received, do a multicast
1097 * solicitation to gather as many advertisements as we can
1098 * before selection occurs.
1100 if (first && time_after(jiffies, fip->sol_time + sol_tov))
1101 fcoe_ctlr_solicit(fip, NULL);
1104 * Put this FCF at the head of the list for priority among equals.
1105 * This helps in the case of an NPV switch which insists we use
1106 * the FCF that answers multicast solicitations, not the others that
1107 * are sending periodic multicast advertisements.
1110 list_move(&fcf->list, &fip->fcfs);
1113 * If this is the first validated FCF, note the time and
1114 * set a timer to trigger selection.
1116 if (mtu_valid && !fip->sel_fcf && !fip->sel_time &&
1117 fcoe_ctlr_fcf_usable(fcf)) {
1118 fip->sel_time = jiffies +
1119 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1120 if (!timer_pending(&fip->timer) ||
1121 time_before(fip->sel_time, fip->timer.expires))
1122 mod_timer(&fip->timer, fip->sel_time);
1126 mutex_unlock(&fip->ctlr_mutex);
1130 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1131 * @fip: The FCoE controller which received the packet
1132 * @skb: The received FIP packet
1134 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1136 struct fc_lport *lport = fip->lp;
1137 struct fip_header *fiph;
1138 struct fc_frame *fp = (struct fc_frame *)skb;
1139 struct fc_frame_header *fh = NULL;
1140 struct fip_desc *desc;
1141 struct fip_encaps *els;
1142 struct fcoe_fcf *sel;
1143 struct fc_stats *stats;
1144 enum fip_desc_type els_dtype = 0;
1147 u8 granted_mac[ETH_ALEN] = { 0 };
1154 fiph = (struct fip_header *)skb->data;
1155 sub = fiph->fip_subcode;
1156 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1159 rlen = ntohs(fiph->fip_dl_len) * 4;
1160 if (rlen + sizeof(*fiph) > skb->len)
1163 desc = (struct fip_desc *)(fiph + 1);
1166 dlen = desc->fip_dlen * FIP_BPW;
1167 if (dlen < sizeof(*desc) || dlen > rlen)
1169 /* Drop ELS if there are duplicate critical descriptors */
1170 if (desc->fip_dtype < 32) {
1171 if ((desc->fip_dtype != FIP_DT_MAC) &&
1172 (desc_mask & 1U << desc->fip_dtype)) {
1173 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1174 "Descriptors in FIP ELS\n");
1177 desc_mask |= (1 << desc->fip_dtype);
1179 switch (desc->fip_dtype) {
1182 if (desc_cnt == 1) {
1183 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1184 "received out of order\n");
1188 * Some switch implementations send two MAC descriptors,
1189 * with first MAC(granted_mac) being the FPMA, and the
1190 * second one(fcoe_mac) is used as destination address
1191 * for sending/receiving FCoE packets. FIP traffic is
1192 * sent using fip_mac. For regular switches, both
1193 * fip_mac and fcoe_mac would be the same.
1197 ((struct fip_mac_desc *)desc)->fd_mac,
1200 if (dlen != sizeof(struct fip_mac_desc))
1203 if ((desc_cnt == 3) && (sel))
1204 memcpy(sel->fcoe_mac,
1205 ((struct fip_mac_desc *)desc)->fd_mac,
1212 if (desc_cnt != 1) {
1213 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1214 "received out of order\n");
1219 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1221 els_len = dlen - sizeof(*els);
1222 els = (struct fip_encaps *)desc;
1223 fh = (struct fc_frame_header *)(els + 1);
1224 els_dtype = desc->fip_dtype;
1227 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1228 "in FIP adv\n", desc->fip_dtype);
1229 /* standard says ignore unknown descriptors >= 128 */
1230 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1232 if (desc_cnt <= 2) {
1233 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1234 "received out of order\n");
1239 desc = (struct fip_desc *)((char *)desc + dlen);
1245 els_op = *(u8 *)(fh + 1);
1247 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1248 sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1249 if (els_op == ELS_LS_ACC) {
1250 if (!is_valid_ether_addr(granted_mac)) {
1251 LIBFCOE_FIP_DBG(fip,
1252 "Invalid MAC address %pM in FIP ELS\n",
1256 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1258 if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1259 fip->flogi_oxid = FC_XID_UNKNOWN;
1260 if (els_dtype == FIP_DT_FLOGI)
1261 fcoe_ctlr_announce(fip);
1263 } else if (els_dtype == FIP_DT_FLOGI &&
1264 !fcoe_ctlr_flogi_retry(fip))
1265 goto drop; /* retrying FLOGI so drop reject */
1268 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1269 (!(1U << FIP_DT_MAC & desc_mask)))) {
1270 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1276 * Convert skb into an fc_frame containing only the ELS.
1278 skb_pull(skb, (u8 *)fh - skb->data);
1279 skb_trim(skb, els_len);
1280 fp = (struct fc_frame *)skb;
1282 fr_sof(fp) = FC_SOF_I3;
1283 fr_eof(fp) = FC_EOF_T;
1285 fr_encaps(fp) = els_dtype;
1287 stats = per_cpu_ptr(lport->stats, get_cpu());
1289 stats->RxWords += skb->len / FIP_BPW;
1292 fc_exch_recv(lport, fp);
1296 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1297 desc->fip_dtype, dlen);
1303 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1304 * @fip: The FCoE controller that received the frame
1305 * @fh: The received FIP header
1307 * There may be multiple VN_Port descriptors.
1308 * The overall length has already been checked.
1310 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1311 struct sk_buff *skb)
1313 struct fip_desc *desc;
1314 struct fip_mac_desc *mp;
1315 struct fip_wwn_desc *wp;
1316 struct fip_vn_desc *vp;
1319 struct fcoe_fcf *fcf = fip->sel_fcf;
1320 struct fc_lport *lport = fip->lp;
1321 struct fc_lport *vn_port = NULL;
1324 int reset_phys_port = 0;
1325 struct fip_vn_desc **vlink_desc_arr = NULL;
1326 struct fip_header *fh = (struct fip_header *)skb->data;
1327 struct ethhdr *eh = eth_hdr(skb);
1329 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1333 * We are yet to select best FCF, but we got CVL in the
1334 * meantime. reset the ctlr and let it rediscover the FCF
1336 LIBFCOE_FIP_DBG(fip, "Resetting fcoe_ctlr as FCF has not been "
1338 mutex_lock(&fip->ctlr_mutex);
1339 fcoe_ctlr_reset(fip);
1340 mutex_unlock(&fip->ctlr_mutex);
1345 * If we've selected an FCF check that the CVL is from there to avoid
1346 * processing CVLs from an unexpected source. If it is from an
1347 * unexpected source drop it on the floor.
1349 if (!ether_addr_equal(eh->h_source, fcf->fcf_mac)) {
1350 LIBFCOE_FIP_DBG(fip, "Dropping CVL due to source address "
1351 "mismatch with FCF src=%pM\n", eh->h_source);
1356 * If we haven't logged into the fabric but receive a CVL we should
1357 * reset everything and go back to solicitation.
1359 if (!lport->port_id) {
1360 LIBFCOE_FIP_DBG(fip, "lport not logged in, resoliciting\n");
1361 mutex_lock(&fip->ctlr_mutex);
1362 fcoe_ctlr_reset(fip);
1363 mutex_unlock(&fip->ctlr_mutex);
1364 fc_lport_reset(fip->lp);
1365 fcoe_ctlr_solicit(fip, NULL);
1370 * mask of required descriptors. Validating each one clears its bit.
1372 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
1374 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1375 desc = (struct fip_desc *)(fh + 1);
1378 * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1379 * before determining max Vx_Port descriptor but a buggy FCF could have
1380 * omitted either or both MAC Address and Name Identifier descriptors
1382 num_vlink_desc = rlen / sizeof(*vp);
1384 vlink_desc_arr = kmalloc_array(num_vlink_desc, sizeof(vp),
1386 if (!vlink_desc_arr)
1390 while (rlen >= sizeof(*desc)) {
1391 dlen = desc->fip_dlen * FIP_BPW;
1394 /* Drop CVL if there are duplicate critical descriptors */
1395 if ((desc->fip_dtype < 32) &&
1396 (desc->fip_dtype != FIP_DT_VN_ID) &&
1397 !(desc_mask & 1U << desc->fip_dtype)) {
1398 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1399 "Descriptors in FIP CVL\n");
1402 switch (desc->fip_dtype) {
1404 mp = (struct fip_mac_desc *)desc;
1405 if (dlen < sizeof(*mp))
1407 if (!ether_addr_equal(mp->fd_mac, fcf->fcf_mac))
1409 desc_mask &= ~BIT(FIP_DT_MAC);
1412 wp = (struct fip_wwn_desc *)desc;
1413 if (dlen < sizeof(*wp))
1415 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1417 desc_mask &= ~BIT(FIP_DT_NAME);
1420 vp = (struct fip_vn_desc *)desc;
1421 if (dlen < sizeof(*vp))
1423 vlink_desc_arr[num_vlink_desc++] = vp;
1424 vn_port = fc_vport_id_lookup(lport,
1425 ntoh24(vp->fd_fc_id));
1426 if (vn_port && (vn_port == lport)) {
1427 mutex_lock(&fip->ctlr_mutex);
1428 per_cpu_ptr(lport->stats,
1429 get_cpu())->VLinkFailureCount++;
1431 fcoe_ctlr_reset(fip);
1432 mutex_unlock(&fip->ctlr_mutex);
1436 /* standard says ignore unknown descriptors >= 128 */
1437 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1441 desc = (struct fip_desc *)((char *)desc + dlen);
1446 * reset only if all required descriptors were present and valid.
1449 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1451 else if (!num_vlink_desc) {
1452 LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1454 * No Vx_Port description. Clear all NPIV ports,
1455 * followed by physical port
1457 mutex_lock(&fip->ctlr_mutex);
1458 per_cpu_ptr(lport->stats, get_cpu())->VLinkFailureCount++;
1460 fcoe_ctlr_reset(fip);
1461 mutex_unlock(&fip->ctlr_mutex);
1463 mutex_lock(&lport->lp_mutex);
1464 list_for_each_entry(vn_port, &lport->vports, list)
1465 fc_lport_reset(vn_port);
1466 mutex_unlock(&lport->lp_mutex);
1468 fc_lport_reset(fip->lp);
1469 fcoe_ctlr_solicit(fip, NULL);
1473 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1474 for (i = 0; i < num_vlink_desc; i++) {
1475 vp = vlink_desc_arr[i];
1476 vn_port = fc_vport_id_lookup(lport,
1477 ntoh24(vp->fd_fc_id));
1482 * 'port_id' is already validated, check MAC address and
1485 if (!ether_addr_equal(fip->get_src_addr(vn_port),
1487 get_unaligned_be64(&vp->fd_wwpn) !=
1491 if (vn_port == lport)
1493 * Physical port, defer processing till all
1494 * listed NPIV ports are cleared
1496 reset_phys_port = 1;
1497 else /* NPIV port */
1498 fc_lport_reset(vn_port);
1501 if (reset_phys_port) {
1502 fc_lport_reset(fip->lp);
1503 fcoe_ctlr_solicit(fip, NULL);
1508 kfree(vlink_desc_arr);
1512 * fcoe_ctlr_recv() - Receive a FIP packet
1513 * @fip: The FCoE controller that received the packet
1514 * @skb: The received FIP packet
1516 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1518 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1520 skb = skb_share_check(skb, GFP_ATOMIC);
1523 skb_queue_tail(&fip->fip_recv_list, skb);
1524 schedule_work(&fip->recv_work);
1526 EXPORT_SYMBOL(fcoe_ctlr_recv);
1529 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1530 * @fip: The FCoE controller that received the frame
1531 * @skb: The received FIP frame
1533 * Returns non-zero if the frame is dropped.
1535 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1537 struct fip_header *fiph;
1539 enum fip_state state;
1540 bool fip_vlan_resp = false;
1544 if (skb_linearize(skb))
1546 if (skb->len < sizeof(*fiph))
1549 if (fip->mode == FIP_MODE_VN2VN) {
1550 if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1551 !ether_addr_equal(eh->h_dest, fcoe_all_vn2vn) &&
1552 !ether_addr_equal(eh->h_dest, fcoe_all_p2p))
1554 } else if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1555 !ether_addr_equal(eh->h_dest, fcoe_all_enode))
1557 fiph = (struct fip_header *)skb->data;
1558 op = ntohs(fiph->fip_op);
1559 sub = fiph->fip_subcode;
1561 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1563 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1566 mutex_lock(&fip->ctlr_mutex);
1568 if (state == FIP_ST_AUTO) {
1570 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1571 state = FIP_ST_ENABLED;
1572 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1574 fip_vlan_resp = fip->fip_resp;
1575 mutex_unlock(&fip->ctlr_mutex);
1577 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1578 return fcoe_ctlr_vn_recv(fip, skb);
1580 if (fip_vlan_resp && op == FIP_OP_VLAN) {
1581 LIBFCOE_FIP_DBG(fip, "fip vlan discovery\n");
1582 return fcoe_ctlr_vlan_recv(fip, skb);
1585 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1586 state != FIP_ST_VNMP_CLAIM)
1589 if (op == FIP_OP_LS) {
1590 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1594 if (state != FIP_ST_ENABLED)
1597 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1598 fcoe_ctlr_recv_adv(fip, skb);
1599 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1600 fcoe_ctlr_recv_clr_vlink(fip, skb);
1609 * fcoe_ctlr_select() - Select the best FCF (if possible)
1610 * @fip: The FCoE controller
1612 * Returns the selected FCF, or NULL if none are usable.
1614 * If there are conflicting advertisements, no FCF can be chosen.
1616 * If there is already a selected FCF, this will choose a better one or
1617 * an equivalent one that hasn't already been sent a FLOGI.
1619 * Called with lock held.
1621 static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
1623 struct fcoe_fcf *fcf;
1624 struct fcoe_fcf *best = fip->sel_fcf;
1626 list_for_each_entry(fcf, &fip->fcfs, list) {
1627 LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1628 "VFID %d mac %pM map %x val %d "
1630 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1631 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1632 fcf->flogi_sent, fcf->pri);
1633 if (!fcoe_ctlr_fcf_usable(fcf)) {
1634 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1635 "map %x %svalid %savailable\n",
1636 fcf->fabric_name, fcf->fc_map,
1637 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1638 (fcf->flags & FIP_FL_AVAIL) ?
1642 if (!best || fcf->pri < best->pri || best->flogi_sent)
1644 if (fcf->fabric_name != best->fabric_name ||
1645 fcf->vfid != best->vfid ||
1646 fcf->fc_map != best->fc_map) {
1647 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1652 fip->sel_fcf = best;
1654 LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
1655 fip->port_ka_time = jiffies +
1656 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1657 fip->ctlr_ka_time = jiffies + best->fka_period;
1658 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1659 mod_timer(&fip->timer, fip->ctlr_ka_time);
1665 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1666 * @fip: The FCoE controller
1668 * Returns non-zero error if it could not be sent.
1670 * Called with ctlr_mutex and ctlr_lock held.
1671 * Caller must verify that fip->sel_fcf is not NULL.
1673 static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1675 struct sk_buff *skb;
1676 struct sk_buff *skb_orig;
1677 struct fc_frame_header *fh;
1680 skb_orig = fip->flogi_req;
1685 * Clone and send the FLOGI request. If clone fails, use original.
1687 skb = skb_clone(skb_orig, GFP_ATOMIC);
1690 fip->flogi_req = NULL;
1692 fh = (struct fc_frame_header *)skb->data;
1693 error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1694 ntoh24(fh->fh_d_id));
1699 fip->send(fip, skb);
1700 fip->sel_fcf->flogi_sent = 1;
1705 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1706 * @fip: The FCoE controller
1708 * Returns non-zero error code if there's no FLOGI request to retry or
1709 * no alternate FCF available.
1711 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1713 struct fcoe_fcf *fcf;
1716 mutex_lock(&fip->ctlr_mutex);
1717 spin_lock_bh(&fip->ctlr_lock);
1718 LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1719 fcf = fcoe_ctlr_select(fip);
1720 if (!fcf || fcf->flogi_sent) {
1721 kfree_skb(fip->flogi_req);
1722 fip->flogi_req = NULL;
1725 fcoe_ctlr_solicit(fip, NULL);
1726 error = fcoe_ctlr_flogi_send_locked(fip);
1728 spin_unlock_bh(&fip->ctlr_lock);
1729 mutex_unlock(&fip->ctlr_mutex);
1735 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1736 * @fip: The FCoE controller that timed out
1738 * Done here because fcoe_ctlr_els_send() can't get mutex.
1740 * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
1742 static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1744 struct fcoe_fcf *fcf;
1746 spin_lock_bh(&fip->ctlr_lock);
1748 if (!fcf || !fip->flogi_req_send)
1751 LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1754 * If this FLOGI is being sent due to a timeout retry
1755 * to the same FCF as before, select a different FCF if possible.
1757 if (fcf->flogi_sent) {
1758 LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1759 fcf = fcoe_ctlr_select(fip);
1760 if (!fcf || fcf->flogi_sent) {
1761 LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1762 list_for_each_entry(fcf, &fip->fcfs, list)
1763 fcf->flogi_sent = 0;
1764 fcf = fcoe_ctlr_select(fip);
1768 fcoe_ctlr_flogi_send_locked(fip);
1769 fip->flogi_req_send = 0;
1771 LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1773 spin_unlock_bh(&fip->ctlr_lock);
1777 * fcoe_ctlr_timeout() - FIP timeout handler
1778 * @arg: The FCoE controller that timed out
1780 static void fcoe_ctlr_timeout(struct timer_list *t)
1782 struct fcoe_ctlr *fip = from_timer(fip, t, timer);
1784 schedule_work(&fip->timer_work);
1788 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1789 * @work: Handle to a FCoE controller
1791 * Ages FCFs. Triggers FCF selection if possible.
1792 * Sends keep-alives and resets.
1794 static void fcoe_ctlr_timer_work(struct work_struct *work)
1796 struct fcoe_ctlr *fip;
1797 struct fc_lport *vport;
1800 u8 send_ctlr_ka = 0;
1801 u8 send_port_ka = 0;
1802 struct fcoe_fcf *sel;
1803 struct fcoe_fcf *fcf;
1804 unsigned long next_timer;
1806 fip = container_of(work, struct fcoe_ctlr, timer_work);
1807 if (fip->mode == FIP_MODE_VN2VN)
1808 return fcoe_ctlr_vn_timeout(fip);
1809 mutex_lock(&fip->ctlr_mutex);
1810 if (fip->state == FIP_ST_DISABLED) {
1811 mutex_unlock(&fip->ctlr_mutex);
1816 next_timer = fcoe_ctlr_age_fcfs(fip);
1819 if (!sel && fip->sel_time) {
1820 if (time_after_eq(jiffies, fip->sel_time)) {
1821 sel = fcoe_ctlr_select(fip);
1823 } else if (time_after(next_timer, fip->sel_time))
1824 next_timer = fip->sel_time;
1827 if (sel && fip->flogi_req_send)
1828 fcoe_ctlr_flogi_send(fip);
1829 else if (!sel && fcf)
1832 if (sel && !sel->fd_flags) {
1833 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1834 fip->ctlr_ka_time = jiffies + sel->fka_period;
1837 if (time_after(next_timer, fip->ctlr_ka_time))
1838 next_timer = fip->ctlr_ka_time;
1840 if (time_after_eq(jiffies, fip->port_ka_time)) {
1841 fip->port_ka_time = jiffies +
1842 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1845 if (time_after(next_timer, fip->port_ka_time))
1846 next_timer = fip->port_ka_time;
1848 if (!list_empty(&fip->fcfs))
1849 mod_timer(&fip->timer, next_timer);
1850 mutex_unlock(&fip->ctlr_mutex);
1853 fc_lport_reset(fip->lp);
1854 /* restart things with a solicitation */
1855 fcoe_ctlr_solicit(fip, NULL);
1859 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1862 mutex_lock(&fip->lp->lp_mutex);
1863 mac = fip->get_src_addr(fip->lp);
1864 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1865 list_for_each_entry(vport, &fip->lp->vports, list) {
1866 mac = fip->get_src_addr(vport);
1867 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1869 mutex_unlock(&fip->lp->lp_mutex);
1874 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1875 * @recv_work: Handle to a FCoE controller
1877 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1879 struct fcoe_ctlr *fip;
1880 struct sk_buff *skb;
1882 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1883 while ((skb = skb_dequeue(&fip->fip_recv_list)))
1884 fcoe_ctlr_recv_handler(fip, skb);
1888 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1889 * @fip: The FCoE controller
1890 * @fp: The FC frame to snoop
1892 * Snoop potential response to FLOGI or even incoming FLOGI.
1894 * The caller has checked that we are waiting for login as indicated
1895 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1897 * The caller is responsible for freeing the frame.
1898 * Fill in the granted_mac address.
1900 * Return non-zero if the frame should not be delivered to libfc.
1902 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1903 struct fc_frame *fp)
1905 struct fc_frame_header *fh;
1909 sa = eth_hdr(&fp->skb)->h_source;
1910 fh = fc_frame_header_get(fp);
1911 if (fh->fh_type != FC_TYPE_ELS)
1914 op = fc_frame_payload_op(fp);
1915 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1916 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1918 mutex_lock(&fip->ctlr_mutex);
1919 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1920 mutex_unlock(&fip->ctlr_mutex);
1923 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1924 LIBFCOE_FIP_DBG(fip,
1925 "received FLOGI LS_ACC using non-FIP mode\n");
1929 * If the src mac addr is FC_OUI-based, then we mark the
1930 * address_mode flag to use FC_OUI-based Ethernet DA.
1931 * Otherwise we use the FCoE gateway addr
1933 if (ether_addr_equal(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1934 fcoe_ctlr_map_dest(fip);
1936 memcpy(fip->dest_addr, sa, ETH_ALEN);
1939 fip->flogi_oxid = FC_XID_UNKNOWN;
1940 mutex_unlock(&fip->ctlr_mutex);
1941 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1942 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1944 * Save source MAC for point-to-point responses.
1946 mutex_lock(&fip->ctlr_mutex);
1947 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1948 memcpy(fip->dest_addr, sa, ETH_ALEN);
1950 if (fip->state == FIP_ST_AUTO)
1951 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1952 "Setting non-FIP mode\n");
1953 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1955 mutex_unlock(&fip->ctlr_mutex);
1959 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1962 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1963 * @mac: The MAC address to convert
1964 * @scheme: The scheme to use when converting
1965 * @port: The port indicator for converting
1967 * Returns: u64 fc world wide name
1969 u64 fcoe_wwn_from_mac(unsigned char mac[ETH_ALEN],
1970 unsigned int scheme, unsigned int port)
1975 /* The MAC is in NO, so flip only the low 48 bits */
1976 host_mac = ((u64) mac[0] << 40) |
1977 ((u64) mac[1] << 32) |
1978 ((u64) mac[2] << 24) |
1979 ((u64) mac[3] << 16) |
1980 ((u64) mac[4] << 8) |
1983 WARN_ON(host_mac >= (1ULL << 48));
1984 wwn = host_mac | ((u64) scheme << 60);
1990 WARN_ON(port >= 0xfff);
1991 wwn |= (u64) port << 48;
2000 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
2003 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
2004 * @rdata: libfc remote port
2006 static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
2008 return container_of(rdata, struct fcoe_rport, rdata);
2012 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
2013 * @fip: The FCoE controller
2014 * @sub: sub-opcode for probe request, reply, or advertisement.
2015 * @dest: The destination Ethernet MAC address
2016 * @min_len: minimum size of the Ethernet payload to be sent
2018 static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
2019 enum fip_vn2vn_subcode sub,
2020 const u8 *dest, size_t min_len)
2022 struct sk_buff *skb;
2023 struct fip_vn2vn_probe_frame {
2025 struct fip_header fip;
2026 struct fip_mac_desc mac;
2027 struct fip_wwn_desc wwnn;
2028 struct fip_vn_desc vn;
2030 struct fip_fc4_feat *ff;
2031 struct fip_size_desc *size;
2036 len = sizeof(*frame);
2038 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2039 dlen = sizeof(struct fip_fc4_feat) +
2040 sizeof(struct fip_size_desc);
2043 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
2044 len = max(len, min_len + sizeof(struct ethhdr));
2046 skb = dev_alloc_skb(len);
2050 frame = (struct fip_vn2vn_probe_frame *)skb->data;
2051 memset(frame, 0, len);
2052 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2054 if (sub == FIP_SC_VN_BEACON) {
2055 hton24(frame->eth.h_source, FIP_VN_FC_MAP);
2056 hton24(frame->eth.h_source + 3, fip->port_id);
2058 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2060 frame->eth.h_proto = htons(ETH_P_FIP);
2062 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2063 frame->fip.fip_op = htons(FIP_OP_VN2VN);
2064 frame->fip.fip_subcode = sub;
2065 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2067 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2068 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2069 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2071 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
2072 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
2073 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
2075 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
2076 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
2077 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
2078 hton24(frame->vn.fd_mac + 3, fip->port_id);
2079 hton24(frame->vn.fd_fc_id, fip->port_id);
2080 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
2083 * For claims, add FC-4 features.
2084 * TBD: Add interface to get fc-4 types and features from libfc.
2086 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2087 ff = (struct fip_fc4_feat *)(frame + 1);
2088 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
2089 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
2090 ff->fd_fts = fip->lp->fcts;
2093 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
2094 fcp_feat |= FCP_FEAT_INIT;
2095 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
2096 fcp_feat |= FCP_FEAT_TARG;
2097 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
2098 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
2100 size = (struct fip_size_desc *)(ff + 1);
2101 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
2102 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
2103 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
2107 skb->protocol = htons(ETH_P_FIP);
2108 skb->priority = fip->priority;
2109 skb_reset_mac_header(skb);
2110 skb_reset_network_header(skb);
2112 fip->send(fip, skb);
2116 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
2117 * @lport: The lport which is receiving the event
2118 * @rdata: remote port private data
2119 * @event: The event that occurred
2121 * Locking Note: The rport lock must not be held when calling this function.
2123 static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
2124 struct fc_rport_priv *rdata,
2125 enum fc_rport_event event)
2127 struct fcoe_ctlr *fip = lport->disc.priv;
2128 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2130 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
2131 rdata->ids.port_id, event);
2133 mutex_lock(&fip->ctlr_mutex);
2135 case RPORT_EV_READY:
2136 frport->login_count = 0;
2139 case RPORT_EV_FAILED:
2141 frport->login_count++;
2142 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
2143 LIBFCOE_FIP_DBG(fip,
2144 "rport FLOGI limited port_id %6.6x\n",
2145 rdata->ids.port_id);
2146 fc_rport_logoff(rdata);
2152 mutex_unlock(&fip->ctlr_mutex);
2155 static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
2156 .event_callback = fcoe_ctlr_vn_rport_callback,
2160 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
2161 * @fip: The FCoE controller
2163 * Called with ctlr_mutex held.
2165 static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
2167 struct fc_rport_priv *rdata;
2169 mutex_lock(&lport->disc.disc_mutex);
2170 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2171 if (kref_get_unless_zero(&rdata->kref)) {
2172 fc_rport_logoff(rdata);
2173 kref_put(&rdata->kref, fc_rport_destroy);
2176 lport->disc.disc_callback = NULL;
2177 mutex_unlock(&lport->disc.disc_mutex);
2181 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2182 * @fip: The FCoE controller
2184 * Called through the local port template for discovery.
2185 * Called without the ctlr_mutex held.
2187 static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2189 struct fcoe_ctlr *fip = lport->disc.priv;
2191 mutex_lock(&fip->ctlr_mutex);
2192 fcoe_ctlr_disc_stop_locked(lport);
2193 mutex_unlock(&fip->ctlr_mutex);
2197 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2198 * @fip: The FCoE controller
2200 * Called through the local port template for discovery.
2201 * Called without the ctlr_mutex held.
2203 static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2205 fcoe_ctlr_disc_stop(lport);
2206 fc_rport_flush_queue();
2211 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2212 * @fip: The FCoE controller
2214 * Called with fcoe_ctlr lock held.
2216 static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2221 fcoe_ctlr_disc_stop_locked(fip->lp);
2224 * Get proposed port ID.
2225 * If this is the first try after link up, use any previous port_id.
2226 * If there was none, use the low bits of the port_name.
2227 * On subsequent tries, get the next random one.
2228 * Don't use reserved IDs, use another non-zero value, just as random.
2230 port_id = fip->port_id;
2231 if (fip->probe_tries)
2232 port_id = prandom_u32_state(&fip->rnd_state) & 0xffff;
2234 port_id = fip->lp->wwpn & 0xffff;
2235 if (!port_id || port_id == 0xffff)
2237 fip->port_id = port_id;
2239 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2241 wait = prandom_u32() % FIP_VN_PROBE_WAIT;
2243 wait = FIP_VN_RLIM_INT;
2244 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2245 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2249 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2250 * @fip: The FCoE controller
2252 * Called with fcoe_ctlr lock held.
2254 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2256 fip->probe_tries = 0;
2257 prandom_seed_state(&fip->rnd_state, fip->lp->wwpn);
2258 fcoe_ctlr_vn_restart(fip);
2262 * fcoe_ctlr_vn_parse - parse probe request or response
2263 * @fip: The FCoE controller
2264 * @skb: incoming packet
2265 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2267 * Returns non-zero error number on error.
2268 * Does not consume the packet.
2270 static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2271 struct sk_buff *skb,
2272 struct fcoe_rport *frport)
2274 struct fip_header *fiph;
2275 struct fip_desc *desc = NULL;
2276 struct fip_mac_desc *macd = NULL;
2277 struct fip_wwn_desc *wwn = NULL;
2278 struct fip_vn_desc *vn = NULL;
2279 struct fip_size_desc *size = NULL;
2286 fiph = (struct fip_header *)skb->data;
2287 frport->flags = ntohs(fiph->fip_flags);
2289 sub = fiph->fip_subcode;
2291 case FIP_SC_VN_PROBE_REQ:
2292 case FIP_SC_VN_PROBE_REP:
2293 case FIP_SC_VN_BEACON:
2294 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2297 case FIP_SC_VN_CLAIM_NOTIFY:
2298 case FIP_SC_VN_CLAIM_REP:
2299 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2300 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2301 BIT(FIP_DT_FCOE_SIZE);
2304 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2308 rlen = ntohs(fiph->fip_dl_len) * 4;
2309 if (rlen + sizeof(*fiph) > skb->len)
2312 desc = (struct fip_desc *)(fiph + 1);
2314 dlen = desc->fip_dlen * FIP_BPW;
2315 if (dlen < sizeof(*desc) || dlen > rlen)
2318 dtype = desc->fip_dtype;
2320 if (!(desc_mask & BIT(dtype))) {
2321 LIBFCOE_FIP_DBG(fip,
2322 "unexpected or duplicated desc "
2324 "FIP VN2VN subtype %u\n",
2328 desc_mask &= ~BIT(dtype);
2333 if (dlen != sizeof(struct fip_mac_desc))
2335 macd = (struct fip_mac_desc *)desc;
2336 if (!is_valid_ether_addr(macd->fd_mac)) {
2337 LIBFCOE_FIP_DBG(fip,
2338 "Invalid MAC addr %pM in FIP VN2VN\n",
2342 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2345 if (dlen != sizeof(struct fip_wwn_desc))
2347 wwn = (struct fip_wwn_desc *)desc;
2348 frport->rdata.ids.node_name =
2349 get_unaligned_be64(&wwn->fd_wwn);
2352 if (dlen != sizeof(struct fip_vn_desc))
2354 vn = (struct fip_vn_desc *)desc;
2355 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2356 frport->rdata.ids.port_id = ntoh24(vn->fd_fc_id);
2357 frport->rdata.ids.port_name =
2358 get_unaligned_be64(&vn->fd_wwpn);
2361 if (dlen != sizeof(struct fip_fc4_feat))
2364 case FIP_DT_FCOE_SIZE:
2365 if (dlen != sizeof(struct fip_size_desc))
2367 size = (struct fip_size_desc *)desc;
2368 frport->fcoe_len = ntohs(size->fd_size);
2371 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2372 "in FIP probe\n", dtype);
2373 /* standard says ignore unknown descriptors >= 128 */
2374 if (dtype < FIP_DT_NON_CRITICAL)
2378 desc = (struct fip_desc *)((char *)desc + dlen);
2384 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2390 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2391 * @fip: The FCoE controller
2393 * Called with ctlr_mutex held.
2395 static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2397 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2398 fip->sol_time = jiffies;
2402 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2403 * @fip: The FCoE controller
2404 * @frport: parsed FCoE rport from the probe request
2406 * Called with ctlr_mutex held.
2408 static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2409 struct fcoe_rport *frport)
2411 if (frport->rdata.ids.port_id != fip->port_id)
2414 switch (fip->state) {
2415 case FIP_ST_VNMP_CLAIM:
2416 case FIP_ST_VNMP_UP:
2417 LIBFCOE_FIP_DBG(fip, "vn_probe_req: send reply, state %x\n",
2419 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2420 frport->enode_mac, 0);
2422 case FIP_ST_VNMP_PROBE1:
2423 case FIP_ST_VNMP_PROBE2:
2425 * Decide whether to reply to the Probe.
2426 * Our selected address is never a "recorded" one, so
2427 * only reply if our WWPN is greater and the
2428 * Probe's REC bit is not set.
2429 * If we don't reply, we will change our address.
2431 if (fip->lp->wwpn > frport->rdata.ids.port_name &&
2432 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2433 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2434 "port_id collision\n");
2435 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2436 frport->enode_mac, 0);
2440 case FIP_ST_VNMP_START:
2441 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2442 "restart VN2VN negotiation\n");
2443 fcoe_ctlr_vn_restart(fip);
2446 LIBFCOE_FIP_DBG(fip, "vn_probe_req: ignore state %x\n",
2453 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2454 * @fip: The FCoE controller
2455 * @frport: parsed FCoE rport from the probe request
2457 * Called with ctlr_mutex held.
2459 static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2460 struct fcoe_rport *frport)
2462 if (frport->rdata.ids.port_id != fip->port_id)
2464 switch (fip->state) {
2465 case FIP_ST_VNMP_START:
2466 case FIP_ST_VNMP_PROBE1:
2467 case FIP_ST_VNMP_PROBE2:
2468 case FIP_ST_VNMP_CLAIM:
2469 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: restart state %x\n",
2471 fcoe_ctlr_vn_restart(fip);
2473 case FIP_ST_VNMP_UP:
2474 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: send claim notify\n");
2475 fcoe_ctlr_vn_send_claim(fip);
2483 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2484 * @fip: The FCoE controller
2485 * @new: newly-parsed FCoE rport as a template for new rdata
2487 * Called with ctlr_mutex held.
2489 static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fcoe_rport *new)
2491 struct fc_lport *lport = fip->lp;
2492 struct fc_rport_priv *rdata;
2493 struct fc_rport_identifiers *ids;
2494 struct fcoe_rport *frport;
2497 port_id = new->rdata.ids.port_id;
2498 if (port_id == fip->port_id)
2501 mutex_lock(&lport->disc.disc_mutex);
2502 rdata = fc_rport_create(lport, port_id);
2504 mutex_unlock(&lport->disc.disc_mutex);
2507 mutex_lock(&rdata->rp_mutex);
2508 mutex_unlock(&lport->disc.disc_mutex);
2510 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2511 rdata->disc_id = lport->disc.disc_id;
2514 if ((ids->port_name != -1 &&
2515 ids->port_name != new->rdata.ids.port_name) ||
2516 (ids->node_name != -1 &&
2517 ids->node_name != new->rdata.ids.node_name)) {
2518 mutex_unlock(&rdata->rp_mutex);
2519 LIBFCOE_FIP_DBG(fip, "vn_add rport logoff %6.6x\n", port_id);
2520 fc_rport_logoff(rdata);
2521 mutex_lock(&rdata->rp_mutex);
2523 ids->port_name = new->rdata.ids.port_name;
2524 ids->node_name = new->rdata.ids.node_name;
2525 mutex_unlock(&rdata->rp_mutex);
2527 frport = fcoe_ctlr_rport(rdata);
2528 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s state %d\n",
2529 port_id, frport->fcoe_len ? "old" : "new",
2531 frport->fcoe_len = new->fcoe_len;
2532 frport->flags = new->flags;
2533 frport->login_count = new->login_count;
2534 memcpy(frport->enode_mac, new->enode_mac, ETH_ALEN);
2535 memcpy(frport->vn_mac, new->vn_mac, ETH_ALEN);
2540 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2541 * @fip: The FCoE controller
2542 * @port_id: The port_id of the remote VN_node
2543 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2545 * Returns non-zero error if no remote port found.
2547 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2549 struct fc_lport *lport = fip->lp;
2550 struct fc_rport_priv *rdata;
2551 struct fcoe_rport *frport;
2554 rdata = fc_rport_lookup(lport, port_id);
2556 frport = fcoe_ctlr_rport(rdata);
2557 memcpy(mac, frport->enode_mac, ETH_ALEN);
2559 kref_put(&rdata->kref, fc_rport_destroy);
2565 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2566 * @fip: The FCoE controller
2567 * @new: newly-parsed FCoE rport as a template for new rdata
2569 * Called with ctlr_mutex held.
2571 static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2572 struct fcoe_rport *new)
2574 if (new->flags & FIP_FL_REC_OR_P2P) {
2575 LIBFCOE_FIP_DBG(fip, "send probe req for P2P/REC\n");
2576 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2579 switch (fip->state) {
2580 case FIP_ST_VNMP_START:
2581 case FIP_ST_VNMP_PROBE1:
2582 case FIP_ST_VNMP_PROBE2:
2583 if (new->rdata.ids.port_id == fip->port_id) {
2584 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2585 "restart, state %d\n",
2587 fcoe_ctlr_vn_restart(fip);
2590 case FIP_ST_VNMP_CLAIM:
2591 case FIP_ST_VNMP_UP:
2592 if (new->rdata.ids.port_id == fip->port_id) {
2593 if (new->rdata.ids.port_name > fip->lp->wwpn) {
2594 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2595 "restart, port_id collision\n");
2596 fcoe_ctlr_vn_restart(fip);
2599 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2600 "send claim notify\n");
2601 fcoe_ctlr_vn_send_claim(fip);
2604 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: send reply to %x\n",
2605 new->rdata.ids.port_id);
2606 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, new->enode_mac,
2607 min((u32)new->fcoe_len,
2608 fcoe_ctlr_fcoe_size(fip)));
2609 fcoe_ctlr_vn_add(fip, new);
2612 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2613 "ignoring claim from %x\n",
2614 new->rdata.ids.port_id);
2620 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2621 * @fip: The FCoE controller that received the frame
2622 * @new: newly-parsed FCoE rport from the Claim Response
2624 * Called with ctlr_mutex held.
2626 static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2627 struct fcoe_rport *new)
2629 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2630 new->rdata.ids.port_id, fcoe_ctlr_state(fip->state));
2631 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2632 fcoe_ctlr_vn_add(fip, new);
2636 * fcoe_ctlr_vn_beacon() - handle received beacon.
2637 * @fip: The FCoE controller that received the frame
2638 * @new: newly-parsed FCoE rport from the Beacon
2640 * Called with ctlr_mutex held.
2642 static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2643 struct fcoe_rport *new)
2645 struct fc_lport *lport = fip->lp;
2646 struct fc_rport_priv *rdata;
2647 struct fcoe_rport *frport;
2649 if (new->flags & FIP_FL_REC_OR_P2P) {
2650 LIBFCOE_FIP_DBG(fip, "p2p beacon while in vn2vn mode\n");
2651 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2654 rdata = fc_rport_lookup(lport, new->rdata.ids.port_id);
2656 if (rdata->ids.node_name == new->rdata.ids.node_name &&
2657 rdata->ids.port_name == new->rdata.ids.port_name) {
2658 frport = fcoe_ctlr_rport(rdata);
2660 LIBFCOE_FIP_DBG(fip, "beacon from rport %x\n",
2661 rdata->ids.port_id);
2662 if (!frport->time && fip->state == FIP_ST_VNMP_UP) {
2663 LIBFCOE_FIP_DBG(fip, "beacon expired "
2665 rdata->ids.port_id);
2666 fc_rport_login(rdata);
2668 frport->time = jiffies;
2670 kref_put(&rdata->kref, fc_rport_destroy);
2673 if (fip->state != FIP_ST_VNMP_UP)
2677 * Beacon from a new neighbor.
2678 * Send a claim notify if one hasn't been sent recently.
2679 * Don't add the neighbor yet.
2681 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2682 new->rdata.ids.port_id);
2683 if (time_after(jiffies,
2684 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2685 fcoe_ctlr_vn_send_claim(fip);
2689 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2690 * @fip: The FCoE controller
2692 * Called with ctlr_mutex held.
2693 * Called only in state FIP_ST_VNMP_UP.
2694 * Returns the soonest time for next age-out or a time far in the future.
2696 static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2698 struct fc_lport *lport = fip->lp;
2699 struct fc_rport_priv *rdata;
2700 struct fcoe_rport *frport;
2701 unsigned long next_time;
2702 unsigned long deadline;
2704 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2705 mutex_lock(&lport->disc.disc_mutex);
2706 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2707 if (!kref_get_unless_zero(&rdata->kref))
2709 frport = fcoe_ctlr_rport(rdata);
2710 if (!frport->time) {
2711 kref_put(&rdata->kref, fc_rport_destroy);
2714 deadline = frport->time +
2715 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2716 if (time_after_eq(jiffies, deadline)) {
2718 LIBFCOE_FIP_DBG(fip,
2719 "port %16.16llx fc_id %6.6x beacon expired\n",
2720 rdata->ids.port_name, rdata->ids.port_id);
2721 fc_rport_logoff(rdata);
2722 } else if (time_before(deadline, next_time))
2723 next_time = deadline;
2724 kref_put(&rdata->kref, fc_rport_destroy);
2726 mutex_unlock(&lport->disc.disc_mutex);
2731 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2732 * @fip: The FCoE controller that received the frame
2733 * @skb: The received FIP frame
2735 * Returns non-zero if the frame is dropped.
2736 * Always consumes the frame.
2738 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2740 struct fip_header *fiph;
2741 enum fip_vn2vn_subcode sub;
2742 struct fcoe_rport frport = { };
2743 int rc, vlan_id = 0;
2745 fiph = (struct fip_header *)skb->data;
2746 sub = fiph->fip_subcode;
2749 vlan_id = skb_vlan_tag_get_id(skb);
2751 if (vlan_id && vlan_id != fip->lp->vlan) {
2752 LIBFCOE_FIP_DBG(fip, "vn_recv drop frame sub %x vlan %d\n",
2758 rc = fcoe_ctlr_vn_parse(fip, skb, &frport);
2760 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2764 mutex_lock(&fip->ctlr_mutex);
2766 case FIP_SC_VN_PROBE_REQ:
2767 fcoe_ctlr_vn_probe_req(fip, &frport);
2769 case FIP_SC_VN_PROBE_REP:
2770 fcoe_ctlr_vn_probe_reply(fip, &frport);
2772 case FIP_SC_VN_CLAIM_NOTIFY:
2773 fcoe_ctlr_vn_claim_notify(fip, &frport);
2775 case FIP_SC_VN_CLAIM_REP:
2776 fcoe_ctlr_vn_claim_resp(fip, &frport);
2778 case FIP_SC_VN_BEACON:
2779 fcoe_ctlr_vn_beacon(fip, &frport);
2782 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2786 mutex_unlock(&fip->ctlr_mutex);
2793 * fcoe_ctlr_vlan_parse - parse vlan discovery request or response
2794 * @fip: The FCoE controller
2795 * @skb: incoming packet
2796 * @rdata: buffer for resulting parsed VLAN entry plus fcoe_rport
2798 * Returns non-zero error number on error.
2799 * Does not consume the packet.
2801 static int fcoe_ctlr_vlan_parse(struct fcoe_ctlr *fip,
2802 struct sk_buff *skb,
2803 struct fcoe_rport *frport)
2805 struct fip_header *fiph;
2806 struct fip_desc *desc = NULL;
2807 struct fip_mac_desc *macd = NULL;
2808 struct fip_wwn_desc *wwn = NULL;
2815 fiph = (struct fip_header *)skb->data;
2816 frport->flags = ntohs(fiph->fip_flags);
2818 sub = fiph->fip_subcode;
2821 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
2824 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2828 rlen = ntohs(fiph->fip_dl_len) * 4;
2829 if (rlen + sizeof(*fiph) > skb->len)
2832 desc = (struct fip_desc *)(fiph + 1);
2834 dlen = desc->fip_dlen * FIP_BPW;
2835 if (dlen < sizeof(*desc) || dlen > rlen)
2838 dtype = desc->fip_dtype;
2840 if (!(desc_mask & BIT(dtype))) {
2841 LIBFCOE_FIP_DBG(fip,
2842 "unexpected or duplicated desc "
2844 "FIP VN2VN subtype %u\n",
2848 desc_mask &= ~BIT(dtype);
2853 if (dlen != sizeof(struct fip_mac_desc))
2855 macd = (struct fip_mac_desc *)desc;
2856 if (!is_valid_ether_addr(macd->fd_mac)) {
2857 LIBFCOE_FIP_DBG(fip,
2858 "Invalid MAC addr %pM in FIP VN2VN\n",
2862 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2865 if (dlen != sizeof(struct fip_wwn_desc))
2867 wwn = (struct fip_wwn_desc *)desc;
2868 frport->rdata.ids.node_name =
2869 get_unaligned_be64(&wwn->fd_wwn);
2872 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2873 "in FIP probe\n", dtype);
2874 /* standard says ignore unknown descriptors >= 128 */
2875 if (dtype < FIP_DT_NON_CRITICAL)
2879 desc = (struct fip_desc *)((char *)desc + dlen);
2885 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2891 * fcoe_ctlr_vlan_send() - Send a FIP VLAN Notification
2892 * @fip: The FCoE controller
2893 * @sub: sub-opcode for vlan notification or vn2vn vlan notification
2894 * @dest: The destination Ethernet MAC address
2895 * @min_len: minimum size of the Ethernet payload to be sent
2897 static void fcoe_ctlr_vlan_send(struct fcoe_ctlr *fip,
2898 enum fip_vlan_subcode sub,
2901 struct sk_buff *skb;
2902 struct fip_vlan_notify_frame {
2904 struct fip_header fip;
2905 struct fip_mac_desc mac;
2906 struct fip_vlan_desc vlan;
2911 len = sizeof(*frame);
2912 dlen = sizeof(frame->mac) + sizeof(frame->vlan);
2913 len = max(len, sizeof(struct ethhdr));
2915 skb = dev_alloc_skb(len);
2919 LIBFCOE_FIP_DBG(fip, "fip %s vlan notification, vlan %d\n",
2920 fip->mode == FIP_MODE_VN2VN ? "vn2vn" : "fcf",
2923 frame = (struct fip_vlan_notify_frame *)skb->data;
2924 memset(frame, 0, len);
2925 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2927 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2928 frame->eth.h_proto = htons(ETH_P_FIP);
2930 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2931 frame->fip.fip_op = htons(FIP_OP_VLAN);
2932 frame->fip.fip_subcode = sub;
2933 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2935 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2936 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2937 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2939 frame->vlan.fd_desc.fip_dtype = FIP_DT_VLAN;
2940 frame->vlan.fd_desc.fip_dlen = sizeof(frame->vlan) / FIP_BPW;
2941 put_unaligned_be16(fip->lp->vlan, &frame->vlan.fd_vlan);
2944 skb->protocol = htons(ETH_P_FIP);
2945 skb->priority = fip->priority;
2946 skb_reset_mac_header(skb);
2947 skb_reset_network_header(skb);
2949 fip->send(fip, skb);
2953 * fcoe_ctlr_vlan_disk_reply() - send FIP VLAN Discovery Notification.
2954 * @fip: The FCoE controller
2955 * @frport: The newly-parsed FCoE rport from the Discovery Request
2957 * Called with ctlr_mutex held.
2959 static void fcoe_ctlr_vlan_disc_reply(struct fcoe_ctlr *fip,
2960 struct fcoe_rport *frport)
2962 enum fip_vlan_subcode sub = FIP_SC_VL_NOTE;
2964 if (fip->mode == FIP_MODE_VN2VN)
2965 sub = FIP_SC_VL_VN2VN_NOTE;
2967 fcoe_ctlr_vlan_send(fip, sub, frport->enode_mac);
2971 * fcoe_ctlr_vlan_recv - vlan request receive handler for VN2VN mode.
2972 * @lport: The local port
2973 * @fp: The received frame
2976 static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2978 struct fip_header *fiph;
2979 enum fip_vlan_subcode sub;
2980 struct fcoe_rport frport = { };
2983 fiph = (struct fip_header *)skb->data;
2984 sub = fiph->fip_subcode;
2985 rc = fcoe_ctlr_vlan_parse(fip, skb, &frport);
2987 LIBFCOE_FIP_DBG(fip, "vlan_recv vlan_parse error %d\n", rc);
2990 mutex_lock(&fip->ctlr_mutex);
2991 if (sub == FIP_SC_VL_REQ)
2992 fcoe_ctlr_vlan_disc_reply(fip, &frport);
2993 mutex_unlock(&fip->ctlr_mutex);
3001 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
3002 * @lport: The local port
3003 * @fp: The received frame
3005 * This should never be called since we don't see RSCNs or other
3006 * fabric-generated ELSes.
3008 static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
3010 struct fc_seq_els_data rjt_data;
3012 rjt_data.reason = ELS_RJT_UNSUP;
3013 rjt_data.explan = ELS_EXPL_NONE;
3014 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
3019 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
3020 * @fip: The FCoE controller
3022 * This sets a flag indicating that remote ports should be created
3023 * and started for the peers we discover. We use the disc_callback
3024 * pointer as that flag. Peers already discovered are created here.
3026 * The lport lock is held during this call. The callback must be done
3027 * later, without holding either the lport or discovery locks.
3028 * The fcoe_ctlr lock may also be held during this call.
3030 static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
3031 enum fc_disc_event),
3032 struct fc_lport *lport)
3034 struct fc_disc *disc = &lport->disc;
3035 struct fcoe_ctlr *fip = disc->priv;
3037 mutex_lock(&disc->disc_mutex);
3038 disc->disc_callback = callback;
3039 disc->disc_id = (disc->disc_id + 2) | 1;
3041 schedule_work(&fip->timer_work);
3042 mutex_unlock(&disc->disc_mutex);
3046 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
3047 * @fip: The FCoE controller
3049 * Starts the FLOGI and PLOGI login process to each discovered rport for which
3050 * we've received at least one beacon.
3051 * Performs the discovery complete callback.
3053 static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
3055 struct fc_lport *lport = fip->lp;
3056 struct fc_disc *disc = &lport->disc;
3057 struct fc_rport_priv *rdata;
3058 struct fcoe_rport *frport;
3059 void (*callback)(struct fc_lport *, enum fc_disc_event);
3061 mutex_lock(&disc->disc_mutex);
3062 callback = disc->pending ? disc->disc_callback : NULL;
3064 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
3065 if (!kref_get_unless_zero(&rdata->kref))
3067 frport = fcoe_ctlr_rport(rdata);
3069 fc_rport_login(rdata);
3070 kref_put(&rdata->kref, fc_rport_destroy);
3072 mutex_unlock(&disc->disc_mutex);
3074 callback(lport, DISC_EV_SUCCESS);
3078 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
3079 * @fip: The FCoE controller
3081 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
3083 unsigned long next_time;
3085 u32 new_port_id = 0;
3087 mutex_lock(&fip->ctlr_mutex);
3088 switch (fip->state) {
3089 case FIP_ST_VNMP_START:
3090 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
3091 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 1st probe request\n");
3092 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3093 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
3095 case FIP_ST_VNMP_PROBE1:
3096 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
3097 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 2nd probe request\n");
3098 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3099 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3101 case FIP_ST_VNMP_PROBE2:
3102 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
3103 new_port_id = fip->port_id;
3104 hton24(mac, FIP_VN_FC_MAP);
3105 hton24(mac + 3, new_port_id);
3106 fcoe_ctlr_map_dest(fip);
3107 fip->update_mac(fip->lp, mac);
3108 LIBFCOE_FIP_DBG(fip, "vn_timeout: send claim notify\n");
3109 fcoe_ctlr_vn_send_claim(fip);
3110 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3112 case FIP_ST_VNMP_CLAIM:
3114 * This may be invoked either by starting discovery so don't
3115 * go to the next state unless it's been long enough.
3117 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3118 if (time_after_eq(jiffies, next_time)) {
3119 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
3120 LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3121 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3123 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3124 fip->port_ka_time = next_time;
3126 fcoe_ctlr_vn_disc(fip);
3128 case FIP_ST_VNMP_UP:
3129 next_time = fcoe_ctlr_vn_age(fip);
3130 if (time_after_eq(jiffies, fip->port_ka_time)) {
3131 LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3132 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3134 fip->port_ka_time = jiffies +
3135 msecs_to_jiffies(FIP_VN_BEACON_INT +
3136 (prandom_u32() % FIP_VN_BEACON_FUZZ));
3138 if (time_before(fip->port_ka_time, next_time))
3139 next_time = fip->port_ka_time;
3141 case FIP_ST_LINK_WAIT:
3144 WARN(1, "unexpected state %d\n", fip->state);
3147 mod_timer(&fip->timer, next_time);
3149 mutex_unlock(&fip->ctlr_mutex);
3151 /* If port ID is new, notify local port after dropping ctlr_mutex */
3153 fc_lport_set_local_id(fip->lp, new_port_id);
3157 * fcoe_ctlr_mode_set() - Set or reset the ctlr's mode
3158 * @lport: The local port to be (re)configured
3159 * @fip: The FCoE controller whose mode is changing
3160 * @fip_mode: The new fip mode
3162 * Note that the we shouldn't be changing the libfc discovery settings
3163 * (fc_disc_config) while an lport is going through the libfc state
3164 * machine. The mode can only be changed when a fcoe_ctlr device is
3165 * disabled, so that should ensure that this routine is only called
3166 * when nothing is happening.
3168 static void fcoe_ctlr_mode_set(struct fc_lport *lport, struct fcoe_ctlr *fip,
3169 enum fip_mode fip_mode)
3173 WARN_ON(lport->state != LPORT_ST_RESET &&
3174 lport->state != LPORT_ST_DISABLED);
3176 if (fip_mode == FIP_MODE_VN2VN) {
3177 lport->rport_priv_size = sizeof(struct fcoe_rport);
3178 lport->point_to_multipoint = 1;
3179 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
3180 lport->tt.disc_start = fcoe_ctlr_disc_start;
3181 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
3182 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
3185 lport->rport_priv_size = 0;
3186 lport->point_to_multipoint = 0;
3187 lport->tt.disc_recv_req = NULL;
3188 lport->tt.disc_start = NULL;
3189 lport->tt.disc_stop = NULL;
3190 lport->tt.disc_stop_final = NULL;
3194 fc_disc_config(lport, priv);
3198 * fcoe_libfc_config() - Sets up libfc related properties for local port
3199 * @lport: The local port to configure libfc for
3200 * @fip: The FCoE controller in use by the local port
3201 * @tt: The libfc function template
3202 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
3204 * Returns : 0 for success
3206 int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
3207 const struct libfc_function_template *tt, int init_fcp)
3209 /* Set the function pointers set by the LLDD */
3210 memcpy(&lport->tt, tt, sizeof(*tt));
3211 if (init_fcp && fc_fcp_init(lport))
3213 fc_exch_init(lport);
3214 fc_elsct_init(lport);
3215 fc_lport_init(lport);
3216 fc_disc_init(lport);
3217 fcoe_ctlr_mode_set(lport, fip, fip->mode);
3220 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
3222 void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
3224 struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
3225 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
3226 struct fcoe_fcf *fcf;
3228 mutex_lock(&fip->ctlr_mutex);
3229 mutex_lock(&ctlr_dev->lock);
3231 fcf = fcoe_fcf_device_priv(fcf_dev);
3233 fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
3235 fcf_dev->selected = 0;
3237 mutex_unlock(&ctlr_dev->lock);
3238 mutex_unlock(&fip->ctlr_mutex);
3240 EXPORT_SYMBOL(fcoe_fcf_get_selected);
3242 void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
3244 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
3245 struct fc_lport *lport = ctlr->lp;
3247 mutex_lock(&ctlr->ctlr_mutex);
3248 switch (ctlr_dev->mode) {
3249 case FIP_CONN_TYPE_VN2VN:
3250 ctlr->mode = FIP_MODE_VN2VN;
3252 case FIP_CONN_TYPE_FABRIC:
3254 ctlr->mode = FIP_MODE_FABRIC;
3258 mutex_unlock(&ctlr->ctlr_mutex);
3260 fcoe_ctlr_mode_set(lport, ctlr, ctlr->mode);
3262 EXPORT_SYMBOL(fcoe_ctlr_set_fip_mode);