Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2008-2009 Cisco Systems, Inc.  All rights reserved.
0004  * Copyright (c) 2009 Intel Corporation.  All rights reserved.
0005  *
0006  * Maintained at www.Open-FCoE.org
0007  */
0008 
0009 #include <linux/types.h>
0010 #include <linux/module.h>
0011 #include <linux/kernel.h>
0012 #include <linux/list.h>
0013 #include <linux/spinlock.h>
0014 #include <linux/timer.h>
0015 #include <linux/netdevice.h>
0016 #include <linux/etherdevice.h>
0017 #include <linux/ethtool.h>
0018 #include <linux/if_ether.h>
0019 #include <linux/if_vlan.h>
0020 #include <linux/errno.h>
0021 #include <linux/bitops.h>
0022 #include <linux/slab.h>
0023 #include <net/rtnetlink.h>
0024 
0025 #include <scsi/fc/fc_els.h>
0026 #include <scsi/fc/fc_fs.h>
0027 #include <scsi/fc/fc_fip.h>
0028 #include <scsi/fc/fc_encaps.h>
0029 #include <scsi/fc/fc_fcoe.h>
0030 #include <scsi/fc/fc_fcp.h>
0031 
0032 #include <scsi/libfc.h>
0033 #include <scsi/libfcoe.h>
0034 
0035 #include "libfcoe.h"
0036 
0037 #define FCOE_CTLR_MIN_FKA   500     /* min keep alive (mS) */
0038 #define FCOE_CTLR_DEF_FKA   FIP_DEF_FKA /* default keep alive (mS) */
0039 
0040 static void fcoe_ctlr_timeout(struct timer_list *);
0041 static void fcoe_ctlr_timer_work(struct work_struct *);
0042 static void fcoe_ctlr_recv_work(struct work_struct *);
0043 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
0044 
0045 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
0046 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
0047 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
0048 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
0049 
0050 static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *, struct sk_buff *);
0051 
0052 static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
0053 static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
0054 static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
0055 static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
0056 
0057 static const char * const fcoe_ctlr_states[] = {
0058     [FIP_ST_DISABLED] = "DISABLED",
0059     [FIP_ST_LINK_WAIT] =    "LINK_WAIT",
0060     [FIP_ST_AUTO] =     "AUTO",
0061     [FIP_ST_NON_FIP] =  "NON_FIP",
0062     [FIP_ST_ENABLED] =  "ENABLED",
0063     [FIP_ST_VNMP_START] =   "VNMP_START",
0064     [FIP_ST_VNMP_PROBE1] =  "VNMP_PROBE1",
0065     [FIP_ST_VNMP_PROBE2] =  "VNMP_PROBE2",
0066     [FIP_ST_VNMP_CLAIM] =   "VNMP_CLAIM",
0067     [FIP_ST_VNMP_UP] =  "VNMP_UP",
0068 };
0069 
0070 static const char *fcoe_ctlr_state(enum fip_state state)
0071 {
0072     const char *cp = "unknown";
0073 
0074     if (state < ARRAY_SIZE(fcoe_ctlr_states))
0075         cp = fcoe_ctlr_states[state];
0076     if (!cp)
0077         cp = "unknown";
0078     return cp;
0079 }
0080 
0081 /**
0082  * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
0083  * @fip: The FCoE controller
0084  * @state: The new state
0085  */
0086 static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
0087 {
0088     if (state == fip->state)
0089         return;
0090     if (fip->lp)
0091         LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
0092             fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
0093     fip->state = state;
0094 }
0095 
0096 /**
0097  * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
0098  * @fcf: The FCF to check
0099  *
0100  * Return non-zero if FCF fcoe_size has been validated.
0101  */
0102 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
0103 {
0104     return (fcf->flags & FIP_FL_SOL) != 0;
0105 }
0106 
0107 /**
0108  * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
0109  * @fcf: The FCF to check
0110  *
0111  * Return non-zero if the FCF is usable.
0112  */
0113 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
0114 {
0115     u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
0116 
0117     return (fcf->flags & flags) == flags;
0118 }
0119 
0120 /**
0121  * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
0122  * @fip: The FCoE controller
0123  */
0124 static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
0125 {
0126     if (fip->mode == FIP_MODE_VN2VN)
0127         hton24(fip->dest_addr, FIP_VN_FC_MAP);
0128     else
0129         hton24(fip->dest_addr, FIP_DEF_FC_MAP);
0130     hton24(fip->dest_addr + 3, 0);
0131     fip->map_dest = 1;
0132 }
0133 
0134 /**
0135  * fcoe_ctlr_init() - Initialize the FCoE Controller instance
0136  * @fip: The FCoE controller to initialize
0137  * @mode: FIP mode to set
0138  */
0139 void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_mode mode)
0140 {
0141     fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
0142     fip->mode = mode;
0143     fip->fip_resp = false;
0144     INIT_LIST_HEAD(&fip->fcfs);
0145     mutex_init(&fip->ctlr_mutex);
0146     spin_lock_init(&fip->ctlr_lock);
0147     fip->flogi_oxid = FC_XID_UNKNOWN;
0148     timer_setup(&fip->timer, fcoe_ctlr_timeout, 0);
0149     INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
0150     INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
0151     skb_queue_head_init(&fip->fip_recv_list);
0152 }
0153 EXPORT_SYMBOL(fcoe_ctlr_init);
0154 
0155 /**
0156  * fcoe_sysfs_fcf_add() - Add a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
0157  * @new: The newly discovered FCF
0158  *
0159  * Called with fip->ctlr_mutex held
0160  */
0161 static int fcoe_sysfs_fcf_add(struct fcoe_fcf *new)
0162 {
0163     struct fcoe_ctlr *fip = new->fip;
0164     struct fcoe_ctlr_device *ctlr_dev;
0165     struct fcoe_fcf_device *temp, *fcf_dev;
0166     int rc = -ENOMEM;
0167 
0168     LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
0169             new->fabric_name, new->fcf_mac);
0170 
0171     temp = kzalloc(sizeof(*temp), GFP_KERNEL);
0172     if (!temp)
0173         goto out;
0174 
0175     temp->fabric_name = new->fabric_name;
0176     temp->switch_name = new->switch_name;
0177     temp->fc_map = new->fc_map;
0178     temp->vfid = new->vfid;
0179     memcpy(temp->mac, new->fcf_mac, ETH_ALEN);
0180     temp->priority = new->pri;
0181     temp->fka_period = new->fka_period;
0182     temp->selected = 0; /* default to unselected */
0183 
0184     /*
0185      * If ctlr_dev doesn't exist then it means we're a libfcoe user
0186      * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device.
0187      * fnic would be an example of a driver with this behavior. In this
0188      * case we want to add the fcoe_fcf to the fcoe_ctlr list, but we
0189      * don't want to make sysfs changes.
0190      */
0191 
0192     ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
0193     if (ctlr_dev) {
0194         mutex_lock(&ctlr_dev->lock);
0195         fcf_dev = fcoe_fcf_device_add(ctlr_dev, temp);
0196         if (unlikely(!fcf_dev)) {
0197             rc = -ENOMEM;
0198             mutex_unlock(&ctlr_dev->lock);
0199             goto out;
0200         }
0201 
0202         /*
0203          * The fcoe_sysfs layer can return a CONNECTED fcf that
0204          * has a priv (fcf was never deleted) or a CONNECTED fcf
0205          * that doesn't have a priv (fcf was deleted). However,
0206          * libfcoe will always delete FCFs before trying to add
0207          * them. This is ensured because both recv_adv and
0208          * age_fcfs are protected by the the fcoe_ctlr's mutex.
0209          * This means that we should never get a FCF with a
0210          * non-NULL priv pointer.
0211          */
0212         BUG_ON(fcf_dev->priv);
0213 
0214         fcf_dev->priv = new;
0215         new->fcf_dev = fcf_dev;
0216         mutex_unlock(&ctlr_dev->lock);
0217     }
0218 
0219     list_add(&new->list, &fip->fcfs);
0220     fip->fcf_count++;
0221     rc = 0;
0222 
0223 out:
0224     kfree(temp);
0225     return rc;
0226 }
0227 
0228 /**
0229  * fcoe_sysfs_fcf_del() - Remove a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
0230  * @new: The FCF to be removed
0231  *
0232  * Called with fip->ctlr_mutex held
0233  */
0234 static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new)
0235 {
0236     struct fcoe_ctlr *fip = new->fip;
0237     struct fcoe_ctlr_device *cdev;
0238     struct fcoe_fcf_device *fcf_dev;
0239 
0240     list_del(&new->list);
0241     fip->fcf_count--;
0242 
0243     /*
0244      * If ctlr_dev doesn't exist then it means we're a libfcoe user
0245      * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device
0246      * or a fcoe_fcf_device.
0247      *
0248      * fnic would be an example of a driver with this behavior. In this
0249      * case we want to remove the fcoe_fcf from the fcoe_ctlr list (above),
0250      * but we don't want to make sysfs changes.
0251      */
0252     cdev = fcoe_ctlr_to_ctlr_dev(fip);
0253     if (cdev) {
0254         mutex_lock(&cdev->lock);
0255         fcf_dev = fcoe_fcf_to_fcf_dev(new);
0256         WARN_ON(!fcf_dev);
0257         new->fcf_dev = NULL;
0258         fcoe_fcf_device_delete(fcf_dev);
0259         mutex_unlock(&cdev->lock);
0260     }
0261     kfree(new);
0262 }
0263 
0264 /**
0265  * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
0266  * @fip: The FCoE controller whose FCFs are to be reset
0267  *
0268  * Called with &fcoe_ctlr lock held.
0269  */
0270 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
0271 {
0272     struct fcoe_fcf *fcf;
0273     struct fcoe_fcf *next;
0274 
0275     fip->sel_fcf = NULL;
0276     list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
0277         fcoe_sysfs_fcf_del(fcf);
0278     }
0279     WARN_ON(fip->fcf_count);
0280 
0281     fip->sel_time = 0;
0282 }
0283 
0284 /**
0285  * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
0286  * @fip: The FCoE controller to tear down
0287  *
0288  * This is called by FCoE drivers before freeing the &fcoe_ctlr.
0289  *
0290  * The receive handler will have been deleted before this to guarantee
0291  * that no more recv_work will be scheduled.
0292  *
0293  * The timer routine will simply return once we set FIP_ST_DISABLED.
0294  * This guarantees that no further timeouts or work will be scheduled.
0295  */
0296 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
0297 {
0298     cancel_work_sync(&fip->recv_work);
0299     skb_queue_purge(&fip->fip_recv_list);
0300 
0301     mutex_lock(&fip->ctlr_mutex);
0302     fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
0303     fcoe_ctlr_reset_fcfs(fip);
0304     mutex_unlock(&fip->ctlr_mutex);
0305     del_timer_sync(&fip->timer);
0306     cancel_work_sync(&fip->timer_work);
0307 }
0308 EXPORT_SYMBOL(fcoe_ctlr_destroy);
0309 
0310 /**
0311  * fcoe_ctlr_announce() - announce new FCF selection
0312  * @fip: The FCoE controller
0313  *
0314  * Also sets the destination MAC for FCoE and control packets
0315  *
0316  * Called with neither ctlr_mutex nor ctlr_lock held.
0317  */
0318 static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
0319 {
0320     struct fcoe_fcf *sel;
0321     struct fcoe_fcf *fcf;
0322 
0323     mutex_lock(&fip->ctlr_mutex);
0324     spin_lock_bh(&fip->ctlr_lock);
0325 
0326     kfree_skb(fip->flogi_req);
0327     fip->flogi_req = NULL;
0328     list_for_each_entry(fcf, &fip->fcfs, list)
0329         fcf->flogi_sent = 0;
0330 
0331     spin_unlock_bh(&fip->ctlr_lock);
0332     sel = fip->sel_fcf;
0333 
0334     if (sel && ether_addr_equal(sel->fcf_mac, fip->dest_addr))
0335         goto unlock;
0336     if (!is_zero_ether_addr(fip->dest_addr)) {
0337         printk(KERN_NOTICE "libfcoe: host%d: "
0338                "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
0339                fip->lp->host->host_no, fip->dest_addr);
0340         eth_zero_addr(fip->dest_addr);
0341     }
0342     if (sel) {
0343         printk(KERN_INFO "libfcoe: host%d: FIP selected "
0344                "Fibre-Channel Forwarder MAC %pM\n",
0345                fip->lp->host->host_no, sel->fcf_mac);
0346         memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
0347         fip->map_dest = 0;
0348     }
0349 unlock:
0350     mutex_unlock(&fip->ctlr_mutex);
0351 }
0352 
0353 /**
0354  * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
0355  * @fip: The FCoE controller to get the maximum FCoE size from
0356  *
0357  * Returns the maximum packet size including the FCoE header and trailer,
0358  * but not including any Ethernet or VLAN headers.
0359  */
0360 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
0361 {
0362     /*
0363      * Determine the max FCoE frame size allowed, including
0364      * FCoE header and trailer.
0365      * Note:  lp->mfs is currently the payload size, not the frame size.
0366      */
0367     return fip->lp->mfs + sizeof(struct fc_frame_header) +
0368         sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
0369 }
0370 
0371 /**
0372  * fcoe_ctlr_solicit() - Send a FIP solicitation
0373  * @fip: The FCoE controller to send the solicitation on
0374  * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
0375  */
0376 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
0377 {
0378     struct sk_buff *skb;
0379     struct fip_sol {
0380         struct ethhdr eth;
0381         struct fip_header fip;
0382         struct {
0383             struct fip_mac_desc mac;
0384             struct fip_wwn_desc wwnn;
0385             struct fip_size_desc size;
0386         } __packed desc;
0387     }  __packed * sol;
0388     u32 fcoe_size;
0389 
0390     skb = dev_alloc_skb(sizeof(*sol));
0391     if (!skb)
0392         return;
0393 
0394     sol = (struct fip_sol *)skb->data;
0395 
0396     memset(sol, 0, sizeof(*sol));
0397     memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
0398     memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
0399     sol->eth.h_proto = htons(ETH_P_FIP);
0400 
0401     sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
0402     sol->fip.fip_op = htons(FIP_OP_DISC);
0403     sol->fip.fip_subcode = FIP_SC_SOL;
0404     sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
0405     sol->fip.fip_flags = htons(FIP_FL_FPMA);
0406     if (fip->spma)
0407         sol->fip.fip_flags |= htons(FIP_FL_SPMA);
0408 
0409     sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
0410     sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
0411     memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
0412 
0413     sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
0414     sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
0415     put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
0416 
0417     fcoe_size = fcoe_ctlr_fcoe_size(fip);
0418     sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
0419     sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
0420     sol->desc.size.fd_size = htons(fcoe_size);
0421 
0422     skb_put(skb, sizeof(*sol));
0423     skb->protocol = htons(ETH_P_FIP);
0424     skb->priority = fip->priority;
0425     skb_reset_mac_header(skb);
0426     skb_reset_network_header(skb);
0427     fip->send(fip, skb);
0428 
0429     if (!fcf)
0430         fip->sol_time = jiffies;
0431 }
0432 
0433 /**
0434  * fcoe_ctlr_link_up() - Start FCoE controller
0435  * @fip: The FCoE controller to start
0436  *
0437  * Called from the LLD when the network link is ready.
0438  */
0439 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
0440 {
0441     mutex_lock(&fip->ctlr_mutex);
0442     if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
0443         mutex_unlock(&fip->ctlr_mutex);
0444         fc_linkup(fip->lp);
0445     } else if (fip->state == FIP_ST_LINK_WAIT) {
0446         if (fip->mode == FIP_MODE_NON_FIP)
0447             fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
0448         else
0449             fcoe_ctlr_set_state(fip, FIP_ST_AUTO);
0450         switch (fip->mode) {
0451         default:
0452             LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
0453             fallthrough;
0454         case FIP_MODE_AUTO:
0455             LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
0456             fallthrough;
0457         case FIP_MODE_FABRIC:
0458         case FIP_MODE_NON_FIP:
0459             mutex_unlock(&fip->ctlr_mutex);
0460             fc_linkup(fip->lp);
0461             fcoe_ctlr_solicit(fip, NULL);
0462             break;
0463         case FIP_MODE_VN2VN:
0464             fcoe_ctlr_vn_start(fip);
0465             mutex_unlock(&fip->ctlr_mutex);
0466             fc_linkup(fip->lp);
0467             break;
0468         }
0469     } else
0470         mutex_unlock(&fip->ctlr_mutex);
0471 }
0472 EXPORT_SYMBOL(fcoe_ctlr_link_up);
0473 
0474 /**
0475  * fcoe_ctlr_reset() - Reset a FCoE controller
0476  * @fip:       The FCoE controller to reset
0477  */
0478 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
0479 {
0480     fcoe_ctlr_reset_fcfs(fip);
0481     del_timer(&fip->timer);
0482     fip->ctlr_ka_time = 0;
0483     fip->port_ka_time = 0;
0484     fip->sol_time = 0;
0485     fip->flogi_oxid = FC_XID_UNKNOWN;
0486     fcoe_ctlr_map_dest(fip);
0487 }
0488 
0489 /**
0490  * fcoe_ctlr_link_down() - Stop a FCoE controller
0491  * @fip: The FCoE controller to be stopped
0492  *
0493  * Returns non-zero if the link was up and now isn't.
0494  *
0495  * Called from the LLD when the network link is not ready.
0496  * There may be multiple calls while the link is down.
0497  */
0498 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
0499 {
0500     int link_dropped;
0501 
0502     LIBFCOE_FIP_DBG(fip, "link down.\n");
0503     mutex_lock(&fip->ctlr_mutex);
0504     fcoe_ctlr_reset(fip);
0505     link_dropped = fip->state != FIP_ST_LINK_WAIT;
0506     fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
0507     mutex_unlock(&fip->ctlr_mutex);
0508 
0509     if (link_dropped)
0510         fc_linkdown(fip->lp);
0511     return link_dropped;
0512 }
0513 EXPORT_SYMBOL(fcoe_ctlr_link_down);
0514 
0515 /**
0516  * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
0517  * @fip:   The FCoE controller to send the FKA on
0518  * @lport: libfc fc_lport to send from
0519  * @ports: 0 for controller keep-alive, 1 for port keep-alive
0520  * @sa:    The source MAC address
0521  *
0522  * A controller keep-alive is sent every fka_period (typically 8 seconds).
0523  * The source MAC is the native MAC address.
0524  *
0525  * A port keep-alive is sent every 90 seconds while logged in.
0526  * The source MAC is the assigned mapped source address.
0527  * The destination is the FCF's F-port.
0528  */
0529 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
0530                       struct fc_lport *lport,
0531                       int ports, u8 *sa)
0532 {
0533     struct sk_buff *skb;
0534     struct fip_kal {
0535         struct ethhdr eth;
0536         struct fip_header fip;
0537         struct fip_mac_desc mac;
0538     } __packed * kal;
0539     struct fip_vn_desc *vn;
0540     u32 len;
0541     struct fc_lport *lp;
0542     struct fcoe_fcf *fcf;
0543 
0544     fcf = fip->sel_fcf;
0545     lp = fip->lp;
0546     if (!fcf || (ports && !lp->port_id))
0547         return;
0548 
0549     len = sizeof(*kal) + ports * sizeof(*vn);
0550     skb = dev_alloc_skb(len);
0551     if (!skb)
0552         return;
0553 
0554     kal = (struct fip_kal *)skb->data;
0555     memset(kal, 0, len);
0556     memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
0557     memcpy(kal->eth.h_source, sa, ETH_ALEN);
0558     kal->eth.h_proto = htons(ETH_P_FIP);
0559 
0560     kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
0561     kal->fip.fip_op = htons(FIP_OP_CTRL);
0562     kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
0563     kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
0564                      ports * sizeof(*vn)) / FIP_BPW);
0565     kal->fip.fip_flags = htons(FIP_FL_FPMA);
0566     if (fip->spma)
0567         kal->fip.fip_flags |= htons(FIP_FL_SPMA);
0568 
0569     kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
0570     kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
0571     memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
0572     if (ports) {
0573         vn = (struct fip_vn_desc *)(kal + 1);
0574         vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
0575         vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
0576         memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
0577         hton24(vn->fd_fc_id, lport->port_id);
0578         put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
0579     }
0580     skb_put(skb, len);
0581     skb->protocol = htons(ETH_P_FIP);
0582     skb->priority = fip->priority;
0583     skb_reset_mac_header(skb);
0584     skb_reset_network_header(skb);
0585     fip->send(fip, skb);
0586 }
0587 
0588 /**
0589  * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
0590  * @fip:   The FCoE controller for the ELS frame
0591  * @lport: The local port
0592  * @dtype: The FIP descriptor type for the frame
0593  * @skb:   The FCoE ELS frame including FC header but no FCoE headers
0594  * @d_id:  The destination port ID.
0595  *
0596  * Returns non-zero error code on failure.
0597  *
0598  * The caller must check that the length is a multiple of 4.
0599  *
0600  * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
0601  * Headroom includes the FIP encapsulation description, FIP header, and
0602  * Ethernet header.  The tailroom is for the FIP MAC descriptor.
0603  */
0604 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
0605                 u8 dtype, struct sk_buff *skb, u32 d_id)
0606 {
0607     struct fip_encaps_head {
0608         struct ethhdr eth;
0609         struct fip_header fip;
0610         struct fip_encaps encaps;
0611     } __packed * cap;
0612     struct fc_frame_header *fh;
0613     struct fip_mac_desc *mac;
0614     struct fcoe_fcf *fcf;
0615     size_t dlen;
0616     u16 fip_flags;
0617     u8 op;
0618 
0619     fh = (struct fc_frame_header *)skb->data;
0620     op = *(u8 *)(fh + 1);
0621     dlen = sizeof(struct fip_encaps) + skb->len;    /* len before push */
0622     cap = skb_push(skb, sizeof(*cap));
0623     memset(cap, 0, sizeof(*cap));
0624 
0625     if (lport->point_to_multipoint) {
0626         if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
0627             return -ENODEV;
0628         fip_flags = 0;
0629     } else {
0630         fcf = fip->sel_fcf;
0631         if (!fcf)
0632             return -ENODEV;
0633         fip_flags = fcf->flags;
0634         fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
0635                      FIP_FL_FPMA;
0636         if (!fip_flags)
0637             return -ENODEV;
0638         memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
0639     }
0640     memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
0641     cap->eth.h_proto = htons(ETH_P_FIP);
0642 
0643     cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
0644     cap->fip.fip_op = htons(FIP_OP_LS);
0645     if (op == ELS_LS_ACC || op == ELS_LS_RJT)
0646         cap->fip.fip_subcode = FIP_SC_REP;
0647     else
0648         cap->fip.fip_subcode = FIP_SC_REQ;
0649     cap->fip.fip_flags = htons(fip_flags);
0650 
0651     cap->encaps.fd_desc.fip_dtype = dtype;
0652     cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
0653 
0654     if (op != ELS_LS_RJT) {
0655         dlen += sizeof(*mac);
0656         mac = skb_put_zero(skb, sizeof(*mac));
0657         mac->fd_desc.fip_dtype = FIP_DT_MAC;
0658         mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
0659         if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
0660             memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
0661         } else if (fip->mode == FIP_MODE_VN2VN) {
0662             hton24(mac->fd_mac, FIP_VN_FC_MAP);
0663             hton24(mac->fd_mac + 3, fip->port_id);
0664         } else if (fip_flags & FIP_FL_SPMA) {
0665             LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
0666             memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
0667         } else {
0668             LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
0669             /* FPMA only FLOGI.  Must leave the MAC desc zeroed. */
0670         }
0671     }
0672     cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
0673 
0674     skb->protocol = htons(ETH_P_FIP);
0675     skb->priority = fip->priority;
0676     skb_reset_mac_header(skb);
0677     skb_reset_network_header(skb);
0678     return 0;
0679 }
0680 
0681 /**
0682  * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
0683  * @fip:    FCoE controller.
0684  * @lport:  libfc fc_lport to send from
0685  * @skb:    FCoE ELS frame including FC header but no FCoE headers.
0686  *
0687  * Returns a non-zero error code if the frame should not be sent.
0688  * Returns zero if the caller should send the frame with FCoE encapsulation.
0689  *
0690  * The caller must check that the length is a multiple of 4.
0691  * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
0692  * The the skb must also be an fc_frame.
0693  *
0694  * This is called from the lower-level driver with spinlocks held,
0695  * so we must not take a mutex here.
0696  */
0697 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
0698                struct sk_buff *skb)
0699 {
0700     struct fc_frame *fp;
0701     struct fc_frame_header *fh;
0702     u16 old_xid;
0703     u8 op;
0704     u8 mac[ETH_ALEN];
0705 
0706     fp = container_of(skb, struct fc_frame, skb);
0707     fh = (struct fc_frame_header *)skb->data;
0708     op = *(u8 *)(fh + 1);
0709 
0710     if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
0711         old_xid = fip->flogi_oxid;
0712         fip->flogi_oxid = ntohs(fh->fh_ox_id);
0713         if (fip->state == FIP_ST_AUTO) {
0714             if (old_xid == FC_XID_UNKNOWN)
0715                 fip->flogi_count = 0;
0716             fip->flogi_count++;
0717             if (fip->flogi_count < 3)
0718                 goto drop;
0719             fcoe_ctlr_map_dest(fip);
0720             return 0;
0721         }
0722         if (fip->state == FIP_ST_NON_FIP)
0723             fcoe_ctlr_map_dest(fip);
0724     }
0725 
0726     if (fip->state == FIP_ST_NON_FIP)
0727         return 0;
0728     if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
0729         goto drop;
0730     switch (op) {
0731     case ELS_FLOGI:
0732         op = FIP_DT_FLOGI;
0733         if (fip->mode == FIP_MODE_VN2VN)
0734             break;
0735         spin_lock_bh(&fip->ctlr_lock);
0736         kfree_skb(fip->flogi_req);
0737         fip->flogi_req = skb;
0738         fip->flogi_req_send = 1;
0739         spin_unlock_bh(&fip->ctlr_lock);
0740         schedule_work(&fip->timer_work);
0741         return -EINPROGRESS;
0742     case ELS_FDISC:
0743         if (ntoh24(fh->fh_s_id))
0744             return 0;
0745         op = FIP_DT_FDISC;
0746         break;
0747     case ELS_LOGO:
0748         if (fip->mode == FIP_MODE_VN2VN) {
0749             if (fip->state != FIP_ST_VNMP_UP)
0750                 goto drop;
0751             if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
0752                 goto drop;
0753         } else {
0754             if (fip->state != FIP_ST_ENABLED)
0755                 return 0;
0756             if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
0757                 return 0;
0758         }
0759         op = FIP_DT_LOGO;
0760         break;
0761     case ELS_LS_ACC:
0762         /*
0763          * If non-FIP, we may have gotten an SID by accepting an FLOGI
0764          * from a point-to-point connection.  Switch to using
0765          * the source mac based on the SID.  The destination
0766          * MAC in this case would have been set by receiving the
0767          * FLOGI.
0768          */
0769         if (fip->state == FIP_ST_NON_FIP) {
0770             if (fip->flogi_oxid == FC_XID_UNKNOWN)
0771                 return 0;
0772             fip->flogi_oxid = FC_XID_UNKNOWN;
0773             fc_fcoe_set_mac(mac, fh->fh_d_id);
0774             fip->update_mac(lport, mac);
0775         }
0776         fallthrough;
0777     case ELS_LS_RJT:
0778         op = fr_encaps(fp);
0779         if (op)
0780             break;
0781         return 0;
0782     default:
0783         if (fip->state != FIP_ST_ENABLED &&
0784             fip->state != FIP_ST_VNMP_UP)
0785             goto drop;
0786         return 0;
0787     }
0788     LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
0789             op, ntoh24(fh->fh_d_id));
0790     if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
0791         goto drop;
0792     fip->send(fip, skb);
0793     return -EINPROGRESS;
0794 drop:
0795     LIBFCOE_FIP_DBG(fip, "drop els_send op %u d_id %x\n",
0796             op, ntoh24(fh->fh_d_id));
0797     kfree_skb(skb);
0798     return -EINVAL;
0799 }
0800 EXPORT_SYMBOL(fcoe_ctlr_els_send);
0801 
0802 /**
0803  * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
0804  * @fip: The FCoE controller to free FCFs on
0805  *
0806  * Called with lock held and preemption disabled.
0807  *
0808  * An FCF is considered old if we have missed two advertisements.
0809  * That is, there have been no valid advertisement from it for 2.5
0810  * times its keep-alive period.
0811  *
0812  * In addition, determine the time when an FCF selection can occur.
0813  *
0814  * Also, increment the MissDiscAdvCount when no advertisement is received
0815  * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
0816  *
0817  * Returns the time in jiffies for the next call.
0818  */
0819 static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
0820 {
0821     struct fcoe_fcf *fcf;
0822     struct fcoe_fcf *next;
0823     unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
0824     unsigned long deadline;
0825     unsigned long sel_time = 0;
0826     struct list_head del_list;
0827 
0828     INIT_LIST_HEAD(&del_list);
0829 
0830     list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
0831         deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
0832         if (fip->sel_fcf == fcf) {
0833             if (time_after(jiffies, deadline)) {
0834                 u64 miss_cnt;
0835 
0836                 miss_cnt = this_cpu_inc_return(fip->lp->stats->MissDiscAdvCount);
0837                 printk(KERN_INFO "libfcoe: host%d: "
0838                        "Missing Discovery Advertisement "
0839                        "for fab %16.16llx count %lld\n",
0840                        fip->lp->host->host_no, fcf->fabric_name,
0841                        miss_cnt);
0842             } else if (time_after(next_timer, deadline))
0843                 next_timer = deadline;
0844         }
0845 
0846         deadline += fcf->fka_period;
0847         if (time_after_eq(jiffies, deadline)) {
0848             if (fip->sel_fcf == fcf)
0849                 fip->sel_fcf = NULL;
0850             /*
0851              * Move to delete list so we can call
0852              * fcoe_sysfs_fcf_del (which can sleep)
0853              * after the put_cpu().
0854              */
0855             list_del(&fcf->list);
0856             list_add(&fcf->list, &del_list);
0857             this_cpu_inc(fip->lp->stats->VLinkFailureCount);
0858         } else {
0859             if (time_after(next_timer, deadline))
0860                 next_timer = deadline;
0861             if (fcoe_ctlr_mtu_valid(fcf) &&
0862                 (!sel_time || time_before(sel_time, fcf->time)))
0863                 sel_time = fcf->time;
0864         }
0865     }
0866 
0867     list_for_each_entry_safe(fcf, next, &del_list, list) {
0868         /* Removes fcf from current list */
0869         fcoe_sysfs_fcf_del(fcf);
0870     }
0871 
0872     if (sel_time && !fip->sel_fcf && !fip->sel_time) {
0873         sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
0874         fip->sel_time = sel_time;
0875     }
0876 
0877     return next_timer;
0878 }
0879 
0880 /**
0881  * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
0882  * @fip: The FCoE controller receiving the advertisement
0883  * @skb: The received FIP advertisement frame
0884  * @fcf: The resulting FCF entry
0885  *
0886  * Returns zero on a valid parsed advertisement,
0887  * otherwise returns non zero value.
0888  */
0889 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
0890                    struct sk_buff *skb, struct fcoe_fcf *fcf)
0891 {
0892     struct fip_header *fiph;
0893     struct fip_desc *desc = NULL;
0894     struct fip_wwn_desc *wwn;
0895     struct fip_fab_desc *fab;
0896     struct fip_fka_desc *fka;
0897     unsigned long t;
0898     size_t rlen;
0899     size_t dlen;
0900     u32 desc_mask;
0901 
0902     memset(fcf, 0, sizeof(*fcf));
0903     fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
0904 
0905     fiph = (struct fip_header *)skb->data;
0906     fcf->flags = ntohs(fiph->fip_flags);
0907 
0908     /*
0909      * mask of required descriptors. validating each one clears its bit.
0910      */
0911     desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
0912             BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
0913 
0914     rlen = ntohs(fiph->fip_dl_len) * 4;
0915     if (rlen + sizeof(*fiph) > skb->len)
0916         return -EINVAL;
0917 
0918     desc = (struct fip_desc *)(fiph + 1);
0919     while (rlen > 0) {
0920         dlen = desc->fip_dlen * FIP_BPW;
0921         if (dlen < sizeof(*desc) || dlen > rlen)
0922             return -EINVAL;
0923         /* Drop Adv if there are duplicate critical descriptors */
0924         if ((desc->fip_dtype < 32) &&
0925             !(desc_mask & 1U << desc->fip_dtype)) {
0926             LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
0927                     "Descriptors in FIP adv\n");
0928             return -EINVAL;
0929         }
0930         switch (desc->fip_dtype) {
0931         case FIP_DT_PRI:
0932             if (dlen != sizeof(struct fip_pri_desc))
0933                 goto len_err;
0934             fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
0935             desc_mask &= ~BIT(FIP_DT_PRI);
0936             break;
0937         case FIP_DT_MAC:
0938             if (dlen != sizeof(struct fip_mac_desc))
0939                 goto len_err;
0940             memcpy(fcf->fcf_mac,
0941                    ((struct fip_mac_desc *)desc)->fd_mac,
0942                    ETH_ALEN);
0943             memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
0944             if (!is_valid_ether_addr(fcf->fcf_mac)) {
0945                 LIBFCOE_FIP_DBG(fip,
0946                     "Invalid MAC addr %pM in FIP adv\n",
0947                     fcf->fcf_mac);
0948                 return -EINVAL;
0949             }
0950             desc_mask &= ~BIT(FIP_DT_MAC);
0951             break;
0952         case FIP_DT_NAME:
0953             if (dlen != sizeof(struct fip_wwn_desc))
0954                 goto len_err;
0955             wwn = (struct fip_wwn_desc *)desc;
0956             fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
0957             desc_mask &= ~BIT(FIP_DT_NAME);
0958             break;
0959         case FIP_DT_FAB:
0960             if (dlen != sizeof(struct fip_fab_desc))
0961                 goto len_err;
0962             fab = (struct fip_fab_desc *)desc;
0963             fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
0964             fcf->vfid = ntohs(fab->fd_vfid);
0965             fcf->fc_map = ntoh24(fab->fd_map);
0966             desc_mask &= ~BIT(FIP_DT_FAB);
0967             break;
0968         case FIP_DT_FKA:
0969             if (dlen != sizeof(struct fip_fka_desc))
0970                 goto len_err;
0971             fka = (struct fip_fka_desc *)desc;
0972             if (fka->fd_flags & FIP_FKA_ADV_D)
0973                 fcf->fd_flags = 1;
0974             t = ntohl(fka->fd_fka_period);
0975             if (t >= FCOE_CTLR_MIN_FKA)
0976                 fcf->fka_period = msecs_to_jiffies(t);
0977             desc_mask &= ~BIT(FIP_DT_FKA);
0978             break;
0979         case FIP_DT_MAP_OUI:
0980         case FIP_DT_FCOE_SIZE:
0981         case FIP_DT_FLOGI:
0982         case FIP_DT_FDISC:
0983         case FIP_DT_LOGO:
0984         case FIP_DT_ELP:
0985         default:
0986             LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
0987                     "in FIP adv\n", desc->fip_dtype);
0988             /* standard says ignore unknown descriptors >= 128 */
0989             if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
0990                 return -EINVAL;
0991             break;
0992         }
0993         desc = (struct fip_desc *)((char *)desc + dlen);
0994         rlen -= dlen;
0995     }
0996     if (!fcf->fc_map || (fcf->fc_map & 0x10000))
0997         return -EINVAL;
0998     if (!fcf->switch_name)
0999         return -EINVAL;
1000     if (desc_mask) {
1001         LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
1002                 desc_mask);
1003         return -EINVAL;
1004     }
1005     return 0;
1006 
1007 len_err:
1008     LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1009             desc->fip_dtype, dlen);
1010     return -EINVAL;
1011 }
1012 
1013 /**
1014  * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
1015  * @fip: The FCoE controller receiving the advertisement
1016  * @skb: The received FIP packet
1017  */
1018 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1019 {
1020     struct fcoe_fcf *fcf;
1021     struct fcoe_fcf new;
1022     unsigned long sol_tov = msecs_to_jiffies(FCOE_CTLR_SOL_TOV);
1023     int first = 0;
1024     int mtu_valid;
1025     int found = 0;
1026     int rc = 0;
1027 
1028     if (fcoe_ctlr_parse_adv(fip, skb, &new))
1029         return;
1030 
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)) {
1038             found = 1;
1039             break;
1040         }
1041     }
1042     if (!found) {
1043         if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
1044             goto out;
1045 
1046         fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
1047         if (!fcf)
1048             goto out;
1049 
1050         memcpy(fcf, &new, sizeof(new));
1051         fcf->fip = fip;
1052         rc = fcoe_sysfs_fcf_add(fcf);
1053         if (rc) {
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);
1057             kfree(fcf);
1058             goto out;
1059         }
1060     } else {
1061         /*
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).
1066          */
1067         fcf->fd_flags = new.fd_flags;
1068         if (!fcoe_ctlr_fcf_usable(fcf))
1069             fcf->flags = new.flags;
1070 
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);
1076         }
1077         fcf->fka_period = new.fka_period;
1078         memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
1079     }
1080 
1081     mtu_valid = fcoe_ctlr_mtu_valid(fcf);
1082     fcf->time = jiffies;
1083     if (!found)
1084         LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
1085                 fcf->fabric_name, fcf->fcf_mac);
1086 
1087     /*
1088      * If this advertisement is not solicited and our max receive size
1089      * hasn't been verified, send a solicited advertisement.
1090      */
1091     if (!mtu_valid)
1092         fcoe_ctlr_solicit(fip, fcf);
1093 
1094     /*
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.
1099      */
1100     if (first && time_after(jiffies, fip->sol_time + sol_tov))
1101         fcoe_ctlr_solicit(fip, NULL);
1102 
1103     /*
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.
1108      */
1109     if (mtu_valid)
1110         list_move(&fcf->list, &fip->fcfs);
1111 
1112     /*
1113      * If this is the first validated FCF, note the time and
1114      * set a timer to trigger selection.
1115      */
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);
1123     }
1124 
1125 out:
1126     mutex_unlock(&fip->ctlr_mutex);
1127 }
1128 
1129 /**
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
1133  */
1134 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1135 {
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     enum fip_desc_type els_dtype = 0;
1144     u8 els_op;
1145     u8 sub;
1146     u8 granted_mac[ETH_ALEN] = { 0 };
1147     size_t els_len = 0;
1148     size_t rlen;
1149     size_t dlen;
1150     u32 desc_mask = 0;
1151     u32 desc_cnt = 0;
1152 
1153     fiph = (struct fip_header *)skb->data;
1154     sub = fiph->fip_subcode;
1155     if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1156         goto drop;
1157 
1158     rlen = ntohs(fiph->fip_dl_len) * 4;
1159     if (rlen + sizeof(*fiph) > skb->len)
1160         goto drop;
1161 
1162     desc = (struct fip_desc *)(fiph + 1);
1163     while (rlen > 0) {
1164         desc_cnt++;
1165         dlen = desc->fip_dlen * FIP_BPW;
1166         if (dlen < sizeof(*desc) || dlen > rlen)
1167             goto drop;
1168         /* Drop ELS if there are duplicate critical descriptors */
1169         if (desc->fip_dtype < 32) {
1170             if ((desc->fip_dtype != FIP_DT_MAC) &&
1171                 (desc_mask & 1U << desc->fip_dtype)) {
1172                 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1173                         "Descriptors in FIP ELS\n");
1174                 goto drop;
1175             }
1176             desc_mask |= (1 << desc->fip_dtype);
1177         }
1178         switch (desc->fip_dtype) {
1179         case FIP_DT_MAC:
1180             sel = fip->sel_fcf;
1181             if (desc_cnt == 1) {
1182                 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1183                         "received out of order\n");
1184                 goto drop;
1185             }
1186             /*
1187              * Some switch implementations send two MAC descriptors,
1188              * with first MAC(granted_mac) being the FPMA, and the
1189              * second one(fcoe_mac) is used as destination address
1190              * for sending/receiving FCoE packets. FIP traffic is
1191              * sent using fip_mac. For regular switches, both
1192              * fip_mac and fcoe_mac would be the same.
1193              */
1194             if (desc_cnt == 2)
1195                 memcpy(granted_mac,
1196                        ((struct fip_mac_desc *)desc)->fd_mac,
1197                        ETH_ALEN);
1198 
1199             if (dlen != sizeof(struct fip_mac_desc))
1200                 goto len_err;
1201 
1202             if ((desc_cnt == 3) && (sel))
1203                 memcpy(sel->fcoe_mac,
1204                        ((struct fip_mac_desc *)desc)->fd_mac,
1205                        ETH_ALEN);
1206             break;
1207         case FIP_DT_FLOGI:
1208         case FIP_DT_FDISC:
1209         case FIP_DT_LOGO:
1210         case FIP_DT_ELP:
1211             if (desc_cnt != 1) {
1212                 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1213                         "received out of order\n");
1214                 goto drop;
1215             }
1216             if (fh)
1217                 goto drop;
1218             if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1219                 goto len_err;
1220             els_len = dlen - sizeof(*els);
1221             els = (struct fip_encaps *)desc;
1222             fh = (struct fc_frame_header *)(els + 1);
1223             els_dtype = desc->fip_dtype;
1224             break;
1225         default:
1226             LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1227                     "in FIP adv\n", desc->fip_dtype);
1228             /* standard says ignore unknown descriptors >= 128 */
1229             if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1230                 goto drop;
1231             if (desc_cnt <= 2) {
1232                 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1233                         "received out of order\n");
1234                 goto drop;
1235             }
1236             break;
1237         }
1238         desc = (struct fip_desc *)((char *)desc + dlen);
1239         rlen -= dlen;
1240     }
1241 
1242     if (!fh)
1243         goto drop;
1244     els_op = *(u8 *)(fh + 1);
1245 
1246     if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1247         sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1248         if (els_op == ELS_LS_ACC) {
1249             if (!is_valid_ether_addr(granted_mac)) {
1250                 LIBFCOE_FIP_DBG(fip,
1251                     "Invalid MAC address %pM in FIP ELS\n",
1252                     granted_mac);
1253                 goto drop;
1254             }
1255             memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1256 
1257             if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1258                 fip->flogi_oxid = FC_XID_UNKNOWN;
1259                 if (els_dtype == FIP_DT_FLOGI)
1260                     fcoe_ctlr_announce(fip);
1261             }
1262         } else if (els_dtype == FIP_DT_FLOGI &&
1263                !fcoe_ctlr_flogi_retry(fip))
1264             goto drop;  /* retrying FLOGI so drop reject */
1265     }
1266 
1267     if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1268         (!(1U << FIP_DT_MAC & desc_mask)))) {
1269         LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1270                 "in FIP ELS\n");
1271         goto drop;
1272     }
1273 
1274     /*
1275      * Convert skb into an fc_frame containing only the ELS.
1276      */
1277     skb_pull(skb, (u8 *)fh - skb->data);
1278     skb_trim(skb, els_len);
1279     fp = (struct fc_frame *)skb;
1280     fc_frame_init(fp);
1281     fr_sof(fp) = FC_SOF_I3;
1282     fr_eof(fp) = FC_EOF_T;
1283     fr_dev(fp) = lport;
1284     fr_encaps(fp) = els_dtype;
1285 
1286     this_cpu_inc(lport->stats->RxFrames);
1287     this_cpu_add(lport->stats->RxWords, skb->len / FIP_BPW);
1288 
1289     fc_exch_recv(lport, fp);
1290     return;
1291 
1292 len_err:
1293     LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1294             desc->fip_dtype, dlen);
1295 drop:
1296     kfree_skb(skb);
1297 }
1298 
1299 /**
1300  * fcoe_ctlr_recv_clr_vlink() - Handle an incoming link reset frame
1301  * @fip: The FCoE controller that received the frame
1302  * @skb: The received FIP packet
1303  *
1304  * There may be multiple VN_Port descriptors.
1305  * The overall length has already been checked.
1306  */
1307 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1308                      struct sk_buff *skb)
1309 {
1310     struct fip_desc *desc;
1311     struct fip_mac_desc *mp;
1312     struct fip_wwn_desc *wp;
1313     struct fip_vn_desc *vp;
1314     size_t rlen;
1315     size_t dlen;
1316     struct fcoe_fcf *fcf = fip->sel_fcf;
1317     struct fc_lport *lport = fip->lp;
1318     struct fc_lport *vn_port = NULL;
1319     u32 desc_mask;
1320     int num_vlink_desc;
1321     int reset_phys_port = 0;
1322     struct fip_vn_desc **vlink_desc_arr = NULL;
1323     struct fip_header *fh = (struct fip_header *)skb->data;
1324     struct ethhdr *eh = eth_hdr(skb);
1325 
1326     LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1327 
1328     if (!fcf) {
1329         /*
1330          * We are yet to select best FCF, but we got CVL in the
1331          * meantime. reset the ctlr and let it rediscover the FCF
1332          */
1333         LIBFCOE_FIP_DBG(fip, "Resetting fcoe_ctlr as FCF has not been "
1334             "selected yet\n");
1335         mutex_lock(&fip->ctlr_mutex);
1336         fcoe_ctlr_reset(fip);
1337         mutex_unlock(&fip->ctlr_mutex);
1338         return;
1339     }
1340 
1341     /*
1342      * If we've selected an FCF check that the CVL is from there to avoid
1343      * processing CVLs from an unexpected source.  If it is from an
1344      * unexpected source drop it on the floor.
1345      */
1346     if (!ether_addr_equal(eh->h_source, fcf->fcf_mac)) {
1347         LIBFCOE_FIP_DBG(fip, "Dropping CVL due to source address "
1348             "mismatch with FCF src=%pM\n", eh->h_source);
1349         return;
1350     }
1351 
1352     /*
1353      * If we haven't logged into the fabric but receive a CVL we should
1354      * reset everything and go back to solicitation.
1355      */
1356     if (!lport->port_id) {
1357         LIBFCOE_FIP_DBG(fip, "lport not logged in, resoliciting\n");
1358         mutex_lock(&fip->ctlr_mutex);
1359         fcoe_ctlr_reset(fip);
1360         mutex_unlock(&fip->ctlr_mutex);
1361         fc_lport_reset(fip->lp);
1362         fcoe_ctlr_solicit(fip, NULL);
1363         return;
1364     }
1365 
1366     /*
1367      * mask of required descriptors.  Validating each one clears its bit.
1368      */
1369     desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
1370 
1371     rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1372     desc = (struct fip_desc *)(fh + 1);
1373 
1374     /*
1375      * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1376      * before determining max Vx_Port descriptor but a buggy FCF could have
1377      * omitted either or both MAC Address and Name Identifier descriptors
1378      */
1379     num_vlink_desc = rlen / sizeof(*vp);
1380     if (num_vlink_desc)
1381         vlink_desc_arr = kmalloc_array(num_vlink_desc, sizeof(vp),
1382                            GFP_ATOMIC);
1383     if (!vlink_desc_arr)
1384         return;
1385     num_vlink_desc = 0;
1386 
1387     while (rlen >= sizeof(*desc)) {
1388         dlen = desc->fip_dlen * FIP_BPW;
1389         if (dlen > rlen)
1390             goto err;
1391         /* Drop CVL if there are duplicate critical descriptors */
1392         if ((desc->fip_dtype < 32) &&
1393             (desc->fip_dtype != FIP_DT_VN_ID) &&
1394             !(desc_mask & 1U << desc->fip_dtype)) {
1395             LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1396                     "Descriptors in FIP CVL\n");
1397             goto err;
1398         }
1399         switch (desc->fip_dtype) {
1400         case FIP_DT_MAC:
1401             mp = (struct fip_mac_desc *)desc;
1402             if (dlen < sizeof(*mp))
1403                 goto err;
1404             if (!ether_addr_equal(mp->fd_mac, fcf->fcf_mac))
1405                 goto err;
1406             desc_mask &= ~BIT(FIP_DT_MAC);
1407             break;
1408         case FIP_DT_NAME:
1409             wp = (struct fip_wwn_desc *)desc;
1410             if (dlen < sizeof(*wp))
1411                 goto err;
1412             if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1413                 goto err;
1414             desc_mask &= ~BIT(FIP_DT_NAME);
1415             break;
1416         case FIP_DT_VN_ID:
1417             vp = (struct fip_vn_desc *)desc;
1418             if (dlen < sizeof(*vp))
1419                 goto err;
1420             vlink_desc_arr[num_vlink_desc++] = vp;
1421             vn_port = fc_vport_id_lookup(lport,
1422                               ntoh24(vp->fd_fc_id));
1423             if (vn_port && (vn_port == lport)) {
1424                 mutex_lock(&fip->ctlr_mutex);
1425                 this_cpu_inc(lport->stats->VLinkFailureCount);
1426                 fcoe_ctlr_reset(fip);
1427                 mutex_unlock(&fip->ctlr_mutex);
1428             }
1429             break;
1430         default:
1431             /* standard says ignore unknown descriptors >= 128 */
1432             if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1433                 goto err;
1434             break;
1435         }
1436         desc = (struct fip_desc *)((char *)desc + dlen);
1437         rlen -= dlen;
1438     }
1439 
1440     /*
1441      * reset only if all required descriptors were present and valid.
1442      */
1443     if (desc_mask)
1444         LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1445                 desc_mask);
1446     else if (!num_vlink_desc) {
1447         LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1448         /*
1449          * No Vx_Port description. Clear all NPIV ports,
1450          * followed by physical port
1451          */
1452         mutex_lock(&fip->ctlr_mutex);
1453         this_cpu_inc(lport->stats->VLinkFailureCount);
1454         fcoe_ctlr_reset(fip);
1455         mutex_unlock(&fip->ctlr_mutex);
1456 
1457         mutex_lock(&lport->lp_mutex);
1458         list_for_each_entry(vn_port, &lport->vports, list)
1459             fc_lport_reset(vn_port);
1460         mutex_unlock(&lport->lp_mutex);
1461 
1462         fc_lport_reset(fip->lp);
1463         fcoe_ctlr_solicit(fip, NULL);
1464     } else {
1465         int i;
1466 
1467         LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1468         for (i = 0; i < num_vlink_desc; i++) {
1469             vp = vlink_desc_arr[i];
1470             vn_port = fc_vport_id_lookup(lport,
1471                              ntoh24(vp->fd_fc_id));
1472             if (!vn_port)
1473                 continue;
1474 
1475             /*
1476              * 'port_id' is already validated, check MAC address and
1477              * wwpn
1478              */
1479             if (!ether_addr_equal(fip->get_src_addr(vn_port),
1480                           vp->fd_mac) ||
1481                 get_unaligned_be64(&vp->fd_wwpn) !=
1482                             vn_port->wwpn)
1483                 continue;
1484 
1485             if (vn_port == lport)
1486                 /*
1487                  * Physical port, defer processing till all
1488                  * listed NPIV ports are cleared
1489                  */
1490                 reset_phys_port = 1;
1491             else    /* NPIV port */
1492                 fc_lport_reset(vn_port);
1493         }
1494 
1495         if (reset_phys_port) {
1496             fc_lport_reset(fip->lp);
1497             fcoe_ctlr_solicit(fip, NULL);
1498         }
1499     }
1500 
1501 err:
1502     kfree(vlink_desc_arr);
1503 }
1504 
1505 /**
1506  * fcoe_ctlr_recv() - Receive a FIP packet
1507  * @fip: The FCoE controller that received the packet
1508  * @skb: The received FIP packet
1509  *
1510  * This may be called from either NET_RX_SOFTIRQ or IRQ.
1511  */
1512 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1513 {
1514     skb = skb_share_check(skb, GFP_ATOMIC);
1515     if (!skb)
1516         return;
1517     skb_queue_tail(&fip->fip_recv_list, skb);
1518     schedule_work(&fip->recv_work);
1519 }
1520 EXPORT_SYMBOL(fcoe_ctlr_recv);
1521 
1522 /**
1523  * fcoe_ctlr_recv_handler() - Receive a FIP frame
1524  * @fip: The FCoE controller that received the frame
1525  * @skb: The received FIP frame
1526  *
1527  * Returns non-zero if the frame is dropped.
1528  */
1529 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1530 {
1531     struct fip_header *fiph;
1532     struct ethhdr *eh;
1533     enum fip_state state;
1534     bool fip_vlan_resp = false;
1535     u16 op;
1536     u8 sub;
1537 
1538     if (skb_linearize(skb))
1539         goto drop;
1540     if (skb->len < sizeof(*fiph))
1541         goto drop;
1542     eh = eth_hdr(skb);
1543     if (fip->mode == FIP_MODE_VN2VN) {
1544         if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1545             !ether_addr_equal(eh->h_dest, fcoe_all_vn2vn) &&
1546             !ether_addr_equal(eh->h_dest, fcoe_all_p2p))
1547             goto drop;
1548     } else if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1549            !ether_addr_equal(eh->h_dest, fcoe_all_enode))
1550         goto drop;
1551     fiph = (struct fip_header *)skb->data;
1552     op = ntohs(fiph->fip_op);
1553     sub = fiph->fip_subcode;
1554 
1555     if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1556         goto drop;
1557     if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1558         goto drop;
1559 
1560     mutex_lock(&fip->ctlr_mutex);
1561     state = fip->state;
1562     if (state == FIP_ST_AUTO) {
1563         fip->map_dest = 0;
1564         fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1565         state = FIP_ST_ENABLED;
1566         LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1567     }
1568     fip_vlan_resp = fip->fip_resp;
1569     mutex_unlock(&fip->ctlr_mutex);
1570 
1571     if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1572         return fcoe_ctlr_vn_recv(fip, skb);
1573 
1574     if (fip_vlan_resp && op == FIP_OP_VLAN) {
1575         LIBFCOE_FIP_DBG(fip, "fip vlan discovery\n");
1576         return fcoe_ctlr_vlan_recv(fip, skb);
1577     }
1578 
1579     if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1580         state != FIP_ST_VNMP_CLAIM)
1581         goto drop;
1582 
1583     if (op == FIP_OP_LS) {
1584         fcoe_ctlr_recv_els(fip, skb);   /* consumes skb */
1585         return 0;
1586     }
1587 
1588     if (state != FIP_ST_ENABLED)
1589         goto drop;
1590 
1591     if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1592         fcoe_ctlr_recv_adv(fip, skb);
1593     else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1594         fcoe_ctlr_recv_clr_vlink(fip, skb);
1595     kfree_skb(skb);
1596     return 0;
1597 drop:
1598     kfree_skb(skb);
1599     return -1;
1600 }
1601 
1602 /**
1603  * fcoe_ctlr_select() - Select the best FCF (if possible)
1604  * @fip: The FCoE controller
1605  *
1606  * Returns the selected FCF, or NULL if none are usable.
1607  *
1608  * If there are conflicting advertisements, no FCF can be chosen.
1609  *
1610  * If there is already a selected FCF, this will choose a better one or
1611  * an equivalent one that hasn't already been sent a FLOGI.
1612  *
1613  * Called with lock held.
1614  */
1615 static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
1616 {
1617     struct fcoe_fcf *fcf;
1618     struct fcoe_fcf *best = fip->sel_fcf;
1619 
1620     list_for_each_entry(fcf, &fip->fcfs, list) {
1621         LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1622                 "VFID %d mac %pM map %x val %d "
1623                 "sent %u pri %u\n",
1624                 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1625                 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1626                 fcf->flogi_sent, fcf->pri);
1627         if (!fcoe_ctlr_fcf_usable(fcf)) {
1628             LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1629                     "map %x %svalid %savailable\n",
1630                     fcf->fabric_name, fcf->fc_map,
1631                     (fcf->flags & FIP_FL_SOL) ? "" : "in",
1632                     (fcf->flags & FIP_FL_AVAIL) ?
1633                     "" : "un");
1634             continue;
1635         }
1636         if (!best || fcf->pri < best->pri || best->flogi_sent)
1637             best = fcf;
1638         if (fcf->fabric_name != best->fabric_name ||
1639             fcf->vfid != best->vfid ||
1640             fcf->fc_map != best->fc_map) {
1641             LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1642                     "or FC-MAP\n");
1643             return NULL;
1644         }
1645     }
1646     fip->sel_fcf = best;
1647     if (best) {
1648         LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
1649         fip->port_ka_time = jiffies +
1650             msecs_to_jiffies(FIP_VN_KA_PERIOD);
1651         fip->ctlr_ka_time = jiffies + best->fka_period;
1652         if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1653             mod_timer(&fip->timer, fip->ctlr_ka_time);
1654     }
1655     return best;
1656 }
1657 
1658 /**
1659  * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1660  * @fip: The FCoE controller
1661  *
1662  * Returns non-zero error if it could not be sent.
1663  *
1664  * Called with ctlr_mutex and ctlr_lock held.
1665  * Caller must verify that fip->sel_fcf is not NULL.
1666  */
1667 static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1668 {
1669     struct sk_buff *skb;
1670     struct sk_buff *skb_orig;
1671     struct fc_frame_header *fh;
1672     int error;
1673 
1674     skb_orig = fip->flogi_req;
1675     if (!skb_orig)
1676         return -EINVAL;
1677 
1678     /*
1679      * Clone and send the FLOGI request.  If clone fails, use original.
1680      */
1681     skb = skb_clone(skb_orig, GFP_ATOMIC);
1682     if (!skb) {
1683         skb = skb_orig;
1684         fip->flogi_req = NULL;
1685     }
1686     fh = (struct fc_frame_header *)skb->data;
1687     error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1688                  ntoh24(fh->fh_d_id));
1689     if (error) {
1690         kfree_skb(skb);
1691         return error;
1692     }
1693     fip->send(fip, skb);
1694     fip->sel_fcf->flogi_sent = 1;
1695     return 0;
1696 }
1697 
1698 /**
1699  * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1700  * @fip: The FCoE controller
1701  *
1702  * Returns non-zero error code if there's no FLOGI request to retry or
1703  * no alternate FCF available.
1704  */
1705 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1706 {
1707     struct fcoe_fcf *fcf;
1708     int error;
1709 
1710     mutex_lock(&fip->ctlr_mutex);
1711     spin_lock_bh(&fip->ctlr_lock);
1712     LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1713     fcf = fcoe_ctlr_select(fip);
1714     if (!fcf || fcf->flogi_sent) {
1715         kfree_skb(fip->flogi_req);
1716         fip->flogi_req = NULL;
1717         error = -ENOENT;
1718     } else {
1719         fcoe_ctlr_solicit(fip, NULL);
1720         error = fcoe_ctlr_flogi_send_locked(fip);
1721     }
1722     spin_unlock_bh(&fip->ctlr_lock);
1723     mutex_unlock(&fip->ctlr_mutex);
1724     return error;
1725 }
1726 
1727 
1728 /**
1729  * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1730  * @fip: The FCoE controller that timed out
1731  *
1732  * Done here because fcoe_ctlr_els_send() can't get mutex.
1733  *
1734  * Called with ctlr_mutex held.  The caller must not hold ctlr_lock.
1735  */
1736 static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1737 {
1738     struct fcoe_fcf *fcf;
1739 
1740     spin_lock_bh(&fip->ctlr_lock);
1741     fcf = fip->sel_fcf;
1742     if (!fcf || !fip->flogi_req_send)
1743         goto unlock;
1744 
1745     LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1746 
1747     /*
1748      * If this FLOGI is being sent due to a timeout retry
1749      * to the same FCF as before, select a different FCF if possible.
1750      */
1751     if (fcf->flogi_sent) {
1752         LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1753         fcf = fcoe_ctlr_select(fip);
1754         if (!fcf || fcf->flogi_sent) {
1755             LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1756             list_for_each_entry(fcf, &fip->fcfs, list)
1757                 fcf->flogi_sent = 0;
1758             fcf = fcoe_ctlr_select(fip);
1759         }
1760     }
1761     if (fcf) {
1762         fcoe_ctlr_flogi_send_locked(fip);
1763         fip->flogi_req_send = 0;
1764     } else /* XXX */
1765         LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1766 unlock:
1767     spin_unlock_bh(&fip->ctlr_lock);
1768 }
1769 
1770 /**
1771  * fcoe_ctlr_timeout() - FIP timeout handler
1772  * @t: Timer context use to obtain the controller reference
1773  */
1774 static void fcoe_ctlr_timeout(struct timer_list *t)
1775 {
1776     struct fcoe_ctlr *fip = from_timer(fip, t, timer);
1777 
1778     schedule_work(&fip->timer_work);
1779 }
1780 
1781 /**
1782  * fcoe_ctlr_timer_work() - Worker thread function for timer work
1783  * @work: Handle to a FCoE controller
1784  *
1785  * Ages FCFs.  Triggers FCF selection if possible.
1786  * Sends keep-alives and resets.
1787  */
1788 static void fcoe_ctlr_timer_work(struct work_struct *work)
1789 {
1790     struct fcoe_ctlr *fip;
1791     struct fc_lport *vport;
1792     u8 *mac;
1793     u8 reset = 0;
1794     u8 send_ctlr_ka = 0;
1795     u8 send_port_ka = 0;
1796     struct fcoe_fcf *sel;
1797     struct fcoe_fcf *fcf;
1798     unsigned long next_timer;
1799 
1800     fip = container_of(work, struct fcoe_ctlr, timer_work);
1801     if (fip->mode == FIP_MODE_VN2VN)
1802         return fcoe_ctlr_vn_timeout(fip);
1803     mutex_lock(&fip->ctlr_mutex);
1804     if (fip->state == FIP_ST_DISABLED) {
1805         mutex_unlock(&fip->ctlr_mutex);
1806         return;
1807     }
1808 
1809     fcf = fip->sel_fcf;
1810     next_timer = fcoe_ctlr_age_fcfs(fip);
1811 
1812     sel = fip->sel_fcf;
1813     if (!sel && fip->sel_time) {
1814         if (time_after_eq(jiffies, fip->sel_time)) {
1815             sel = fcoe_ctlr_select(fip);
1816             fip->sel_time = 0;
1817         } else if (time_after(next_timer, fip->sel_time))
1818             next_timer = fip->sel_time;
1819     }
1820 
1821     if (sel && fip->flogi_req_send)
1822         fcoe_ctlr_flogi_send(fip);
1823     else if (!sel && fcf)
1824         reset = 1;
1825 
1826     if (sel && !sel->fd_flags) {
1827         if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1828             fip->ctlr_ka_time = jiffies + sel->fka_period;
1829             send_ctlr_ka = 1;
1830         }
1831         if (time_after(next_timer, fip->ctlr_ka_time))
1832             next_timer = fip->ctlr_ka_time;
1833 
1834         if (time_after_eq(jiffies, fip->port_ka_time)) {
1835             fip->port_ka_time = jiffies +
1836                 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1837             send_port_ka = 1;
1838         }
1839         if (time_after(next_timer, fip->port_ka_time))
1840             next_timer = fip->port_ka_time;
1841     }
1842     if (!list_empty(&fip->fcfs))
1843         mod_timer(&fip->timer, next_timer);
1844     mutex_unlock(&fip->ctlr_mutex);
1845 
1846     if (reset) {
1847         fc_lport_reset(fip->lp);
1848         /* restart things with a solicitation */
1849         fcoe_ctlr_solicit(fip, NULL);
1850     }
1851 
1852     if (send_ctlr_ka)
1853         fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1854 
1855     if (send_port_ka) {
1856         mutex_lock(&fip->lp->lp_mutex);
1857         mac = fip->get_src_addr(fip->lp);
1858         fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1859         list_for_each_entry(vport, &fip->lp->vports, list) {
1860             mac = fip->get_src_addr(vport);
1861             fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1862         }
1863         mutex_unlock(&fip->lp->lp_mutex);
1864     }
1865 }
1866 
1867 /**
1868  * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1869  * @recv_work: Handle to a FCoE controller
1870  */
1871 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1872 {
1873     struct fcoe_ctlr *fip;
1874     struct sk_buff *skb;
1875 
1876     fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1877     while ((skb = skb_dequeue(&fip->fip_recv_list)))
1878         fcoe_ctlr_recv_handler(fip, skb);
1879 }
1880 
1881 /**
1882  * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1883  * @fip: The FCoE controller
1884  * @lport: The local port
1885  * @fp:  The FC frame to snoop
1886  *
1887  * Snoop potential response to FLOGI or even incoming FLOGI.
1888  *
1889  * The caller has checked that we are waiting for login as indicated
1890  * by fip->flogi_oxid != FC_XID_UNKNOWN.
1891  *
1892  * The caller is responsible for freeing the frame.
1893  * Fill in the granted_mac address.
1894  *
1895  * Return non-zero if the frame should not be delivered to libfc.
1896  */
1897 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1898              struct fc_frame *fp)
1899 {
1900     struct fc_frame_header *fh;
1901     u8 op;
1902     u8 *sa;
1903 
1904     sa = eth_hdr(&fp->skb)->h_source;
1905     fh = fc_frame_header_get(fp);
1906     if (fh->fh_type != FC_TYPE_ELS)
1907         return 0;
1908 
1909     op = fc_frame_payload_op(fp);
1910     if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1911         fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1912 
1913         mutex_lock(&fip->ctlr_mutex);
1914         if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1915             mutex_unlock(&fip->ctlr_mutex);
1916             return -EINVAL;
1917         }
1918         fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1919         LIBFCOE_FIP_DBG(fip,
1920                 "received FLOGI LS_ACC using non-FIP mode\n");
1921 
1922         /*
1923          * FLOGI accepted.
1924          * If the src mac addr is FC_OUI-based, then we mark the
1925          * address_mode flag to use FC_OUI-based Ethernet DA.
1926          * Otherwise we use the FCoE gateway addr
1927          */
1928         if (ether_addr_equal(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1929             fcoe_ctlr_map_dest(fip);
1930         } else {
1931             memcpy(fip->dest_addr, sa, ETH_ALEN);
1932             fip->map_dest = 0;
1933         }
1934         fip->flogi_oxid = FC_XID_UNKNOWN;
1935         mutex_unlock(&fip->ctlr_mutex);
1936         fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1937     } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1938         /*
1939          * Save source MAC for point-to-point responses.
1940          */
1941         mutex_lock(&fip->ctlr_mutex);
1942         if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1943             memcpy(fip->dest_addr, sa, ETH_ALEN);
1944             fip->map_dest = 0;
1945             if (fip->state == FIP_ST_AUTO)
1946                 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1947                         "Setting non-FIP mode\n");
1948             fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1949         }
1950         mutex_unlock(&fip->ctlr_mutex);
1951     }
1952     return 0;
1953 }
1954 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1955 
1956 /**
1957  * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1958  * @mac:    The MAC address to convert
1959  * @scheme: The scheme to use when converting
1960  * @port:   The port indicator for converting
1961  *
1962  * Returns: u64 fc world wide name
1963  */
1964 u64 fcoe_wwn_from_mac(unsigned char mac[ETH_ALEN],
1965               unsigned int scheme, unsigned int port)
1966 {
1967     u64 wwn;
1968     u64 host_mac;
1969 
1970     /* The MAC is in NO, so flip only the low 48 bits */
1971     host_mac = ((u64) mac[0] << 40) |
1972         ((u64) mac[1] << 32) |
1973         ((u64) mac[2] << 24) |
1974         ((u64) mac[3] << 16) |
1975         ((u64) mac[4] << 8) |
1976         (u64) mac[5];
1977 
1978     WARN_ON(host_mac >= (1ULL << 48));
1979     wwn = host_mac | ((u64) scheme << 60);
1980     switch (scheme) {
1981     case 1:
1982         WARN_ON(port != 0);
1983         break;
1984     case 2:
1985         WARN_ON(port >= 0xfff);
1986         wwn |= (u64) port << 48;
1987         break;
1988     default:
1989         WARN_ON(1);
1990         break;
1991     }
1992 
1993     return wwn;
1994 }
1995 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1996 
1997 /**
1998  * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1999  * @rdata: libfc remote port
2000  */
2001 static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
2002 {
2003     return container_of(rdata, struct fcoe_rport, rdata);
2004 }
2005 
2006 /**
2007  * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
2008  * @fip: The FCoE controller
2009  * @sub: sub-opcode for probe request, reply, or advertisement.
2010  * @dest: The destination Ethernet MAC address
2011  * @min_len: minimum size of the Ethernet payload to be sent
2012  */
2013 static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
2014                   enum fip_vn2vn_subcode sub,
2015                   const u8 *dest, size_t min_len)
2016 {
2017     struct sk_buff *skb;
2018     struct fip_vn2vn_probe_frame {
2019         struct ethhdr eth;
2020         struct fip_header fip;
2021         struct fip_mac_desc mac;
2022         struct fip_wwn_desc wwnn;
2023         struct fip_vn_desc vn;
2024     } __packed * frame;
2025     struct fip_fc4_feat *ff;
2026     struct fip_size_desc *size;
2027     u32 fcp_feat;
2028     size_t len;
2029     size_t dlen;
2030 
2031     len = sizeof(*frame);
2032     dlen = 0;
2033     if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2034         dlen = sizeof(struct fip_fc4_feat) +
2035                sizeof(struct fip_size_desc);
2036         len += dlen;
2037     }
2038     dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
2039     len = max(len, min_len + sizeof(struct ethhdr));
2040 
2041     skb = dev_alloc_skb(len);
2042     if (!skb)
2043         return;
2044 
2045     frame = (struct fip_vn2vn_probe_frame *)skb->data;
2046     memset(frame, 0, len);
2047     memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2048 
2049     if (sub == FIP_SC_VN_BEACON) {
2050         hton24(frame->eth.h_source, FIP_VN_FC_MAP);
2051         hton24(frame->eth.h_source + 3, fip->port_id);
2052     } else {
2053         memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2054     }
2055     frame->eth.h_proto = htons(ETH_P_FIP);
2056 
2057     frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2058     frame->fip.fip_op = htons(FIP_OP_VN2VN);
2059     frame->fip.fip_subcode = sub;
2060     frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2061 
2062     frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2063     frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2064     memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2065 
2066     frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
2067     frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
2068     put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
2069 
2070     frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
2071     frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
2072     hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
2073     hton24(frame->vn.fd_mac + 3, fip->port_id);
2074     hton24(frame->vn.fd_fc_id, fip->port_id);
2075     put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
2076 
2077     /*
2078      * For claims, add FC-4 features.
2079      * TBD: Add interface to get fc-4 types and features from libfc.
2080      */
2081     if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2082         ff = (struct fip_fc4_feat *)(frame + 1);
2083         ff->fd_desc.fip_dtype = FIP_DT_FC4F;
2084         ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
2085         ff->fd_fts = fip->lp->fcts;
2086 
2087         fcp_feat = 0;
2088         if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
2089             fcp_feat |= FCP_FEAT_INIT;
2090         if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
2091             fcp_feat |= FCP_FEAT_TARG;
2092         fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
2093         ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
2094 
2095         size = (struct fip_size_desc *)(ff + 1);
2096         size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
2097         size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
2098         size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
2099     }
2100 
2101     skb_put(skb, len);
2102     skb->protocol = htons(ETH_P_FIP);
2103     skb->priority = fip->priority;
2104     skb_reset_mac_header(skb);
2105     skb_reset_network_header(skb);
2106 
2107     fip->send(fip, skb);
2108 }
2109 
2110 /**
2111  * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
2112  * @lport: The lport which is receiving the event
2113  * @rdata: remote port private data
2114  * @event: The event that occurred
2115  *
2116  * Locking Note:  The rport lock must not be held when calling this function.
2117  */
2118 static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
2119                     struct fc_rport_priv *rdata,
2120                     enum fc_rport_event event)
2121 {
2122     struct fcoe_ctlr *fip = lport->disc.priv;
2123     struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2124 
2125     LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
2126             rdata->ids.port_id, event);
2127 
2128     mutex_lock(&fip->ctlr_mutex);
2129     switch (event) {
2130     case RPORT_EV_READY:
2131         frport->login_count = 0;
2132         break;
2133     case RPORT_EV_LOGO:
2134     case RPORT_EV_FAILED:
2135     case RPORT_EV_STOP:
2136         frport->login_count++;
2137         if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
2138             LIBFCOE_FIP_DBG(fip,
2139                     "rport FLOGI limited port_id %6.6x\n",
2140                     rdata->ids.port_id);
2141             fc_rport_logoff(rdata);
2142         }
2143         break;
2144     default:
2145         break;
2146     }
2147     mutex_unlock(&fip->ctlr_mutex);
2148 }
2149 
2150 static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
2151     .event_callback = fcoe_ctlr_vn_rport_callback,
2152 };
2153 
2154 /**
2155  * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
2156  * @lport: The local port
2157  *
2158  * Called with ctlr_mutex held.
2159  */
2160 static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
2161 {
2162     struct fc_rport_priv *rdata;
2163 
2164     mutex_lock(&lport->disc.disc_mutex);
2165     list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2166         if (kref_get_unless_zero(&rdata->kref)) {
2167             fc_rport_logoff(rdata);
2168             kref_put(&rdata->kref, fc_rport_destroy);
2169         }
2170     }
2171     lport->disc.disc_callback = NULL;
2172     mutex_unlock(&lport->disc.disc_mutex);
2173 }
2174 
2175 /**
2176  * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2177  * @lport: The local port
2178  *
2179  * Called through the local port template for discovery.
2180  * Called without the ctlr_mutex held.
2181  */
2182 static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2183 {
2184     struct fcoe_ctlr *fip = lport->disc.priv;
2185 
2186     mutex_lock(&fip->ctlr_mutex);
2187     fcoe_ctlr_disc_stop_locked(lport);
2188     mutex_unlock(&fip->ctlr_mutex);
2189 }
2190 
2191 /**
2192  * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2193  * @lport: The local port
2194  *
2195  * Called through the local port template for discovery.
2196  * Called without the ctlr_mutex held.
2197  */
2198 static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2199 {
2200     fcoe_ctlr_disc_stop(lport);
2201     fc_rport_flush_queue();
2202     synchronize_rcu();
2203 }
2204 
2205 /**
2206  * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2207  * @fip: The FCoE controller
2208  *
2209  * Called with fcoe_ctlr lock held.
2210  */
2211 static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2212 {
2213     unsigned long wait;
2214     u32 port_id;
2215 
2216     fcoe_ctlr_disc_stop_locked(fip->lp);
2217 
2218     /*
2219      * Get proposed port ID.
2220      * If this is the first try after link up, use any previous port_id.
2221      * If there was none, use the low bits of the port_name.
2222      * On subsequent tries, get the next random one.
2223      * Don't use reserved IDs, use another non-zero value, just as random.
2224      */
2225     port_id = fip->port_id;
2226     if (fip->probe_tries)
2227         port_id = prandom_u32_state(&fip->rnd_state) & 0xffff;
2228     else if (!port_id)
2229         port_id = fip->lp->wwpn & 0xffff;
2230     if (!port_id || port_id == 0xffff)
2231         port_id = 1;
2232     fip->port_id = port_id;
2233 
2234     if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2235         fip->probe_tries++;
2236         wait = prandom_u32() % FIP_VN_PROBE_WAIT;
2237     } else
2238         wait = FIP_VN_RLIM_INT;
2239     mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2240     fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2241 }
2242 
2243 /**
2244  * fcoe_ctlr_vn_start() - Start in VN2VN mode
2245  * @fip: The FCoE controller
2246  *
2247  * Called with fcoe_ctlr lock held.
2248  */
2249 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2250 {
2251     fip->probe_tries = 0;
2252     prandom_seed_state(&fip->rnd_state, fip->lp->wwpn);
2253     fcoe_ctlr_vn_restart(fip);
2254 }
2255 
2256 /**
2257  * fcoe_ctlr_vn_parse - parse probe request or response
2258  * @fip: The FCoE controller
2259  * @skb: incoming packet
2260  * @frport: parsed FCoE rport from the probe request
2261  *
2262  * Returns non-zero error number on error.
2263  * Does not consume the packet.
2264  */
2265 static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2266                   struct sk_buff *skb,
2267                   struct fcoe_rport *frport)
2268 {
2269     struct fip_header *fiph;
2270     struct fip_desc *desc = NULL;
2271     struct fip_mac_desc *macd = NULL;
2272     struct fip_wwn_desc *wwn = NULL;
2273     struct fip_vn_desc *vn = NULL;
2274     struct fip_size_desc *size = NULL;
2275     size_t rlen;
2276     size_t dlen;
2277     u32 desc_mask = 0;
2278     u32 dtype;
2279     u8 sub;
2280 
2281     fiph = (struct fip_header *)skb->data;
2282     frport->flags = ntohs(fiph->fip_flags);
2283 
2284     sub = fiph->fip_subcode;
2285     switch (sub) {
2286     case FIP_SC_VN_PROBE_REQ:
2287     case FIP_SC_VN_PROBE_REP:
2288     case FIP_SC_VN_BEACON:
2289         desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2290                 BIT(FIP_DT_VN_ID);
2291         break;
2292     case FIP_SC_VN_CLAIM_NOTIFY:
2293     case FIP_SC_VN_CLAIM_REP:
2294         desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2295                 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2296                 BIT(FIP_DT_FCOE_SIZE);
2297         break;
2298     default:
2299         LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2300         return -EINVAL;
2301     }
2302 
2303     rlen = ntohs(fiph->fip_dl_len) * 4;
2304     if (rlen + sizeof(*fiph) > skb->len)
2305         return -EINVAL;
2306 
2307     desc = (struct fip_desc *)(fiph + 1);
2308     while (rlen > 0) {
2309         dlen = desc->fip_dlen * FIP_BPW;
2310         if (dlen < sizeof(*desc) || dlen > rlen)
2311             return -EINVAL;
2312 
2313         dtype = desc->fip_dtype;
2314         if (dtype < 32) {
2315             if (!(desc_mask & BIT(dtype))) {
2316                 LIBFCOE_FIP_DBG(fip,
2317                         "unexpected or duplicated desc "
2318                         "desc type %u in "
2319                         "FIP VN2VN subtype %u\n",
2320                         dtype, sub);
2321                 return -EINVAL;
2322             }
2323             desc_mask &= ~BIT(dtype);
2324         }
2325 
2326         switch (dtype) {
2327         case FIP_DT_MAC:
2328             if (dlen != sizeof(struct fip_mac_desc))
2329                 goto len_err;
2330             macd = (struct fip_mac_desc *)desc;
2331             if (!is_valid_ether_addr(macd->fd_mac)) {
2332                 LIBFCOE_FIP_DBG(fip,
2333                     "Invalid MAC addr %pM in FIP VN2VN\n",
2334                      macd->fd_mac);
2335                 return -EINVAL;
2336             }
2337             memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2338             break;
2339         case FIP_DT_NAME:
2340             if (dlen != sizeof(struct fip_wwn_desc))
2341                 goto len_err;
2342             wwn = (struct fip_wwn_desc *)desc;
2343             frport->rdata.ids.node_name =
2344                 get_unaligned_be64(&wwn->fd_wwn);
2345             break;
2346         case FIP_DT_VN_ID:
2347             if (dlen != sizeof(struct fip_vn_desc))
2348                 goto len_err;
2349             vn = (struct fip_vn_desc *)desc;
2350             memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2351             frport->rdata.ids.port_id = ntoh24(vn->fd_fc_id);
2352             frport->rdata.ids.port_name =
2353                 get_unaligned_be64(&vn->fd_wwpn);
2354             break;
2355         case FIP_DT_FC4F:
2356             if (dlen != sizeof(struct fip_fc4_feat))
2357                 goto len_err;
2358             break;
2359         case FIP_DT_FCOE_SIZE:
2360             if (dlen != sizeof(struct fip_size_desc))
2361                 goto len_err;
2362             size = (struct fip_size_desc *)desc;
2363             frport->fcoe_len = ntohs(size->fd_size);
2364             break;
2365         default:
2366             LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2367                     "in FIP probe\n", dtype);
2368             /* standard says ignore unknown descriptors >= 128 */
2369             if (dtype < FIP_DT_NON_CRITICAL)
2370                 return -EINVAL;
2371             break;
2372         }
2373         desc = (struct fip_desc *)((char *)desc + dlen);
2374         rlen -= dlen;
2375     }
2376     return 0;
2377 
2378 len_err:
2379     LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2380             dtype, dlen);
2381     return -EINVAL;
2382 }
2383 
2384 /**
2385  * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2386  * @fip: The FCoE controller
2387  *
2388  * Called with ctlr_mutex held.
2389  */
2390 static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2391 {
2392     fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2393     fip->sol_time = jiffies;
2394 }
2395 
2396 /**
2397  * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2398  * @fip: The FCoE controller
2399  * @frport: parsed FCoE rport from the probe request
2400  *
2401  * Called with ctlr_mutex held.
2402  */
2403 static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2404                    struct fcoe_rport *frport)
2405 {
2406     if (frport->rdata.ids.port_id != fip->port_id)
2407         return;
2408 
2409     switch (fip->state) {
2410     case FIP_ST_VNMP_CLAIM:
2411     case FIP_ST_VNMP_UP:
2412         LIBFCOE_FIP_DBG(fip, "vn_probe_req: send reply, state %x\n",
2413                 fip->state);
2414         fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2415                   frport->enode_mac, 0);
2416         break;
2417     case FIP_ST_VNMP_PROBE1:
2418     case FIP_ST_VNMP_PROBE2:
2419         /*
2420          * Decide whether to reply to the Probe.
2421          * Our selected address is never a "recorded" one, so
2422          * only reply if our WWPN is greater and the
2423          * Probe's REC bit is not set.
2424          * If we don't reply, we will change our address.
2425          */
2426         if (fip->lp->wwpn > frport->rdata.ids.port_name &&
2427             !(frport->flags & FIP_FL_REC_OR_P2P)) {
2428             LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2429                     "port_id collision\n");
2430             fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2431                       frport->enode_mac, 0);
2432             break;
2433         }
2434         fallthrough;
2435     case FIP_ST_VNMP_START:
2436         LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2437                 "restart VN2VN negotiation\n");
2438         fcoe_ctlr_vn_restart(fip);
2439         break;
2440     default:
2441         LIBFCOE_FIP_DBG(fip, "vn_probe_req: ignore state %x\n",
2442                 fip->state);
2443         break;
2444     }
2445 }
2446 
2447 /**
2448  * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2449  * @fip: The FCoE controller
2450  * @frport: parsed FCoE rport from the probe request
2451  *
2452  * Called with ctlr_mutex held.
2453  */
2454 static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2455                      struct fcoe_rport *frport)
2456 {
2457     if (frport->rdata.ids.port_id != fip->port_id)
2458         return;
2459     switch (fip->state) {
2460     case FIP_ST_VNMP_START:
2461     case FIP_ST_VNMP_PROBE1:
2462     case FIP_ST_VNMP_PROBE2:
2463     case FIP_ST_VNMP_CLAIM:
2464         LIBFCOE_FIP_DBG(fip, "vn_probe_reply: restart state %x\n",
2465                 fip->state);
2466         fcoe_ctlr_vn_restart(fip);
2467         break;
2468     case FIP_ST_VNMP_UP:
2469         LIBFCOE_FIP_DBG(fip, "vn_probe_reply: send claim notify\n");
2470         fcoe_ctlr_vn_send_claim(fip);
2471         break;
2472     default:
2473         break;
2474     }
2475 }
2476 
2477 /**
2478  * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2479  * @fip: The FCoE controller
2480  * @new: newly-parsed FCoE rport as a template for new rdata
2481  *
2482  * Called with ctlr_mutex held.
2483  */
2484 static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fcoe_rport *new)
2485 {
2486     struct fc_lport *lport = fip->lp;
2487     struct fc_rport_priv *rdata;
2488     struct fc_rport_identifiers *ids;
2489     struct fcoe_rport *frport;
2490     u32 port_id;
2491 
2492     port_id = new->rdata.ids.port_id;
2493     if (port_id == fip->port_id)
2494         return;
2495 
2496     mutex_lock(&lport->disc.disc_mutex);
2497     rdata = fc_rport_create(lport, port_id);
2498     if (!rdata) {
2499         mutex_unlock(&lport->disc.disc_mutex);
2500         return;
2501     }
2502     mutex_lock(&rdata->rp_mutex);
2503     mutex_unlock(&lport->disc.disc_mutex);
2504 
2505     rdata->ops = &fcoe_ctlr_vn_rport_ops;
2506     rdata->disc_id = lport->disc.disc_id;
2507 
2508     ids = &rdata->ids;
2509     if ((ids->port_name != -1 &&
2510          ids->port_name != new->rdata.ids.port_name) ||
2511         (ids->node_name != -1 &&
2512          ids->node_name != new->rdata.ids.node_name)) {
2513         mutex_unlock(&rdata->rp_mutex);
2514         LIBFCOE_FIP_DBG(fip, "vn_add rport logoff %6.6x\n", port_id);
2515         fc_rport_logoff(rdata);
2516         mutex_lock(&rdata->rp_mutex);
2517     }
2518     ids->port_name = new->rdata.ids.port_name;
2519     ids->node_name = new->rdata.ids.node_name;
2520     mutex_unlock(&rdata->rp_mutex);
2521 
2522     frport = fcoe_ctlr_rport(rdata);
2523     LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s state %d\n",
2524             port_id, frport->fcoe_len ? "old" : "new",
2525             rdata->rp_state);
2526     frport->fcoe_len = new->fcoe_len;
2527     frport->flags = new->flags;
2528     frport->login_count = new->login_count;
2529     memcpy(frport->enode_mac, new->enode_mac, ETH_ALEN);
2530     memcpy(frport->vn_mac, new->vn_mac, ETH_ALEN);
2531     frport->time = 0;
2532 }
2533 
2534 /**
2535  * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2536  * @fip: The FCoE controller
2537  * @port_id:  The port_id of the remote VN_node
2538  * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2539  *
2540  * Returns non-zero error if no remote port found.
2541  */
2542 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2543 {
2544     struct fc_lport *lport = fip->lp;
2545     struct fc_rport_priv *rdata;
2546     struct fcoe_rport *frport;
2547     int ret = -1;
2548 
2549     rdata = fc_rport_lookup(lport, port_id);
2550     if (rdata) {
2551         frport = fcoe_ctlr_rport(rdata);
2552         memcpy(mac, frport->enode_mac, ETH_ALEN);
2553         ret = 0;
2554         kref_put(&rdata->kref, fc_rport_destroy);
2555     }
2556     return ret;
2557 }
2558 
2559 /**
2560  * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2561  * @fip: The FCoE controller
2562  * @new: newly-parsed FCoE rport as a template for new rdata
2563  *
2564  * Called with ctlr_mutex held.
2565  */
2566 static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2567                       struct fcoe_rport *new)
2568 {
2569     if (new->flags & FIP_FL_REC_OR_P2P) {
2570         LIBFCOE_FIP_DBG(fip, "send probe req for P2P/REC\n");
2571         fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2572         return;
2573     }
2574     switch (fip->state) {
2575     case FIP_ST_VNMP_START:
2576     case FIP_ST_VNMP_PROBE1:
2577     case FIP_ST_VNMP_PROBE2:
2578         if (new->rdata.ids.port_id == fip->port_id) {
2579             LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2580                     "restart, state %d\n",
2581                     fip->state);
2582             fcoe_ctlr_vn_restart(fip);
2583         }
2584         break;
2585     case FIP_ST_VNMP_CLAIM:
2586     case FIP_ST_VNMP_UP:
2587         if (new->rdata.ids.port_id == fip->port_id) {
2588             if (new->rdata.ids.port_name > fip->lp->wwpn) {
2589                 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2590                         "restart, port_id collision\n");
2591                 fcoe_ctlr_vn_restart(fip);
2592                 break;
2593             }
2594             LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2595                     "send claim notify\n");
2596             fcoe_ctlr_vn_send_claim(fip);
2597             break;
2598         }
2599         LIBFCOE_FIP_DBG(fip, "vn_claim_notify: send reply to %x\n",
2600                 new->rdata.ids.port_id);
2601         fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, new->enode_mac,
2602                   min((u32)new->fcoe_len,
2603                       fcoe_ctlr_fcoe_size(fip)));
2604         fcoe_ctlr_vn_add(fip, new);
2605         break;
2606     default:
2607         LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2608                 "ignoring claim from %x\n",
2609                 new->rdata.ids.port_id);
2610         break;
2611     }
2612 }
2613 
2614 /**
2615  * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2616  * @fip: The FCoE controller that received the frame
2617  * @new: newly-parsed FCoE rport from the Claim Response
2618  *
2619  * Called with ctlr_mutex held.
2620  */
2621 static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2622                     struct fcoe_rport *new)
2623 {
2624     LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2625             new->rdata.ids.port_id, fcoe_ctlr_state(fip->state));
2626     if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2627         fcoe_ctlr_vn_add(fip, new);
2628 }
2629 
2630 /**
2631  * fcoe_ctlr_vn_beacon() - handle received beacon.
2632  * @fip: The FCoE controller that received the frame
2633  * @new: newly-parsed FCoE rport from the Beacon
2634  *
2635  * Called with ctlr_mutex held.
2636  */
2637 static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2638                 struct fcoe_rport *new)
2639 {
2640     struct fc_lport *lport = fip->lp;
2641     struct fc_rport_priv *rdata;
2642     struct fcoe_rport *frport;
2643 
2644     if (new->flags & FIP_FL_REC_OR_P2P) {
2645         LIBFCOE_FIP_DBG(fip, "p2p beacon while in vn2vn mode\n");
2646         fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2647         return;
2648     }
2649     rdata = fc_rport_lookup(lport, new->rdata.ids.port_id);
2650     if (rdata) {
2651         if (rdata->ids.node_name == new->rdata.ids.node_name &&
2652             rdata->ids.port_name == new->rdata.ids.port_name) {
2653             frport = fcoe_ctlr_rport(rdata);
2654 
2655             LIBFCOE_FIP_DBG(fip, "beacon from rport %x\n",
2656                     rdata->ids.port_id);
2657             if (!frport->time && fip->state == FIP_ST_VNMP_UP) {
2658                 LIBFCOE_FIP_DBG(fip, "beacon expired "
2659                         "for rport %x\n",
2660                         rdata->ids.port_id);
2661                 fc_rport_login(rdata);
2662             }
2663             frport->time = jiffies;
2664         }
2665         kref_put(&rdata->kref, fc_rport_destroy);
2666         return;
2667     }
2668     if (fip->state != FIP_ST_VNMP_UP)
2669         return;
2670 
2671     /*
2672      * Beacon from a new neighbor.
2673      * Send a claim notify if one hasn't been sent recently.
2674      * Don't add the neighbor yet.
2675      */
2676     LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2677             new->rdata.ids.port_id);
2678     if (time_after(jiffies,
2679                fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2680         fcoe_ctlr_vn_send_claim(fip);
2681 }
2682 
2683 /**
2684  * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2685  * @fip: The FCoE controller
2686  *
2687  * Called with ctlr_mutex held.
2688  * Called only in state FIP_ST_VNMP_UP.
2689  * Returns the soonest time for next age-out or a time far in the future.
2690  */
2691 static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2692 {
2693     struct fc_lport *lport = fip->lp;
2694     struct fc_rport_priv *rdata;
2695     struct fcoe_rport *frport;
2696     unsigned long next_time;
2697     unsigned long deadline;
2698 
2699     next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2700     mutex_lock(&lport->disc.disc_mutex);
2701     list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2702         if (!kref_get_unless_zero(&rdata->kref))
2703             continue;
2704         frport = fcoe_ctlr_rport(rdata);
2705         if (!frport->time) {
2706             kref_put(&rdata->kref, fc_rport_destroy);
2707             continue;
2708         }
2709         deadline = frport->time +
2710                msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2711         if (time_after_eq(jiffies, deadline)) {
2712             frport->time = 0;
2713             LIBFCOE_FIP_DBG(fip,
2714                 "port %16.16llx fc_id %6.6x beacon expired\n",
2715                 rdata->ids.port_name, rdata->ids.port_id);
2716             fc_rport_logoff(rdata);
2717         } else if (time_before(deadline, next_time))
2718             next_time = deadline;
2719         kref_put(&rdata->kref, fc_rport_destroy);
2720     }
2721     mutex_unlock(&lport->disc.disc_mutex);
2722     return next_time;
2723 }
2724 
2725 /**
2726  * fcoe_ctlr_vn_recv() - Receive a FIP frame
2727  * @fip: The FCoE controller that received the frame
2728  * @skb: The received FIP frame
2729  *
2730  * Returns non-zero if the frame is dropped.
2731  * Always consumes the frame.
2732  */
2733 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2734 {
2735     struct fip_header *fiph;
2736     enum fip_vn2vn_subcode sub;
2737     struct fcoe_rport frport = { };
2738     int rc, vlan_id = 0;
2739 
2740     fiph = (struct fip_header *)skb->data;
2741     sub = fiph->fip_subcode;
2742 
2743     if (fip->lp->vlan)
2744         vlan_id = skb_vlan_tag_get_id(skb);
2745 
2746     if (vlan_id && vlan_id != fip->lp->vlan) {
2747         LIBFCOE_FIP_DBG(fip, "vn_recv drop frame sub %x vlan %d\n",
2748                 sub, vlan_id);
2749         rc = -EAGAIN;
2750         goto drop;
2751     }
2752 
2753     rc = fcoe_ctlr_vn_parse(fip, skb, &frport);
2754     if (rc) {
2755         LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2756         goto drop;
2757     }
2758 
2759     mutex_lock(&fip->ctlr_mutex);
2760     switch (sub) {
2761     case FIP_SC_VN_PROBE_REQ:
2762         fcoe_ctlr_vn_probe_req(fip, &frport);
2763         break;
2764     case FIP_SC_VN_PROBE_REP:
2765         fcoe_ctlr_vn_probe_reply(fip, &frport);
2766         break;
2767     case FIP_SC_VN_CLAIM_NOTIFY:
2768         fcoe_ctlr_vn_claim_notify(fip, &frport);
2769         break;
2770     case FIP_SC_VN_CLAIM_REP:
2771         fcoe_ctlr_vn_claim_resp(fip, &frport);
2772         break;
2773     case FIP_SC_VN_BEACON:
2774         fcoe_ctlr_vn_beacon(fip, &frport);
2775         break;
2776     default:
2777         LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2778         rc = -1;
2779         break;
2780     }
2781     mutex_unlock(&fip->ctlr_mutex);
2782 drop:
2783     kfree_skb(skb);
2784     return rc;
2785 }
2786 
2787 /**
2788  * fcoe_ctlr_vlan_parse - parse vlan discovery request or response
2789  * @fip: The FCoE controller
2790  * @skb: incoming packet
2791  * @frport: parsed FCoE rport from the probe request
2792  *
2793  * Returns non-zero error number on error.
2794  * Does not consume the packet.
2795  */
2796 static int fcoe_ctlr_vlan_parse(struct fcoe_ctlr *fip,
2797                   struct sk_buff *skb,
2798                   struct fcoe_rport *frport)
2799 {
2800     struct fip_header *fiph;
2801     struct fip_desc *desc = NULL;
2802     struct fip_mac_desc *macd = NULL;
2803     struct fip_wwn_desc *wwn = NULL;
2804     size_t rlen;
2805     size_t dlen;
2806     u32 desc_mask = 0;
2807     u32 dtype;
2808     u8 sub;
2809 
2810     fiph = (struct fip_header *)skb->data;
2811     frport->flags = ntohs(fiph->fip_flags);
2812 
2813     sub = fiph->fip_subcode;
2814     switch (sub) {
2815     case FIP_SC_VL_REQ:
2816         desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
2817         break;
2818     default:
2819         LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2820         return -EINVAL;
2821     }
2822 
2823     rlen = ntohs(fiph->fip_dl_len) * 4;
2824     if (rlen + sizeof(*fiph) > skb->len)
2825         return -EINVAL;
2826 
2827     desc = (struct fip_desc *)(fiph + 1);
2828     while (rlen > 0) {
2829         dlen = desc->fip_dlen * FIP_BPW;
2830         if (dlen < sizeof(*desc) || dlen > rlen)
2831             return -EINVAL;
2832 
2833         dtype = desc->fip_dtype;
2834         if (dtype < 32) {
2835             if (!(desc_mask & BIT(dtype))) {
2836                 LIBFCOE_FIP_DBG(fip,
2837                         "unexpected or duplicated desc "
2838                         "desc type %u in "
2839                         "FIP VN2VN subtype %u\n",
2840                         dtype, sub);
2841                 return -EINVAL;
2842             }
2843             desc_mask &= ~BIT(dtype);
2844         }
2845 
2846         switch (dtype) {
2847         case FIP_DT_MAC:
2848             if (dlen != sizeof(struct fip_mac_desc))
2849                 goto len_err;
2850             macd = (struct fip_mac_desc *)desc;
2851             if (!is_valid_ether_addr(macd->fd_mac)) {
2852                 LIBFCOE_FIP_DBG(fip,
2853                     "Invalid MAC addr %pM in FIP VN2VN\n",
2854                      macd->fd_mac);
2855                 return -EINVAL;
2856             }
2857             memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2858             break;
2859         case FIP_DT_NAME:
2860             if (dlen != sizeof(struct fip_wwn_desc))
2861                 goto len_err;
2862             wwn = (struct fip_wwn_desc *)desc;
2863             frport->rdata.ids.node_name =
2864                 get_unaligned_be64(&wwn->fd_wwn);
2865             break;
2866         default:
2867             LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2868                     "in FIP probe\n", dtype);
2869             /* standard says ignore unknown descriptors >= 128 */
2870             if (dtype < FIP_DT_NON_CRITICAL)
2871                 return -EINVAL;
2872             break;
2873         }
2874         desc = (struct fip_desc *)((char *)desc + dlen);
2875         rlen -= dlen;
2876     }
2877     return 0;
2878 
2879 len_err:
2880     LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2881             dtype, dlen);
2882     return -EINVAL;
2883 }
2884 
2885 /**
2886  * fcoe_ctlr_vlan_send() - Send a FIP VLAN Notification
2887  * @fip: The FCoE controller
2888  * @sub: sub-opcode for vlan notification or vn2vn vlan notification
2889  * @dest: The destination Ethernet MAC address
2890  */
2891 static void fcoe_ctlr_vlan_send(struct fcoe_ctlr *fip,
2892                   enum fip_vlan_subcode sub,
2893                   const u8 *dest)
2894 {
2895     struct sk_buff *skb;
2896     struct fip_vlan_notify_frame {
2897         struct ethhdr eth;
2898         struct fip_header fip;
2899         struct fip_mac_desc mac;
2900         struct fip_vlan_desc vlan;
2901     } __packed * frame;
2902     size_t len;
2903     size_t dlen;
2904 
2905     len = sizeof(*frame);
2906     dlen = sizeof(frame->mac) + sizeof(frame->vlan);
2907     len = max(len, sizeof(struct ethhdr));
2908 
2909     skb = dev_alloc_skb(len);
2910     if (!skb)
2911         return;
2912 
2913     LIBFCOE_FIP_DBG(fip, "fip %s vlan notification, vlan %d\n",
2914             fip->mode == FIP_MODE_VN2VN ? "vn2vn" : "fcf",
2915             fip->lp->vlan);
2916 
2917     frame = (struct fip_vlan_notify_frame *)skb->data;
2918     memset(frame, 0, len);
2919     memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2920 
2921     memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2922     frame->eth.h_proto = htons(ETH_P_FIP);
2923 
2924     frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2925     frame->fip.fip_op = htons(FIP_OP_VLAN);
2926     frame->fip.fip_subcode = sub;
2927     frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2928 
2929     frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2930     frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2931     memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2932 
2933     frame->vlan.fd_desc.fip_dtype = FIP_DT_VLAN;
2934     frame->vlan.fd_desc.fip_dlen = sizeof(frame->vlan) / FIP_BPW;
2935     put_unaligned_be16(fip->lp->vlan, &frame->vlan.fd_vlan);
2936 
2937     skb_put(skb, len);
2938     skb->protocol = htons(ETH_P_FIP);
2939     skb->priority = fip->priority;
2940     skb_reset_mac_header(skb);
2941     skb_reset_network_header(skb);
2942 
2943     fip->send(fip, skb);
2944 }
2945 
2946 /**
2947  * fcoe_ctlr_vlan_disc_reply() - send FIP VLAN Discovery Notification.
2948  * @fip: The FCoE controller
2949  * @frport: The newly-parsed FCoE rport from the Discovery Request
2950  *
2951  * Called with ctlr_mutex held.
2952  */
2953 static void fcoe_ctlr_vlan_disc_reply(struct fcoe_ctlr *fip,
2954                       struct fcoe_rport *frport)
2955 {
2956     enum fip_vlan_subcode sub = FIP_SC_VL_NOTE;
2957 
2958     if (fip->mode == FIP_MODE_VN2VN)
2959         sub = FIP_SC_VL_VN2VN_NOTE;
2960 
2961     fcoe_ctlr_vlan_send(fip, sub, frport->enode_mac);
2962 }
2963 
2964 /**
2965  * fcoe_ctlr_vlan_recv - vlan request receive handler for VN2VN mode.
2966  * @fip: The FCoE controller
2967  * @skb: The received FIP packet
2968  */
2969 static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2970 {
2971     struct fip_header *fiph;
2972     enum fip_vlan_subcode sub;
2973     struct fcoe_rport frport = { };
2974     int rc;
2975 
2976     fiph = (struct fip_header *)skb->data;
2977     sub = fiph->fip_subcode;
2978     rc = fcoe_ctlr_vlan_parse(fip, skb, &frport);
2979     if (rc) {
2980         LIBFCOE_FIP_DBG(fip, "vlan_recv vlan_parse error %d\n", rc);
2981         goto drop;
2982     }
2983     mutex_lock(&fip->ctlr_mutex);
2984     if (sub == FIP_SC_VL_REQ)
2985         fcoe_ctlr_vlan_disc_reply(fip, &frport);
2986     mutex_unlock(&fip->ctlr_mutex);
2987 
2988 drop:
2989     kfree_skb(skb);
2990     return rc;
2991 }
2992 
2993 /**
2994  * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
2995  * @lport: The local port
2996  * @fp: The received frame
2997  *
2998  * This should never be called since we don't see RSCNs or other
2999  * fabric-generated ELSes.
3000  */
3001 static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
3002 {
3003     struct fc_seq_els_data rjt_data;
3004 
3005     rjt_data.reason = ELS_RJT_UNSUP;
3006     rjt_data.explan = ELS_EXPL_NONE;
3007     fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
3008     fc_frame_free(fp);
3009 }
3010 
3011 /*
3012  * fcoe_ctlr_disc_start - start discovery for VN2VN mode.
3013  *
3014  * This sets a flag indicating that remote ports should be created
3015  * and started for the peers we discover.  We use the disc_callback
3016  * pointer as that flag.  Peers already discovered are created here.
3017  *
3018  * The lport lock is held during this call. The callback must be done
3019  * later, without holding either the lport or discovery locks.
3020  * The fcoe_ctlr lock may also be held during this call.
3021  */
3022 static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
3023                           enum fc_disc_event),
3024                  struct fc_lport *lport)
3025 {
3026     struct fc_disc *disc = &lport->disc;
3027     struct fcoe_ctlr *fip = disc->priv;
3028 
3029     mutex_lock(&disc->disc_mutex);
3030     disc->disc_callback = callback;
3031     disc->disc_id = (disc->disc_id + 2) | 1;
3032     disc->pending = 1;
3033     schedule_work(&fip->timer_work);
3034     mutex_unlock(&disc->disc_mutex);
3035 }
3036 
3037 /**
3038  * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
3039  * @fip: The FCoE controller
3040  *
3041  * Starts the FLOGI and PLOGI login process to each discovered rport for which
3042  * we've received at least one beacon.
3043  * Performs the discovery complete callback.
3044  */
3045 static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
3046 {
3047     struct fc_lport *lport = fip->lp;
3048     struct fc_disc *disc = &lport->disc;
3049     struct fc_rport_priv *rdata;
3050     struct fcoe_rport *frport;
3051     void (*callback)(struct fc_lport *, enum fc_disc_event);
3052 
3053     mutex_lock(&disc->disc_mutex);
3054     callback = disc->pending ? disc->disc_callback : NULL;
3055     disc->pending = 0;
3056     list_for_each_entry_rcu(rdata, &disc->rports, peers) {
3057         if (!kref_get_unless_zero(&rdata->kref))
3058             continue;
3059         frport = fcoe_ctlr_rport(rdata);
3060         if (frport->time)
3061             fc_rport_login(rdata);
3062         kref_put(&rdata->kref, fc_rport_destroy);
3063     }
3064     mutex_unlock(&disc->disc_mutex);
3065     if (callback)
3066         callback(lport, DISC_EV_SUCCESS);
3067 }
3068 
3069 /**
3070  * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
3071  * @fip: The FCoE controller
3072  */
3073 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
3074 {
3075     unsigned long next_time;
3076     u8 mac[ETH_ALEN];
3077     u32 new_port_id = 0;
3078 
3079     mutex_lock(&fip->ctlr_mutex);
3080     switch (fip->state) {
3081     case FIP_ST_VNMP_START:
3082         fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
3083         LIBFCOE_FIP_DBG(fip, "vn_timeout: send 1st probe request\n");
3084         fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3085         next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
3086         break;
3087     case FIP_ST_VNMP_PROBE1:
3088         fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
3089         LIBFCOE_FIP_DBG(fip, "vn_timeout: send 2nd probe request\n");
3090         fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3091         next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3092         break;
3093     case FIP_ST_VNMP_PROBE2:
3094         fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
3095         new_port_id = fip->port_id;
3096         hton24(mac, FIP_VN_FC_MAP);
3097         hton24(mac + 3, new_port_id);
3098         fcoe_ctlr_map_dest(fip);
3099         fip->update_mac(fip->lp, mac);
3100         LIBFCOE_FIP_DBG(fip, "vn_timeout: send claim notify\n");
3101         fcoe_ctlr_vn_send_claim(fip);
3102         next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3103         break;
3104     case FIP_ST_VNMP_CLAIM:
3105         /*
3106          * This may be invoked either by starting discovery so don't
3107          * go to the next state unless it's been long enough.
3108          */
3109         next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3110         if (time_after_eq(jiffies, next_time)) {
3111             fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
3112             LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3113             fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3114                       fcoe_all_vn2vn, 0);
3115             next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3116             fip->port_ka_time = next_time;
3117         }
3118         fcoe_ctlr_vn_disc(fip);
3119         break;
3120     case FIP_ST_VNMP_UP:
3121         next_time = fcoe_ctlr_vn_age(fip);
3122         if (time_after_eq(jiffies, fip->port_ka_time)) {
3123             LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3124             fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3125                       fcoe_all_vn2vn, 0);
3126             fip->port_ka_time = jiffies +
3127                  msecs_to_jiffies(FIP_VN_BEACON_INT +
3128                     (prandom_u32() % FIP_VN_BEACON_FUZZ));
3129         }
3130         if (time_before(fip->port_ka_time, next_time))
3131             next_time = fip->port_ka_time;
3132         break;
3133     case FIP_ST_LINK_WAIT:
3134         goto unlock;
3135     default:
3136         WARN(1, "unexpected state %d\n", fip->state);
3137         goto unlock;
3138     }
3139     mod_timer(&fip->timer, next_time);
3140 unlock:
3141     mutex_unlock(&fip->ctlr_mutex);
3142 
3143     /* If port ID is new, notify local port after dropping ctlr_mutex */
3144     if (new_port_id)
3145         fc_lport_set_local_id(fip->lp, new_port_id);
3146 }
3147 
3148 /**
3149  * fcoe_ctlr_mode_set() - Set or reset the ctlr's mode
3150  * @lport: The local port to be (re)configured
3151  * @fip:   The FCoE controller whose mode is changing
3152  * @fip_mode: The new fip mode
3153  *
3154  * Note that the we shouldn't be changing the libfc discovery settings
3155  * (fc_disc_config) while an lport is going through the libfc state
3156  * machine. The mode can only be changed when a fcoe_ctlr device is
3157  * disabled, so that should ensure that this routine is only called
3158  * when nothing is happening.
3159  */
3160 static void fcoe_ctlr_mode_set(struct fc_lport *lport, struct fcoe_ctlr *fip,
3161                    enum fip_mode fip_mode)
3162 {
3163     void *priv;
3164 
3165     WARN_ON(lport->state != LPORT_ST_RESET &&
3166         lport->state != LPORT_ST_DISABLED);
3167 
3168     if (fip_mode == FIP_MODE_VN2VN) {
3169         lport->rport_priv_size = sizeof(struct fcoe_rport);
3170         lport->point_to_multipoint = 1;
3171         lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
3172         lport->tt.disc_start = fcoe_ctlr_disc_start;
3173         lport->tt.disc_stop = fcoe_ctlr_disc_stop;
3174         lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
3175         priv = fip;
3176     } else {
3177         lport->rport_priv_size = 0;
3178         lport->point_to_multipoint = 0;
3179         lport->tt.disc_recv_req = NULL;
3180         lport->tt.disc_start = NULL;
3181         lport->tt.disc_stop = NULL;
3182         lport->tt.disc_stop_final = NULL;
3183         priv = lport;
3184     }
3185 
3186     fc_disc_config(lport, priv);
3187 }
3188 
3189 /**
3190  * fcoe_libfc_config() - Sets up libfc related properties for local port
3191  * @lport:    The local port to configure libfc for
3192  * @fip:      The FCoE controller in use by the local port
3193  * @tt:       The libfc function template
3194  * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
3195  *
3196  * Returns : 0 for success
3197  */
3198 int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
3199               const struct libfc_function_template *tt, int init_fcp)
3200 {
3201     /* Set the function pointers set by the LLDD */
3202     memcpy(&lport->tt, tt, sizeof(*tt));
3203     if (init_fcp && fc_fcp_init(lport))
3204         return -ENOMEM;
3205     fc_exch_init(lport);
3206     fc_elsct_init(lport);
3207     fc_lport_init(lport);
3208     fc_disc_init(lport);
3209     fcoe_ctlr_mode_set(lport, fip, fip->mode);
3210     return 0;
3211 }
3212 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
3213 
3214 void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
3215 {
3216     struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
3217     struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
3218     struct fcoe_fcf *fcf;
3219 
3220     mutex_lock(&fip->ctlr_mutex);
3221     mutex_lock(&ctlr_dev->lock);
3222 
3223     fcf = fcoe_fcf_device_priv(fcf_dev);
3224     if (fcf)
3225         fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
3226     else
3227         fcf_dev->selected = 0;
3228 
3229     mutex_unlock(&ctlr_dev->lock);
3230     mutex_unlock(&fip->ctlr_mutex);
3231 }
3232 EXPORT_SYMBOL(fcoe_fcf_get_selected);
3233 
3234 void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
3235 {
3236     struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
3237     struct fc_lport *lport = ctlr->lp;
3238 
3239     mutex_lock(&ctlr->ctlr_mutex);
3240     switch (ctlr_dev->mode) {
3241     case FIP_CONN_TYPE_VN2VN:
3242         ctlr->mode = FIP_MODE_VN2VN;
3243         break;
3244     case FIP_CONN_TYPE_FABRIC:
3245     default:
3246         ctlr->mode = FIP_MODE_FABRIC;
3247         break;
3248     }
3249 
3250     mutex_unlock(&ctlr->ctlr_mutex);
3251 
3252     fcoe_ctlr_mode_set(lport, ctlr, ctlr->mode);
3253 }
3254 EXPORT_SYMBOL(fcoe_ctlr_set_fip_mode);