aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/fcoe
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-08-04 18:15:15 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-08-04 18:15:15 -0400
commit03da30986793385af57eeca3296253c887b742e6 (patch)
tree9c46dbe51c9d0856990649dd917ab45474b7be87 /drivers/scsi/fcoe
parent6ba74014c1ab0e37af7de6f64b4eccbbae3cb9e7 (diff)
parent339f4f4eab80caa6cf0d39fb057ad6ddb84ba91e (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
* git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6: (276 commits) [SCSI] zfcp: Trigger logging in the FCP channel on qdio error conditions [SCSI] zfcp: Introduce experimental support for DIF/DIX [SCSI] zfcp: Enable data division support for FCP devices [SCSI] zfcp: Prevent access on uninitialized memory. [SCSI] zfcp: Post events through FC transport class [SCSI] zfcp: Cleanup QDIO attachment and improve processing. [SCSI] zfcp: Cleanup function parameters for sbal value. [SCSI] zfcp: Use correct width for timer_interval field [SCSI] zfcp: Remove SCSI device when removing unit [SCSI] zfcp: Use memdup_user and kstrdup [SCSI] zfcp: Fix retry after failed "open port" erp action [SCSI] zfcp: Fail erp after timeout [SCSI] zfcp: Use forced_reopen in terminate_rport_io callback [SCSI] zfcp: Register SCSI devices after successful fc_remote_port_add [SCSI] zfcp: Do not try "forced close" when port is already closed [SCSI] zfcp: Do not unblock rport from REOPEN_PORT_FORCED [SCSI] sd: add support for runtime PM [SCSI] implement runtime Power Management [SCSI] convert to the new PM framework [SCSI] Unify SAM_ and SAM_STAT_ macros ...
Diffstat (limited to 'drivers/scsi/fcoe')
-rw-r--r--drivers/scsi/fcoe/fcoe.c147
-rw-r--r--drivers/scsi/fcoe/libfcoe.c1519
2 files changed, 1417 insertions, 249 deletions
diff --git a/drivers/scsi/fcoe/fcoe.c b/drivers/scsi/fcoe/fcoe.c
index 1a429ed6da9d..e79605a61155 100644
--- a/drivers/scsi/fcoe/fcoe.c
+++ b/drivers/scsi/fcoe/fcoe.c
@@ -117,9 +117,14 @@ static void fcoe_recv_frame(struct sk_buff *skb);
117 117
118static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *); 118static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
119 119
120module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR); 120module_param_call(create, fcoe_create, NULL, (void *)FIP_MODE_AUTO, S_IWUSR);
121__MODULE_PARM_TYPE(create, "string"); 121__MODULE_PARM_TYPE(create, "string");
122MODULE_PARM_DESC(create, " Creates fcoe instance on a ethernet interface"); 122MODULE_PARM_DESC(create, " Creates fcoe instance on a ethernet interface");
123module_param_call(create_vn2vn, fcoe_create, NULL,
124 (void *)FIP_MODE_VN2VN, S_IWUSR);
125__MODULE_PARM_TYPE(create_vn2vn, "string");
126MODULE_PARM_DESC(create_vn2vn, " Creates a VN_node to VN_node FCoE instance "
127 "on an Ethernet interface");
123module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR); 128module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
124__MODULE_PARM_TYPE(destroy, "string"); 129__MODULE_PARM_TYPE(destroy, "string");
125MODULE_PARM_DESC(destroy, " Destroys fcoe instance on a ethernet interface"); 130MODULE_PARM_DESC(destroy, " Destroys fcoe instance on a ethernet interface");
@@ -315,7 +320,11 @@ static int fcoe_interface_setup(struct fcoe_interface *fcoe,
315 dev_uc_add(netdev, flogi_maddr); 320 dev_uc_add(netdev, flogi_maddr);
316 if (fip->spma) 321 if (fip->spma)
317 dev_uc_add(netdev, fip->ctl_src_addr); 322 dev_uc_add(netdev, fip->ctl_src_addr);
318 dev_mc_add(netdev, FIP_ALL_ENODE_MACS); 323 if (fip->mode == FIP_MODE_VN2VN) {
324 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
325 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
326 } else
327 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
319 328
320 /* 329 /*
321 * setup the receive function from ethernet driver 330 * setup the receive function from ethernet driver
@@ -337,10 +346,12 @@ static int fcoe_interface_setup(struct fcoe_interface *fcoe,
337/** 346/**
338 * fcoe_interface_create() - Create a FCoE interface on a net device 347 * fcoe_interface_create() - Create a FCoE interface on a net device
339 * @netdev: The net device to create the FCoE interface on 348 * @netdev: The net device to create the FCoE interface on
349 * @fip_mode: The mode to use for FIP
340 * 350 *
341 * Returns: pointer to a struct fcoe_interface or NULL on error 351 * Returns: pointer to a struct fcoe_interface or NULL on error
342 */ 352 */
343static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev) 353static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
354 enum fip_state fip_mode)
344{ 355{
345 struct fcoe_interface *fcoe; 356 struct fcoe_interface *fcoe;
346 int err; 357 int err;
@@ -357,7 +368,7 @@ static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
357 /* 368 /*
358 * Initialize FIP. 369 * Initialize FIP.
359 */ 370 */
360 fcoe_ctlr_init(&fcoe->ctlr); 371 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
361 fcoe->ctlr.send = fcoe_fip_send; 372 fcoe->ctlr.send = fcoe_fip_send;
362 fcoe->ctlr.update_mac = fcoe_update_src_mac; 373 fcoe->ctlr.update_mac = fcoe_update_src_mac;
363 fcoe->ctlr.get_src_addr = fcoe_get_src_mac; 374 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
@@ -401,7 +412,11 @@ void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
401 dev_uc_del(netdev, flogi_maddr); 412 dev_uc_del(netdev, flogi_maddr);
402 if (fip->spma) 413 if (fip->spma)
403 dev_uc_del(netdev, fip->ctl_src_addr); 414 dev_uc_del(netdev, fip->ctl_src_addr);
404 dev_mc_del(netdev, FIP_ALL_ENODE_MACS); 415 if (fip->mode == FIP_MODE_VN2VN) {
416 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
417 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
418 } else
419 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
405 420
406 /* Tell the LLD we are done w/ FCoE */ 421 /* Tell the LLD we are done w/ FCoE */
407 ops = netdev->netdev_ops; 422 ops = netdev->netdev_ops;
@@ -574,6 +589,50 @@ static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
574} 589}
575 590
576/** 591/**
592 * fcoe_netdev_features_change - Updates the lport's offload flags based
593 * on the LLD netdev's FCoE feature flags
594 */
595static void fcoe_netdev_features_change(struct fc_lport *lport,
596 struct net_device *netdev)
597{
598 mutex_lock(&lport->lp_mutex);
599
600 if (netdev->features & NETIF_F_SG)
601 lport->sg_supp = 1;
602 else
603 lport->sg_supp = 0;
604
605 if (netdev->features & NETIF_F_FCOE_CRC) {
606 lport->crc_offload = 1;
607 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
608 } else {
609 lport->crc_offload = 0;
610 }
611
612 if (netdev->features & NETIF_F_FSO) {
613 lport->seq_offload = 1;
614 lport->lso_max = netdev->gso_max_size;
615 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
616 lport->lso_max);
617 } else {
618 lport->seq_offload = 0;
619 lport->lso_max = 0;
620 }
621
622 if (netdev->fcoe_ddp_xid) {
623 lport->lro_enabled = 1;
624 lport->lro_xid = netdev->fcoe_ddp_xid;
625 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
626 lport->lro_xid);
627 } else {
628 lport->lro_enabled = 0;
629 lport->lro_xid = 0;
630 }
631
632 mutex_unlock(&lport->lp_mutex);
633}
634
635/**
577 * fcoe_netdev_config() - Set up net devive for SW FCoE 636 * fcoe_netdev_config() - Set up net devive for SW FCoE
578 * @lport: The local port that is associated with the net device 637 * @lport: The local port that is associated with the net device
579 * @netdev: The associated net device 638 * @netdev: The associated net device
@@ -588,7 +647,6 @@ static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
588 u64 wwnn, wwpn; 647 u64 wwnn, wwpn;
589 struct fcoe_interface *fcoe; 648 struct fcoe_interface *fcoe;
590 struct fcoe_port *port; 649 struct fcoe_port *port;
591 int vid = 0;
592 650
593 /* Setup lport private data to point to fcoe softc */ 651 /* Setup lport private data to point to fcoe softc */
594 port = lport_priv(lport); 652 port = lport_priv(lport);
@@ -609,25 +667,8 @@ static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
609 return -EINVAL; 667 return -EINVAL;
610 668
611 /* offload features support */ 669 /* offload features support */
612 if (netdev->features & NETIF_F_SG) 670 fcoe_netdev_features_change(lport, netdev);
613 lport->sg_supp = 1;
614 671
615 if (netdev->features & NETIF_F_FCOE_CRC) {
616 lport->crc_offload = 1;
617 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
618 }
619 if (netdev->features & NETIF_F_FSO) {
620 lport->seq_offload = 1;
621 lport->lso_max = netdev->gso_max_size;
622 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
623 lport->lso_max);
624 }
625 if (netdev->fcoe_ddp_xid) {
626 lport->lro_enabled = 1;
627 lport->lro_xid = netdev->fcoe_ddp_xid;
628 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
629 lport->lro_xid);
630 }
631 skb_queue_head_init(&port->fcoe_pending_queue); 672 skb_queue_head_init(&port->fcoe_pending_queue);
632 port->fcoe_pending_queue_active = 0; 673 port->fcoe_pending_queue_active = 0;
633 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport); 674 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
@@ -635,20 +676,12 @@ static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
635 fcoe_link_speed_update(lport); 676 fcoe_link_speed_update(lport);
636 677
637 if (!lport->vport) { 678 if (!lport->vport) {
638 /*
639 * Use NAA 1&2 (FC-FS Rev. 2.0, Sec. 15) to generate WWNN/WWPN:
640 * For WWNN, we use NAA 1 w/ bit 27-16 of word 0 as 0.
641 * For WWPN, we use NAA 2 w/ bit 27-16 of word 0 from VLAN ID
642 */
643 if (netdev->priv_flags & IFF_802_1Q_VLAN)
644 vid = vlan_dev_vlan_id(netdev);
645
646 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN)) 679 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
647 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0); 680 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
648 fc_set_wwnn(lport, wwnn); 681 fc_set_wwnn(lport, wwnn);
649 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN)) 682 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
650 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 683 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
651 2, vid); 684 2, 0);
652 fc_set_wwpn(lport, wwpn); 685 fc_set_wwpn(lport, wwpn);
653 } 686 }
654 687
@@ -967,7 +1000,7 @@ static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
967 } 1000 }
968 1001
969 /* Initialize the library */ 1002 /* Initialize the library */
970 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ); 1003 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
971 if (rc) { 1004 if (rc) {
972 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the " 1005 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
973 "interface\n"); 1006 "interface\n");
@@ -1210,6 +1243,8 @@ int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1210 struct fcoe_interface *fcoe; 1243 struct fcoe_interface *fcoe;
1211 struct fc_frame_header *fh; 1244 struct fc_frame_header *fh;
1212 struct fcoe_percpu_s *fps; 1245 struct fcoe_percpu_s *fps;
1246 struct fcoe_port *port;
1247 struct ethhdr *eh;
1213 unsigned int cpu; 1248 unsigned int cpu;
1214 1249
1215 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type); 1250 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
@@ -1227,9 +1262,21 @@ int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
1227 skb_tail_pointer(skb), skb_end_pointer(skb), 1262 skb_tail_pointer(skb), skb_end_pointer(skb),
1228 skb->csum, skb->dev ? skb->dev->name : "<NULL>"); 1263 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
1229 1264
1230 /* check for FCOE packet type */ 1265 /* check for mac addresses */
1231 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) { 1266 eh = eth_hdr(skb);
1232 FCOE_NETDEV_DBG(netdev, "Wrong FC type frame"); 1267 port = lport_priv(lport);
1268 if (compare_ether_addr(eh->h_dest, port->data_src_addr) &&
1269 compare_ether_addr(eh->h_dest, fcoe->ctlr.ctl_src_addr) &&
1270 compare_ether_addr(eh->h_dest, (u8[6])FC_FCOE_FLOGI_MAC)) {
1271 FCOE_NETDEV_DBG(netdev, "wrong destination mac address:%pM\n",
1272 eh->h_dest);
1273 goto err;
1274 }
1275
1276 if (is_fip_mode(&fcoe->ctlr) &&
1277 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1278 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1279 eh->h_source);
1233 goto err; 1280 goto err;
1234 } 1281 }
1235 1282
@@ -1512,11 +1559,9 @@ int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
1512 /* fill up mac and fcoe headers */ 1559 /* fill up mac and fcoe headers */
1513 eh = eth_hdr(skb); 1560 eh = eth_hdr(skb);
1514 eh->h_proto = htons(ETH_P_FCOE); 1561 eh->h_proto = htons(ETH_P_FCOE);
1562 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1515 if (fcoe->ctlr.map_dest) 1563 if (fcoe->ctlr.map_dest)
1516 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id); 1564 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
1517 else
1518 /* insert GW address */
1519 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
1520 1565
1521 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN)) 1566 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1522 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN); 1567 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
@@ -1834,6 +1879,9 @@ static int fcoe_device_notification(struct notifier_block *notifier,
1834 schedule_work(&port->destroy_work); 1879 schedule_work(&port->destroy_work);
1835 goto out; 1880 goto out;
1836 break; 1881 break;
1882 case NETDEV_FEAT_CHANGE:
1883 fcoe_netdev_features_change(lport, netdev);
1884 break;
1837 default: 1885 default:
1838 FCOE_NETDEV_DBG(netdev, "Unknown event %ld " 1886 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
1839 "from netdev netlink\n", event); 1887 "from netdev netlink\n", event);
@@ -1918,8 +1966,8 @@ static int fcoe_disable(const char *buffer, struct kernel_param *kp)
1918 rtnl_unlock(); 1966 rtnl_unlock();
1919 1967
1920 if (fcoe) { 1968 if (fcoe) {
1921 fc_fabric_logoff(fcoe->ctlr.lp);
1922 fcoe_ctlr_link_down(&fcoe->ctlr); 1969 fcoe_ctlr_link_down(&fcoe->ctlr);
1970 fcoe_clean_pending_queue(fcoe->ctlr.lp);
1923 } else 1971 } else
1924 rc = -ENODEV; 1972 rc = -ENODEV;
1925 1973
@@ -1972,12 +2020,10 @@ static int fcoe_enable(const char *buffer, struct kernel_param *kp)
1972 fcoe = fcoe_hostlist_lookup_port(netdev); 2020 fcoe = fcoe_hostlist_lookup_port(netdev);
1973 rtnl_unlock(); 2021 rtnl_unlock();
1974 2022
1975 if (fcoe) { 2023 if (!fcoe)
1976 if (!fcoe_link_ok(fcoe->ctlr.lp))
1977 fcoe_ctlr_link_up(&fcoe->ctlr);
1978 rc = fc_fabric_login(fcoe->ctlr.lp);
1979 } else
1980 rc = -ENODEV; 2024 rc = -ENODEV;
2025 else if (!fcoe_link_ok(fcoe->ctlr.lp))
2026 fcoe_ctlr_link_up(&fcoe->ctlr);
1981 2027
1982 dev_put(netdev); 2028 dev_put(netdev);
1983out_nodev: 2029out_nodev:
@@ -2031,8 +2077,8 @@ static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
2031 rc = -ENODEV; 2077 rc = -ENODEV;
2032 goto out_putdev; 2078 goto out_putdev;
2033 } 2079 }
2034 list_del(&fcoe->list);
2035 fcoe_interface_cleanup(fcoe); 2080 fcoe_interface_cleanup(fcoe);
2081 list_del(&fcoe->list);
2036 /* RTNL mutex is dropped by fcoe_if_destroy */ 2082 /* RTNL mutex is dropped by fcoe_if_destroy */
2037 fcoe_if_destroy(fcoe->ctlr.lp); 2083 fcoe_if_destroy(fcoe->ctlr.lp);
2038 2084
@@ -2070,6 +2116,7 @@ static void fcoe_destroy_work(struct work_struct *work)
2070 */ 2116 */
2071static int fcoe_create(const char *buffer, struct kernel_param *kp) 2117static int fcoe_create(const char *buffer, struct kernel_param *kp)
2072{ 2118{
2119 enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
2073 int rc; 2120 int rc;
2074 struct fcoe_interface *fcoe; 2121 struct fcoe_interface *fcoe;
2075 struct fc_lport *lport; 2122 struct fc_lport *lport;
@@ -2111,7 +2158,7 @@ static int fcoe_create(const char *buffer, struct kernel_param *kp)
2111 goto out_putdev; 2158 goto out_putdev;
2112 } 2159 }
2113 2160
2114 fcoe = fcoe_interface_create(netdev); 2161 fcoe = fcoe_interface_create(netdev, fip_mode);
2115 if (!fcoe) { 2162 if (!fcoe) {
2116 rc = -ENOMEM; 2163 rc = -ENOMEM;
2117 goto out_putdev; 2164 goto out_putdev;
@@ -2521,6 +2568,8 @@ static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2521 switch (op) { 2568 switch (op) {
2522 case ELS_FLOGI: 2569 case ELS_FLOGI:
2523 case ELS_FDISC: 2570 case ELS_FDISC:
2571 if (lport->point_to_multipoint)
2572 break;
2524 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp, 2573 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2525 fip, timeout); 2574 fip, timeout);
2526 case ELS_LOGO: 2575 case ELS_LOGO:
diff --git a/drivers/scsi/fcoe/libfcoe.c b/drivers/scsi/fcoe/libfcoe.c
index 50aaa4bcfc50..aa503d83092a 100644
--- a/drivers/scsi/fcoe/libfcoe.c
+++ b/drivers/scsi/fcoe/libfcoe.c
@@ -39,6 +39,7 @@
39#include <scsi/fc/fc_fip.h> 39#include <scsi/fc/fc_fip.h>
40#include <scsi/fc/fc_encaps.h> 40#include <scsi/fc/fc_encaps.h>
41#include <scsi/fc/fc_fcoe.h> 41#include <scsi/fc/fc_fcoe.h>
42#include <scsi/fc/fc_fcp.h>
42 43
43#include <scsi/libfc.h> 44#include <scsi/libfc.h>
44#include <scsi/libfcoe.h> 45#include <scsi/libfcoe.h>
@@ -54,7 +55,15 @@ static void fcoe_ctlr_timeout(unsigned long);
54static void fcoe_ctlr_timer_work(struct work_struct *); 55static void fcoe_ctlr_timer_work(struct work_struct *);
55static void fcoe_ctlr_recv_work(struct work_struct *); 56static void fcoe_ctlr_recv_work(struct work_struct *);
56 57
58static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
59static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
60static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
61static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
62
57static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS; 63static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
64static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
65static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
66static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
58 67
59unsigned int libfcoe_debug_logging; 68unsigned int libfcoe_debug_logging;
60module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR); 69module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
@@ -80,6 +89,45 @@ do { \
80 printk(KERN_INFO "host%d: fip: " fmt, \ 89 printk(KERN_INFO "host%d: fip: " fmt, \
81 (fip)->lp->host->host_no, ##args);) 90 (fip)->lp->host->host_no, ##args);)
82 91
92static const char *fcoe_ctlr_states[] = {
93 [FIP_ST_DISABLED] = "DISABLED",
94 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
95 [FIP_ST_AUTO] = "AUTO",
96 [FIP_ST_NON_FIP] = "NON_FIP",
97 [FIP_ST_ENABLED] = "ENABLED",
98 [FIP_ST_VNMP_START] = "VNMP_START",
99 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
100 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
101 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
102 [FIP_ST_VNMP_UP] = "VNMP_UP",
103};
104
105static const char *fcoe_ctlr_state(enum fip_state state)
106{
107 const char *cp = "unknown";
108
109 if (state < ARRAY_SIZE(fcoe_ctlr_states))
110 cp = fcoe_ctlr_states[state];
111 if (!cp)
112 cp = "unknown";
113 return cp;
114}
115
116/**
117 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
118 * @fip: The FCoE controller
119 * @state: The new state
120 */
121static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
122{
123 if (state == fip->state)
124 return;
125 if (fip->lp)
126 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
127 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
128 fip->state = state;
129}
130
83/** 131/**
84 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid 132 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
85 * @fcf: The FCF to check 133 * @fcf: The FCF to check
@@ -105,15 +153,29 @@ static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
105} 153}
106 154
107/** 155/**
156 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
157 * @fip: The FCoE controller
158 */
159static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
160{
161 if (fip->mode == FIP_MODE_VN2VN)
162 hton24(fip->dest_addr, FIP_VN_FC_MAP);
163 else
164 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
165 hton24(fip->dest_addr + 3, 0);
166 fip->map_dest = 1;
167}
168
169/**
108 * fcoe_ctlr_init() - Initialize the FCoE Controller instance 170 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
109 * @fip: The FCoE controller to initialize 171 * @fip: The FCoE controller to initialize
110 */ 172 */
111void fcoe_ctlr_init(struct fcoe_ctlr *fip) 173void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
112{ 174{
113 fip->state = FIP_ST_LINK_WAIT; 175 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
114 fip->mode = FIP_ST_AUTO; 176 fip->mode = mode;
115 INIT_LIST_HEAD(&fip->fcfs); 177 INIT_LIST_HEAD(&fip->fcfs);
116 spin_lock_init(&fip->lock); 178 mutex_init(&fip->ctlr_mutex);
117 fip->flogi_oxid = FC_XID_UNKNOWN; 179 fip->flogi_oxid = FC_XID_UNKNOWN;
118 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip); 180 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
119 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work); 181 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
@@ -159,10 +221,10 @@ void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
159 cancel_work_sync(&fip->recv_work); 221 cancel_work_sync(&fip->recv_work);
160 skb_queue_purge(&fip->fip_recv_list); 222 skb_queue_purge(&fip->fip_recv_list);
161 223
162 spin_lock_bh(&fip->lock); 224 mutex_lock(&fip->ctlr_mutex);
163 fip->state = FIP_ST_DISABLED; 225 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
164 fcoe_ctlr_reset_fcfs(fip); 226 fcoe_ctlr_reset_fcfs(fip);
165 spin_unlock_bh(&fip->lock); 227 mutex_unlock(&fip->ctlr_mutex);
166 del_timer_sync(&fip->timer); 228 del_timer_sync(&fip->timer);
167 cancel_work_sync(&fip->timer_work); 229 cancel_work_sync(&fip->timer_work);
168} 230}
@@ -255,19 +317,33 @@ static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
255 */ 317 */
256void fcoe_ctlr_link_up(struct fcoe_ctlr *fip) 318void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
257{ 319{
258 spin_lock_bh(&fip->lock); 320 mutex_lock(&fip->ctlr_mutex);
259 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) { 321 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
260 spin_unlock_bh(&fip->lock); 322 mutex_unlock(&fip->ctlr_mutex);
261 fc_linkup(fip->lp); 323 fc_linkup(fip->lp);
262 } else if (fip->state == FIP_ST_LINK_WAIT) { 324 } else if (fip->state == FIP_ST_LINK_WAIT) {
263 fip->state = fip->mode; 325 fcoe_ctlr_set_state(fip, fip->mode);
264 spin_unlock_bh(&fip->lock); 326 switch (fip->mode) {
265 if (fip->state == FIP_ST_AUTO) 327 default:
328 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
329 /* fall-through */
330 case FIP_MODE_AUTO:
266 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n"); 331 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
267 fc_linkup(fip->lp); 332 /* fall-through */
268 fcoe_ctlr_solicit(fip, NULL); 333 case FIP_MODE_FABRIC:
334 case FIP_MODE_NON_FIP:
335 mutex_unlock(&fip->ctlr_mutex);
336 fc_linkup(fip->lp);
337 fcoe_ctlr_solicit(fip, NULL);
338 break;
339 case FIP_MODE_VN2VN:
340 fcoe_ctlr_vn_start(fip);
341 mutex_unlock(&fip->ctlr_mutex);
342 fc_linkup(fip->lp);
343 break;
344 }
269 } else 345 } else
270 spin_unlock_bh(&fip->lock); 346 mutex_unlock(&fip->ctlr_mutex);
271} 347}
272EXPORT_SYMBOL(fcoe_ctlr_link_up); 348EXPORT_SYMBOL(fcoe_ctlr_link_up);
273 349
@@ -283,7 +359,7 @@ static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
283 fip->port_ka_time = 0; 359 fip->port_ka_time = 0;
284 fip->sol_time = 0; 360 fip->sol_time = 0;
285 fip->flogi_oxid = FC_XID_UNKNOWN; 361 fip->flogi_oxid = FC_XID_UNKNOWN;
286 fip->map_dest = 0; 362 fcoe_ctlr_map_dest(fip);
287} 363}
288 364
289/** 365/**
@@ -300,11 +376,11 @@ int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
300 int link_dropped; 376 int link_dropped;
301 377
302 LIBFCOE_FIP_DBG(fip, "link down.\n"); 378 LIBFCOE_FIP_DBG(fip, "link down.\n");
303 spin_lock_bh(&fip->lock); 379 mutex_lock(&fip->ctlr_mutex);
304 fcoe_ctlr_reset(fip); 380 fcoe_ctlr_reset(fip);
305 link_dropped = fip->state != FIP_ST_LINK_WAIT; 381 link_dropped = fip->state != FIP_ST_LINK_WAIT;
306 fip->state = FIP_ST_LINK_WAIT; 382 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
307 spin_unlock_bh(&fip->lock); 383 mutex_unlock(&fip->ctlr_mutex);
308 384
309 if (link_dropped) 385 if (link_dropped)
310 fc_linkdown(fip->lp); 386 fc_linkdown(fip->lp);
@@ -343,7 +419,7 @@ static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
343 419
344 fcf = fip->sel_fcf; 420 fcf = fip->sel_fcf;
345 lp = fip->lp; 421 lp = fip->lp;
346 if (!fcf || !lp->port_id) 422 if (!fcf || (ports && !lp->port_id))
347 return; 423 return;
348 424
349 len = sizeof(*kal) + ports * sizeof(*vn); 425 len = sizeof(*kal) + ports * sizeof(*vn);
@@ -389,6 +465,7 @@ static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
389 * @fip: The FCoE controller for the ELS frame 465 * @fip: The FCoE controller for the ELS frame
390 * @dtype: The FIP descriptor type for the frame 466 * @dtype: The FIP descriptor type for the frame
391 * @skb: The FCoE ELS frame including FC header but no FCoE headers 467 * @skb: The FCoE ELS frame including FC header but no FCoE headers
468 * @d_id: The destination port ID.
392 * 469 *
393 * Returns non-zero error code on failure. 470 * Returns non-zero error code on failure.
394 * 471 *
@@ -399,58 +476,75 @@ static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
399 * Ethernet header. The tailroom is for the FIP MAC descriptor. 476 * Ethernet header. The tailroom is for the FIP MAC descriptor.
400 */ 477 */
401static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport, 478static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
402 u8 dtype, struct sk_buff *skb) 479 u8 dtype, struct sk_buff *skb, u32 d_id)
403{ 480{
404 struct fip_encaps_head { 481 struct fip_encaps_head {
405 struct ethhdr eth; 482 struct ethhdr eth;
406 struct fip_header fip; 483 struct fip_header fip;
407 struct fip_encaps encaps; 484 struct fip_encaps encaps;
408 } __attribute__((packed)) *cap; 485 } __attribute__((packed)) *cap;
486 struct fc_frame_header *fh;
409 struct fip_mac_desc *mac; 487 struct fip_mac_desc *mac;
410 struct fcoe_fcf *fcf; 488 struct fcoe_fcf *fcf;
411 size_t dlen; 489 size_t dlen;
412 u16 fip_flags; 490 u16 fip_flags;
491 u8 op;
413 492
414 fcf = fip->sel_fcf; 493 fh = (struct fc_frame_header *)skb->data;
415 if (!fcf) 494 op = *(u8 *)(fh + 1);
416 return -ENODEV;
417
418 /* set flags according to both FCF and lport's capability on SPMA */
419 fip_flags = fcf->flags;
420 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA : FIP_FL_FPMA;
421 if (!fip_flags)
422 return -ENODEV;
423
424 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */ 495 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
425 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap)); 496 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
426
427 memset(cap, 0, sizeof(*cap)); 497 memset(cap, 0, sizeof(*cap));
428 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN); 498
499 if (lport->point_to_multipoint) {
500 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
501 return -ENODEV;
502 fip_flags = 0;
503 } else {
504 fcf = fip->sel_fcf;
505 if (!fcf)
506 return -ENODEV;
507 fip_flags = fcf->flags;
508 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
509 FIP_FL_FPMA;
510 if (!fip_flags)
511 return -ENODEV;
512 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
513 }
429 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN); 514 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
430 cap->eth.h_proto = htons(ETH_P_FIP); 515 cap->eth.h_proto = htons(ETH_P_FIP);
431 516
432 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER); 517 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
433 cap->fip.fip_op = htons(FIP_OP_LS); 518 cap->fip.fip_op = htons(FIP_OP_LS);
434 cap->fip.fip_subcode = FIP_SC_REQ; 519 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
435 cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW); 520 cap->fip.fip_subcode = FIP_SC_REP;
521 else
522 cap->fip.fip_subcode = FIP_SC_REQ;
436 cap->fip.fip_flags = htons(fip_flags); 523 cap->fip.fip_flags = htons(fip_flags);
437 524
438 cap->encaps.fd_desc.fip_dtype = dtype; 525 cap->encaps.fd_desc.fip_dtype = dtype;
439 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW; 526 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
440 527
441 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac)); 528 if (op != ELS_LS_RJT) {
442 memset(mac, 0, sizeof(*mac)); 529 dlen += sizeof(*mac);
443 mac->fd_desc.fip_dtype = FIP_DT_MAC; 530 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
444 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW; 531 memset(mac, 0, sizeof(*mac));
445 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) { 532 mac->fd_desc.fip_dtype = FIP_DT_MAC;
446 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN); 533 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
447 } else if (fip_flags & FIP_FL_SPMA) { 534 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
448 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n"); 535 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
449 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN); 536 } else if (fip->mode == FIP_MODE_VN2VN) {
450 } else { 537 hton24(mac->fd_mac, FIP_VN_FC_MAP);
451 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n"); 538 hton24(mac->fd_mac + 3, fip->port_id);
452 /* FPMA only FLOGI must leave the MAC desc set to all 0s */ 539 } else if (fip_flags & FIP_FL_SPMA) {
540 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
541 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
542 } else {
543 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
544 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
545 }
453 } 546 }
547 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
454 548
455 skb->protocol = htons(ETH_P_FIP); 549 skb->protocol = htons(ETH_P_FIP);
456 skb_reset_mac_header(skb); 550 skb_reset_mac_header(skb);
@@ -469,19 +563,22 @@ static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
469 * 563 *
470 * The caller must check that the length is a multiple of 4. 564 * The caller must check that the length is a multiple of 4.
471 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes). 565 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
566 * The the skb must also be an fc_frame.
472 */ 567 */
473int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport, 568int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
474 struct sk_buff *skb) 569 struct sk_buff *skb)
475{ 570{
571 struct fc_frame *fp;
476 struct fc_frame_header *fh; 572 struct fc_frame_header *fh;
477 u16 old_xid; 573 u16 old_xid;
478 u8 op; 574 u8 op;
479 u8 mac[ETH_ALEN]; 575 u8 mac[ETH_ALEN];
480 576
577 fp = container_of(skb, struct fc_frame, skb);
481 fh = (struct fc_frame_header *)skb->data; 578 fh = (struct fc_frame_header *)skb->data;
482 op = *(u8 *)(fh + 1); 579 op = *(u8 *)(fh + 1);
483 580
484 if (op == ELS_FLOGI) { 581 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
485 old_xid = fip->flogi_oxid; 582 old_xid = fip->flogi_oxid;
486 fip->flogi_oxid = ntohs(fh->fh_ox_id); 583 fip->flogi_oxid = ntohs(fh->fh_ox_id);
487 if (fip->state == FIP_ST_AUTO) { 584 if (fip->state == FIP_ST_AUTO) {
@@ -490,18 +587,17 @@ int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
490 fip->flogi_count++; 587 fip->flogi_count++;
491 if (fip->flogi_count < 3) 588 if (fip->flogi_count < 3)
492 goto drop; 589 goto drop;
493 fip->map_dest = 1; 590 fcoe_ctlr_map_dest(fip);
494 return 0; 591 return 0;
495 } 592 }
496 if (fip->state == FIP_ST_NON_FIP) 593 if (fip->state == FIP_ST_NON_FIP)
497 fip->map_dest = 1; 594 fcoe_ctlr_map_dest(fip);
498 } 595 }
499 596
500 if (fip->state == FIP_ST_NON_FIP) 597 if (fip->state == FIP_ST_NON_FIP)
501 return 0; 598 return 0;
502 if (!fip->sel_fcf) 599 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
503 goto drop; 600 goto drop;
504
505 switch (op) { 601 switch (op) {
506 case ELS_FLOGI: 602 case ELS_FLOGI:
507 op = FIP_DT_FLOGI; 603 op = FIP_DT_FLOGI;
@@ -512,36 +608,49 @@ int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
512 op = FIP_DT_FDISC; 608 op = FIP_DT_FDISC;
513 break; 609 break;
514 case ELS_LOGO: 610 case ELS_LOGO:
515 if (fip->state != FIP_ST_ENABLED) 611 if (fip->mode == FIP_MODE_VN2VN) {
516 return 0; 612 if (fip->state != FIP_ST_VNMP_UP)
517 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI) 613 return -EINVAL;
518 return 0; 614 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
615 return -EINVAL;
616 } else {
617 if (fip->state != FIP_ST_ENABLED)
618 return 0;
619 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
620 return 0;
621 }
519 op = FIP_DT_LOGO; 622 op = FIP_DT_LOGO;
520 break; 623 break;
521 case ELS_LS_ACC: 624 case ELS_LS_ACC:
522 if (fip->flogi_oxid == FC_XID_UNKNOWN)
523 return 0;
524 if (!ntoh24(fh->fh_s_id))
525 return 0;
526 if (fip->state == FIP_ST_AUTO)
527 return 0;
528 /* 625 /*
529 * Here we must've gotten an SID by accepting an FLOGI 626 * If non-FIP, we may have gotten an SID by accepting an FLOGI
530 * from a point-to-point connection. Switch to using 627 * from a point-to-point connection. Switch to using
531 * the source mac based on the SID. The destination 628 * the source mac based on the SID. The destination
532 * MAC in this case would have been set by receving the 629 * MAC in this case would have been set by receving the
533 * FLOGI. 630 * FLOGI.
534 */ 631 */
535 fip->flogi_oxid = FC_XID_UNKNOWN; 632 if (fip->state == FIP_ST_NON_FIP) {
536 fc_fcoe_set_mac(mac, fh->fh_d_id); 633 if (fip->flogi_oxid == FC_XID_UNKNOWN)
537 fip->update_mac(lport, mac); 634 return 0;
635 fip->flogi_oxid = FC_XID_UNKNOWN;
636 fc_fcoe_set_mac(mac, fh->fh_d_id);
637 fip->update_mac(lport, mac);
638 }
639 /* fall through */
640 case ELS_LS_RJT:
641 op = fr_encaps(fp);
642 if (op)
643 break;
538 return 0; 644 return 0;
539 default: 645 default:
540 if (fip->state != FIP_ST_ENABLED) 646 if (fip->state != FIP_ST_ENABLED &&
647 fip->state != FIP_ST_VNMP_UP)
541 goto drop; 648 goto drop;
542 return 0; 649 return 0;
543 } 650 }
544 if (fcoe_ctlr_encaps(fip, lport, op, skb)) 651 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
652 op, ntoh24(fh->fh_d_id));
653 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
545 goto drop; 654 goto drop;
546 fip->send(fip, skb); 655 fip->send(fip, skb);
547 return -EINPROGRESS; 656 return -EINPROGRESS;
@@ -557,60 +666,66 @@ EXPORT_SYMBOL(fcoe_ctlr_els_send);
557 * 666 *
558 * Called with lock held and preemption disabled. 667 * Called with lock held and preemption disabled.
559 * 668 *
560 * An FCF is considered old if we have missed three advertisements. 669 * An FCF is considered old if we have missed two advertisements.
561 * That is, there have been no valid advertisement from it for three 670 * That is, there have been no valid advertisement from it for 2.5
562 * times its keep-alive period including fuzz. 671 * times its keep-alive period.
563 * 672 *
564 * In addition, determine the time when an FCF selection can occur. 673 * In addition, determine the time when an FCF selection can occur.
565 * 674 *
566 * Also, increment the MissDiscAdvCount when no advertisement is received 675 * Also, increment the MissDiscAdvCount when no advertisement is received
567 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB). 676 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
677 *
678 * Returns the time in jiffies for the next call.
568 */ 679 */
569static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip) 680static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
570{ 681{
571 struct fcoe_fcf *fcf; 682 struct fcoe_fcf *fcf;
572 struct fcoe_fcf *next; 683 struct fcoe_fcf *next;
684 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
685 unsigned long deadline;
573 unsigned long sel_time = 0; 686 unsigned long sel_time = 0;
574 unsigned long mda_time = 0;
575 struct fcoe_dev_stats *stats; 687 struct fcoe_dev_stats *stats;
576 688
689 stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
690
577 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) { 691 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
578 mda_time = fcf->fka_period + (fcf->fka_period >> 1); 692 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
579 if ((fip->sel_fcf == fcf) && 693 if (fip->sel_fcf == fcf) {
580 (time_after(jiffies, fcf->time + mda_time))) { 694 if (time_after(jiffies, deadline)) {
581 mod_timer(&fip->timer, jiffies + mda_time); 695 stats->MissDiscAdvCount++;
582 stats = per_cpu_ptr(fip->lp->dev_stats, 696 printk(KERN_INFO "libfcoe: host%d: "
583 smp_processor_id()); 697 "Missing Discovery Advertisement "
584 stats->MissDiscAdvCount++; 698 "for fab %16.16llx count %lld\n",
585 printk(KERN_INFO "libfcoe: host%d: Missing Discovery " 699 fip->lp->host->host_no, fcf->fabric_name,
586 "Advertisement for fab %16.16llx count %lld\n", 700 stats->MissDiscAdvCount);
587 fip->lp->host->host_no, fcf->fabric_name, 701 } else if (time_after(next_timer, deadline))
588 stats->MissDiscAdvCount); 702 next_timer = deadline;
589 } 703 }
590 if (time_after(jiffies, fcf->time + fcf->fka_period * 3 + 704
591 msecs_to_jiffies(FIP_FCF_FUZZ * 3))) { 705 deadline += fcf->fka_period;
706 if (time_after_eq(jiffies, deadline)) {
592 if (fip->sel_fcf == fcf) 707 if (fip->sel_fcf == fcf)
593 fip->sel_fcf = NULL; 708 fip->sel_fcf = NULL;
594 list_del(&fcf->list); 709 list_del(&fcf->list);
595 WARN_ON(!fip->fcf_count); 710 WARN_ON(!fip->fcf_count);
596 fip->fcf_count--; 711 fip->fcf_count--;
597 kfree(fcf); 712 kfree(fcf);
598 stats = per_cpu_ptr(fip->lp->dev_stats,
599 smp_processor_id());
600 stats->VLinkFailureCount++; 713 stats->VLinkFailureCount++;
601 } else if (fcoe_ctlr_mtu_valid(fcf) && 714 } else {
602 (!sel_time || time_before(sel_time, fcf->time))) { 715 if (time_after(next_timer, deadline))
603 sel_time = fcf->time; 716 next_timer = deadline;
717 if (fcoe_ctlr_mtu_valid(fcf) &&
718 (!sel_time || time_before(sel_time, fcf->time)))
719 sel_time = fcf->time;
604 } 720 }
605 } 721 }
606 if (sel_time) { 722 put_cpu();
723 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
607 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY); 724 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
608 fip->sel_time = sel_time; 725 fip->sel_time = sel_time;
609 if (time_before(sel_time, fip->timer.expires))
610 mod_timer(&fip->timer, sel_time);
611 } else {
612 fip->sel_time = 0;
613 } 726 }
727
728 return next_timer;
614} 729}
615 730
616/** 731/**
@@ -633,6 +748,7 @@ static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
633 unsigned long t; 748 unsigned long t;
634 size_t rlen; 749 size_t rlen;
635 size_t dlen; 750 size_t dlen;
751 u32 desc_mask;
636 752
637 memset(fcf, 0, sizeof(*fcf)); 753 memset(fcf, 0, sizeof(*fcf));
638 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA); 754 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
@@ -640,6 +756,12 @@ static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
640 fiph = (struct fip_header *)skb->data; 756 fiph = (struct fip_header *)skb->data;
641 fcf->flags = ntohs(fiph->fip_flags); 757 fcf->flags = ntohs(fiph->fip_flags);
642 758
759 /*
760 * mask of required descriptors. validating each one clears its bit.
761 */
762 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
763 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
764
643 rlen = ntohs(fiph->fip_dl_len) * 4; 765 rlen = ntohs(fiph->fip_dl_len) * 4;
644 if (rlen + sizeof(*fiph) > skb->len) 766 if (rlen + sizeof(*fiph) > skb->len)
645 return -EINVAL; 767 return -EINVAL;
@@ -649,11 +771,19 @@ static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
649 dlen = desc->fip_dlen * FIP_BPW; 771 dlen = desc->fip_dlen * FIP_BPW;
650 if (dlen < sizeof(*desc) || dlen > rlen) 772 if (dlen < sizeof(*desc) || dlen > rlen)
651 return -EINVAL; 773 return -EINVAL;
774 /* Drop Adv if there are duplicate critical descriptors */
775 if ((desc->fip_dtype < 32) &&
776 !(desc_mask & 1U << desc->fip_dtype)) {
777 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
778 "Descriptors in FIP adv\n");
779 return -EINVAL;
780 }
652 switch (desc->fip_dtype) { 781 switch (desc->fip_dtype) {
653 case FIP_DT_PRI: 782 case FIP_DT_PRI:
654 if (dlen != sizeof(struct fip_pri_desc)) 783 if (dlen != sizeof(struct fip_pri_desc))
655 goto len_err; 784 goto len_err;
656 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri; 785 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
786 desc_mask &= ~BIT(FIP_DT_PRI);
657 break; 787 break;
658 case FIP_DT_MAC: 788 case FIP_DT_MAC:
659 if (dlen != sizeof(struct fip_mac_desc)) 789 if (dlen != sizeof(struct fip_mac_desc))
@@ -662,16 +792,19 @@ static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
662 ((struct fip_mac_desc *)desc)->fd_mac, 792 ((struct fip_mac_desc *)desc)->fd_mac,
663 ETH_ALEN); 793 ETH_ALEN);
664 if (!is_valid_ether_addr(fcf->fcf_mac)) { 794 if (!is_valid_ether_addr(fcf->fcf_mac)) {
665 LIBFCOE_FIP_DBG(fip, "Invalid MAC address " 795 LIBFCOE_FIP_DBG(fip,
666 "in FIP adv\n"); 796 "Invalid MAC addr %pM in FIP adv\n",
797 fcf->fcf_mac);
667 return -EINVAL; 798 return -EINVAL;
668 } 799 }
800 desc_mask &= ~BIT(FIP_DT_MAC);
669 break; 801 break;
670 case FIP_DT_NAME: 802 case FIP_DT_NAME:
671 if (dlen != sizeof(struct fip_wwn_desc)) 803 if (dlen != sizeof(struct fip_wwn_desc))
672 goto len_err; 804 goto len_err;
673 wwn = (struct fip_wwn_desc *)desc; 805 wwn = (struct fip_wwn_desc *)desc;
674 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn); 806 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
807 desc_mask &= ~BIT(FIP_DT_NAME);
675 break; 808 break;
676 case FIP_DT_FAB: 809 case FIP_DT_FAB:
677 if (dlen != sizeof(struct fip_fab_desc)) 810 if (dlen != sizeof(struct fip_fab_desc))
@@ -680,6 +813,7 @@ static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
680 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn); 813 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
681 fcf->vfid = ntohs(fab->fd_vfid); 814 fcf->vfid = ntohs(fab->fd_vfid);
682 fcf->fc_map = ntoh24(fab->fd_map); 815 fcf->fc_map = ntoh24(fab->fd_map);
816 desc_mask &= ~BIT(FIP_DT_FAB);
683 break; 817 break;
684 case FIP_DT_FKA: 818 case FIP_DT_FKA:
685 if (dlen != sizeof(struct fip_fka_desc)) 819 if (dlen != sizeof(struct fip_fka_desc))
@@ -690,6 +824,7 @@ static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
690 t = ntohl(fka->fd_fka_period); 824 t = ntohl(fka->fd_fka_period);
691 if (t >= FCOE_CTLR_MIN_FKA) 825 if (t >= FCOE_CTLR_MIN_FKA)
692 fcf->fka_period = msecs_to_jiffies(t); 826 fcf->fka_period = msecs_to_jiffies(t);
827 desc_mask &= ~BIT(FIP_DT_FKA);
693 break; 828 break;
694 case FIP_DT_MAP_OUI: 829 case FIP_DT_MAP_OUI:
695 case FIP_DT_FCOE_SIZE: 830 case FIP_DT_FCOE_SIZE:
@@ -703,15 +838,20 @@ static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
703 /* standard says ignore unknown descriptors >= 128 */ 838 /* standard says ignore unknown descriptors >= 128 */
704 if (desc->fip_dtype < FIP_DT_VENDOR_BASE) 839 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
705 return -EINVAL; 840 return -EINVAL;
706 continue; 841 break;
707 } 842 }
708 desc = (struct fip_desc *)((char *)desc + dlen); 843 desc = (struct fip_desc *)((char *)desc + dlen);
709 rlen -= dlen; 844 rlen -= dlen;
710 } 845 }
711 if (!fcf->fc_map || (fcf->fc_map & 0x10000)) 846 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
712 return -EINVAL; 847 return -EINVAL;
713 if (!fcf->switch_name || !fcf->fabric_name) 848 if (!fcf->switch_name)
849 return -EINVAL;
850 if (desc_mask) {
851 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
852 desc_mask);
714 return -EINVAL; 853 return -EINVAL;
854 }
715 return 0; 855 return 0;
716 856
717len_err: 857len_err:
@@ -737,7 +877,7 @@ static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
737 if (fcoe_ctlr_parse_adv(fip, skb, &new)) 877 if (fcoe_ctlr_parse_adv(fip, skb, &new))
738 return; 878 return;
739 879
740 spin_lock_bh(&fip->lock); 880 mutex_lock(&fip->ctlr_mutex);
741 first = list_empty(&fip->fcfs); 881 first = list_empty(&fip->fcfs);
742 found = NULL; 882 found = NULL;
743 list_for_each_entry(fcf, &fip->fcfs, list) { 883 list_for_each_entry(fcf, &fip->fcfs, list) {
@@ -762,18 +902,21 @@ static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
762 list_add(&fcf->list, &fip->fcfs); 902 list_add(&fcf->list, &fip->fcfs);
763 } else { 903 } else {
764 /* 904 /*
765 * Flags in advertisements are ignored once the FCF is 905 * Update the FCF's keep-alive descriptor flags.
766 * selected. Flags in unsolicited advertisements are 906 * Other flag changes from new advertisements are
767 * ignored after a usable solicited advertisement 907 * ignored after a solicited advertisement is
768 * has been received. 908 * received and the FCF is selectable (usable).
769 */ 909 */
770 if (fcf == fip->sel_fcf) { 910 fcf->fd_flags = new.fd_flags;
911 if (!fcoe_ctlr_fcf_usable(fcf))
912 fcf->flags = new.flags;
913
914 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
771 fip->ctlr_ka_time -= fcf->fka_period; 915 fip->ctlr_ka_time -= fcf->fka_period;
772 fip->ctlr_ka_time += new.fka_period; 916 fip->ctlr_ka_time += new.fka_period;
773 if (time_before(fip->ctlr_ka_time, fip->timer.expires)) 917 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
774 mod_timer(&fip->timer, fip->ctlr_ka_time); 918 mod_timer(&fip->timer, fip->ctlr_ka_time);
775 } else if (!fcoe_ctlr_fcf_usable(fcf)) 919 }
776 fcf->flags = new.flags;
777 fcf->fka_period = new.fka_period; 920 fcf->fka_period = new.fka_period;
778 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN); 921 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
779 } 922 }
@@ -805,7 +948,7 @@ static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
805 * If this is the first validated FCF, note the time and 948 * If this is the first validated FCF, note the time and
806 * set a timer to trigger selection. 949 * set a timer to trigger selection.
807 */ 950 */
808 if (mtu_valid && !fip->sel_time && fcoe_ctlr_fcf_usable(fcf)) { 951 if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
809 fip->sel_time = jiffies + 952 fip->sel_time = jiffies +
810 msecs_to_jiffies(FCOE_CTLR_START_DELAY); 953 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
811 if (!timer_pending(&fip->timer) || 954 if (!timer_pending(&fip->timer) ||
@@ -813,7 +956,7 @@ static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
813 mod_timer(&fip->timer, fip->sel_time); 956 mod_timer(&fip->timer, fip->sel_time);
814 } 957 }
815out: 958out:
816 spin_unlock_bh(&fip->lock); 959 mutex_unlock(&fip->ctlr_mutex);
817} 960}
818 961
819/** 962/**
@@ -837,6 +980,8 @@ static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
837 size_t els_len = 0; 980 size_t els_len = 0;
838 size_t rlen; 981 size_t rlen;
839 size_t dlen; 982 size_t dlen;
983 u32 desc_mask = 0;
984 u32 desc_cnt = 0;
840 985
841 fiph = (struct fip_header *)skb->data; 986 fiph = (struct fip_header *)skb->data;
842 sub = fiph->fip_subcode; 987 sub = fiph->fip_subcode;
@@ -849,27 +994,42 @@ static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
849 994
850 desc = (struct fip_desc *)(fiph + 1); 995 desc = (struct fip_desc *)(fiph + 1);
851 while (rlen > 0) { 996 while (rlen > 0) {
997 desc_cnt++;
852 dlen = desc->fip_dlen * FIP_BPW; 998 dlen = desc->fip_dlen * FIP_BPW;
853 if (dlen < sizeof(*desc) || dlen > rlen) 999 if (dlen < sizeof(*desc) || dlen > rlen)
854 goto drop; 1000 goto drop;
1001 /* Drop ELS if there are duplicate critical descriptors */
1002 if (desc->fip_dtype < 32) {
1003 if (desc_mask & 1U << desc->fip_dtype) {
1004 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1005 "Descriptors in FIP ELS\n");
1006 goto drop;
1007 }
1008 desc_mask |= (1 << desc->fip_dtype);
1009 }
855 switch (desc->fip_dtype) { 1010 switch (desc->fip_dtype) {
856 case FIP_DT_MAC: 1011 case FIP_DT_MAC:
1012 if (desc_cnt == 1) {
1013 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1014 "received out of order\n");
1015 goto drop;
1016 }
1017
857 if (dlen != sizeof(struct fip_mac_desc)) 1018 if (dlen != sizeof(struct fip_mac_desc))
858 goto len_err; 1019 goto len_err;
859 memcpy(granted_mac, 1020 memcpy(granted_mac,
860 ((struct fip_mac_desc *)desc)->fd_mac, 1021 ((struct fip_mac_desc *)desc)->fd_mac,
861 ETH_ALEN); 1022 ETH_ALEN);
862 if (!is_valid_ether_addr(granted_mac)) {
863 LIBFCOE_FIP_DBG(fip, "Invalid MAC address "
864 "in FIP ELS\n");
865 goto drop;
866 }
867 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
868 break; 1023 break;
869 case FIP_DT_FLOGI: 1024 case FIP_DT_FLOGI:
870 case FIP_DT_FDISC: 1025 case FIP_DT_FDISC:
871 case FIP_DT_LOGO: 1026 case FIP_DT_LOGO:
872 case FIP_DT_ELP: 1027 case FIP_DT_ELP:
1028 if (desc_cnt != 1) {
1029 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1030 "received out of order\n");
1031 goto drop;
1032 }
873 if (fh) 1033 if (fh)
874 goto drop; 1034 goto drop;
875 if (dlen < sizeof(*els) + sizeof(*fh) + 1) 1035 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
@@ -885,7 +1045,12 @@ static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
885 /* standard says ignore unknown descriptors >= 128 */ 1045 /* standard says ignore unknown descriptors >= 128 */
886 if (desc->fip_dtype < FIP_DT_VENDOR_BASE) 1046 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
887 goto drop; 1047 goto drop;
888 continue; 1048 if (desc_cnt <= 2) {
1049 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1050 "received out of order\n");
1051 goto drop;
1052 }
1053 break;
889 } 1054 }
890 desc = (struct fip_desc *)((char *)desc + dlen); 1055 desc = (struct fip_desc *)((char *)desc + dlen);
891 rlen -= dlen; 1056 rlen -= dlen;
@@ -895,10 +1060,27 @@ static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
895 goto drop; 1060 goto drop;
896 els_op = *(u8 *)(fh + 1); 1061 els_op = *(u8 *)(fh + 1);
897 1062
898 if (els_dtype == FIP_DT_FLOGI && sub == FIP_SC_REP && 1063 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
899 fip->flogi_oxid == ntohs(fh->fh_ox_id) && 1064 sub == FIP_SC_REP && els_op == ELS_LS_ACC &&
900 els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac)) 1065 fip->mode != FIP_MODE_VN2VN) {
901 fip->flogi_oxid = FC_XID_UNKNOWN; 1066 if (!is_valid_ether_addr(granted_mac)) {
1067 LIBFCOE_FIP_DBG(fip,
1068 "Invalid MAC address %pM in FIP ELS\n",
1069 granted_mac);
1070 goto drop;
1071 }
1072 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1073
1074 if (fip->flogi_oxid == ntohs(fh->fh_ox_id))
1075 fip->flogi_oxid = FC_XID_UNKNOWN;
1076 }
1077
1078 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1079 (!(1U << FIP_DT_MAC & desc_mask)))) {
1080 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1081 "in FIP ELS\n");
1082 goto drop;
1083 }
902 1084
903 /* 1085 /*
904 * Convert skb into an fc_frame containing only the ELS. 1086 * Convert skb into an fc_frame containing only the ELS.
@@ -910,6 +1092,7 @@ static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
910 fr_sof(fp) = FC_SOF_I3; 1092 fr_sof(fp) = FC_SOF_I3;
911 fr_eof(fp) = FC_EOF_T; 1093 fr_eof(fp) = FC_EOF_T;
912 fr_dev(fp) = lport; 1094 fr_dev(fp) = lport;
1095 fr_encaps(fp) = els_dtype;
913 1096
914 stats = per_cpu_ptr(lport->dev_stats, get_cpu()); 1097 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
915 stats->RxFrames++; 1098 stats->RxFrames++;
@@ -945,7 +1128,9 @@ static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
945 size_t dlen; 1128 size_t dlen;
946 struct fcoe_fcf *fcf = fip->sel_fcf; 1129 struct fcoe_fcf *fcf = fip->sel_fcf;
947 struct fc_lport *lport = fip->lp; 1130 struct fc_lport *lport = fip->lp;
948 u32 desc_mask; 1131 struct fc_lport *vn_port = NULL;
1132 u32 desc_mask;
1133 int is_vn_port = 0;
949 1134
950 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n"); 1135 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
951 1136
@@ -963,6 +1148,13 @@ static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
963 dlen = desc->fip_dlen * FIP_BPW; 1148 dlen = desc->fip_dlen * FIP_BPW;
964 if (dlen > rlen) 1149 if (dlen > rlen)
965 return; 1150 return;
1151 /* Drop CVL if there are duplicate critical descriptors */
1152 if ((desc->fip_dtype < 32) &&
1153 !(desc_mask & 1U << desc->fip_dtype)) {
1154 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1155 "Descriptors in FIP CVL\n");
1156 return;
1157 }
966 switch (desc->fip_dtype) { 1158 switch (desc->fip_dtype) {
967 case FIP_DT_MAC: 1159 case FIP_DT_MAC:
968 mp = (struct fip_mac_desc *)desc; 1160 mp = (struct fip_mac_desc *)desc;
@@ -987,8 +1179,26 @@ static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
987 if (compare_ether_addr(vp->fd_mac, 1179 if (compare_ether_addr(vp->fd_mac,
988 fip->get_src_addr(lport)) == 0 && 1180 fip->get_src_addr(lport)) == 0 &&
989 get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn && 1181 get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
990 ntoh24(vp->fd_fc_id) == lport->port_id) 1182 ntoh24(vp->fd_fc_id) == lport->port_id) {
991 desc_mask &= ~BIT(FIP_DT_VN_ID); 1183 desc_mask &= ~BIT(FIP_DT_VN_ID);
1184 break;
1185 }
1186 /* check if clr_vlink is for NPIV port */
1187 mutex_lock(&lport->lp_mutex);
1188 list_for_each_entry(vn_port, &lport->vports, list) {
1189 if (compare_ether_addr(vp->fd_mac,
1190 fip->get_src_addr(vn_port)) == 0 &&
1191 (get_unaligned_be64(&vp->fd_wwpn)
1192 == vn_port->wwpn) &&
1193 (ntoh24(vp->fd_fc_id) ==
1194 fc_host_port_id(vn_port->host))) {
1195 desc_mask &= ~BIT(FIP_DT_VN_ID);
1196 is_vn_port = 1;
1197 break;
1198 }
1199 }
1200 mutex_unlock(&lport->lp_mutex);
1201
992 break; 1202 break;
993 default: 1203 default:
994 /* standard says ignore unknown descriptors >= 128 */ 1204 /* standard says ignore unknown descriptors >= 128 */
@@ -1009,14 +1219,19 @@ static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1009 } else { 1219 } else {
1010 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n"); 1220 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1011 1221
1012 spin_lock_bh(&fip->lock); 1222 if (is_vn_port)
1013 per_cpu_ptr(lport->dev_stats, 1223 fc_lport_reset(vn_port);
1014 smp_processor_id())->VLinkFailureCount++; 1224 else {
1015 fcoe_ctlr_reset(fip); 1225 mutex_lock(&fip->ctlr_mutex);
1016 spin_unlock_bh(&fip->lock); 1226 per_cpu_ptr(lport->dev_stats,
1017 1227 get_cpu())->VLinkFailureCount++;
1018 fc_lport_reset(fip->lp); 1228 put_cpu();
1019 fcoe_ctlr_solicit(fip, NULL); 1229 fcoe_ctlr_reset(fip);
1230 mutex_unlock(&fip->ctlr_mutex);
1231
1232 fc_lport_reset(fip->lp);
1233 fcoe_ctlr_solicit(fip, NULL);
1234 }
1020 } 1235 }
1021} 1236}
1022 1237
@@ -1054,8 +1269,13 @@ static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1054 if (skb->len < sizeof(*fiph)) 1269 if (skb->len < sizeof(*fiph))
1055 goto drop; 1270 goto drop;
1056 eh = eth_hdr(skb); 1271 eh = eth_hdr(skb);
1057 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) && 1272 if (fip->mode == FIP_MODE_VN2VN) {
1058 compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS)) 1273 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1274 compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1275 compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1276 goto drop;
1277 } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1278 compare_ether_addr(eh->h_dest, fcoe_all_enode))
1059 goto drop; 1279 goto drop;
1060 fiph = (struct fip_header *)skb->data; 1280 fiph = (struct fip_header *)skb->data;
1061 op = ntohs(fiph->fip_op); 1281 op = ntohs(fiph->fip_op);
@@ -1066,22 +1286,31 @@ static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1066 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len) 1286 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1067 goto drop; 1287 goto drop;
1068 1288
1069 spin_lock_bh(&fip->lock); 1289 mutex_lock(&fip->ctlr_mutex);
1070 state = fip->state; 1290 state = fip->state;
1071 if (state == FIP_ST_AUTO) { 1291 if (state == FIP_ST_AUTO) {
1072 fip->map_dest = 0; 1292 fip->map_dest = 0;
1073 fip->state = FIP_ST_ENABLED; 1293 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1074 state = FIP_ST_ENABLED; 1294 state = FIP_ST_ENABLED;
1075 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n"); 1295 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1076 } 1296 }
1077 spin_unlock_bh(&fip->lock); 1297 mutex_unlock(&fip->ctlr_mutex);
1078 if (state != FIP_ST_ENABLED) 1298
1299 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1300 return fcoe_ctlr_vn_recv(fip, skb);
1301
1302 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1303 state != FIP_ST_VNMP_CLAIM)
1079 goto drop; 1304 goto drop;
1080 1305
1081 if (op == FIP_OP_LS) { 1306 if (op == FIP_OP_LS) {
1082 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */ 1307 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1083 return 0; 1308 return 0;
1084 } 1309 }
1310
1311 if (state != FIP_ST_ENABLED)
1312 goto drop;
1313
1085 if (op == FIP_OP_DISC && sub == FIP_SC_ADV) 1314 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1086 fcoe_ctlr_recv_adv(fip, skb); 1315 fcoe_ctlr_recv_adv(fip, skb);
1087 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) 1316 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
@@ -1140,30 +1369,53 @@ static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1140/** 1369/**
1141 * fcoe_ctlr_timeout() - FIP timeout handler 1370 * fcoe_ctlr_timeout() - FIP timeout handler
1142 * @arg: The FCoE controller that timed out 1371 * @arg: The FCoE controller that timed out
1143 *
1144 * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
1145 */ 1372 */
1146static void fcoe_ctlr_timeout(unsigned long arg) 1373static void fcoe_ctlr_timeout(unsigned long arg)
1147{ 1374{
1148 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg; 1375 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1376
1377 schedule_work(&fip->timer_work);
1378}
1379
1380/**
1381 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1382 * @work: Handle to a FCoE controller
1383 *
1384 * Ages FCFs. Triggers FCF selection if possible.
1385 * Sends keep-alives and resets.
1386 */
1387static void fcoe_ctlr_timer_work(struct work_struct *work)
1388{
1389 struct fcoe_ctlr *fip;
1390 struct fc_lport *vport;
1391 u8 *mac;
1392 u8 reset = 0;
1393 u8 send_ctlr_ka = 0;
1394 u8 send_port_ka = 0;
1149 struct fcoe_fcf *sel; 1395 struct fcoe_fcf *sel;
1150 struct fcoe_fcf *fcf; 1396 struct fcoe_fcf *fcf;
1151 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD); 1397 unsigned long next_timer;
1152 1398
1153 spin_lock_bh(&fip->lock); 1399 fip = container_of(work, struct fcoe_ctlr, timer_work);
1400 if (fip->mode == FIP_MODE_VN2VN)
1401 return fcoe_ctlr_vn_timeout(fip);
1402 mutex_lock(&fip->ctlr_mutex);
1154 if (fip->state == FIP_ST_DISABLED) { 1403 if (fip->state == FIP_ST_DISABLED) {
1155 spin_unlock_bh(&fip->lock); 1404 mutex_unlock(&fip->ctlr_mutex);
1156 return; 1405 return;
1157 } 1406 }
1158 1407
1159 fcf = fip->sel_fcf; 1408 fcf = fip->sel_fcf;
1160 fcoe_ctlr_age_fcfs(fip); 1409 next_timer = fcoe_ctlr_age_fcfs(fip);
1161 1410
1162 sel = fip->sel_fcf; 1411 sel = fip->sel_fcf;
1163 if (!sel && fip->sel_time && time_after_eq(jiffies, fip->sel_time)) { 1412 if (!sel && fip->sel_time) {
1164 fcoe_ctlr_select(fip); 1413 if (time_after_eq(jiffies, fip->sel_time)) {
1165 sel = fip->sel_fcf; 1414 fcoe_ctlr_select(fip);
1166 fip->sel_time = 0; 1415 sel = fip->sel_fcf;
1416 fip->sel_time = 0;
1417 } else if (time_after(next_timer, fip->sel_time))
1418 next_timer = fip->sel_time;
1167 } 1419 }
1168 1420
1169 if (sel != fcf) { 1421 if (sel != fcf) {
@@ -1173,23 +1425,25 @@ static void fcoe_ctlr_timeout(unsigned long arg)
1173 "Fibre-Channel Forwarder MAC %pM\n", 1425 "Fibre-Channel Forwarder MAC %pM\n",
1174 fip->lp->host->host_no, sel->fcf_mac); 1426 fip->lp->host->host_no, sel->fcf_mac);
1175 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN); 1427 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1428 fip->map_dest = 0;
1176 fip->port_ka_time = jiffies + 1429 fip->port_ka_time = jiffies +
1177 msecs_to_jiffies(FIP_VN_KA_PERIOD); 1430 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1178 fip->ctlr_ka_time = jiffies + sel->fka_period; 1431 fip->ctlr_ka_time = jiffies + sel->fka_period;
1432 if (time_after(next_timer, fip->ctlr_ka_time))
1433 next_timer = fip->ctlr_ka_time;
1179 } else { 1434 } else {
1180 printk(KERN_NOTICE "libfcoe: host%d: " 1435 printk(KERN_NOTICE "libfcoe: host%d: "
1181 "FIP Fibre-Channel Forwarder timed out. " 1436 "FIP Fibre-Channel Forwarder timed out. "
1182 "Starting FCF discovery.\n", 1437 "Starting FCF discovery.\n",
1183 fip->lp->host->host_no); 1438 fip->lp->host->host_no);
1184 fip->reset_req = 1; 1439 reset = 1;
1185 schedule_work(&fip->timer_work);
1186 } 1440 }
1187 } 1441 }
1188 1442
1189 if (sel && !sel->fd_flags) { 1443 if (sel && !sel->fd_flags) {
1190 if (time_after_eq(jiffies, fip->ctlr_ka_time)) { 1444 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1191 fip->ctlr_ka_time = jiffies + sel->fka_period; 1445 fip->ctlr_ka_time = jiffies + sel->fka_period;
1192 fip->send_ctlr_ka = 1; 1446 send_ctlr_ka = 1;
1193 } 1447 }
1194 if (time_after(next_timer, fip->ctlr_ka_time)) 1448 if (time_after(next_timer, fip->ctlr_ka_time))
1195 next_timer = fip->ctlr_ka_time; 1449 next_timer = fip->ctlr_ka_time;
@@ -1197,50 +1451,25 @@ static void fcoe_ctlr_timeout(unsigned long arg)
1197 if (time_after_eq(jiffies, fip->port_ka_time)) { 1451 if (time_after_eq(jiffies, fip->port_ka_time)) {
1198 fip->port_ka_time = jiffies + 1452 fip->port_ka_time = jiffies +
1199 msecs_to_jiffies(FIP_VN_KA_PERIOD); 1453 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1200 fip->send_port_ka = 1; 1454 send_port_ka = 1;
1201 } 1455 }
1202 if (time_after(next_timer, fip->port_ka_time)) 1456 if (time_after(next_timer, fip->port_ka_time))
1203 next_timer = fip->port_ka_time; 1457 next_timer = fip->port_ka_time;
1204 mod_timer(&fip->timer, next_timer);
1205 } else if (fip->sel_time) {
1206 next_timer = fip->sel_time +
1207 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1208 mod_timer(&fip->timer, next_timer);
1209 } 1458 }
1210 if (fip->send_ctlr_ka || fip->send_port_ka) 1459 if (!list_empty(&fip->fcfs))
1211 schedule_work(&fip->timer_work); 1460 mod_timer(&fip->timer, next_timer);
1212 spin_unlock_bh(&fip->lock); 1461 mutex_unlock(&fip->ctlr_mutex);
1213}
1214
1215/**
1216 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1217 * @work: Handle to a FCoE controller
1218 *
1219 * Sends keep-alives and resets which must not
1220 * be called from the timer directly, since they use a mutex.
1221 */
1222static void fcoe_ctlr_timer_work(struct work_struct *work)
1223{
1224 struct fcoe_ctlr *fip;
1225 struct fc_lport *vport;
1226 u8 *mac;
1227 int reset;
1228
1229 fip = container_of(work, struct fcoe_ctlr, timer_work);
1230 spin_lock_bh(&fip->lock);
1231 reset = fip->reset_req;
1232 fip->reset_req = 0;
1233 spin_unlock_bh(&fip->lock);
1234 1462
1235 if (reset) 1463 if (reset) {
1236 fc_lport_reset(fip->lp); 1464 fc_lport_reset(fip->lp);
1465 /* restart things with a solicitation */
1466 fcoe_ctlr_solicit(fip, NULL);
1467 }
1237 1468
1238 if (fip->send_ctlr_ka) { 1469 if (send_ctlr_ka)
1239 fip->send_ctlr_ka = 0;
1240 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr); 1470 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1241 } 1471
1242 if (fip->send_port_ka) { 1472 if (send_port_ka) {
1243 fip->send_port_ka = 0;
1244 mutex_lock(&fip->lp->lp_mutex); 1473 mutex_lock(&fip->lp->lp_mutex);
1245 mac = fip->get_src_addr(fip->lp); 1474 mac = fip->get_src_addr(fip->lp);
1246 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac); 1475 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
@@ -1297,12 +1526,12 @@ int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1297 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP && 1526 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1298 fip->flogi_oxid == ntohs(fh->fh_ox_id)) { 1527 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1299 1528
1300 spin_lock_bh(&fip->lock); 1529 mutex_lock(&fip->ctlr_mutex);
1301 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) { 1530 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1302 spin_unlock_bh(&fip->lock); 1531 mutex_unlock(&fip->ctlr_mutex);
1303 return -EINVAL; 1532 return -EINVAL;
1304 } 1533 }
1305 fip->state = FIP_ST_NON_FIP; 1534 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1306 LIBFCOE_FIP_DBG(fip, 1535 LIBFCOE_FIP_DBG(fip,
1307 "received FLOGI LS_ACC using non-FIP mode\n"); 1536 "received FLOGI LS_ACC using non-FIP mode\n");
1308 1537
@@ -1313,28 +1542,28 @@ int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1313 * Otherwise we use the FCoE gateway addr 1542 * Otherwise we use the FCoE gateway addr
1314 */ 1543 */
1315 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) { 1544 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1316 fip->map_dest = 1; 1545 fcoe_ctlr_map_dest(fip);
1317 } else { 1546 } else {
1318 memcpy(fip->dest_addr, sa, ETH_ALEN); 1547 memcpy(fip->dest_addr, sa, ETH_ALEN);
1319 fip->map_dest = 0; 1548 fip->map_dest = 0;
1320 } 1549 }
1321 fip->flogi_oxid = FC_XID_UNKNOWN; 1550 fip->flogi_oxid = FC_XID_UNKNOWN;
1322 spin_unlock_bh(&fip->lock); 1551 mutex_unlock(&fip->ctlr_mutex);
1323 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id); 1552 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1324 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) { 1553 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1325 /* 1554 /*
1326 * Save source MAC for point-to-point responses. 1555 * Save source MAC for point-to-point responses.
1327 */ 1556 */
1328 spin_lock_bh(&fip->lock); 1557 mutex_lock(&fip->ctlr_mutex);
1329 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) { 1558 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1330 memcpy(fip->dest_addr, sa, ETH_ALEN); 1559 memcpy(fip->dest_addr, sa, ETH_ALEN);
1331 fip->map_dest = 0; 1560 fip->map_dest = 0;
1332 if (fip->state == FIP_ST_AUTO) 1561 if (fip->state == FIP_ST_AUTO)
1333 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. " 1562 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1334 "Setting non-FIP mode\n"); 1563 "Setting non-FIP mode\n");
1335 fip->state = FIP_ST_NON_FIP; 1564 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1336 } 1565 }
1337 spin_unlock_bh(&fip->lock); 1566 mutex_unlock(&fip->ctlr_mutex);
1338 } 1567 }
1339 return 0; 1568 return 0;
1340} 1569}
@@ -1382,26 +1611,916 @@ u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1382EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac); 1611EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1383 1612
1384/** 1613/**
1614 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1615 * @rdata: libfc remote port
1616 */
1617static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1618{
1619 return (struct fcoe_rport *)(rdata + 1);
1620}
1621
1622/**
1623 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1624 * @fip: The FCoE controller
1625 * @sub: sub-opcode for probe request, reply, or advertisement.
1626 * @dest: The destination Ethernet MAC address
1627 * @min_len: minimum size of the Ethernet payload to be sent
1628 */
1629static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1630 enum fip_vn2vn_subcode sub,
1631 const u8 *dest, size_t min_len)
1632{
1633 struct sk_buff *skb;
1634 struct fip_frame {
1635 struct ethhdr eth;
1636 struct fip_header fip;
1637 struct fip_mac_desc mac;
1638 struct fip_wwn_desc wwnn;
1639 struct fip_vn_desc vn;
1640 } __attribute__((packed)) *frame;
1641 struct fip_fc4_feat *ff;
1642 struct fip_size_desc *size;
1643 u32 fcp_feat;
1644 size_t len;
1645 size_t dlen;
1646
1647 len = sizeof(*frame);
1648 dlen = 0;
1649 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1650 dlen = sizeof(struct fip_fc4_feat) +
1651 sizeof(struct fip_size_desc);
1652 len += dlen;
1653 }
1654 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1655 len = max(len, min_len + sizeof(struct ethhdr));
1656
1657 skb = dev_alloc_skb(len);
1658 if (!skb)
1659 return;
1660
1661 frame = (struct fip_frame *)skb->data;
1662 memset(frame, 0, len);
1663 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1664 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1665 frame->eth.h_proto = htons(ETH_P_FIP);
1666
1667 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1668 frame->fip.fip_op = htons(FIP_OP_VN2VN);
1669 frame->fip.fip_subcode = sub;
1670 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1671
1672 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1673 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1674 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1675
1676 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1677 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1678 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1679
1680 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
1681 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
1682 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
1683 hton24(frame->vn.fd_mac + 3, fip->port_id);
1684 hton24(frame->vn.fd_fc_id, fip->port_id);
1685 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
1686
1687 /*
1688 * For claims, add FC-4 features.
1689 * TBD: Add interface to get fc-4 types and features from libfc.
1690 */
1691 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1692 ff = (struct fip_fc4_feat *)(frame + 1);
1693 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
1694 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
1695 ff->fd_fts = fip->lp->fcts;
1696
1697 fcp_feat = 0;
1698 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
1699 fcp_feat |= FCP_FEAT_INIT;
1700 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
1701 fcp_feat |= FCP_FEAT_TARG;
1702 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
1703 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
1704
1705 size = (struct fip_size_desc *)(ff + 1);
1706 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
1707 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
1708 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
1709 }
1710
1711 skb_put(skb, len);
1712 skb->protocol = htons(ETH_P_FIP);
1713 skb_reset_mac_header(skb);
1714 skb_reset_network_header(skb);
1715
1716 fip->send(fip, skb);
1717}
1718
1719/**
1720 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
1721 * @lport: The lport which is receiving the event
1722 * @rdata: remote port private data
1723 * @event: The event that occured
1724 *
1725 * Locking Note: The rport lock must not be held when calling this function.
1726 */
1727static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
1728 struct fc_rport_priv *rdata,
1729 enum fc_rport_event event)
1730{
1731 struct fcoe_ctlr *fip = lport->disc.priv;
1732 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1733
1734 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
1735 rdata->ids.port_id, event);
1736
1737 mutex_lock(&fip->ctlr_mutex);
1738 switch (event) {
1739 case RPORT_EV_READY:
1740 frport->login_count = 0;
1741 break;
1742 case RPORT_EV_LOGO:
1743 case RPORT_EV_FAILED:
1744 case RPORT_EV_STOP:
1745 frport->login_count++;
1746 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
1747 LIBFCOE_FIP_DBG(fip,
1748 "rport FLOGI limited port_id %6.6x\n",
1749 rdata->ids.port_id);
1750 lport->tt.rport_logoff(rdata);
1751 }
1752 break;
1753 default:
1754 break;
1755 }
1756 mutex_unlock(&fip->ctlr_mutex);
1757}
1758
1759static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
1760 .event_callback = fcoe_ctlr_vn_rport_callback,
1761};
1762
1763/**
1764 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
1765 * @fip: The FCoE controller
1766 *
1767 * Called with ctlr_mutex held.
1768 */
1769static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
1770{
1771 mutex_lock(&lport->disc.disc_mutex);
1772 lport->disc.disc_callback = NULL;
1773 mutex_unlock(&lport->disc.disc_mutex);
1774}
1775
1776/**
1777 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
1778 * @fip: The FCoE controller
1779 *
1780 * Called through the local port template for discovery.
1781 * Called without the ctlr_mutex held.
1782 */
1783static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
1784{
1785 struct fcoe_ctlr *fip = lport->disc.priv;
1786
1787 mutex_lock(&fip->ctlr_mutex);
1788 fcoe_ctlr_disc_stop_locked(lport);
1789 mutex_unlock(&fip->ctlr_mutex);
1790}
1791
1792/**
1793 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
1794 * @fip: The FCoE controller
1795 *
1796 * Called through the local port template for discovery.
1797 * Called without the ctlr_mutex held.
1798 */
1799static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
1800{
1801 fcoe_ctlr_disc_stop(lport);
1802 lport->tt.rport_flush_queue();
1803 synchronize_rcu();
1804}
1805
1806/**
1807 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
1808 * @fip: The FCoE controller
1809 *
1810 * Called with fcoe_ctlr lock held.
1811 */
1812static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
1813{
1814 unsigned long wait;
1815 u32 port_id;
1816
1817 fcoe_ctlr_disc_stop_locked(fip->lp);
1818
1819 /*
1820 * Get proposed port ID.
1821 * If this is the first try after link up, use any previous port_id.
1822 * If there was none, use the low bits of the port_name.
1823 * On subsequent tries, get the next random one.
1824 * Don't use reserved IDs, use another non-zero value, just as random.
1825 */
1826 port_id = fip->port_id;
1827 if (fip->probe_tries)
1828 port_id = prandom32(&fip->rnd_state) & 0xffff;
1829 else if (!port_id)
1830 port_id = fip->lp->wwpn & 0xffff;
1831 if (!port_id || port_id == 0xffff)
1832 port_id = 1;
1833 fip->port_id = port_id;
1834
1835 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
1836 fip->probe_tries++;
1837 wait = random32() % FIP_VN_PROBE_WAIT;
1838 } else
1839 wait = FIP_VN_RLIM_INT;
1840 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
1841 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
1842}
1843
1844/**
1845 * fcoe_ctlr_vn_start() - Start in VN2VN mode
1846 * @fip: The FCoE controller
1847 *
1848 * Called with fcoe_ctlr lock held.
1849 */
1850static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
1851{
1852 fip->probe_tries = 0;
1853 prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
1854 fcoe_ctlr_vn_restart(fip);
1855}
1856
1857/**
1858 * fcoe_ctlr_vn_parse - parse probe request or response
1859 * @fip: The FCoE controller
1860 * @skb: incoming packet
1861 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
1862 *
1863 * Returns non-zero error number on error.
1864 * Does not consume the packet.
1865 */
1866static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
1867 struct sk_buff *skb,
1868 struct fc_rport_priv *rdata)
1869{
1870 struct fip_header *fiph;
1871 struct fip_desc *desc = NULL;
1872 struct fip_mac_desc *macd = NULL;
1873 struct fip_wwn_desc *wwn = NULL;
1874 struct fip_vn_desc *vn = NULL;
1875 struct fip_size_desc *size = NULL;
1876 struct fcoe_rport *frport;
1877 size_t rlen;
1878 size_t dlen;
1879 u32 desc_mask = 0;
1880 u32 dtype;
1881 u8 sub;
1882
1883 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
1884 frport = fcoe_ctlr_rport(rdata);
1885
1886 fiph = (struct fip_header *)skb->data;
1887 frport->flags = ntohs(fiph->fip_flags);
1888
1889 sub = fiph->fip_subcode;
1890 switch (sub) {
1891 case FIP_SC_VN_PROBE_REQ:
1892 case FIP_SC_VN_PROBE_REP:
1893 case FIP_SC_VN_BEACON:
1894 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1895 BIT(FIP_DT_VN_ID);
1896 break;
1897 case FIP_SC_VN_CLAIM_NOTIFY:
1898 case FIP_SC_VN_CLAIM_REP:
1899 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1900 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
1901 BIT(FIP_DT_FCOE_SIZE);
1902 break;
1903 default:
1904 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
1905 return -EINVAL;
1906 }
1907
1908 rlen = ntohs(fiph->fip_dl_len) * 4;
1909 if (rlen + sizeof(*fiph) > skb->len)
1910 return -EINVAL;
1911
1912 desc = (struct fip_desc *)(fiph + 1);
1913 while (rlen > 0) {
1914 dlen = desc->fip_dlen * FIP_BPW;
1915 if (dlen < sizeof(*desc) || dlen > rlen)
1916 return -EINVAL;
1917
1918 dtype = desc->fip_dtype;
1919 if (dtype < 32) {
1920 if (!(desc_mask & BIT(dtype))) {
1921 LIBFCOE_FIP_DBG(fip,
1922 "unexpected or duplicated desc "
1923 "desc type %u in "
1924 "FIP VN2VN subtype %u\n",
1925 dtype, sub);
1926 return -EINVAL;
1927 }
1928 desc_mask &= ~BIT(dtype);
1929 }
1930
1931 switch (dtype) {
1932 case FIP_DT_MAC:
1933 if (dlen != sizeof(struct fip_mac_desc))
1934 goto len_err;
1935 macd = (struct fip_mac_desc *)desc;
1936 if (!is_valid_ether_addr(macd->fd_mac)) {
1937 LIBFCOE_FIP_DBG(fip,
1938 "Invalid MAC addr %pM in FIP VN2VN\n",
1939 macd->fd_mac);
1940 return -EINVAL;
1941 }
1942 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
1943 break;
1944 case FIP_DT_NAME:
1945 if (dlen != sizeof(struct fip_wwn_desc))
1946 goto len_err;
1947 wwn = (struct fip_wwn_desc *)desc;
1948 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
1949 break;
1950 case FIP_DT_VN_ID:
1951 if (dlen != sizeof(struct fip_vn_desc))
1952 goto len_err;
1953 vn = (struct fip_vn_desc *)desc;
1954 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
1955 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
1956 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
1957 break;
1958 case FIP_DT_FC4F:
1959 if (dlen != sizeof(struct fip_fc4_feat))
1960 goto len_err;
1961 break;
1962 case FIP_DT_FCOE_SIZE:
1963 if (dlen != sizeof(struct fip_size_desc))
1964 goto len_err;
1965 size = (struct fip_size_desc *)desc;
1966 frport->fcoe_len = ntohs(size->fd_size);
1967 break;
1968 default:
1969 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1970 "in FIP probe\n", dtype);
1971 /* standard says ignore unknown descriptors >= 128 */
1972 if (dtype < FIP_DT_VENDOR_BASE)
1973 return -EINVAL;
1974 break;
1975 }
1976 desc = (struct fip_desc *)((char *)desc + dlen);
1977 rlen -= dlen;
1978 }
1979 return 0;
1980
1981len_err:
1982 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1983 dtype, dlen);
1984 return -EINVAL;
1985}
1986
1987/**
1988 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
1989 * @fip: The FCoE controller
1990 *
1991 * Called with ctlr_mutex held.
1992 */
1993static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
1994{
1995 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
1996 fip->sol_time = jiffies;
1997}
1998
1999/**
2000 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2001 * @fip: The FCoE controller
2002 * @rdata: parsed remote port with frport from the probe request
2003 *
2004 * Called with ctlr_mutex held.
2005 */
2006static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2007 struct fc_rport_priv *rdata)
2008{
2009 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2010
2011 if (rdata->ids.port_id != fip->port_id)
2012 return;
2013
2014 switch (fip->state) {
2015 case FIP_ST_VNMP_CLAIM:
2016 case FIP_ST_VNMP_UP:
2017 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2018 frport->enode_mac, 0);
2019 break;
2020 case FIP_ST_VNMP_PROBE1:
2021 case FIP_ST_VNMP_PROBE2:
2022 /*
2023 * Decide whether to reply to the Probe.
2024 * Our selected address is never a "recorded" one, so
2025 * only reply if our WWPN is greater and the
2026 * Probe's REC bit is not set.
2027 * If we don't reply, we will change our address.
2028 */
2029 if (fip->lp->wwpn > rdata->ids.port_name &&
2030 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2031 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2032 frport->enode_mac, 0);
2033 break;
2034 }
2035 /* fall through */
2036 case FIP_ST_VNMP_START:
2037 fcoe_ctlr_vn_restart(fip);
2038 break;
2039 default:
2040 break;
2041 }
2042}
2043
2044/**
2045 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2046 * @fip: The FCoE controller
2047 * @rdata: parsed remote port with frport from the probe request
2048 *
2049 * Called with ctlr_mutex held.
2050 */
2051static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2052 struct fc_rport_priv *rdata)
2053{
2054 if (rdata->ids.port_id != fip->port_id)
2055 return;
2056 switch (fip->state) {
2057 case FIP_ST_VNMP_START:
2058 case FIP_ST_VNMP_PROBE1:
2059 case FIP_ST_VNMP_PROBE2:
2060 case FIP_ST_VNMP_CLAIM:
2061 fcoe_ctlr_vn_restart(fip);
2062 break;
2063 case FIP_ST_VNMP_UP:
2064 fcoe_ctlr_vn_send_claim(fip);
2065 break;
2066 default:
2067 break;
2068 }
2069}
2070
2071/**
2072 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2073 * @fip: The FCoE controller
2074 * @new: newly-parsed remote port with frport as a template for new rdata
2075 *
2076 * Called with ctlr_mutex held.
2077 */
2078static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2079{
2080 struct fc_lport *lport = fip->lp;
2081 struct fc_rport_priv *rdata;
2082 struct fc_rport_identifiers *ids;
2083 struct fcoe_rport *frport;
2084 u32 port_id;
2085
2086 port_id = new->ids.port_id;
2087 if (port_id == fip->port_id)
2088 return;
2089
2090 mutex_lock(&lport->disc.disc_mutex);
2091 rdata = lport->tt.rport_create(lport, port_id);
2092 if (!rdata) {
2093 mutex_unlock(&lport->disc.disc_mutex);
2094 return;
2095 }
2096
2097 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2098 rdata->disc_id = lport->disc.disc_id;
2099
2100 ids = &rdata->ids;
2101 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2102 (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2103 lport->tt.rport_logoff(rdata);
2104 ids->port_name = new->ids.port_name;
2105 ids->node_name = new->ids.node_name;
2106 mutex_unlock(&lport->disc.disc_mutex);
2107
2108 frport = fcoe_ctlr_rport(rdata);
2109 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2110 port_id, frport->fcoe_len ? "old" : "new");
2111 *frport = *fcoe_ctlr_rport(new);
2112 frport->time = 0;
2113}
2114
2115/**
2116 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2117 * @fip: The FCoE controller
2118 * @port_id: The port_id of the remote VN_node
2119 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2120 *
2121 * Returns non-zero error if no remote port found.
2122 */
2123static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2124{
2125 struct fc_lport *lport = fip->lp;
2126 struct fc_rport_priv *rdata;
2127 struct fcoe_rport *frport;
2128 int ret = -1;
2129
2130 rcu_read_lock();
2131 rdata = lport->tt.rport_lookup(lport, port_id);
2132 if (rdata) {
2133 frport = fcoe_ctlr_rport(rdata);
2134 memcpy(mac, frport->enode_mac, ETH_ALEN);
2135 ret = 0;
2136 }
2137 rcu_read_unlock();
2138 return ret;
2139}
2140
2141/**
2142 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2143 * @fip: The FCoE controller
2144 * @new: newly-parsed remote port with frport as a template for new rdata
2145 *
2146 * Called with ctlr_mutex held.
2147 */
2148static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2149 struct fc_rport_priv *new)
2150{
2151 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2152
2153 if (frport->flags & FIP_FL_REC_OR_P2P) {
2154 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2155 return;
2156 }
2157 switch (fip->state) {
2158 case FIP_ST_VNMP_START:
2159 case FIP_ST_VNMP_PROBE1:
2160 case FIP_ST_VNMP_PROBE2:
2161 if (new->ids.port_id == fip->port_id)
2162 fcoe_ctlr_vn_restart(fip);
2163 break;
2164 case FIP_ST_VNMP_CLAIM:
2165 case FIP_ST_VNMP_UP:
2166 if (new->ids.port_id == fip->port_id) {
2167 if (new->ids.port_name > fip->lp->wwpn) {
2168 fcoe_ctlr_vn_restart(fip);
2169 break;
2170 }
2171 fcoe_ctlr_vn_send_claim(fip);
2172 break;
2173 }
2174 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2175 min((u32)frport->fcoe_len,
2176 fcoe_ctlr_fcoe_size(fip)));
2177 fcoe_ctlr_vn_add(fip, new);
2178 break;
2179 default:
2180 break;
2181 }
2182}
2183
2184/**
2185 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2186 * @fip: The FCoE controller that received the frame
2187 * @new: newly-parsed remote port with frport from the Claim Response
2188 *
2189 * Called with ctlr_mutex held.
2190 */
2191static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2192 struct fc_rport_priv *new)
2193{
2194 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2195 new->ids.port_id, fcoe_ctlr_state(fip->state));
2196 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2197 fcoe_ctlr_vn_add(fip, new);
2198}
2199
2200/**
2201 * fcoe_ctlr_vn_beacon() - handle received beacon.
2202 * @fip: The FCoE controller that received the frame
2203 * @new: newly-parsed remote port with frport from the Beacon
2204 *
2205 * Called with ctlr_mutex held.
2206 */
2207static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2208 struct fc_rport_priv *new)
2209{
2210 struct fc_lport *lport = fip->lp;
2211 struct fc_rport_priv *rdata;
2212 struct fcoe_rport *frport;
2213
2214 frport = fcoe_ctlr_rport(new);
2215 if (frport->flags & FIP_FL_REC_OR_P2P) {
2216 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2217 return;
2218 }
2219 mutex_lock(&lport->disc.disc_mutex);
2220 rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2221 if (rdata)
2222 kref_get(&rdata->kref);
2223 mutex_unlock(&lport->disc.disc_mutex);
2224 if (rdata) {
2225 if (rdata->ids.node_name == new->ids.node_name &&
2226 rdata->ids.port_name == new->ids.port_name) {
2227 frport = fcoe_ctlr_rport(rdata);
2228 if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2229 lport->tt.rport_login(rdata);
2230 frport->time = jiffies;
2231 }
2232 kref_put(&rdata->kref, lport->tt.rport_destroy);
2233 return;
2234 }
2235 if (fip->state != FIP_ST_VNMP_UP)
2236 return;
2237
2238 /*
2239 * Beacon from a new neighbor.
2240 * Send a claim notify if one hasn't been sent recently.
2241 * Don't add the neighbor yet.
2242 */
2243 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2244 new->ids.port_id);
2245 if (time_after(jiffies,
2246 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2247 fcoe_ctlr_vn_send_claim(fip);
2248}
2249
2250/**
2251 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2252 * @fip: The FCoE controller
2253 *
2254 * Called with ctlr_mutex held.
2255 * Called only in state FIP_ST_VNMP_UP.
2256 * Returns the soonest time for next age-out or a time far in the future.
2257 */
2258static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2259{
2260 struct fc_lport *lport = fip->lp;
2261 struct fc_rport_priv *rdata;
2262 struct fcoe_rport *frport;
2263 unsigned long next_time;
2264 unsigned long deadline;
2265
2266 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2267 mutex_lock(&lport->disc.disc_mutex);
2268 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2269 frport = fcoe_ctlr_rport(rdata);
2270 if (!frport->time)
2271 continue;
2272 deadline = frport->time +
2273 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2274 if (time_after_eq(jiffies, deadline)) {
2275 frport->time = 0;
2276 LIBFCOE_FIP_DBG(fip,
2277 "port %16.16llx fc_id %6.6x beacon expired\n",
2278 rdata->ids.port_name, rdata->ids.port_id);
2279 lport->tt.rport_logoff(rdata);
2280 } else if (time_before(deadline, next_time))
2281 next_time = deadline;
2282 }
2283 mutex_unlock(&lport->disc.disc_mutex);
2284 return next_time;
2285}
2286
2287/**
2288 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2289 * @fip: The FCoE controller that received the frame
2290 * @skb: The received FIP frame
2291 *
2292 * Returns non-zero if the frame is dropped.
2293 * Always consumes the frame.
2294 */
2295static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2296{
2297 struct fip_header *fiph;
2298 enum fip_vn2vn_subcode sub;
2299 union {
2300 struct fc_rport_priv rdata;
2301 struct fcoe_rport frport;
2302 } buf;
2303 int rc;
2304
2305 fiph = (struct fip_header *)skb->data;
2306 sub = fiph->fip_subcode;
2307
2308 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2309 if (rc) {
2310 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2311 goto drop;
2312 }
2313
2314 mutex_lock(&fip->ctlr_mutex);
2315 switch (sub) {
2316 case FIP_SC_VN_PROBE_REQ:
2317 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2318 break;
2319 case FIP_SC_VN_PROBE_REP:
2320 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2321 break;
2322 case FIP_SC_VN_CLAIM_NOTIFY:
2323 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2324 break;
2325 case FIP_SC_VN_CLAIM_REP:
2326 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2327 break;
2328 case FIP_SC_VN_BEACON:
2329 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2330 break;
2331 default:
2332 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2333 rc = -1;
2334 break;
2335 }
2336 mutex_unlock(&fip->ctlr_mutex);
2337drop:
2338 kfree_skb(skb);
2339 return rc;
2340}
2341
2342/**
2343 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
2344 * @lport: The local port
2345 * @fp: The received frame
2346 *
2347 * This should never be called since we don't see RSCNs or other
2348 * fabric-generated ELSes.
2349 */
2350static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
2351{
2352 struct fc_seq_els_data rjt_data;
2353
2354 rjt_data.reason = ELS_RJT_UNSUP;
2355 rjt_data.explan = ELS_EXPL_NONE;
2356 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
2357 fc_frame_free(fp);
2358}
2359
2360/**
2361 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2362 * @fip: The FCoE controller
2363 *
2364 * This sets a flag indicating that remote ports should be created
2365 * and started for the peers we discover. We use the disc_callback
2366 * pointer as that flag. Peers already discovered are created here.
2367 *
2368 * The lport lock is held during this call. The callback must be done
2369 * later, without holding either the lport or discovery locks.
2370 * The fcoe_ctlr lock may also be held during this call.
2371 */
2372static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2373 enum fc_disc_event),
2374 struct fc_lport *lport)
2375{
2376 struct fc_disc *disc = &lport->disc;
2377 struct fcoe_ctlr *fip = disc->priv;
2378
2379 mutex_lock(&disc->disc_mutex);
2380 disc->disc_callback = callback;
2381 disc->disc_id = (disc->disc_id + 2) | 1;
2382 disc->pending = 1;
2383 schedule_work(&fip->timer_work);
2384 mutex_unlock(&disc->disc_mutex);
2385}
2386
2387/**
2388 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2389 * @fip: The FCoE controller
2390 *
2391 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2392 * we've received at least one beacon.
2393 * Performs the discovery complete callback.
2394 */
2395static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2396{
2397 struct fc_lport *lport = fip->lp;
2398 struct fc_disc *disc = &lport->disc;
2399 struct fc_rport_priv *rdata;
2400 struct fcoe_rport *frport;
2401 void (*callback)(struct fc_lport *, enum fc_disc_event);
2402
2403 mutex_lock(&disc->disc_mutex);
2404 callback = disc->pending ? disc->disc_callback : NULL;
2405 disc->pending = 0;
2406 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2407 frport = fcoe_ctlr_rport(rdata);
2408 if (frport->time)
2409 lport->tt.rport_login(rdata);
2410 }
2411 mutex_unlock(&disc->disc_mutex);
2412 if (callback)
2413 callback(lport, DISC_EV_SUCCESS);
2414}
2415
2416/**
2417 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2418 * @fip: The FCoE controller
2419 */
2420static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2421{
2422 unsigned long next_time;
2423 u8 mac[ETH_ALEN];
2424 u32 new_port_id = 0;
2425
2426 mutex_lock(&fip->ctlr_mutex);
2427 switch (fip->state) {
2428 case FIP_ST_VNMP_START:
2429 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2430 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2431 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2432 break;
2433 case FIP_ST_VNMP_PROBE1:
2434 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2435 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2436 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2437 break;
2438 case FIP_ST_VNMP_PROBE2:
2439 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2440 new_port_id = fip->port_id;
2441 hton24(mac, FIP_VN_FC_MAP);
2442 hton24(mac + 3, new_port_id);
2443 fcoe_ctlr_map_dest(fip);
2444 fip->update_mac(fip->lp, mac);
2445 fcoe_ctlr_vn_send_claim(fip);
2446 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2447 break;
2448 case FIP_ST_VNMP_CLAIM:
2449 /*
2450 * This may be invoked either by starting discovery so don't
2451 * go to the next state unless it's been long enough.
2452 */
2453 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2454 if (time_after_eq(jiffies, next_time)) {
2455 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2456 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2457 fcoe_all_vn2vn, 0);
2458 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2459 fip->port_ka_time = next_time;
2460 }
2461 fcoe_ctlr_vn_disc(fip);
2462 break;
2463 case FIP_ST_VNMP_UP:
2464 next_time = fcoe_ctlr_vn_age(fip);
2465 if (time_after_eq(jiffies, fip->port_ka_time)) {
2466 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2467 fcoe_all_vn2vn, 0);
2468 fip->port_ka_time = jiffies +
2469 msecs_to_jiffies(FIP_VN_BEACON_INT +
2470 (random32() % FIP_VN_BEACON_FUZZ));
2471 }
2472 if (time_before(fip->port_ka_time, next_time))
2473 next_time = fip->port_ka_time;
2474 break;
2475 case FIP_ST_LINK_WAIT:
2476 goto unlock;
2477 default:
2478 WARN(1, "unexpected state %d", fip->state);
2479 goto unlock;
2480 }
2481 mod_timer(&fip->timer, next_time);
2482unlock:
2483 mutex_unlock(&fip->ctlr_mutex);
2484
2485 /* If port ID is new, notify local port after dropping ctlr_mutex */
2486 if (new_port_id)
2487 fc_lport_set_local_id(fip->lp, new_port_id);
2488}
2489
2490/**
1385 * fcoe_libfc_config() - Sets up libfc related properties for local port 2491 * fcoe_libfc_config() - Sets up libfc related properties for local port
1386 * @lp: The local port to configure libfc for 2492 * @lp: The local port to configure libfc for
2493 * @fip: The FCoE controller in use by the local port
1387 * @tt: The libfc function template 2494 * @tt: The libfc function template
2495 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
1388 * 2496 *
1389 * Returns : 0 for success 2497 * Returns : 0 for success
1390 */ 2498 */
1391int fcoe_libfc_config(struct fc_lport *lport, 2499int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
1392 struct libfc_function_template *tt) 2500 const struct libfc_function_template *tt, int init_fcp)
1393{ 2501{
1394 /* Set the function pointers set by the LLDD */ 2502 /* Set the function pointers set by the LLDD */
1395 memcpy(&lport->tt, tt, sizeof(*tt)); 2503 memcpy(&lport->tt, tt, sizeof(*tt));
1396 if (fc_fcp_init(lport)) 2504 if (init_fcp && fc_fcp_init(lport))
1397 return -ENOMEM; 2505 return -ENOMEM;
1398 fc_exch_init(lport); 2506 fc_exch_init(lport);
1399 fc_elsct_init(lport); 2507 fc_elsct_init(lport);
1400 fc_lport_init(lport); 2508 fc_lport_init(lport);
2509 if (fip->mode == FIP_MODE_VN2VN)
2510 lport->rport_priv_size = sizeof(struct fcoe_rport);
1401 fc_rport_init(lport); 2511 fc_rport_init(lport);
1402 fc_disc_init(lport); 2512 if (fip->mode == FIP_MODE_VN2VN) {
1403 2513 lport->point_to_multipoint = 1;
2514 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2515 lport->tt.disc_start = fcoe_ctlr_disc_start;
2516 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2517 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2518 mutex_init(&lport->disc.disc_mutex);
2519 INIT_LIST_HEAD(&lport->disc.rports);
2520 lport->disc.priv = fip;
2521 } else {
2522 fc_disc_init(lport);
2523 }
1404 return 0; 2524 return 0;
1405} 2525}
1406EXPORT_SYMBOL_GPL(fcoe_libfc_config); 2526EXPORT_SYMBOL_GPL(fcoe_libfc_config);
1407