Linux Audio

Check our new training course

Loading...
v3.1
 
   1/*
   2 * Copyright (c) 2008-2009 Cisco Systems, Inc.  All rights reserved.
   3 * Copyright (c) 2009 Intel Corporation.  All rights reserved.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms and conditions of the GNU General Public License,
   7 * version 2, as published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12 * more details.
  13 *
  14 * You should have received a copy of the GNU General Public License along with
  15 * this program; if not, write to the Free Software Foundation, Inc.,
  16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17 *
  18 * Maintained at www.Open-FCoE.org
  19 */
  20
  21#include <linux/types.h>
  22#include <linux/module.h>
  23#include <linux/kernel.h>
  24#include <linux/list.h>
  25#include <linux/spinlock.h>
  26#include <linux/timer.h>
  27#include <linux/netdevice.h>
  28#include <linux/etherdevice.h>
  29#include <linux/ethtool.h>
  30#include <linux/if_ether.h>
  31#include <linux/if_vlan.h>
  32#include <linux/errno.h>
  33#include <linux/bitops.h>
  34#include <linux/slab.h>
  35#include <net/rtnetlink.h>
  36
  37#include <scsi/fc/fc_els.h>
  38#include <scsi/fc/fc_fs.h>
  39#include <scsi/fc/fc_fip.h>
  40#include <scsi/fc/fc_encaps.h>
  41#include <scsi/fc/fc_fcoe.h>
  42#include <scsi/fc/fc_fcp.h>
  43
  44#include <scsi/libfc.h>
  45#include <scsi/libfcoe.h>
  46
  47#include "libfcoe.h"
  48
  49#define	FCOE_CTLR_MIN_FKA	500		/* min keep alive (mS) */
  50#define	FCOE_CTLR_DEF_FKA	FIP_DEF_FKA	/* default keep alive (mS) */
  51
  52static void fcoe_ctlr_timeout(unsigned long);
  53static void fcoe_ctlr_timer_work(struct work_struct *);
  54static void fcoe_ctlr_recv_work(struct work_struct *);
  55static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
  56
  57static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
  58static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
  59static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
  60static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
  61
 
 
  62static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
  63static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
  64static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
  65static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
  66
  67static const char * const fcoe_ctlr_states[] = {
  68	[FIP_ST_DISABLED] =	"DISABLED",
  69	[FIP_ST_LINK_WAIT] =	"LINK_WAIT",
  70	[FIP_ST_AUTO] =		"AUTO",
  71	[FIP_ST_NON_FIP] =	"NON_FIP",
  72	[FIP_ST_ENABLED] =	"ENABLED",
  73	[FIP_ST_VNMP_START] =	"VNMP_START",
  74	[FIP_ST_VNMP_PROBE1] =	"VNMP_PROBE1",
  75	[FIP_ST_VNMP_PROBE2] =	"VNMP_PROBE2",
  76	[FIP_ST_VNMP_CLAIM] =	"VNMP_CLAIM",
  77	[FIP_ST_VNMP_UP] =	"VNMP_UP",
  78};
  79
  80static const char *fcoe_ctlr_state(enum fip_state state)
  81{
  82	const char *cp = "unknown";
  83
  84	if (state < ARRAY_SIZE(fcoe_ctlr_states))
  85		cp = fcoe_ctlr_states[state];
  86	if (!cp)
  87		cp = "unknown";
  88	return cp;
  89}
  90
  91/**
  92 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
  93 * @fip: The FCoE controller
  94 * @state: The new state
  95 */
  96static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
  97{
  98	if (state == fip->state)
  99		return;
 100	if (fip->lp)
 101		LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
 102			fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
 103	fip->state = state;
 104}
 105
 106/**
 107 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
 108 * @fcf: The FCF to check
 109 *
 110 * Return non-zero if FCF fcoe_size has been validated.
 111 */
 112static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
 113{
 114	return (fcf->flags & FIP_FL_SOL) != 0;
 115}
 116
 117/**
 118 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
 119 * @fcf: The FCF to check
 120 *
 121 * Return non-zero if the FCF is usable.
 122 */
 123static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
 124{
 125	u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
 126
 127	return (fcf->flags & flags) == flags;
 128}
 129
 130/**
 131 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
 132 * @fip: The FCoE controller
 133 */
 134static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
 135{
 136	if (fip->mode == FIP_MODE_VN2VN)
 137		hton24(fip->dest_addr, FIP_VN_FC_MAP);
 138	else
 139		hton24(fip->dest_addr, FIP_DEF_FC_MAP);
 140	hton24(fip->dest_addr + 3, 0);
 141	fip->map_dest = 1;
 142}
 143
 144/**
 145 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
 146 * @fip: The FCoE controller to initialize
 147 */
 148void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
 149{
 150	fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
 151	fip->mode = mode;
 
 152	INIT_LIST_HEAD(&fip->fcfs);
 153	mutex_init(&fip->ctlr_mutex);
 154	spin_lock_init(&fip->ctlr_lock);
 155	fip->flogi_oxid = FC_XID_UNKNOWN;
 156	setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
 157	INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
 158	INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
 159	skb_queue_head_init(&fip->fip_recv_list);
 160}
 161EXPORT_SYMBOL(fcoe_ctlr_init);
 162
 163/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 164 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
 165 * @fip: The FCoE controller whose FCFs are to be reset
 166 *
 167 * Called with &fcoe_ctlr lock held.
 168 */
 169static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
 170{
 171	struct fcoe_fcf *fcf;
 172	struct fcoe_fcf *next;
 173
 174	fip->sel_fcf = NULL;
 175	list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
 176		list_del(&fcf->list);
 177		kfree(fcf);
 178	}
 179	fip->fcf_count = 0;
 
 180	fip->sel_time = 0;
 181}
 182
 183/**
 184 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
 185 * @fip: The FCoE controller to tear down
 186 *
 187 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
 188 *
 189 * The receive handler will have been deleted before this to guarantee
 190 * that no more recv_work will be scheduled.
 191 *
 192 * The timer routine will simply return once we set FIP_ST_DISABLED.
 193 * This guarantees that no further timeouts or work will be scheduled.
 194 */
 195void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
 196{
 197	cancel_work_sync(&fip->recv_work);
 198	skb_queue_purge(&fip->fip_recv_list);
 199
 200	mutex_lock(&fip->ctlr_mutex);
 201	fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
 202	fcoe_ctlr_reset_fcfs(fip);
 203	mutex_unlock(&fip->ctlr_mutex);
 204	del_timer_sync(&fip->timer);
 205	cancel_work_sync(&fip->timer_work);
 206}
 207EXPORT_SYMBOL(fcoe_ctlr_destroy);
 208
 209/**
 210 * fcoe_ctlr_announce() - announce new FCF selection
 211 * @fip: The FCoE controller
 212 *
 213 * Also sets the destination MAC for FCoE and control packets
 214 *
 215 * Called with neither ctlr_mutex nor ctlr_lock held.
 216 */
 217static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
 218{
 219	struct fcoe_fcf *sel;
 220	struct fcoe_fcf *fcf;
 221
 222	mutex_lock(&fip->ctlr_mutex);
 223	spin_lock_bh(&fip->ctlr_lock);
 224
 225	kfree_skb(fip->flogi_req);
 226	fip->flogi_req = NULL;
 227	list_for_each_entry(fcf, &fip->fcfs, list)
 228		fcf->flogi_sent = 0;
 229
 230	spin_unlock_bh(&fip->ctlr_lock);
 231	sel = fip->sel_fcf;
 232
 233	if (sel && !compare_ether_addr(sel->fcf_mac, fip->dest_addr))
 234		goto unlock;
 235	if (!is_zero_ether_addr(fip->dest_addr)) {
 236		printk(KERN_NOTICE "libfcoe: host%d: "
 237		       "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
 238		       fip->lp->host->host_no, fip->dest_addr);
 239		memset(fip->dest_addr, 0, ETH_ALEN);
 240	}
 241	if (sel) {
 242		printk(KERN_INFO "libfcoe: host%d: FIP selected "
 243		       "Fibre-Channel Forwarder MAC %pM\n",
 244		       fip->lp->host->host_no, sel->fcf_mac);
 245		memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
 246		fip->map_dest = 0;
 247	}
 248unlock:
 249	mutex_unlock(&fip->ctlr_mutex);
 250}
 251
 252/**
 253 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
 254 * @fip: The FCoE controller to get the maximum FCoE size from
 255 *
 256 * Returns the maximum packet size including the FCoE header and trailer,
 257 * but not including any Ethernet or VLAN headers.
 258 */
 259static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
 260{
 261	/*
 262	 * Determine the max FCoE frame size allowed, including
 263	 * FCoE header and trailer.
 264	 * Note:  lp->mfs is currently the payload size, not the frame size.
 265	 */
 266	return fip->lp->mfs + sizeof(struct fc_frame_header) +
 267		sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
 268}
 269
 270/**
 271 * fcoe_ctlr_solicit() - Send a FIP solicitation
 272 * @fip: The FCoE controller to send the solicitation on
 273 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
 274 */
 275static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
 276{
 277	struct sk_buff *skb;
 278	struct fip_sol {
 279		struct ethhdr eth;
 280		struct fip_header fip;
 281		struct {
 282			struct fip_mac_desc mac;
 283			struct fip_wwn_desc wwnn;
 284			struct fip_size_desc size;
 285		} __packed desc;
 286	}  __packed * sol;
 287	u32 fcoe_size;
 288
 289	skb = dev_alloc_skb(sizeof(*sol));
 290	if (!skb)
 291		return;
 292
 293	sol = (struct fip_sol *)skb->data;
 294
 295	memset(sol, 0, sizeof(*sol));
 296	memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
 297	memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
 298	sol->eth.h_proto = htons(ETH_P_FIP);
 299
 300	sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
 301	sol->fip.fip_op = htons(FIP_OP_DISC);
 302	sol->fip.fip_subcode = FIP_SC_SOL;
 303	sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
 304	sol->fip.fip_flags = htons(FIP_FL_FPMA);
 305	if (fip->spma)
 306		sol->fip.fip_flags |= htons(FIP_FL_SPMA);
 307
 308	sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
 309	sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
 310	memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
 311
 312	sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
 313	sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
 314	put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
 315
 316	fcoe_size = fcoe_ctlr_fcoe_size(fip);
 317	sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
 318	sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
 319	sol->desc.size.fd_size = htons(fcoe_size);
 320
 321	skb_put(skb, sizeof(*sol));
 322	skb->protocol = htons(ETH_P_FIP);
 
 323	skb_reset_mac_header(skb);
 324	skb_reset_network_header(skb);
 325	fip->send(fip, skb);
 326
 327	if (!fcf)
 328		fip->sol_time = jiffies;
 329}
 330
 331/**
 332 * fcoe_ctlr_link_up() - Start FCoE controller
 333 * @fip: The FCoE controller to start
 334 *
 335 * Called from the LLD when the network link is ready.
 336 */
 337void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
 338{
 339	mutex_lock(&fip->ctlr_mutex);
 340	if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
 341		mutex_unlock(&fip->ctlr_mutex);
 342		fc_linkup(fip->lp);
 343	} else if (fip->state == FIP_ST_LINK_WAIT) {
 344		fcoe_ctlr_set_state(fip, fip->mode);
 
 
 
 345		switch (fip->mode) {
 346		default:
 347			LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
 348			/* fall-through */
 349		case FIP_MODE_AUTO:
 350			LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
 351			/* fall-through */
 352		case FIP_MODE_FABRIC:
 353		case FIP_MODE_NON_FIP:
 354			mutex_unlock(&fip->ctlr_mutex);
 355			fc_linkup(fip->lp);
 356			fcoe_ctlr_solicit(fip, NULL);
 357			break;
 358		case FIP_MODE_VN2VN:
 359			fcoe_ctlr_vn_start(fip);
 360			mutex_unlock(&fip->ctlr_mutex);
 361			fc_linkup(fip->lp);
 362			break;
 363		}
 364	} else
 365		mutex_unlock(&fip->ctlr_mutex);
 366}
 367EXPORT_SYMBOL(fcoe_ctlr_link_up);
 368
 369/**
 370 * fcoe_ctlr_reset() - Reset a FCoE controller
 371 * @fip:       The FCoE controller to reset
 372 */
 373static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
 374{
 375	fcoe_ctlr_reset_fcfs(fip);
 376	del_timer(&fip->timer);
 377	fip->ctlr_ka_time = 0;
 378	fip->port_ka_time = 0;
 379	fip->sol_time = 0;
 380	fip->flogi_oxid = FC_XID_UNKNOWN;
 381	fcoe_ctlr_map_dest(fip);
 382}
 383
 384/**
 385 * fcoe_ctlr_link_down() - Stop a FCoE controller
 386 * @fip: The FCoE controller to be stopped
 387 *
 388 * Returns non-zero if the link was up and now isn't.
 389 *
 390 * Called from the LLD when the network link is not ready.
 391 * There may be multiple calls while the link is down.
 392 */
 393int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
 394{
 395	int link_dropped;
 396
 397	LIBFCOE_FIP_DBG(fip, "link down.\n");
 398	mutex_lock(&fip->ctlr_mutex);
 399	fcoe_ctlr_reset(fip);
 400	link_dropped = fip->state != FIP_ST_LINK_WAIT;
 401	fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
 402	mutex_unlock(&fip->ctlr_mutex);
 403
 404	if (link_dropped)
 405		fc_linkdown(fip->lp);
 406	return link_dropped;
 407}
 408EXPORT_SYMBOL(fcoe_ctlr_link_down);
 409
 410/**
 411 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
 412 * @fip:   The FCoE controller to send the FKA on
 413 * @lport: libfc fc_lport to send from
 414 * @ports: 0 for controller keep-alive, 1 for port keep-alive
 415 * @sa:	   The source MAC address
 416 *
 417 * A controller keep-alive is sent every fka_period (typically 8 seconds).
 418 * The source MAC is the native MAC address.
 419 *
 420 * A port keep-alive is sent every 90 seconds while logged in.
 421 * The source MAC is the assigned mapped source address.
 422 * The destination is the FCF's F-port.
 423 */
 424static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
 425				      struct fc_lport *lport,
 426				      int ports, u8 *sa)
 427{
 428	struct sk_buff *skb;
 429	struct fip_kal {
 430		struct ethhdr eth;
 431		struct fip_header fip;
 432		struct fip_mac_desc mac;
 433	} __packed * kal;
 434	struct fip_vn_desc *vn;
 435	u32 len;
 436	struct fc_lport *lp;
 437	struct fcoe_fcf *fcf;
 438
 439	fcf = fip->sel_fcf;
 440	lp = fip->lp;
 441	if (!fcf || (ports && !lp->port_id))
 442		return;
 443
 444	len = sizeof(*kal) + ports * sizeof(*vn);
 445	skb = dev_alloc_skb(len);
 446	if (!skb)
 447		return;
 448
 449	kal = (struct fip_kal *)skb->data;
 450	memset(kal, 0, len);
 451	memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
 452	memcpy(kal->eth.h_source, sa, ETH_ALEN);
 453	kal->eth.h_proto = htons(ETH_P_FIP);
 454
 455	kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
 456	kal->fip.fip_op = htons(FIP_OP_CTRL);
 457	kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
 458	kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
 459				     ports * sizeof(*vn)) / FIP_BPW);
 460	kal->fip.fip_flags = htons(FIP_FL_FPMA);
 461	if (fip->spma)
 462		kal->fip.fip_flags |= htons(FIP_FL_SPMA);
 463
 464	kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
 465	kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
 466	memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
 467	if (ports) {
 468		vn = (struct fip_vn_desc *)(kal + 1);
 469		vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
 470		vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
 471		memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
 472		hton24(vn->fd_fc_id, lport->port_id);
 473		put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
 474	}
 475	skb_put(skb, len);
 476	skb->protocol = htons(ETH_P_FIP);
 
 477	skb_reset_mac_header(skb);
 478	skb_reset_network_header(skb);
 479	fip->send(fip, skb);
 480}
 481
 482/**
 483 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
 484 * @fip:   The FCoE controller for the ELS frame
 485 * @dtype: The FIP descriptor type for the frame
 486 * @skb:   The FCoE ELS frame including FC header but no FCoE headers
 487 * @d_id:  The destination port ID.
 488 *
 489 * Returns non-zero error code on failure.
 490 *
 491 * The caller must check that the length is a multiple of 4.
 492 *
 493 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
 494 * Headroom includes the FIP encapsulation description, FIP header, and
 495 * Ethernet header.  The tailroom is for the FIP MAC descriptor.
 496 */
 497static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
 498			    u8 dtype, struct sk_buff *skb, u32 d_id)
 499{
 500	struct fip_encaps_head {
 501		struct ethhdr eth;
 502		struct fip_header fip;
 503		struct fip_encaps encaps;
 504	} __packed * cap;
 505	struct fc_frame_header *fh;
 506	struct fip_mac_desc *mac;
 507	struct fcoe_fcf *fcf;
 508	size_t dlen;
 509	u16 fip_flags;
 510	u8 op;
 511
 512	fh = (struct fc_frame_header *)skb->data;
 513	op = *(u8 *)(fh + 1);
 514	dlen = sizeof(struct fip_encaps) + skb->len;	/* len before push */
 515	cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
 516	memset(cap, 0, sizeof(*cap));
 517
 518	if (lport->point_to_multipoint) {
 519		if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
 520			return -ENODEV;
 521		fip_flags = 0;
 522	} else {
 523		fcf = fip->sel_fcf;
 524		if (!fcf)
 525			return -ENODEV;
 526		fip_flags = fcf->flags;
 527		fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
 528					 FIP_FL_FPMA;
 529		if (!fip_flags)
 530			return -ENODEV;
 531		memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
 532	}
 533	memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
 534	cap->eth.h_proto = htons(ETH_P_FIP);
 535
 536	cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
 537	cap->fip.fip_op = htons(FIP_OP_LS);
 538	if (op == ELS_LS_ACC || op == ELS_LS_RJT)
 539		cap->fip.fip_subcode = FIP_SC_REP;
 540	else
 541		cap->fip.fip_subcode = FIP_SC_REQ;
 542	cap->fip.fip_flags = htons(fip_flags);
 543
 544	cap->encaps.fd_desc.fip_dtype = dtype;
 545	cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
 546
 547	if (op != ELS_LS_RJT) {
 548		dlen += sizeof(*mac);
 549		mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
 550		memset(mac, 0, sizeof(*mac));
 551		mac->fd_desc.fip_dtype = FIP_DT_MAC;
 552		mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
 553		if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
 554			memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
 555		} else if (fip->mode == FIP_MODE_VN2VN) {
 556			hton24(mac->fd_mac, FIP_VN_FC_MAP);
 557			hton24(mac->fd_mac + 3, fip->port_id);
 558		} else if (fip_flags & FIP_FL_SPMA) {
 559			LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
 560			memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
 561		} else {
 562			LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
 563			/* FPMA only FLOGI.  Must leave the MAC desc zeroed. */
 564		}
 565	}
 566	cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
 567
 568	skb->protocol = htons(ETH_P_FIP);
 
 569	skb_reset_mac_header(skb);
 570	skb_reset_network_header(skb);
 571	return 0;
 572}
 573
 574/**
 575 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
 576 * @fip:	FCoE controller.
 577 * @lport:	libfc fc_lport to send from
 578 * @skb:	FCoE ELS frame including FC header but no FCoE headers.
 579 *
 580 * Returns a non-zero error code if the frame should not be sent.
 581 * Returns zero if the caller should send the frame with FCoE encapsulation.
 582 *
 583 * The caller must check that the length is a multiple of 4.
 584 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
 585 * The the skb must also be an fc_frame.
 586 *
 587 * This is called from the lower-level driver with spinlocks held,
 588 * so we must not take a mutex here.
 589 */
 590int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
 591		       struct sk_buff *skb)
 592{
 593	struct fc_frame *fp;
 594	struct fc_frame_header *fh;
 595	u16 old_xid;
 596	u8 op;
 597	u8 mac[ETH_ALEN];
 598
 599	fp = container_of(skb, struct fc_frame, skb);
 600	fh = (struct fc_frame_header *)skb->data;
 601	op = *(u8 *)(fh + 1);
 602
 603	if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
 604		old_xid = fip->flogi_oxid;
 605		fip->flogi_oxid = ntohs(fh->fh_ox_id);
 606		if (fip->state == FIP_ST_AUTO) {
 607			if (old_xid == FC_XID_UNKNOWN)
 608				fip->flogi_count = 0;
 609			fip->flogi_count++;
 610			if (fip->flogi_count < 3)
 611				goto drop;
 612			fcoe_ctlr_map_dest(fip);
 613			return 0;
 614		}
 615		if (fip->state == FIP_ST_NON_FIP)
 616			fcoe_ctlr_map_dest(fip);
 617	}
 618
 619	if (fip->state == FIP_ST_NON_FIP)
 620		return 0;
 621	if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
 622		goto drop;
 623	switch (op) {
 624	case ELS_FLOGI:
 625		op = FIP_DT_FLOGI;
 626		if (fip->mode == FIP_MODE_VN2VN)
 627			break;
 628		spin_lock_bh(&fip->ctlr_lock);
 629		kfree_skb(fip->flogi_req);
 630		fip->flogi_req = skb;
 631		fip->flogi_req_send = 1;
 632		spin_unlock_bh(&fip->ctlr_lock);
 633		schedule_work(&fip->timer_work);
 634		return -EINPROGRESS;
 635	case ELS_FDISC:
 636		if (ntoh24(fh->fh_s_id))
 637			return 0;
 638		op = FIP_DT_FDISC;
 639		break;
 640	case ELS_LOGO:
 641		if (fip->mode == FIP_MODE_VN2VN) {
 642			if (fip->state != FIP_ST_VNMP_UP)
 643				return -EINVAL;
 644			if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
 645				return -EINVAL;
 646		} else {
 647			if (fip->state != FIP_ST_ENABLED)
 648				return 0;
 649			if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
 650				return 0;
 651		}
 652		op = FIP_DT_LOGO;
 653		break;
 654	case ELS_LS_ACC:
 655		/*
 656		 * If non-FIP, we may have gotten an SID by accepting an FLOGI
 657		 * from a point-to-point connection.  Switch to using
 658		 * the source mac based on the SID.  The destination
 659		 * MAC in this case would have been set by receiving the
 660		 * FLOGI.
 661		 */
 662		if (fip->state == FIP_ST_NON_FIP) {
 663			if (fip->flogi_oxid == FC_XID_UNKNOWN)
 664				return 0;
 665			fip->flogi_oxid = FC_XID_UNKNOWN;
 666			fc_fcoe_set_mac(mac, fh->fh_d_id);
 667			fip->update_mac(lport, mac);
 668		}
 669		/* fall through */
 670	case ELS_LS_RJT:
 671		op = fr_encaps(fp);
 672		if (op)
 673			break;
 674		return 0;
 675	default:
 676		if (fip->state != FIP_ST_ENABLED &&
 677		    fip->state != FIP_ST_VNMP_UP)
 678			goto drop;
 679		return 0;
 680	}
 681	LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
 682			op, ntoh24(fh->fh_d_id));
 683	if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
 684		goto drop;
 685	fip->send(fip, skb);
 686	return -EINPROGRESS;
 687drop:
 
 
 688	kfree_skb(skb);
 689	return -EINVAL;
 690}
 691EXPORT_SYMBOL(fcoe_ctlr_els_send);
 692
 693/**
 694 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
 695 * @fip: The FCoE controller to free FCFs on
 696 *
 697 * Called with lock held and preemption disabled.
 698 *
 699 * An FCF is considered old if we have missed two advertisements.
 700 * That is, there have been no valid advertisement from it for 2.5
 701 * times its keep-alive period.
 702 *
 703 * In addition, determine the time when an FCF selection can occur.
 704 *
 705 * Also, increment the MissDiscAdvCount when no advertisement is received
 706 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
 707 *
 708 * Returns the time in jiffies for the next call.
 709 */
 710static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
 711{
 712	struct fcoe_fcf *fcf;
 713	struct fcoe_fcf *next;
 714	unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
 715	unsigned long deadline;
 716	unsigned long sel_time = 0;
 717	struct fcoe_dev_stats *stats;
 
 
 
 718
 719	stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
 720
 721	list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
 722		deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
 723		if (fip->sel_fcf == fcf) {
 724			if (time_after(jiffies, deadline)) {
 725				stats->MissDiscAdvCount++;
 726				printk(KERN_INFO "libfcoe: host%d: "
 727				       "Missing Discovery Advertisement "
 728				       "for fab %16.16llx count %lld\n",
 729				       fip->lp->host->host_no, fcf->fabric_name,
 730				       stats->MissDiscAdvCount);
 731			} else if (time_after(next_timer, deadline))
 732				next_timer = deadline;
 733		}
 734
 735		deadline += fcf->fka_period;
 736		if (time_after_eq(jiffies, deadline)) {
 737			if (fip->sel_fcf == fcf)
 738				fip->sel_fcf = NULL;
 
 
 
 
 
 739			list_del(&fcf->list);
 740			WARN_ON(!fip->fcf_count);
 741			fip->fcf_count--;
 742			kfree(fcf);
 743			stats->VLinkFailureCount++;
 744		} else {
 745			if (time_after(next_timer, deadline))
 746				next_timer = deadline;
 747			if (fcoe_ctlr_mtu_valid(fcf) &&
 748			    (!sel_time || time_before(sel_time, fcf->time)))
 749				sel_time = fcf->time;
 750		}
 751	}
 752	put_cpu();
 
 
 
 
 
 
 753	if (sel_time && !fip->sel_fcf && !fip->sel_time) {
 754		sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
 755		fip->sel_time = sel_time;
 756	}
 757
 758	return next_timer;
 759}
 760
 761/**
 762 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
 763 * @fip: The FCoE controller receiving the advertisement
 764 * @skb: The received FIP advertisement frame
 765 * @fcf: The resulting FCF entry
 766 *
 767 * Returns zero on a valid parsed advertisement,
 768 * otherwise returns non zero value.
 769 */
 770static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
 771			       struct sk_buff *skb, struct fcoe_fcf *fcf)
 772{
 773	struct fip_header *fiph;
 774	struct fip_desc *desc = NULL;
 775	struct fip_wwn_desc *wwn;
 776	struct fip_fab_desc *fab;
 777	struct fip_fka_desc *fka;
 778	unsigned long t;
 779	size_t rlen;
 780	size_t dlen;
 781	u32 desc_mask;
 782
 783	memset(fcf, 0, sizeof(*fcf));
 784	fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
 785
 786	fiph = (struct fip_header *)skb->data;
 787	fcf->flags = ntohs(fiph->fip_flags);
 788
 789	/*
 790	 * mask of required descriptors. validating each one clears its bit.
 791	 */
 792	desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
 793			BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
 794
 795	rlen = ntohs(fiph->fip_dl_len) * 4;
 796	if (rlen + sizeof(*fiph) > skb->len)
 797		return -EINVAL;
 798
 799	desc = (struct fip_desc *)(fiph + 1);
 800	while (rlen > 0) {
 801		dlen = desc->fip_dlen * FIP_BPW;
 802		if (dlen < sizeof(*desc) || dlen > rlen)
 803			return -EINVAL;
 804		/* Drop Adv if there are duplicate critical descriptors */
 805		if ((desc->fip_dtype < 32) &&
 806		    !(desc_mask & 1U << desc->fip_dtype)) {
 807			LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
 808					"Descriptors in FIP adv\n");
 809			return -EINVAL;
 810		}
 811		switch (desc->fip_dtype) {
 812		case FIP_DT_PRI:
 813			if (dlen != sizeof(struct fip_pri_desc))
 814				goto len_err;
 815			fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
 816			desc_mask &= ~BIT(FIP_DT_PRI);
 817			break;
 818		case FIP_DT_MAC:
 819			if (dlen != sizeof(struct fip_mac_desc))
 820				goto len_err;
 821			memcpy(fcf->fcf_mac,
 822			       ((struct fip_mac_desc *)desc)->fd_mac,
 823			       ETH_ALEN);
 
 824			if (!is_valid_ether_addr(fcf->fcf_mac)) {
 825				LIBFCOE_FIP_DBG(fip,
 826					"Invalid MAC addr %pM in FIP adv\n",
 827					fcf->fcf_mac);
 828				return -EINVAL;
 829			}
 830			desc_mask &= ~BIT(FIP_DT_MAC);
 831			break;
 832		case FIP_DT_NAME:
 833			if (dlen != sizeof(struct fip_wwn_desc))
 834				goto len_err;
 835			wwn = (struct fip_wwn_desc *)desc;
 836			fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
 837			desc_mask &= ~BIT(FIP_DT_NAME);
 838			break;
 839		case FIP_DT_FAB:
 840			if (dlen != sizeof(struct fip_fab_desc))
 841				goto len_err;
 842			fab = (struct fip_fab_desc *)desc;
 843			fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
 844			fcf->vfid = ntohs(fab->fd_vfid);
 845			fcf->fc_map = ntoh24(fab->fd_map);
 846			desc_mask &= ~BIT(FIP_DT_FAB);
 847			break;
 848		case FIP_DT_FKA:
 849			if (dlen != sizeof(struct fip_fka_desc))
 850				goto len_err;
 851			fka = (struct fip_fka_desc *)desc;
 852			if (fka->fd_flags & FIP_FKA_ADV_D)
 853				fcf->fd_flags = 1;
 854			t = ntohl(fka->fd_fka_period);
 855			if (t >= FCOE_CTLR_MIN_FKA)
 856				fcf->fka_period = msecs_to_jiffies(t);
 857			desc_mask &= ~BIT(FIP_DT_FKA);
 858			break;
 859		case FIP_DT_MAP_OUI:
 860		case FIP_DT_FCOE_SIZE:
 861		case FIP_DT_FLOGI:
 862		case FIP_DT_FDISC:
 863		case FIP_DT_LOGO:
 864		case FIP_DT_ELP:
 865		default:
 866			LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
 867					"in FIP adv\n", desc->fip_dtype);
 868			/* standard says ignore unknown descriptors >= 128 */
 869			if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
 870				return -EINVAL;
 871			break;
 872		}
 873		desc = (struct fip_desc *)((char *)desc + dlen);
 874		rlen -= dlen;
 875	}
 876	if (!fcf->fc_map || (fcf->fc_map & 0x10000))
 877		return -EINVAL;
 878	if (!fcf->switch_name)
 879		return -EINVAL;
 880	if (desc_mask) {
 881		LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
 882				desc_mask);
 883		return -EINVAL;
 884	}
 885	return 0;
 886
 887len_err:
 888	LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
 889			desc->fip_dtype, dlen);
 890	return -EINVAL;
 891}
 892
 893/**
 894 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
 895 * @fip: The FCoE controller receiving the advertisement
 896 * @skb: The received FIP packet
 897 */
 898static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
 899{
 900	struct fcoe_fcf *fcf;
 901	struct fcoe_fcf new;
 902	struct fcoe_fcf *found;
 903	unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
 904	int first = 0;
 905	int mtu_valid;
 
 
 906
 907	if (fcoe_ctlr_parse_adv(fip, skb, &new))
 908		return;
 909
 910	mutex_lock(&fip->ctlr_mutex);
 911	first = list_empty(&fip->fcfs);
 912	found = NULL;
 913	list_for_each_entry(fcf, &fip->fcfs, list) {
 914		if (fcf->switch_name == new.switch_name &&
 915		    fcf->fabric_name == new.fabric_name &&
 916		    fcf->fc_map == new.fc_map &&
 917		    compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
 918			found = fcf;
 919			break;
 920		}
 921	}
 922	if (!found) {
 923		if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
 924			goto out;
 925
 926		fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
 927		if (!fcf)
 928			goto out;
 929
 930		fip->fcf_count++;
 931		memcpy(fcf, &new, sizeof(new));
 932		list_add(&fcf->list, &fip->fcfs);
 
 
 
 
 
 
 
 
 933	} else {
 934		/*
 935		 * Update the FCF's keep-alive descriptor flags.
 936		 * Other flag changes from new advertisements are
 937		 * ignored after a solicited advertisement is
 938		 * received and the FCF is selectable (usable).
 939		 */
 940		fcf->fd_flags = new.fd_flags;
 941		if (!fcoe_ctlr_fcf_usable(fcf))
 942			fcf->flags = new.flags;
 943
 944		if (fcf == fip->sel_fcf && !fcf->fd_flags) {
 945			fip->ctlr_ka_time -= fcf->fka_period;
 946			fip->ctlr_ka_time += new.fka_period;
 947			if (time_before(fip->ctlr_ka_time, fip->timer.expires))
 948				mod_timer(&fip->timer, fip->ctlr_ka_time);
 949		}
 950		fcf->fka_period = new.fka_period;
 951		memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
 952	}
 
 953	mtu_valid = fcoe_ctlr_mtu_valid(fcf);
 954	fcf->time = jiffies;
 955	if (!found)
 956		LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
 957				fcf->fabric_name, fcf->fcf_mac);
 958
 959	/*
 960	 * If this advertisement is not solicited and our max receive size
 961	 * hasn't been verified, send a solicited advertisement.
 962	 */
 963	if (!mtu_valid)
 964		fcoe_ctlr_solicit(fip, fcf);
 965
 966	/*
 967	 * If its been a while since we did a solicit, and this is
 968	 * the first advertisement we've received, do a multicast
 969	 * solicitation to gather as many advertisements as we can
 970	 * before selection occurs.
 971	 */
 972	if (first && time_after(jiffies, fip->sol_time + sol_tov))
 973		fcoe_ctlr_solicit(fip, NULL);
 974
 975	/*
 976	 * Put this FCF at the head of the list for priority among equals.
 977	 * This helps in the case of an NPV switch which insists we use
 978	 * the FCF that answers multicast solicitations, not the others that
 979	 * are sending periodic multicast advertisements.
 980	 */
 981	if (mtu_valid)
 982		list_move(&fcf->list, &fip->fcfs);
 983
 984	/*
 985	 * If this is the first validated FCF, note the time and
 986	 * set a timer to trigger selection.
 987	 */
 988	if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
 
 989		fip->sel_time = jiffies +
 990			msecs_to_jiffies(FCOE_CTLR_START_DELAY);
 991		if (!timer_pending(&fip->timer) ||
 992		    time_before(fip->sel_time, fip->timer.expires))
 993			mod_timer(&fip->timer, fip->sel_time);
 994	}
 
 995out:
 996	mutex_unlock(&fip->ctlr_mutex);
 997}
 998
 999/**
1000 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1001 * @fip: The FCoE controller which received the packet
1002 * @skb: The received FIP packet
1003 */
1004static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1005{
1006	struct fc_lport *lport = fip->lp;
1007	struct fip_header *fiph;
1008	struct fc_frame *fp = (struct fc_frame *)skb;
1009	struct fc_frame_header *fh = NULL;
1010	struct fip_desc *desc;
1011	struct fip_encaps *els;
1012	struct fcoe_dev_stats *stats;
 
1013	enum fip_desc_type els_dtype = 0;
1014	u8 els_op;
1015	u8 sub;
1016	u8 granted_mac[ETH_ALEN] = { 0 };
1017	size_t els_len = 0;
1018	size_t rlen;
1019	size_t dlen;
1020	u32 desc_mask = 0;
1021	u32 desc_cnt = 0;
1022
1023	fiph = (struct fip_header *)skb->data;
1024	sub = fiph->fip_subcode;
1025	if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1026		goto drop;
1027
1028	rlen = ntohs(fiph->fip_dl_len) * 4;
1029	if (rlen + sizeof(*fiph) > skb->len)
1030		goto drop;
1031
1032	desc = (struct fip_desc *)(fiph + 1);
1033	while (rlen > 0) {
1034		desc_cnt++;
1035		dlen = desc->fip_dlen * FIP_BPW;
1036		if (dlen < sizeof(*desc) || dlen > rlen)
1037			goto drop;
1038		/* Drop ELS if there are duplicate critical descriptors */
1039		if (desc->fip_dtype < 32) {
1040			if (desc_mask & 1U << desc->fip_dtype) {
 
1041				LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1042						"Descriptors in FIP ELS\n");
1043				goto drop;
1044			}
1045			desc_mask |= (1 << desc->fip_dtype);
1046		}
1047		switch (desc->fip_dtype) {
1048		case FIP_DT_MAC:
 
1049			if (desc_cnt == 1) {
1050				LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1051						"received out of order\n");
1052				goto drop;
1053			}
 
 
 
 
 
 
 
 
 
 
 
 
1054
1055			if (dlen != sizeof(struct fip_mac_desc))
1056				goto len_err;
1057			memcpy(granted_mac,
1058			       ((struct fip_mac_desc *)desc)->fd_mac,
1059			       ETH_ALEN);
 
 
1060			break;
1061		case FIP_DT_FLOGI:
1062		case FIP_DT_FDISC:
1063		case FIP_DT_LOGO:
1064		case FIP_DT_ELP:
1065			if (desc_cnt != 1) {
1066				LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1067						"received out of order\n");
1068				goto drop;
1069			}
1070			if (fh)
1071				goto drop;
1072			if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1073				goto len_err;
1074			els_len = dlen - sizeof(*els);
1075			els = (struct fip_encaps *)desc;
1076			fh = (struct fc_frame_header *)(els + 1);
1077			els_dtype = desc->fip_dtype;
1078			break;
1079		default:
1080			LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1081					"in FIP adv\n", desc->fip_dtype);
1082			/* standard says ignore unknown descriptors >= 128 */
1083			if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1084				goto drop;
1085			if (desc_cnt <= 2) {
1086				LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1087						"received out of order\n");
1088				goto drop;
1089			}
1090			break;
1091		}
1092		desc = (struct fip_desc *)((char *)desc + dlen);
1093		rlen -= dlen;
1094	}
1095
1096	if (!fh)
1097		goto drop;
1098	els_op = *(u8 *)(fh + 1);
1099
1100	if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1101	    sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1102		if (els_op == ELS_LS_ACC) {
1103			if (!is_valid_ether_addr(granted_mac)) {
1104				LIBFCOE_FIP_DBG(fip,
1105					"Invalid MAC address %pM in FIP ELS\n",
1106					granted_mac);
1107				goto drop;
1108			}
1109			memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1110
1111			if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1112				fip->flogi_oxid = FC_XID_UNKNOWN;
1113				if (els_dtype == FIP_DT_FLOGI)
1114					fcoe_ctlr_announce(fip);
1115			}
1116		} else if (els_dtype == FIP_DT_FLOGI &&
1117			   !fcoe_ctlr_flogi_retry(fip))
1118			goto drop;	/* retrying FLOGI so drop reject */
1119	}
1120
1121	if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1122	    (!(1U << FIP_DT_MAC & desc_mask)))) {
1123		LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1124				"in FIP ELS\n");
1125		goto drop;
1126	}
1127
1128	/*
1129	 * Convert skb into an fc_frame containing only the ELS.
1130	 */
1131	skb_pull(skb, (u8 *)fh - skb->data);
1132	skb_trim(skb, els_len);
1133	fp = (struct fc_frame *)skb;
1134	fc_frame_init(fp);
1135	fr_sof(fp) = FC_SOF_I3;
1136	fr_eof(fp) = FC_EOF_T;
1137	fr_dev(fp) = lport;
1138	fr_encaps(fp) = els_dtype;
1139
1140	stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1141	stats->RxFrames++;
1142	stats->RxWords += skb->len / FIP_BPW;
1143	put_cpu();
1144
1145	fc_exch_recv(lport, fp);
1146	return;
1147
1148len_err:
1149	LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1150			desc->fip_dtype, dlen);
1151drop:
1152	kfree_skb(skb);
1153}
1154
1155/**
1156 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1157 * @fip: The FCoE controller that received the frame
1158 * @fh:	 The received FIP header
1159 *
1160 * There may be multiple VN_Port descriptors.
1161 * The overall length has already been checked.
1162 */
1163static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1164				     struct fip_header *fh)
1165{
1166	struct fip_desc *desc;
1167	struct fip_mac_desc *mp;
1168	struct fip_wwn_desc *wp;
1169	struct fip_vn_desc *vp;
1170	size_t rlen;
1171	size_t dlen;
1172	struct fcoe_fcf *fcf = fip->sel_fcf;
1173	struct fc_lport *lport = fip->lp;
1174	struct fc_lport *vn_port = NULL;
1175	u32 desc_mask;
1176	int num_vlink_desc;
1177	int reset_phys_port = 0;
1178	struct fip_vn_desc **vlink_desc_arr = NULL;
 
 
1179
1180	LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1181
1182	if (!fcf || !lport->port_id)
 
 
 
 
 
 
 
 
 
1183		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1184
1185	/*
1186	 * mask of required descriptors.  Validating each one clears its bit.
1187	 */
1188	desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
1189
1190	rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1191	desc = (struct fip_desc *)(fh + 1);
1192
1193	/*
1194	 * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1195	 * before determining max Vx_Port descriptor but a buggy FCF could have
1196	 * omited either or both MAC Address and Name Identifier descriptors
1197	 */
1198	num_vlink_desc = rlen / sizeof(*vp);
1199	if (num_vlink_desc)
1200		vlink_desc_arr = kmalloc(sizeof(vp) * num_vlink_desc,
1201					 GFP_ATOMIC);
1202	if (!vlink_desc_arr)
1203		return;
1204	num_vlink_desc = 0;
1205
1206	while (rlen >= sizeof(*desc)) {
1207		dlen = desc->fip_dlen * FIP_BPW;
1208		if (dlen > rlen)
1209			goto err;
1210		/* Drop CVL if there are duplicate critical descriptors */
1211		if ((desc->fip_dtype < 32) &&
1212		    (desc->fip_dtype != FIP_DT_VN_ID) &&
1213		    !(desc_mask & 1U << desc->fip_dtype)) {
1214			LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1215					"Descriptors in FIP CVL\n");
1216			goto err;
1217		}
1218		switch (desc->fip_dtype) {
1219		case FIP_DT_MAC:
1220			mp = (struct fip_mac_desc *)desc;
1221			if (dlen < sizeof(*mp))
1222				goto err;
1223			if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
1224				goto err;
1225			desc_mask &= ~BIT(FIP_DT_MAC);
1226			break;
1227		case FIP_DT_NAME:
1228			wp = (struct fip_wwn_desc *)desc;
1229			if (dlen < sizeof(*wp))
1230				goto err;
1231			if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1232				goto err;
1233			desc_mask &= ~BIT(FIP_DT_NAME);
1234			break;
1235		case FIP_DT_VN_ID:
1236			vp = (struct fip_vn_desc *)desc;
1237			if (dlen < sizeof(*vp))
1238				goto err;
1239			vlink_desc_arr[num_vlink_desc++] = vp;
1240			vn_port = fc_vport_id_lookup(lport,
1241						      ntoh24(vp->fd_fc_id));
1242			if (vn_port && (vn_port == lport)) {
1243				mutex_lock(&fip->ctlr_mutex);
1244				per_cpu_ptr(lport->dev_stats,
1245					    get_cpu())->VLinkFailureCount++;
1246				put_cpu();
1247				fcoe_ctlr_reset(fip);
1248				mutex_unlock(&fip->ctlr_mutex);
1249			}
1250			break;
1251		default:
1252			/* standard says ignore unknown descriptors >= 128 */
1253			if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1254				goto err;
1255			break;
1256		}
1257		desc = (struct fip_desc *)((char *)desc + dlen);
1258		rlen -= dlen;
1259	}
1260
1261	/*
1262	 * reset only if all required descriptors were present and valid.
1263	 */
1264	if (desc_mask)
1265		LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1266				desc_mask);
1267	else if (!num_vlink_desc) {
1268		LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1269		/*
1270		 * No Vx_Port description. Clear all NPIV ports,
1271		 * followed by physical port
1272		 */
1273		mutex_lock(&lport->lp_mutex);
1274		list_for_each_entry(vn_port, &lport->vports, list)
1275			fc_lport_reset(vn_port);
1276		mutex_unlock(&lport->lp_mutex);
1277
1278		mutex_lock(&fip->ctlr_mutex);
1279		per_cpu_ptr(lport->dev_stats,
1280			    get_cpu())->VLinkFailureCount++;
1281		put_cpu();
1282		fcoe_ctlr_reset(fip);
1283		mutex_unlock(&fip->ctlr_mutex);
1284
 
 
 
 
 
1285		fc_lport_reset(fip->lp);
1286		fcoe_ctlr_solicit(fip, NULL);
1287	} else {
1288		int i;
1289
1290		LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1291		for (i = 0; i < num_vlink_desc; i++) {
1292			vp = vlink_desc_arr[i];
1293			vn_port = fc_vport_id_lookup(lport,
1294						     ntoh24(vp->fd_fc_id));
1295			if (!vn_port)
1296				continue;
1297
1298			/*
1299			 * 'port_id' is already validated, check MAC address and
1300			 * wwpn
1301			 */
1302			if (compare_ether_addr(fip->get_src_addr(vn_port),
1303						vp->fd_mac) != 0 ||
1304				get_unaligned_be64(&vp->fd_wwpn) !=
1305							vn_port->wwpn)
1306				continue;
1307
1308			if (vn_port == lport)
1309				/*
1310				 * Physical port, defer processing till all
1311				 * listed NPIV ports are cleared
1312				 */
1313				reset_phys_port = 1;
1314			else    /* NPIV port */
1315				fc_lport_reset(vn_port);
1316		}
1317
1318		if (reset_phys_port) {
1319			fc_lport_reset(fip->lp);
1320			fcoe_ctlr_solicit(fip, NULL);
1321		}
1322	}
1323
1324err:
1325	kfree(vlink_desc_arr);
1326}
1327
1328/**
1329 * fcoe_ctlr_recv() - Receive a FIP packet
1330 * @fip: The FCoE controller that received the packet
1331 * @skb: The received FIP packet
1332 *
1333 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1334 */
1335void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1336{
 
 
 
1337	skb_queue_tail(&fip->fip_recv_list, skb);
1338	schedule_work(&fip->recv_work);
1339}
1340EXPORT_SYMBOL(fcoe_ctlr_recv);
1341
1342/**
1343 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1344 * @fip: The FCoE controller that received the frame
1345 * @skb: The received FIP frame
1346 *
1347 * Returns non-zero if the frame is dropped.
1348 */
1349static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1350{
1351	struct fip_header *fiph;
1352	struct ethhdr *eh;
1353	enum fip_state state;
 
1354	u16 op;
1355	u8 sub;
1356
1357	if (skb_linearize(skb))
1358		goto drop;
1359	if (skb->len < sizeof(*fiph))
1360		goto drop;
1361	eh = eth_hdr(skb);
1362	if (fip->mode == FIP_MODE_VN2VN) {
1363		if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1364		    compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1365		    compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1366			goto drop;
1367	} else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1368		   compare_ether_addr(eh->h_dest, fcoe_all_enode))
1369		goto drop;
1370	fiph = (struct fip_header *)skb->data;
1371	op = ntohs(fiph->fip_op);
1372	sub = fiph->fip_subcode;
1373
1374	if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1375		goto drop;
1376	if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1377		goto drop;
1378
1379	mutex_lock(&fip->ctlr_mutex);
1380	state = fip->state;
1381	if (state == FIP_ST_AUTO) {
1382		fip->map_dest = 0;
1383		fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1384		state = FIP_ST_ENABLED;
1385		LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1386	}
 
1387	mutex_unlock(&fip->ctlr_mutex);
1388
1389	if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1390		return fcoe_ctlr_vn_recv(fip, skb);
1391
 
 
 
 
 
1392	if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1393	    state != FIP_ST_VNMP_CLAIM)
1394		goto drop;
1395
1396	if (op == FIP_OP_LS) {
1397		fcoe_ctlr_recv_els(fip, skb);	/* consumes skb */
1398		return 0;
1399	}
1400
1401	if (state != FIP_ST_ENABLED)
1402		goto drop;
1403
1404	if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1405		fcoe_ctlr_recv_adv(fip, skb);
1406	else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1407		fcoe_ctlr_recv_clr_vlink(fip, fiph);
1408	kfree_skb(skb);
1409	return 0;
1410drop:
1411	kfree_skb(skb);
1412	return -1;
1413}
1414
1415/**
1416 * fcoe_ctlr_select() - Select the best FCF (if possible)
1417 * @fip: The FCoE controller
1418 *
1419 * Returns the selected FCF, or NULL if none are usable.
1420 *
1421 * If there are conflicting advertisements, no FCF can be chosen.
1422 *
1423 * If there is already a selected FCF, this will choose a better one or
1424 * an equivalent one that hasn't already been sent a FLOGI.
1425 *
1426 * Called with lock held.
1427 */
1428static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
1429{
1430	struct fcoe_fcf *fcf;
1431	struct fcoe_fcf *best = fip->sel_fcf;
1432	struct fcoe_fcf *first;
1433
1434	first = list_first_entry(&fip->fcfs, struct fcoe_fcf, list);
1435
1436	list_for_each_entry(fcf, &fip->fcfs, list) {
1437		LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1438				"VFID %d mac %pM map %x val %d "
1439				"sent %u pri %u\n",
1440				fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1441				fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1442				fcf->flogi_sent, fcf->pri);
1443		if (fcf->fabric_name != first->fabric_name ||
1444		    fcf->vfid != first->vfid ||
1445		    fcf->fc_map != first->fc_map) {
1446			LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1447					"or FC-MAP\n");
1448			return NULL;
1449		}
1450		if (fcf->flogi_sent)
1451			continue;
1452		if (!fcoe_ctlr_fcf_usable(fcf)) {
1453			LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1454					"map %x %svalid %savailable\n",
1455					fcf->fabric_name, fcf->fc_map,
1456					(fcf->flags & FIP_FL_SOL) ? "" : "in",
1457					(fcf->flags & FIP_FL_AVAIL) ?
1458					"" : "un");
1459			continue;
1460		}
1461		if (!best || fcf->pri < best->pri || best->flogi_sent)
1462			best = fcf;
 
 
 
 
 
 
 
1463	}
1464	fip->sel_fcf = best;
1465	if (best) {
1466		LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
1467		fip->port_ka_time = jiffies +
1468			msecs_to_jiffies(FIP_VN_KA_PERIOD);
1469		fip->ctlr_ka_time = jiffies + best->fka_period;
1470		if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1471			mod_timer(&fip->timer, fip->ctlr_ka_time);
1472	}
1473	return best;
1474}
1475
1476/**
1477 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1478 * @fip: The FCoE controller
1479 *
1480 * Returns non-zero error if it could not be sent.
1481 *
1482 * Called with ctlr_mutex and ctlr_lock held.
1483 * Caller must verify that fip->sel_fcf is not NULL.
1484 */
1485static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1486{
1487	struct sk_buff *skb;
1488	struct sk_buff *skb_orig;
1489	struct fc_frame_header *fh;
1490	int error;
1491
1492	skb_orig = fip->flogi_req;
1493	if (!skb_orig)
1494		return -EINVAL;
1495
1496	/*
1497	 * Clone and send the FLOGI request.  If clone fails, use original.
1498	 */
1499	skb = skb_clone(skb_orig, GFP_ATOMIC);
1500	if (!skb) {
1501		skb = skb_orig;
1502		fip->flogi_req = NULL;
1503	}
1504	fh = (struct fc_frame_header *)skb->data;
1505	error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1506				 ntoh24(fh->fh_d_id));
1507	if (error) {
1508		kfree_skb(skb);
1509		return error;
1510	}
1511	fip->send(fip, skb);
1512	fip->sel_fcf->flogi_sent = 1;
1513	return 0;
1514}
1515
1516/**
1517 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1518 * @fip: The FCoE controller
1519 *
1520 * Returns non-zero error code if there's no FLOGI request to retry or
1521 * no alternate FCF available.
1522 */
1523static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1524{
1525	struct fcoe_fcf *fcf;
1526	int error;
1527
1528	mutex_lock(&fip->ctlr_mutex);
1529	spin_lock_bh(&fip->ctlr_lock);
1530	LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1531	fcf = fcoe_ctlr_select(fip);
1532	if (!fcf || fcf->flogi_sent) {
1533		kfree_skb(fip->flogi_req);
1534		fip->flogi_req = NULL;
1535		error = -ENOENT;
1536	} else {
1537		fcoe_ctlr_solicit(fip, NULL);
1538		error = fcoe_ctlr_flogi_send_locked(fip);
1539	}
1540	spin_unlock_bh(&fip->ctlr_lock);
1541	mutex_unlock(&fip->ctlr_mutex);
1542	return error;
1543}
1544
1545
1546/**
1547 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1548 * @fip: The FCoE controller that timed out
1549 *
1550 * Done here because fcoe_ctlr_els_send() can't get mutex.
1551 *
1552 * Called with ctlr_mutex held.  The caller must not hold ctlr_lock.
1553 */
1554static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1555{
1556	struct fcoe_fcf *fcf;
1557
1558	spin_lock_bh(&fip->ctlr_lock);
1559	fcf = fip->sel_fcf;
1560	if (!fcf || !fip->flogi_req_send)
1561		goto unlock;
1562
1563	LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1564
1565	/*
1566	 * If this FLOGI is being sent due to a timeout retry
1567	 * to the same FCF as before, select a different FCF if possible.
1568	 */
1569	if (fcf->flogi_sent) {
1570		LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1571		fcf = fcoe_ctlr_select(fip);
1572		if (!fcf || fcf->flogi_sent) {
1573			LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1574			list_for_each_entry(fcf, &fip->fcfs, list)
1575				fcf->flogi_sent = 0;
1576			fcf = fcoe_ctlr_select(fip);
1577		}
1578	}
1579	if (fcf) {
1580		fcoe_ctlr_flogi_send_locked(fip);
1581		fip->flogi_req_send = 0;
1582	} else /* XXX */
1583		LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1584unlock:
1585	spin_unlock_bh(&fip->ctlr_lock);
1586}
1587
1588/**
1589 * fcoe_ctlr_timeout() - FIP timeout handler
1590 * @arg: The FCoE controller that timed out
1591 */
1592static void fcoe_ctlr_timeout(unsigned long arg)
1593{
1594	struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1595
1596	schedule_work(&fip->timer_work);
1597}
1598
1599/**
1600 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1601 * @work: Handle to a FCoE controller
1602 *
1603 * Ages FCFs.  Triggers FCF selection if possible.
1604 * Sends keep-alives and resets.
1605 */
1606static void fcoe_ctlr_timer_work(struct work_struct *work)
1607{
1608	struct fcoe_ctlr *fip;
1609	struct fc_lport *vport;
1610	u8 *mac;
1611	u8 reset = 0;
1612	u8 send_ctlr_ka = 0;
1613	u8 send_port_ka = 0;
1614	struct fcoe_fcf *sel;
1615	struct fcoe_fcf *fcf;
1616	unsigned long next_timer;
1617
1618	fip = container_of(work, struct fcoe_ctlr, timer_work);
1619	if (fip->mode == FIP_MODE_VN2VN)
1620		return fcoe_ctlr_vn_timeout(fip);
1621	mutex_lock(&fip->ctlr_mutex);
1622	if (fip->state == FIP_ST_DISABLED) {
1623		mutex_unlock(&fip->ctlr_mutex);
1624		return;
1625	}
1626
1627	fcf = fip->sel_fcf;
1628	next_timer = fcoe_ctlr_age_fcfs(fip);
1629
1630	sel = fip->sel_fcf;
1631	if (!sel && fip->sel_time) {
1632		if (time_after_eq(jiffies, fip->sel_time)) {
1633			sel = fcoe_ctlr_select(fip);
1634			fip->sel_time = 0;
1635		} else if (time_after(next_timer, fip->sel_time))
1636			next_timer = fip->sel_time;
1637	}
1638
1639	if (sel && fip->flogi_req_send)
1640		fcoe_ctlr_flogi_send(fip);
1641	else if (!sel && fcf)
1642		reset = 1;
1643
1644	if (sel && !sel->fd_flags) {
1645		if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1646			fip->ctlr_ka_time = jiffies + sel->fka_period;
1647			send_ctlr_ka = 1;
1648		}
1649		if (time_after(next_timer, fip->ctlr_ka_time))
1650			next_timer = fip->ctlr_ka_time;
1651
1652		if (time_after_eq(jiffies, fip->port_ka_time)) {
1653			fip->port_ka_time = jiffies +
1654				msecs_to_jiffies(FIP_VN_KA_PERIOD);
1655			send_port_ka = 1;
1656		}
1657		if (time_after(next_timer, fip->port_ka_time))
1658			next_timer = fip->port_ka_time;
1659	}
1660	if (!list_empty(&fip->fcfs))
1661		mod_timer(&fip->timer, next_timer);
1662	mutex_unlock(&fip->ctlr_mutex);
1663
1664	if (reset) {
1665		fc_lport_reset(fip->lp);
1666		/* restart things with a solicitation */
1667		fcoe_ctlr_solicit(fip, NULL);
1668	}
1669
1670	if (send_ctlr_ka)
1671		fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1672
1673	if (send_port_ka) {
1674		mutex_lock(&fip->lp->lp_mutex);
1675		mac = fip->get_src_addr(fip->lp);
1676		fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1677		list_for_each_entry(vport, &fip->lp->vports, list) {
1678			mac = fip->get_src_addr(vport);
1679			fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1680		}
1681		mutex_unlock(&fip->lp->lp_mutex);
1682	}
1683}
1684
1685/**
1686 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1687 * @recv_work: Handle to a FCoE controller
1688 */
1689static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1690{
1691	struct fcoe_ctlr *fip;
1692	struct sk_buff *skb;
1693
1694	fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1695	while ((skb = skb_dequeue(&fip->fip_recv_list)))
1696		fcoe_ctlr_recv_handler(fip, skb);
1697}
1698
1699/**
1700 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1701 * @fip: The FCoE controller
1702 * @fp:	 The FC frame to snoop
1703 *
1704 * Snoop potential response to FLOGI or even incoming FLOGI.
1705 *
1706 * The caller has checked that we are waiting for login as indicated
1707 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1708 *
1709 * The caller is responsible for freeing the frame.
1710 * Fill in the granted_mac address.
1711 *
1712 * Return non-zero if the frame should not be delivered to libfc.
1713 */
1714int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1715			 struct fc_frame *fp)
1716{
1717	struct fc_frame_header *fh;
1718	u8 op;
1719	u8 *sa;
1720
1721	sa = eth_hdr(&fp->skb)->h_source;
1722	fh = fc_frame_header_get(fp);
1723	if (fh->fh_type != FC_TYPE_ELS)
1724		return 0;
1725
1726	op = fc_frame_payload_op(fp);
1727	if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1728	    fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1729
1730		mutex_lock(&fip->ctlr_mutex);
1731		if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1732			mutex_unlock(&fip->ctlr_mutex);
1733			return -EINVAL;
1734		}
1735		fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1736		LIBFCOE_FIP_DBG(fip,
1737				"received FLOGI LS_ACC using non-FIP mode\n");
1738
1739		/*
1740		 * FLOGI accepted.
1741		 * If the src mac addr is FC_OUI-based, then we mark the
1742		 * address_mode flag to use FC_OUI-based Ethernet DA.
1743		 * Otherwise we use the FCoE gateway addr
1744		 */
1745		if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1746			fcoe_ctlr_map_dest(fip);
1747		} else {
1748			memcpy(fip->dest_addr, sa, ETH_ALEN);
1749			fip->map_dest = 0;
1750		}
1751		fip->flogi_oxid = FC_XID_UNKNOWN;
1752		mutex_unlock(&fip->ctlr_mutex);
1753		fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1754	} else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1755		/*
1756		 * Save source MAC for point-to-point responses.
1757		 */
1758		mutex_lock(&fip->ctlr_mutex);
1759		if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1760			memcpy(fip->dest_addr, sa, ETH_ALEN);
1761			fip->map_dest = 0;
1762			if (fip->state == FIP_ST_AUTO)
1763				LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1764						"Setting non-FIP mode\n");
1765			fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1766		}
1767		mutex_unlock(&fip->ctlr_mutex);
1768	}
1769	return 0;
1770}
1771EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1772
1773/**
1774 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1775 * @mac:    The MAC address to convert
1776 * @scheme: The scheme to use when converting
1777 * @port:   The port indicator for converting
1778 *
1779 * Returns: u64 fc world wide name
1780 */
1781u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1782		      unsigned int scheme, unsigned int port)
1783{
1784	u64 wwn;
1785	u64 host_mac;
1786
1787	/* The MAC is in NO, so flip only the low 48 bits */
1788	host_mac = ((u64) mac[0] << 40) |
1789		((u64) mac[1] << 32) |
1790		((u64) mac[2] << 24) |
1791		((u64) mac[3] << 16) |
1792		((u64) mac[4] << 8) |
1793		(u64) mac[5];
1794
1795	WARN_ON(host_mac >= (1ULL << 48));
1796	wwn = host_mac | ((u64) scheme << 60);
1797	switch (scheme) {
1798	case 1:
1799		WARN_ON(port != 0);
1800		break;
1801	case 2:
1802		WARN_ON(port >= 0xfff);
1803		wwn |= (u64) port << 48;
1804		break;
1805	default:
1806		WARN_ON(1);
1807		break;
1808	}
1809
1810	return wwn;
1811}
1812EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1813
1814/**
1815 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1816 * @rdata: libfc remote port
1817 */
1818static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1819{
1820	return (struct fcoe_rport *)(rdata + 1);
1821}
1822
1823/**
1824 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1825 * @fip: The FCoE controller
1826 * @sub: sub-opcode for probe request, reply, or advertisement.
1827 * @dest: The destination Ethernet MAC address
1828 * @min_len: minimum size of the Ethernet payload to be sent
1829 */
1830static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1831			      enum fip_vn2vn_subcode sub,
1832			      const u8 *dest, size_t min_len)
1833{
1834	struct sk_buff *skb;
1835	struct fip_frame {
1836		struct ethhdr eth;
1837		struct fip_header fip;
1838		struct fip_mac_desc mac;
1839		struct fip_wwn_desc wwnn;
1840		struct fip_vn_desc vn;
1841	} __packed * frame;
1842	struct fip_fc4_feat *ff;
1843	struct fip_size_desc *size;
1844	u32 fcp_feat;
1845	size_t len;
1846	size_t dlen;
1847
1848	len = sizeof(*frame);
1849	dlen = 0;
1850	if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1851		dlen = sizeof(struct fip_fc4_feat) +
1852		       sizeof(struct fip_size_desc);
1853		len += dlen;
1854	}
1855	dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1856	len = max(len, min_len + sizeof(struct ethhdr));
1857
1858	skb = dev_alloc_skb(len);
1859	if (!skb)
1860		return;
1861
1862	frame = (struct fip_frame *)skb->data;
1863	memset(frame, 0, len);
1864	memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1865	memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
 
 
 
 
 
 
1866	frame->eth.h_proto = htons(ETH_P_FIP);
1867
1868	frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1869	frame->fip.fip_op = htons(FIP_OP_VN2VN);
1870	frame->fip.fip_subcode = sub;
1871	frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1872
1873	frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1874	frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1875	memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1876
1877	frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1878	frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1879	put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1880
1881	frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
1882	frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
1883	hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
1884	hton24(frame->vn.fd_mac + 3, fip->port_id);
1885	hton24(frame->vn.fd_fc_id, fip->port_id);
1886	put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
1887
1888	/*
1889	 * For claims, add FC-4 features.
1890	 * TBD: Add interface to get fc-4 types and features from libfc.
1891	 */
1892	if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1893		ff = (struct fip_fc4_feat *)(frame + 1);
1894		ff->fd_desc.fip_dtype = FIP_DT_FC4F;
1895		ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
1896		ff->fd_fts = fip->lp->fcts;
1897
1898		fcp_feat = 0;
1899		if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
1900			fcp_feat |= FCP_FEAT_INIT;
1901		if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
1902			fcp_feat |= FCP_FEAT_TARG;
1903		fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
1904		ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
1905
1906		size = (struct fip_size_desc *)(ff + 1);
1907		size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
1908		size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
1909		size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
1910	}
1911
1912	skb_put(skb, len);
1913	skb->protocol = htons(ETH_P_FIP);
 
1914	skb_reset_mac_header(skb);
1915	skb_reset_network_header(skb);
1916
1917	fip->send(fip, skb);
1918}
1919
1920/**
1921 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
1922 * @lport: The lport which is receiving the event
1923 * @rdata: remote port private data
1924 * @event: The event that occurred
1925 *
1926 * Locking Note:  The rport lock must not be held when calling this function.
1927 */
1928static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
1929					struct fc_rport_priv *rdata,
1930					enum fc_rport_event event)
1931{
1932	struct fcoe_ctlr *fip = lport->disc.priv;
1933	struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1934
1935	LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
1936			rdata->ids.port_id, event);
1937
1938	mutex_lock(&fip->ctlr_mutex);
1939	switch (event) {
1940	case RPORT_EV_READY:
1941		frport->login_count = 0;
1942		break;
1943	case RPORT_EV_LOGO:
1944	case RPORT_EV_FAILED:
1945	case RPORT_EV_STOP:
1946		frport->login_count++;
1947		if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
1948			LIBFCOE_FIP_DBG(fip,
1949					"rport FLOGI limited port_id %6.6x\n",
1950					rdata->ids.port_id);
1951			lport->tt.rport_logoff(rdata);
1952		}
1953		break;
1954	default:
1955		break;
1956	}
1957	mutex_unlock(&fip->ctlr_mutex);
1958}
1959
1960static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
1961	.event_callback = fcoe_ctlr_vn_rport_callback,
1962};
1963
1964/**
1965 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
1966 * @fip: The FCoE controller
1967 *
1968 * Called with ctlr_mutex held.
1969 */
1970static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
1971{
 
 
1972	mutex_lock(&lport->disc.disc_mutex);
 
 
 
 
 
 
1973	lport->disc.disc_callback = NULL;
1974	mutex_unlock(&lport->disc.disc_mutex);
1975}
1976
1977/**
1978 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
1979 * @fip: The FCoE controller
1980 *
1981 * Called through the local port template for discovery.
1982 * Called without the ctlr_mutex held.
1983 */
1984static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
1985{
1986	struct fcoe_ctlr *fip = lport->disc.priv;
1987
1988	mutex_lock(&fip->ctlr_mutex);
1989	fcoe_ctlr_disc_stop_locked(lport);
1990	mutex_unlock(&fip->ctlr_mutex);
1991}
1992
1993/**
1994 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
1995 * @fip: The FCoE controller
1996 *
1997 * Called through the local port template for discovery.
1998 * Called without the ctlr_mutex held.
1999 */
2000static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2001{
2002	fcoe_ctlr_disc_stop(lport);
2003	lport->tt.rport_flush_queue();
2004	synchronize_rcu();
2005}
2006
2007/**
2008 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2009 * @fip: The FCoE controller
2010 *
2011 * Called with fcoe_ctlr lock held.
2012 */
2013static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2014{
2015	unsigned long wait;
2016	u32 port_id;
2017
2018	fcoe_ctlr_disc_stop_locked(fip->lp);
2019
2020	/*
2021	 * Get proposed port ID.
2022	 * If this is the first try after link up, use any previous port_id.
2023	 * If there was none, use the low bits of the port_name.
2024	 * On subsequent tries, get the next random one.
2025	 * Don't use reserved IDs, use another non-zero value, just as random.
2026	 */
2027	port_id = fip->port_id;
2028	if (fip->probe_tries)
2029		port_id = prandom32(&fip->rnd_state) & 0xffff;
2030	else if (!port_id)
2031		port_id = fip->lp->wwpn & 0xffff;
2032	if (!port_id || port_id == 0xffff)
2033		port_id = 1;
2034	fip->port_id = port_id;
2035
2036	if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2037		fip->probe_tries++;
2038		wait = random32() % FIP_VN_PROBE_WAIT;
2039	} else
2040		wait = FIP_VN_RLIM_INT;
2041	mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2042	fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2043}
2044
2045/**
2046 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2047 * @fip: The FCoE controller
2048 *
2049 * Called with fcoe_ctlr lock held.
2050 */
2051static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2052{
2053	fip->probe_tries = 0;
2054	prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
2055	fcoe_ctlr_vn_restart(fip);
2056}
2057
2058/**
2059 * fcoe_ctlr_vn_parse - parse probe request or response
2060 * @fip: The FCoE controller
2061 * @skb: incoming packet
2062 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2063 *
2064 * Returns non-zero error number on error.
2065 * Does not consume the packet.
2066 */
2067static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2068			      struct sk_buff *skb,
2069			      struct fc_rport_priv *rdata)
2070{
2071	struct fip_header *fiph;
2072	struct fip_desc *desc = NULL;
2073	struct fip_mac_desc *macd = NULL;
2074	struct fip_wwn_desc *wwn = NULL;
2075	struct fip_vn_desc *vn = NULL;
2076	struct fip_size_desc *size = NULL;
2077	struct fcoe_rport *frport;
2078	size_t rlen;
2079	size_t dlen;
2080	u32 desc_mask = 0;
2081	u32 dtype;
2082	u8 sub;
2083
2084	memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2085	frport = fcoe_ctlr_rport(rdata);
2086
2087	fiph = (struct fip_header *)skb->data;
2088	frport->flags = ntohs(fiph->fip_flags);
2089
2090	sub = fiph->fip_subcode;
2091	switch (sub) {
2092	case FIP_SC_VN_PROBE_REQ:
2093	case FIP_SC_VN_PROBE_REP:
2094	case FIP_SC_VN_BEACON:
2095		desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2096			    BIT(FIP_DT_VN_ID);
2097		break;
2098	case FIP_SC_VN_CLAIM_NOTIFY:
2099	case FIP_SC_VN_CLAIM_REP:
2100		desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2101			    BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2102			    BIT(FIP_DT_FCOE_SIZE);
2103		break;
2104	default:
2105		LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2106		return -EINVAL;
2107	}
2108
2109	rlen = ntohs(fiph->fip_dl_len) * 4;
2110	if (rlen + sizeof(*fiph) > skb->len)
2111		return -EINVAL;
2112
2113	desc = (struct fip_desc *)(fiph + 1);
2114	while (rlen > 0) {
2115		dlen = desc->fip_dlen * FIP_BPW;
2116		if (dlen < sizeof(*desc) || dlen > rlen)
2117			return -EINVAL;
2118
2119		dtype = desc->fip_dtype;
2120		if (dtype < 32) {
2121			if (!(desc_mask & BIT(dtype))) {
2122				LIBFCOE_FIP_DBG(fip,
2123						"unexpected or duplicated desc "
2124						"desc type %u in "
2125						"FIP VN2VN subtype %u\n",
2126						dtype, sub);
2127				return -EINVAL;
2128			}
2129			desc_mask &= ~BIT(dtype);
2130		}
2131
2132		switch (dtype) {
2133		case FIP_DT_MAC:
2134			if (dlen != sizeof(struct fip_mac_desc))
2135				goto len_err;
2136			macd = (struct fip_mac_desc *)desc;
2137			if (!is_valid_ether_addr(macd->fd_mac)) {
2138				LIBFCOE_FIP_DBG(fip,
2139					"Invalid MAC addr %pM in FIP VN2VN\n",
2140					 macd->fd_mac);
2141				return -EINVAL;
2142			}
2143			memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2144			break;
2145		case FIP_DT_NAME:
2146			if (dlen != sizeof(struct fip_wwn_desc))
2147				goto len_err;
2148			wwn = (struct fip_wwn_desc *)desc;
2149			rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
 
2150			break;
2151		case FIP_DT_VN_ID:
2152			if (dlen != sizeof(struct fip_vn_desc))
2153				goto len_err;
2154			vn = (struct fip_vn_desc *)desc;
2155			memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2156			rdata->ids.port_id = ntoh24(vn->fd_fc_id);
2157			rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
 
2158			break;
2159		case FIP_DT_FC4F:
2160			if (dlen != sizeof(struct fip_fc4_feat))
2161				goto len_err;
2162			break;
2163		case FIP_DT_FCOE_SIZE:
2164			if (dlen != sizeof(struct fip_size_desc))
2165				goto len_err;
2166			size = (struct fip_size_desc *)desc;
2167			frport->fcoe_len = ntohs(size->fd_size);
2168			break;
2169		default:
2170			LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2171					"in FIP probe\n", dtype);
2172			/* standard says ignore unknown descriptors >= 128 */
2173			if (dtype < FIP_DT_VENDOR_BASE)
2174				return -EINVAL;
2175			break;
2176		}
2177		desc = (struct fip_desc *)((char *)desc + dlen);
2178		rlen -= dlen;
2179	}
2180	return 0;
2181
2182len_err:
2183	LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2184			dtype, dlen);
2185	return -EINVAL;
2186}
2187
2188/**
2189 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2190 * @fip: The FCoE controller
2191 *
2192 * Called with ctlr_mutex held.
2193 */
2194static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2195{
2196	fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2197	fip->sol_time = jiffies;
2198}
2199
2200/**
2201 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2202 * @fip: The FCoE controller
2203 * @rdata: parsed remote port with frport from the probe request
2204 *
2205 * Called with ctlr_mutex held.
2206 */
2207static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2208				   struct fc_rport_priv *rdata)
2209{
2210	struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2211
2212	if (rdata->ids.port_id != fip->port_id)
2213		return;
2214
2215	switch (fip->state) {
2216	case FIP_ST_VNMP_CLAIM:
2217	case FIP_ST_VNMP_UP:
 
 
2218		fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2219				  frport->enode_mac, 0);
2220		break;
2221	case FIP_ST_VNMP_PROBE1:
2222	case FIP_ST_VNMP_PROBE2:
2223		/*
2224		 * Decide whether to reply to the Probe.
2225		 * Our selected address is never a "recorded" one, so
2226		 * only reply if our WWPN is greater and the
2227		 * Probe's REC bit is not set.
2228		 * If we don't reply, we will change our address.
2229		 */
2230		if (fip->lp->wwpn > rdata->ids.port_name &&
2231		    !(frport->flags & FIP_FL_REC_OR_P2P)) {
 
 
2232			fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2233					  frport->enode_mac, 0);
2234			break;
2235		}
2236		/* fall through */
2237	case FIP_ST_VNMP_START:
 
 
2238		fcoe_ctlr_vn_restart(fip);
2239		break;
2240	default:
 
 
2241		break;
2242	}
2243}
2244
2245/**
2246 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2247 * @fip: The FCoE controller
2248 * @rdata: parsed remote port with frport from the probe request
2249 *
2250 * Called with ctlr_mutex held.
2251 */
2252static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2253				   struct fc_rport_priv *rdata)
2254{
2255	if (rdata->ids.port_id != fip->port_id)
2256		return;
2257	switch (fip->state) {
2258	case FIP_ST_VNMP_START:
2259	case FIP_ST_VNMP_PROBE1:
2260	case FIP_ST_VNMP_PROBE2:
2261	case FIP_ST_VNMP_CLAIM:
 
 
2262		fcoe_ctlr_vn_restart(fip);
2263		break;
2264	case FIP_ST_VNMP_UP:
 
2265		fcoe_ctlr_vn_send_claim(fip);
2266		break;
2267	default:
2268		break;
2269	}
2270}
2271
2272/**
2273 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2274 * @fip: The FCoE controller
2275 * @new: newly-parsed remote port with frport as a template for new rdata
2276 *
2277 * Called with ctlr_mutex held.
2278 */
2279static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2280{
2281	struct fc_lport *lport = fip->lp;
2282	struct fc_rport_priv *rdata;
2283	struct fc_rport_identifiers *ids;
2284	struct fcoe_rport *frport;
2285	u32 port_id;
2286
2287	port_id = new->ids.port_id;
2288	if (port_id == fip->port_id)
2289		return;
2290
2291	mutex_lock(&lport->disc.disc_mutex);
2292	rdata = lport->tt.rport_create(lport, port_id);
2293	if (!rdata) {
2294		mutex_unlock(&lport->disc.disc_mutex);
2295		return;
2296	}
 
 
2297
2298	rdata->ops = &fcoe_ctlr_vn_rport_ops;
2299	rdata->disc_id = lport->disc.disc_id;
2300
2301	ids = &rdata->ids;
2302	if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2303	    (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2304		lport->tt.rport_logoff(rdata);
2305	ids->port_name = new->ids.port_name;
2306	ids->node_name = new->ids.node_name;
2307	mutex_unlock(&lport->disc.disc_mutex);
 
 
 
 
 
 
2308
2309	frport = fcoe_ctlr_rport(rdata);
2310	LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2311			port_id, frport->fcoe_len ? "old" : "new");
2312	*frport = *fcoe_ctlr_rport(new);
 
 
 
 
 
2313	frport->time = 0;
2314}
2315
2316/**
2317 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2318 * @fip: The FCoE controller
2319 * @port_id:  The port_id of the remote VN_node
2320 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2321 *
2322 * Returns non-zero error if no remote port found.
2323 */
2324static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2325{
2326	struct fc_lport *lport = fip->lp;
2327	struct fc_rport_priv *rdata;
2328	struct fcoe_rport *frport;
2329	int ret = -1;
2330
2331	rcu_read_lock();
2332	rdata = lport->tt.rport_lookup(lport, port_id);
2333	if (rdata) {
2334		frport = fcoe_ctlr_rport(rdata);
2335		memcpy(mac, frport->enode_mac, ETH_ALEN);
2336		ret = 0;
 
2337	}
2338	rcu_read_unlock();
2339	return ret;
2340}
2341
2342/**
2343 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2344 * @fip: The FCoE controller
2345 * @new: newly-parsed remote port with frport as a template for new rdata
2346 *
2347 * Called with ctlr_mutex held.
2348 */
2349static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2350				      struct fc_rport_priv *new)
2351{
2352	struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2353
2354	if (frport->flags & FIP_FL_REC_OR_P2P) {
2355		fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2356		return;
2357	}
2358	switch (fip->state) {
2359	case FIP_ST_VNMP_START:
2360	case FIP_ST_VNMP_PROBE1:
2361	case FIP_ST_VNMP_PROBE2:
2362		if (new->ids.port_id == fip->port_id)
 
 
 
2363			fcoe_ctlr_vn_restart(fip);
 
2364		break;
2365	case FIP_ST_VNMP_CLAIM:
2366	case FIP_ST_VNMP_UP:
2367		if (new->ids.port_id == fip->port_id) {
2368			if (new->ids.port_name > fip->lp->wwpn) {
 
 
2369				fcoe_ctlr_vn_restart(fip);
2370				break;
2371			}
 
 
2372			fcoe_ctlr_vn_send_claim(fip);
2373			break;
2374		}
2375		fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2376				  min((u32)frport->fcoe_len,
 
 
2377				      fcoe_ctlr_fcoe_size(fip)));
2378		fcoe_ctlr_vn_add(fip, new);
2379		break;
2380	default:
 
 
 
2381		break;
2382	}
2383}
2384
2385/**
2386 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2387 * @fip: The FCoE controller that received the frame
2388 * @new: newly-parsed remote port with frport from the Claim Response
2389 *
2390 * Called with ctlr_mutex held.
2391 */
2392static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2393				    struct fc_rport_priv *new)
2394{
2395	LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2396			new->ids.port_id, fcoe_ctlr_state(fip->state));
2397	if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2398		fcoe_ctlr_vn_add(fip, new);
2399}
2400
2401/**
2402 * fcoe_ctlr_vn_beacon() - handle received beacon.
2403 * @fip: The FCoE controller that received the frame
2404 * @new: newly-parsed remote port with frport from the Beacon
2405 *
2406 * Called with ctlr_mutex held.
2407 */
2408static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2409				struct fc_rport_priv *new)
2410{
2411	struct fc_lport *lport = fip->lp;
2412	struct fc_rport_priv *rdata;
2413	struct fcoe_rport *frport;
2414
2415	frport = fcoe_ctlr_rport(new);
2416	if (frport->flags & FIP_FL_REC_OR_P2P) {
2417		fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2418		return;
2419	}
2420	mutex_lock(&lport->disc.disc_mutex);
2421	rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2422	if (rdata)
2423		kref_get(&rdata->kref);
2424	mutex_unlock(&lport->disc.disc_mutex);
2425	if (rdata) {
2426		if (rdata->ids.node_name == new->ids.node_name &&
2427		    rdata->ids.port_name == new->ids.port_name) {
2428			frport = fcoe_ctlr_rport(rdata);
2429			if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2430				lport->tt.rport_login(rdata);
 
 
 
 
 
 
 
2431			frport->time = jiffies;
2432		}
2433		kref_put(&rdata->kref, lport->tt.rport_destroy);
2434		return;
2435	}
2436	if (fip->state != FIP_ST_VNMP_UP)
2437		return;
2438
2439	/*
2440	 * Beacon from a new neighbor.
2441	 * Send a claim notify if one hasn't been sent recently.
2442	 * Don't add the neighbor yet.
2443	 */
2444	LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2445			new->ids.port_id);
2446	if (time_after(jiffies,
2447		       fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2448		fcoe_ctlr_vn_send_claim(fip);
2449}
2450
2451/**
2452 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2453 * @fip: The FCoE controller
2454 *
2455 * Called with ctlr_mutex held.
2456 * Called only in state FIP_ST_VNMP_UP.
2457 * Returns the soonest time for next age-out or a time far in the future.
2458 */
2459static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2460{
2461	struct fc_lport *lport = fip->lp;
2462	struct fc_rport_priv *rdata;
2463	struct fcoe_rport *frport;
2464	unsigned long next_time;
2465	unsigned long deadline;
2466
2467	next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2468	mutex_lock(&lport->disc.disc_mutex);
2469	list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
 
 
2470		frport = fcoe_ctlr_rport(rdata);
2471		if (!frport->time)
 
2472			continue;
 
2473		deadline = frport->time +
2474			   msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2475		if (time_after_eq(jiffies, deadline)) {
2476			frport->time = 0;
2477			LIBFCOE_FIP_DBG(fip,
2478				"port %16.16llx fc_id %6.6x beacon expired\n",
2479				rdata->ids.port_name, rdata->ids.port_id);
2480			lport->tt.rport_logoff(rdata);
2481		} else if (time_before(deadline, next_time))
2482			next_time = deadline;
 
2483	}
2484	mutex_unlock(&lport->disc.disc_mutex);
2485	return next_time;
2486}
2487
2488/**
2489 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2490 * @fip: The FCoE controller that received the frame
2491 * @skb: The received FIP frame
2492 *
2493 * Returns non-zero if the frame is dropped.
2494 * Always consumes the frame.
2495 */
2496static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2497{
2498	struct fip_header *fiph;
2499	enum fip_vn2vn_subcode sub;
2500	struct {
2501		struct fc_rport_priv rdata;
2502		struct fcoe_rport frport;
2503	} buf;
2504	int rc;
2505
2506	fiph = (struct fip_header *)skb->data;
2507	sub = fiph->fip_subcode;
2508
2509	rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
 
 
 
 
 
 
 
 
 
 
2510	if (rc) {
2511		LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2512		goto drop;
2513	}
2514
2515	mutex_lock(&fip->ctlr_mutex);
2516	switch (sub) {
2517	case FIP_SC_VN_PROBE_REQ:
2518		fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2519		break;
2520	case FIP_SC_VN_PROBE_REP:
2521		fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2522		break;
2523	case FIP_SC_VN_CLAIM_NOTIFY:
2524		fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2525		break;
2526	case FIP_SC_VN_CLAIM_REP:
2527		fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2528		break;
2529	case FIP_SC_VN_BEACON:
2530		fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2531		break;
2532	default:
2533		LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2534		rc = -1;
2535		break;
2536	}
2537	mutex_unlock(&fip->ctlr_mutex);
2538drop:
2539	kfree_skb(skb);
2540	return rc;
2541}
2542
2543/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2544 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
2545 * @lport: The local port
2546 * @fp: The received frame
2547 *
2548 * This should never be called since we don't see RSCNs or other
2549 * fabric-generated ELSes.
2550 */
2551static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
2552{
2553	struct fc_seq_els_data rjt_data;
2554
2555	rjt_data.reason = ELS_RJT_UNSUP;
2556	rjt_data.explan = ELS_EXPL_NONE;
2557	lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
2558	fc_frame_free(fp);
2559}
2560
2561/**
2562 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2563 * @fip: The FCoE controller
2564 *
2565 * This sets a flag indicating that remote ports should be created
2566 * and started for the peers we discover.  We use the disc_callback
2567 * pointer as that flag.  Peers already discovered are created here.
2568 *
2569 * The lport lock is held during this call. The callback must be done
2570 * later, without holding either the lport or discovery locks.
2571 * The fcoe_ctlr lock may also be held during this call.
2572 */
2573static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2574						  enum fc_disc_event),
2575				 struct fc_lport *lport)
2576{
2577	struct fc_disc *disc = &lport->disc;
2578	struct fcoe_ctlr *fip = disc->priv;
2579
2580	mutex_lock(&disc->disc_mutex);
2581	disc->disc_callback = callback;
2582	disc->disc_id = (disc->disc_id + 2) | 1;
2583	disc->pending = 1;
2584	schedule_work(&fip->timer_work);
2585	mutex_unlock(&disc->disc_mutex);
2586}
2587
2588/**
2589 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2590 * @fip: The FCoE controller
2591 *
2592 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2593 * we've received at least one beacon.
2594 * Performs the discovery complete callback.
2595 */
2596static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2597{
2598	struct fc_lport *lport = fip->lp;
2599	struct fc_disc *disc = &lport->disc;
2600	struct fc_rport_priv *rdata;
2601	struct fcoe_rport *frport;
2602	void (*callback)(struct fc_lport *, enum fc_disc_event);
2603
2604	mutex_lock(&disc->disc_mutex);
2605	callback = disc->pending ? disc->disc_callback : NULL;
2606	disc->pending = 0;
2607	list_for_each_entry_rcu(rdata, &disc->rports, peers) {
 
 
2608		frport = fcoe_ctlr_rport(rdata);
2609		if (frport->time)
2610			lport->tt.rport_login(rdata);
 
2611	}
2612	mutex_unlock(&disc->disc_mutex);
2613	if (callback)
2614		callback(lport, DISC_EV_SUCCESS);
2615}
2616
2617/**
2618 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2619 * @fip: The FCoE controller
2620 */
2621static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2622{
2623	unsigned long next_time;
2624	u8 mac[ETH_ALEN];
2625	u32 new_port_id = 0;
2626
2627	mutex_lock(&fip->ctlr_mutex);
2628	switch (fip->state) {
2629	case FIP_ST_VNMP_START:
2630		fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
 
2631		fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2632		next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2633		break;
2634	case FIP_ST_VNMP_PROBE1:
2635		fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
 
2636		fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2637		next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2638		break;
2639	case FIP_ST_VNMP_PROBE2:
2640		fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2641		new_port_id = fip->port_id;
2642		hton24(mac, FIP_VN_FC_MAP);
2643		hton24(mac + 3, new_port_id);
2644		fcoe_ctlr_map_dest(fip);
2645		fip->update_mac(fip->lp, mac);
 
2646		fcoe_ctlr_vn_send_claim(fip);
2647		next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2648		break;
2649	case FIP_ST_VNMP_CLAIM:
2650		/*
2651		 * This may be invoked either by starting discovery so don't
2652		 * go to the next state unless it's been long enough.
2653		 */
2654		next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2655		if (time_after_eq(jiffies, next_time)) {
2656			fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
 
2657			fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2658					  fcoe_all_vn2vn, 0);
2659			next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2660			fip->port_ka_time = next_time;
2661		}
2662		fcoe_ctlr_vn_disc(fip);
2663		break;
2664	case FIP_ST_VNMP_UP:
2665		next_time = fcoe_ctlr_vn_age(fip);
2666		if (time_after_eq(jiffies, fip->port_ka_time)) {
 
2667			fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2668					  fcoe_all_vn2vn, 0);
2669			fip->port_ka_time = jiffies +
2670				 msecs_to_jiffies(FIP_VN_BEACON_INT +
2671					(random32() % FIP_VN_BEACON_FUZZ));
2672		}
2673		if (time_before(fip->port_ka_time, next_time))
2674			next_time = fip->port_ka_time;
2675		break;
2676	case FIP_ST_LINK_WAIT:
2677		goto unlock;
2678	default:
2679		WARN(1, "unexpected state %d\n", fip->state);
2680		goto unlock;
2681	}
2682	mod_timer(&fip->timer, next_time);
2683unlock:
2684	mutex_unlock(&fip->ctlr_mutex);
2685
2686	/* If port ID is new, notify local port after dropping ctlr_mutex */
2687	if (new_port_id)
2688		fc_lport_set_local_id(fip->lp, new_port_id);
2689}
2690
2691/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2692 * fcoe_libfc_config() - Sets up libfc related properties for local port
2693 * @lp: The local port to configure libfc for
2694 * @fip: The FCoE controller in use by the local port
2695 * @tt: The libfc function template
2696 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
2697 *
2698 * Returns : 0 for success
2699 */
2700int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2701		      const struct libfc_function_template *tt, int init_fcp)
2702{
2703	/* Set the function pointers set by the LLDD */
2704	memcpy(&lport->tt, tt, sizeof(*tt));
2705	if (init_fcp && fc_fcp_init(lport))
2706		return -ENOMEM;
2707	fc_exch_init(lport);
2708	fc_elsct_init(lport);
2709	fc_lport_init(lport);
2710	if (fip->mode == FIP_MODE_VN2VN)
2711		lport->rport_priv_size = sizeof(struct fcoe_rport);
2712	fc_rport_init(lport);
2713	if (fip->mode == FIP_MODE_VN2VN) {
2714		lport->point_to_multipoint = 1;
2715		lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2716		lport->tt.disc_start = fcoe_ctlr_disc_start;
2717		lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2718		lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2719		mutex_init(&lport->disc.disc_mutex);
2720		INIT_LIST_HEAD(&lport->disc.rports);
2721		lport->disc.priv = fip;
2722	} else {
2723		fc_disc_init(lport);
2724	}
2725	return 0;
2726}
2727EXPORT_SYMBOL_GPL(fcoe_libfc_config);
v5.4
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (c) 2008-2009 Cisco Systems, Inc.  All rights reserved.
   4 * Copyright (c) 2009 Intel Corporation.  All rights reserved.
   5 *
 
 
 
 
 
 
 
 
 
 
 
 
 
   6 * Maintained at www.Open-FCoE.org
   7 */
   8
   9#include <linux/types.h>
  10#include <linux/module.h>
  11#include <linux/kernel.h>
  12#include <linux/list.h>
  13#include <linux/spinlock.h>
  14#include <linux/timer.h>
  15#include <linux/netdevice.h>
  16#include <linux/etherdevice.h>
  17#include <linux/ethtool.h>
  18#include <linux/if_ether.h>
  19#include <linux/if_vlan.h>
  20#include <linux/errno.h>
  21#include <linux/bitops.h>
  22#include <linux/slab.h>
  23#include <net/rtnetlink.h>
  24
  25#include <scsi/fc/fc_els.h>
  26#include <scsi/fc/fc_fs.h>
  27#include <scsi/fc/fc_fip.h>
  28#include <scsi/fc/fc_encaps.h>
  29#include <scsi/fc/fc_fcoe.h>
  30#include <scsi/fc/fc_fcp.h>
  31
  32#include <scsi/libfc.h>
  33#include <scsi/libfcoe.h>
  34
  35#include "libfcoe.h"
  36
  37#define	FCOE_CTLR_MIN_FKA	500		/* min keep alive (mS) */
  38#define	FCOE_CTLR_DEF_FKA	FIP_DEF_FKA	/* default keep alive (mS) */
  39
  40static void fcoe_ctlr_timeout(struct timer_list *);
  41static void fcoe_ctlr_timer_work(struct work_struct *);
  42static void fcoe_ctlr_recv_work(struct work_struct *);
  43static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
  44
  45static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
  46static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
  47static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
  48static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
  49
  50static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *, struct sk_buff *);
  51
  52static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
  53static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
  54static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
  55static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
  56
  57static const char * const fcoe_ctlr_states[] = {
  58	[FIP_ST_DISABLED] =	"DISABLED",
  59	[FIP_ST_LINK_WAIT] =	"LINK_WAIT",
  60	[FIP_ST_AUTO] =		"AUTO",
  61	[FIP_ST_NON_FIP] =	"NON_FIP",
  62	[FIP_ST_ENABLED] =	"ENABLED",
  63	[FIP_ST_VNMP_START] =	"VNMP_START",
  64	[FIP_ST_VNMP_PROBE1] =	"VNMP_PROBE1",
  65	[FIP_ST_VNMP_PROBE2] =	"VNMP_PROBE2",
  66	[FIP_ST_VNMP_CLAIM] =	"VNMP_CLAIM",
  67	[FIP_ST_VNMP_UP] =	"VNMP_UP",
  68};
  69
  70static const char *fcoe_ctlr_state(enum fip_state state)
  71{
  72	const char *cp = "unknown";
  73
  74	if (state < ARRAY_SIZE(fcoe_ctlr_states))
  75		cp = fcoe_ctlr_states[state];
  76	if (!cp)
  77		cp = "unknown";
  78	return cp;
  79}
  80
  81/**
  82 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
  83 * @fip: The FCoE controller
  84 * @state: The new state
  85 */
  86static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
  87{
  88	if (state == fip->state)
  89		return;
  90	if (fip->lp)
  91		LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
  92			fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
  93	fip->state = state;
  94}
  95
  96/**
  97 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
  98 * @fcf: The FCF to check
  99 *
 100 * Return non-zero if FCF fcoe_size has been validated.
 101 */
 102static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
 103{
 104	return (fcf->flags & FIP_FL_SOL) != 0;
 105}
 106
 107/**
 108 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
 109 * @fcf: The FCF to check
 110 *
 111 * Return non-zero if the FCF is usable.
 112 */
 113static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
 114{
 115	u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
 116
 117	return (fcf->flags & flags) == flags;
 118}
 119
 120/**
 121 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
 122 * @fip: The FCoE controller
 123 */
 124static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
 125{
 126	if (fip->mode == FIP_MODE_VN2VN)
 127		hton24(fip->dest_addr, FIP_VN_FC_MAP);
 128	else
 129		hton24(fip->dest_addr, FIP_DEF_FC_MAP);
 130	hton24(fip->dest_addr + 3, 0);
 131	fip->map_dest = 1;
 132}
 133
 134/**
 135 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
 136 * @fip: The FCoE controller to initialize
 137 */
 138void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_mode mode)
 139{
 140	fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
 141	fip->mode = mode;
 142	fip->fip_resp = false;
 143	INIT_LIST_HEAD(&fip->fcfs);
 144	mutex_init(&fip->ctlr_mutex);
 145	spin_lock_init(&fip->ctlr_lock);
 146	fip->flogi_oxid = FC_XID_UNKNOWN;
 147	timer_setup(&fip->timer, fcoe_ctlr_timeout, 0);
 148	INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
 149	INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
 150	skb_queue_head_init(&fip->fip_recv_list);
 151}
 152EXPORT_SYMBOL(fcoe_ctlr_init);
 153
 154/**
 155 * fcoe_sysfs_fcf_add() - Add a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
 156 * @new: The newly discovered FCF
 157 *
 158 * Called with fip->ctlr_mutex held
 159 */
 160static int fcoe_sysfs_fcf_add(struct fcoe_fcf *new)
 161{
 162	struct fcoe_ctlr *fip = new->fip;
 163	struct fcoe_ctlr_device *ctlr_dev;
 164	struct fcoe_fcf_device *temp, *fcf_dev;
 165	int rc = -ENOMEM;
 166
 167	LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
 168			new->fabric_name, new->fcf_mac);
 169
 170	temp = kzalloc(sizeof(*temp), GFP_KERNEL);
 171	if (!temp)
 172		goto out;
 173
 174	temp->fabric_name = new->fabric_name;
 175	temp->switch_name = new->switch_name;
 176	temp->fc_map = new->fc_map;
 177	temp->vfid = new->vfid;
 178	memcpy(temp->mac, new->fcf_mac, ETH_ALEN);
 179	temp->priority = new->pri;
 180	temp->fka_period = new->fka_period;
 181	temp->selected = 0; /* default to unselected */
 182
 183	/*
 184	 * If ctlr_dev doesn't exist then it means we're a libfcoe user
 185	 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device.
 186	 * fnic would be an example of a driver with this behavior. In this
 187	 * case we want to add the fcoe_fcf to the fcoe_ctlr list, but we
 188	 * don't want to make sysfs changes.
 189	 */
 190
 191	ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
 192	if (ctlr_dev) {
 193		mutex_lock(&ctlr_dev->lock);
 194		fcf_dev = fcoe_fcf_device_add(ctlr_dev, temp);
 195		if (unlikely(!fcf_dev)) {
 196			rc = -ENOMEM;
 197			mutex_unlock(&ctlr_dev->lock);
 198			goto out;
 199		}
 200
 201		/*
 202		 * The fcoe_sysfs layer can return a CONNECTED fcf that
 203		 * has a priv (fcf was never deleted) or a CONNECTED fcf
 204		 * that doesn't have a priv (fcf was deleted). However,
 205		 * libfcoe will always delete FCFs before trying to add
 206		 * them. This is ensured because both recv_adv and
 207		 * age_fcfs are protected by the the fcoe_ctlr's mutex.
 208		 * This means that we should never get a FCF with a
 209		 * non-NULL priv pointer.
 210		 */
 211		BUG_ON(fcf_dev->priv);
 212
 213		fcf_dev->priv = new;
 214		new->fcf_dev = fcf_dev;
 215		mutex_unlock(&ctlr_dev->lock);
 216	}
 217
 218	list_add(&new->list, &fip->fcfs);
 219	fip->fcf_count++;
 220	rc = 0;
 221
 222out:
 223	kfree(temp);
 224	return rc;
 225}
 226
 227/**
 228 * fcoe_sysfs_fcf_del() - Remove a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
 229 * @new: The FCF to be removed
 230 *
 231 * Called with fip->ctlr_mutex held
 232 */
 233static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new)
 234{
 235	struct fcoe_ctlr *fip = new->fip;
 236	struct fcoe_ctlr_device *cdev;
 237	struct fcoe_fcf_device *fcf_dev;
 238
 239	list_del(&new->list);
 240	fip->fcf_count--;
 241
 242	/*
 243	 * If ctlr_dev doesn't exist then it means we're a libfcoe user
 244	 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device
 245	 * or a fcoe_fcf_device.
 246	 *
 247	 * fnic would be an example of a driver with this behavior. In this
 248	 * case we want to remove the fcoe_fcf from the fcoe_ctlr list (above),
 249	 * but we don't want to make sysfs changes.
 250	 */
 251	cdev = fcoe_ctlr_to_ctlr_dev(fip);
 252	if (cdev) {
 253		mutex_lock(&cdev->lock);
 254		fcf_dev = fcoe_fcf_to_fcf_dev(new);
 255		WARN_ON(!fcf_dev);
 256		new->fcf_dev = NULL;
 257		fcoe_fcf_device_delete(fcf_dev);
 258		kfree(new);
 259		mutex_unlock(&cdev->lock);
 260	}
 261}
 262
 263/**
 264 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
 265 * @fip: The FCoE controller whose FCFs are to be reset
 266 *
 267 * Called with &fcoe_ctlr lock held.
 268 */
 269static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
 270{
 271	struct fcoe_fcf *fcf;
 272	struct fcoe_fcf *next;
 273
 274	fip->sel_fcf = NULL;
 275	list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
 276		fcoe_sysfs_fcf_del(fcf);
 
 277	}
 278	WARN_ON(fip->fcf_count);
 279
 280	fip->sel_time = 0;
 281}
 282
 283/**
 284 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
 285 * @fip: The FCoE controller to tear down
 286 *
 287 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
 288 *
 289 * The receive handler will have been deleted before this to guarantee
 290 * that no more recv_work will be scheduled.
 291 *
 292 * The timer routine will simply return once we set FIP_ST_DISABLED.
 293 * This guarantees that no further timeouts or work will be scheduled.
 294 */
 295void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
 296{
 297	cancel_work_sync(&fip->recv_work);
 298	skb_queue_purge(&fip->fip_recv_list);
 299
 300	mutex_lock(&fip->ctlr_mutex);
 301	fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
 302	fcoe_ctlr_reset_fcfs(fip);
 303	mutex_unlock(&fip->ctlr_mutex);
 304	del_timer_sync(&fip->timer);
 305	cancel_work_sync(&fip->timer_work);
 306}
 307EXPORT_SYMBOL(fcoe_ctlr_destroy);
 308
 309/**
 310 * fcoe_ctlr_announce() - announce new FCF selection
 311 * @fip: The FCoE controller
 312 *
 313 * Also sets the destination MAC for FCoE and control packets
 314 *
 315 * Called with neither ctlr_mutex nor ctlr_lock held.
 316 */
 317static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
 318{
 319	struct fcoe_fcf *sel;
 320	struct fcoe_fcf *fcf;
 321
 322	mutex_lock(&fip->ctlr_mutex);
 323	spin_lock_bh(&fip->ctlr_lock);
 324
 325	kfree_skb(fip->flogi_req);
 326	fip->flogi_req = NULL;
 327	list_for_each_entry(fcf, &fip->fcfs, list)
 328		fcf->flogi_sent = 0;
 329
 330	spin_unlock_bh(&fip->ctlr_lock);
 331	sel = fip->sel_fcf;
 332
 333	if (sel && ether_addr_equal(sel->fcf_mac, fip->dest_addr))
 334		goto unlock;
 335	if (!is_zero_ether_addr(fip->dest_addr)) {
 336		printk(KERN_NOTICE "libfcoe: host%d: "
 337		       "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
 338		       fip->lp->host->host_no, fip->dest_addr);
 339		memset(fip->dest_addr, 0, ETH_ALEN);
 340	}
 341	if (sel) {
 342		printk(KERN_INFO "libfcoe: host%d: FIP selected "
 343		       "Fibre-Channel Forwarder MAC %pM\n",
 344		       fip->lp->host->host_no, sel->fcf_mac);
 345		memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
 346		fip->map_dest = 0;
 347	}
 348unlock:
 349	mutex_unlock(&fip->ctlr_mutex);
 350}
 351
 352/**
 353 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
 354 * @fip: The FCoE controller to get the maximum FCoE size from
 355 *
 356 * Returns the maximum packet size including the FCoE header and trailer,
 357 * but not including any Ethernet or VLAN headers.
 358 */
 359static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
 360{
 361	/*
 362	 * Determine the max FCoE frame size allowed, including
 363	 * FCoE header and trailer.
 364	 * Note:  lp->mfs is currently the payload size, not the frame size.
 365	 */
 366	return fip->lp->mfs + sizeof(struct fc_frame_header) +
 367		sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
 368}
 369
 370/**
 371 * fcoe_ctlr_solicit() - Send a FIP solicitation
 372 * @fip: The FCoE controller to send the solicitation on
 373 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
 374 */
 375static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
 376{
 377	struct sk_buff *skb;
 378	struct fip_sol {
 379		struct ethhdr eth;
 380		struct fip_header fip;
 381		struct {
 382			struct fip_mac_desc mac;
 383			struct fip_wwn_desc wwnn;
 384			struct fip_size_desc size;
 385		} __packed desc;
 386	}  __packed * sol;
 387	u32 fcoe_size;
 388
 389	skb = dev_alloc_skb(sizeof(*sol));
 390	if (!skb)
 391		return;
 392
 393	sol = (struct fip_sol *)skb->data;
 394
 395	memset(sol, 0, sizeof(*sol));
 396	memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
 397	memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
 398	sol->eth.h_proto = htons(ETH_P_FIP);
 399
 400	sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
 401	sol->fip.fip_op = htons(FIP_OP_DISC);
 402	sol->fip.fip_subcode = FIP_SC_SOL;
 403	sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
 404	sol->fip.fip_flags = htons(FIP_FL_FPMA);
 405	if (fip->spma)
 406		sol->fip.fip_flags |= htons(FIP_FL_SPMA);
 407
 408	sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
 409	sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
 410	memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
 411
 412	sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
 413	sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
 414	put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
 415
 416	fcoe_size = fcoe_ctlr_fcoe_size(fip);
 417	sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
 418	sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
 419	sol->desc.size.fd_size = htons(fcoe_size);
 420
 421	skb_put(skb, sizeof(*sol));
 422	skb->protocol = htons(ETH_P_FIP);
 423	skb->priority = fip->priority;
 424	skb_reset_mac_header(skb);
 425	skb_reset_network_header(skb);
 426	fip->send(fip, skb);
 427
 428	if (!fcf)
 429		fip->sol_time = jiffies;
 430}
 431
 432/**
 433 * fcoe_ctlr_link_up() - Start FCoE controller
 434 * @fip: The FCoE controller to start
 435 *
 436 * Called from the LLD when the network link is ready.
 437 */
 438void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
 439{
 440	mutex_lock(&fip->ctlr_mutex);
 441	if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
 442		mutex_unlock(&fip->ctlr_mutex);
 443		fc_linkup(fip->lp);
 444	} else if (fip->state == FIP_ST_LINK_WAIT) {
 445		if (fip->mode == FIP_MODE_NON_FIP)
 446			fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
 447		else
 448			fcoe_ctlr_set_state(fip, FIP_ST_AUTO);
 449		switch (fip->mode) {
 450		default:
 451			LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
 452			/* fall-through */
 453		case FIP_MODE_AUTO:
 454			LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
 455			/* fall-through */
 456		case FIP_MODE_FABRIC:
 457		case FIP_MODE_NON_FIP:
 458			mutex_unlock(&fip->ctlr_mutex);
 459			fc_linkup(fip->lp);
 460			fcoe_ctlr_solicit(fip, NULL);
 461			break;
 462		case FIP_MODE_VN2VN:
 463			fcoe_ctlr_vn_start(fip);
 464			mutex_unlock(&fip->ctlr_mutex);
 465			fc_linkup(fip->lp);
 466			break;
 467		}
 468	} else
 469		mutex_unlock(&fip->ctlr_mutex);
 470}
 471EXPORT_SYMBOL(fcoe_ctlr_link_up);
 472
 473/**
 474 * fcoe_ctlr_reset() - Reset a FCoE controller
 475 * @fip:       The FCoE controller to reset
 476 */
 477static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
 478{
 479	fcoe_ctlr_reset_fcfs(fip);
 480	del_timer(&fip->timer);
 481	fip->ctlr_ka_time = 0;
 482	fip->port_ka_time = 0;
 483	fip->sol_time = 0;
 484	fip->flogi_oxid = FC_XID_UNKNOWN;
 485	fcoe_ctlr_map_dest(fip);
 486}
 487
 488/**
 489 * fcoe_ctlr_link_down() - Stop a FCoE controller
 490 * @fip: The FCoE controller to be stopped
 491 *
 492 * Returns non-zero if the link was up and now isn't.
 493 *
 494 * Called from the LLD when the network link is not ready.
 495 * There may be multiple calls while the link is down.
 496 */
 497int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
 498{
 499	int link_dropped;
 500
 501	LIBFCOE_FIP_DBG(fip, "link down.\n");
 502	mutex_lock(&fip->ctlr_mutex);
 503	fcoe_ctlr_reset(fip);
 504	link_dropped = fip->state != FIP_ST_LINK_WAIT;
 505	fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
 506	mutex_unlock(&fip->ctlr_mutex);
 507
 508	if (link_dropped)
 509		fc_linkdown(fip->lp);
 510	return link_dropped;
 511}
 512EXPORT_SYMBOL(fcoe_ctlr_link_down);
 513
 514/**
 515 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
 516 * @fip:   The FCoE controller to send the FKA on
 517 * @lport: libfc fc_lport to send from
 518 * @ports: 0 for controller keep-alive, 1 for port keep-alive
 519 * @sa:	   The source MAC address
 520 *
 521 * A controller keep-alive is sent every fka_period (typically 8 seconds).
 522 * The source MAC is the native MAC address.
 523 *
 524 * A port keep-alive is sent every 90 seconds while logged in.
 525 * The source MAC is the assigned mapped source address.
 526 * The destination is the FCF's F-port.
 527 */
 528static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
 529				      struct fc_lport *lport,
 530				      int ports, u8 *sa)
 531{
 532	struct sk_buff *skb;
 533	struct fip_kal {
 534		struct ethhdr eth;
 535		struct fip_header fip;
 536		struct fip_mac_desc mac;
 537	} __packed * kal;
 538	struct fip_vn_desc *vn;
 539	u32 len;
 540	struct fc_lport *lp;
 541	struct fcoe_fcf *fcf;
 542
 543	fcf = fip->sel_fcf;
 544	lp = fip->lp;
 545	if (!fcf || (ports && !lp->port_id))
 546		return;
 547
 548	len = sizeof(*kal) + ports * sizeof(*vn);
 549	skb = dev_alloc_skb(len);
 550	if (!skb)
 551		return;
 552
 553	kal = (struct fip_kal *)skb->data;
 554	memset(kal, 0, len);
 555	memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
 556	memcpy(kal->eth.h_source, sa, ETH_ALEN);
 557	kal->eth.h_proto = htons(ETH_P_FIP);
 558
 559	kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
 560	kal->fip.fip_op = htons(FIP_OP_CTRL);
 561	kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
 562	kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
 563				     ports * sizeof(*vn)) / FIP_BPW);
 564	kal->fip.fip_flags = htons(FIP_FL_FPMA);
 565	if (fip->spma)
 566		kal->fip.fip_flags |= htons(FIP_FL_SPMA);
 567
 568	kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
 569	kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
 570	memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
 571	if (ports) {
 572		vn = (struct fip_vn_desc *)(kal + 1);
 573		vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
 574		vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
 575		memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
 576		hton24(vn->fd_fc_id, lport->port_id);
 577		put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
 578	}
 579	skb_put(skb, len);
 580	skb->protocol = htons(ETH_P_FIP);
 581	skb->priority = fip->priority;
 582	skb_reset_mac_header(skb);
 583	skb_reset_network_header(skb);
 584	fip->send(fip, skb);
 585}
 586
 587/**
 588 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
 589 * @fip:   The FCoE controller for the ELS frame
 590 * @dtype: The FIP descriptor type for the frame
 591 * @skb:   The FCoE ELS frame including FC header but no FCoE headers
 592 * @d_id:  The destination port ID.
 593 *
 594 * Returns non-zero error code on failure.
 595 *
 596 * The caller must check that the length is a multiple of 4.
 597 *
 598 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
 599 * Headroom includes the FIP encapsulation description, FIP header, and
 600 * Ethernet header.  The tailroom is for the FIP MAC descriptor.
 601 */
 602static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
 603			    u8 dtype, struct sk_buff *skb, u32 d_id)
 604{
 605	struct fip_encaps_head {
 606		struct ethhdr eth;
 607		struct fip_header fip;
 608		struct fip_encaps encaps;
 609	} __packed * cap;
 610	struct fc_frame_header *fh;
 611	struct fip_mac_desc *mac;
 612	struct fcoe_fcf *fcf;
 613	size_t dlen;
 614	u16 fip_flags;
 615	u8 op;
 616
 617	fh = (struct fc_frame_header *)skb->data;
 618	op = *(u8 *)(fh + 1);
 619	dlen = sizeof(struct fip_encaps) + skb->len;	/* len before push */
 620	cap = skb_push(skb, sizeof(*cap));
 621	memset(cap, 0, sizeof(*cap));
 622
 623	if (lport->point_to_multipoint) {
 624		if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
 625			return -ENODEV;
 626		fip_flags = 0;
 627	} else {
 628		fcf = fip->sel_fcf;
 629		if (!fcf)
 630			return -ENODEV;
 631		fip_flags = fcf->flags;
 632		fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
 633					 FIP_FL_FPMA;
 634		if (!fip_flags)
 635			return -ENODEV;
 636		memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
 637	}
 638	memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
 639	cap->eth.h_proto = htons(ETH_P_FIP);
 640
 641	cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
 642	cap->fip.fip_op = htons(FIP_OP_LS);
 643	if (op == ELS_LS_ACC || op == ELS_LS_RJT)
 644		cap->fip.fip_subcode = FIP_SC_REP;
 645	else
 646		cap->fip.fip_subcode = FIP_SC_REQ;
 647	cap->fip.fip_flags = htons(fip_flags);
 648
 649	cap->encaps.fd_desc.fip_dtype = dtype;
 650	cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
 651
 652	if (op != ELS_LS_RJT) {
 653		dlen += sizeof(*mac);
 654		mac = skb_put_zero(skb, sizeof(*mac));
 
 655		mac->fd_desc.fip_dtype = FIP_DT_MAC;
 656		mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
 657		if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
 658			memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
 659		} else if (fip->mode == FIP_MODE_VN2VN) {
 660			hton24(mac->fd_mac, FIP_VN_FC_MAP);
 661			hton24(mac->fd_mac + 3, fip->port_id);
 662		} else if (fip_flags & FIP_FL_SPMA) {
 663			LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
 664			memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
 665		} else {
 666			LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
 667			/* FPMA only FLOGI.  Must leave the MAC desc zeroed. */
 668		}
 669	}
 670	cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
 671
 672	skb->protocol = htons(ETH_P_FIP);
 673	skb->priority = fip->priority;
 674	skb_reset_mac_header(skb);
 675	skb_reset_network_header(skb);
 676	return 0;
 677}
 678
 679/**
 680 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
 681 * @fip:	FCoE controller.
 682 * @lport:	libfc fc_lport to send from
 683 * @skb:	FCoE ELS frame including FC header but no FCoE headers.
 684 *
 685 * Returns a non-zero error code if the frame should not be sent.
 686 * Returns zero if the caller should send the frame with FCoE encapsulation.
 687 *
 688 * The caller must check that the length is a multiple of 4.
 689 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
 690 * The the skb must also be an fc_frame.
 691 *
 692 * This is called from the lower-level driver with spinlocks held,
 693 * so we must not take a mutex here.
 694 */
 695int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
 696		       struct sk_buff *skb)
 697{
 698	struct fc_frame *fp;
 699	struct fc_frame_header *fh;
 700	u16 old_xid;
 701	u8 op;
 702	u8 mac[ETH_ALEN];
 703
 704	fp = container_of(skb, struct fc_frame, skb);
 705	fh = (struct fc_frame_header *)skb->data;
 706	op = *(u8 *)(fh + 1);
 707
 708	if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
 709		old_xid = fip->flogi_oxid;
 710		fip->flogi_oxid = ntohs(fh->fh_ox_id);
 711		if (fip->state == FIP_ST_AUTO) {
 712			if (old_xid == FC_XID_UNKNOWN)
 713				fip->flogi_count = 0;
 714			fip->flogi_count++;
 715			if (fip->flogi_count < 3)
 716				goto drop;
 717			fcoe_ctlr_map_dest(fip);
 718			return 0;
 719		}
 720		if (fip->state == FIP_ST_NON_FIP)
 721			fcoe_ctlr_map_dest(fip);
 722	}
 723
 724	if (fip->state == FIP_ST_NON_FIP)
 725		return 0;
 726	if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
 727		goto drop;
 728	switch (op) {
 729	case ELS_FLOGI:
 730		op = FIP_DT_FLOGI;
 731		if (fip->mode == FIP_MODE_VN2VN)
 732			break;
 733		spin_lock_bh(&fip->ctlr_lock);
 734		kfree_skb(fip->flogi_req);
 735		fip->flogi_req = skb;
 736		fip->flogi_req_send = 1;
 737		spin_unlock_bh(&fip->ctlr_lock);
 738		schedule_work(&fip->timer_work);
 739		return -EINPROGRESS;
 740	case ELS_FDISC:
 741		if (ntoh24(fh->fh_s_id))
 742			return 0;
 743		op = FIP_DT_FDISC;
 744		break;
 745	case ELS_LOGO:
 746		if (fip->mode == FIP_MODE_VN2VN) {
 747			if (fip->state != FIP_ST_VNMP_UP)
 748				goto drop;
 749			if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
 750				goto drop;
 751		} else {
 752			if (fip->state != FIP_ST_ENABLED)
 753				return 0;
 754			if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
 755				return 0;
 756		}
 757		op = FIP_DT_LOGO;
 758		break;
 759	case ELS_LS_ACC:
 760		/*
 761		 * If non-FIP, we may have gotten an SID by accepting an FLOGI
 762		 * from a point-to-point connection.  Switch to using
 763		 * the source mac based on the SID.  The destination
 764		 * MAC in this case would have been set by receiving the
 765		 * FLOGI.
 766		 */
 767		if (fip->state == FIP_ST_NON_FIP) {
 768			if (fip->flogi_oxid == FC_XID_UNKNOWN)
 769				return 0;
 770			fip->flogi_oxid = FC_XID_UNKNOWN;
 771			fc_fcoe_set_mac(mac, fh->fh_d_id);
 772			fip->update_mac(lport, mac);
 773		}
 774		/* fall through */
 775	case ELS_LS_RJT:
 776		op = fr_encaps(fp);
 777		if (op)
 778			break;
 779		return 0;
 780	default:
 781		if (fip->state != FIP_ST_ENABLED &&
 782		    fip->state != FIP_ST_VNMP_UP)
 783			goto drop;
 784		return 0;
 785	}
 786	LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
 787			op, ntoh24(fh->fh_d_id));
 788	if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
 789		goto drop;
 790	fip->send(fip, skb);
 791	return -EINPROGRESS;
 792drop:
 793	LIBFCOE_FIP_DBG(fip, "drop els_send op %u d_id %x\n",
 794			op, ntoh24(fh->fh_d_id));
 795	kfree_skb(skb);
 796	return -EINVAL;
 797}
 798EXPORT_SYMBOL(fcoe_ctlr_els_send);
 799
 800/**
 801 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
 802 * @fip: The FCoE controller to free FCFs on
 803 *
 804 * Called with lock held and preemption disabled.
 805 *
 806 * An FCF is considered old if we have missed two advertisements.
 807 * That is, there have been no valid advertisement from it for 2.5
 808 * times its keep-alive period.
 809 *
 810 * In addition, determine the time when an FCF selection can occur.
 811 *
 812 * Also, increment the MissDiscAdvCount when no advertisement is received
 813 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
 814 *
 815 * Returns the time in jiffies for the next call.
 816 */
 817static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
 818{
 819	struct fcoe_fcf *fcf;
 820	struct fcoe_fcf *next;
 821	unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
 822	unsigned long deadline;
 823	unsigned long sel_time = 0;
 824	struct list_head del_list;
 825	struct fc_stats *stats;
 826
 827	INIT_LIST_HEAD(&del_list);
 828
 829	stats = per_cpu_ptr(fip->lp->stats, get_cpu());
 830
 831	list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
 832		deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
 833		if (fip->sel_fcf == fcf) {
 834			if (time_after(jiffies, deadline)) {
 835				stats->MissDiscAdvCount++;
 836				printk(KERN_INFO "libfcoe: host%d: "
 837				       "Missing Discovery Advertisement "
 838				       "for fab %16.16llx count %lld\n",
 839				       fip->lp->host->host_no, fcf->fabric_name,
 840				       stats->MissDiscAdvCount);
 841			} else if (time_after(next_timer, deadline))
 842				next_timer = deadline;
 843		}
 844
 845		deadline += fcf->fka_period;
 846		if (time_after_eq(jiffies, deadline)) {
 847			if (fip->sel_fcf == fcf)
 848				fip->sel_fcf = NULL;
 849			/*
 850			 * Move to delete list so we can call
 851			 * fcoe_sysfs_fcf_del (which can sleep)
 852			 * after the put_cpu().
 853			 */
 854			list_del(&fcf->list);
 855			list_add(&fcf->list, &del_list);
 
 
 856			stats->VLinkFailureCount++;
 857		} else {
 858			if (time_after(next_timer, deadline))
 859				next_timer = deadline;
 860			if (fcoe_ctlr_mtu_valid(fcf) &&
 861			    (!sel_time || time_before(sel_time, fcf->time)))
 862				sel_time = fcf->time;
 863		}
 864	}
 865	put_cpu();
 866
 867	list_for_each_entry_safe(fcf, next, &del_list, list) {
 868		/* Removes fcf from current list */
 869		fcoe_sysfs_fcf_del(fcf);
 870	}
 871
 872	if (sel_time && !fip->sel_fcf && !fip->sel_time) {
 873		sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
 874		fip->sel_time = sel_time;
 875	}
 876
 877	return next_timer;
 878}
 879
 880/**
 881 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
 882 * @fip: The FCoE controller receiving the advertisement
 883 * @skb: The received FIP advertisement frame
 884 * @fcf: The resulting FCF entry
 885 *
 886 * Returns zero on a valid parsed advertisement,
 887 * otherwise returns non zero value.
 888 */
 889static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
 890			       struct sk_buff *skb, struct fcoe_fcf *fcf)
 891{
 892	struct fip_header *fiph;
 893	struct fip_desc *desc = NULL;
 894	struct fip_wwn_desc *wwn;
 895	struct fip_fab_desc *fab;
 896	struct fip_fka_desc *fka;
 897	unsigned long t;
 898	size_t rlen;
 899	size_t dlen;
 900	u32 desc_mask;
 901
 902	memset(fcf, 0, sizeof(*fcf));
 903	fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
 904
 905	fiph = (struct fip_header *)skb->data;
 906	fcf->flags = ntohs(fiph->fip_flags);
 907
 908	/*
 909	 * mask of required descriptors. validating each one clears its bit.
 910	 */
 911	desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
 912			BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
 913
 914	rlen = ntohs(fiph->fip_dl_len) * 4;
 915	if (rlen + sizeof(*fiph) > skb->len)
 916		return -EINVAL;
 917
 918	desc = (struct fip_desc *)(fiph + 1);
 919	while (rlen > 0) {
 920		dlen = desc->fip_dlen * FIP_BPW;
 921		if (dlen < sizeof(*desc) || dlen > rlen)
 922			return -EINVAL;
 923		/* Drop Adv if there are duplicate critical descriptors */
 924		if ((desc->fip_dtype < 32) &&
 925		    !(desc_mask & 1U << desc->fip_dtype)) {
 926			LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
 927					"Descriptors in FIP adv\n");
 928			return -EINVAL;
 929		}
 930		switch (desc->fip_dtype) {
 931		case FIP_DT_PRI:
 932			if (dlen != sizeof(struct fip_pri_desc))
 933				goto len_err;
 934			fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
 935			desc_mask &= ~BIT(FIP_DT_PRI);
 936			break;
 937		case FIP_DT_MAC:
 938			if (dlen != sizeof(struct fip_mac_desc))
 939				goto len_err;
 940			memcpy(fcf->fcf_mac,
 941			       ((struct fip_mac_desc *)desc)->fd_mac,
 942			       ETH_ALEN);
 943			memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
 944			if (!is_valid_ether_addr(fcf->fcf_mac)) {
 945				LIBFCOE_FIP_DBG(fip,
 946					"Invalid MAC addr %pM in FIP adv\n",
 947					fcf->fcf_mac);
 948				return -EINVAL;
 949			}
 950			desc_mask &= ~BIT(FIP_DT_MAC);
 951			break;
 952		case FIP_DT_NAME:
 953			if (dlen != sizeof(struct fip_wwn_desc))
 954				goto len_err;
 955			wwn = (struct fip_wwn_desc *)desc;
 956			fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
 957			desc_mask &= ~BIT(FIP_DT_NAME);
 958			break;
 959		case FIP_DT_FAB:
 960			if (dlen != sizeof(struct fip_fab_desc))
 961				goto len_err;
 962			fab = (struct fip_fab_desc *)desc;
 963			fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
 964			fcf->vfid = ntohs(fab->fd_vfid);
 965			fcf->fc_map = ntoh24(fab->fd_map);
 966			desc_mask &= ~BIT(FIP_DT_FAB);
 967			break;
 968		case FIP_DT_FKA:
 969			if (dlen != sizeof(struct fip_fka_desc))
 970				goto len_err;
 971			fka = (struct fip_fka_desc *)desc;
 972			if (fka->fd_flags & FIP_FKA_ADV_D)
 973				fcf->fd_flags = 1;
 974			t = ntohl(fka->fd_fka_period);
 975			if (t >= FCOE_CTLR_MIN_FKA)
 976				fcf->fka_period = msecs_to_jiffies(t);
 977			desc_mask &= ~BIT(FIP_DT_FKA);
 978			break;
 979		case FIP_DT_MAP_OUI:
 980		case FIP_DT_FCOE_SIZE:
 981		case FIP_DT_FLOGI:
 982		case FIP_DT_FDISC:
 983		case FIP_DT_LOGO:
 984		case FIP_DT_ELP:
 985		default:
 986			LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
 987					"in FIP adv\n", desc->fip_dtype);
 988			/* standard says ignore unknown descriptors >= 128 */
 989			if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
 990				return -EINVAL;
 991			break;
 992		}
 993		desc = (struct fip_desc *)((char *)desc + dlen);
 994		rlen -= dlen;
 995	}
 996	if (!fcf->fc_map || (fcf->fc_map & 0x10000))
 997		return -EINVAL;
 998	if (!fcf->switch_name)
 999		return -EINVAL;
1000	if (desc_mask) {
1001		LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
1002				desc_mask);
1003		return -EINVAL;
1004	}
1005	return 0;
1006
1007len_err:
1008	LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1009			desc->fip_dtype, dlen);
1010	return -EINVAL;
1011}
1012
1013/**
1014 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
1015 * @fip: The FCoE controller receiving the advertisement
1016 * @skb: The received FIP packet
1017 */
1018static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1019{
1020	struct fcoe_fcf *fcf;
1021	struct fcoe_fcf new;
1022	unsigned long sol_tov = msecs_to_jiffies(FCOE_CTLR_SOL_TOV);
 
1023	int first = 0;
1024	int mtu_valid;
1025	int found = 0;
1026	int rc = 0;
1027
1028	if (fcoe_ctlr_parse_adv(fip, skb, &new))
1029		return;
1030
1031	mutex_lock(&fip->ctlr_mutex);
1032	first = list_empty(&fip->fcfs);
 
1033	list_for_each_entry(fcf, &fip->fcfs, list) {
1034		if (fcf->switch_name == new.switch_name &&
1035		    fcf->fabric_name == new.fabric_name &&
1036		    fcf->fc_map == new.fc_map &&
1037		    ether_addr_equal(fcf->fcf_mac, new.fcf_mac)) {
1038			found = 1;
1039			break;
1040		}
1041	}
1042	if (!found) {
1043		if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
1044			goto out;
1045
1046		fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
1047		if (!fcf)
1048			goto out;
1049
 
1050		memcpy(fcf, &new, sizeof(new));
1051		fcf->fip = fip;
1052		rc = fcoe_sysfs_fcf_add(fcf);
1053		if (rc) {
1054			printk(KERN_ERR "Failed to allocate sysfs instance "
1055			       "for FCF, fab %16.16llx mac %pM\n",
1056			       new.fabric_name, new.fcf_mac);
1057			kfree(fcf);
1058			goto out;
1059		}
1060	} else {
1061		/*
1062		 * Update the FCF's keep-alive descriptor flags.
1063		 * Other flag changes from new advertisements are
1064		 * ignored after a solicited advertisement is
1065		 * received and the FCF is selectable (usable).
1066		 */
1067		fcf->fd_flags = new.fd_flags;
1068		if (!fcoe_ctlr_fcf_usable(fcf))
1069			fcf->flags = new.flags;
1070
1071		if (fcf == fip->sel_fcf && !fcf->fd_flags) {
1072			fip->ctlr_ka_time -= fcf->fka_period;
1073			fip->ctlr_ka_time += new.fka_period;
1074			if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1075				mod_timer(&fip->timer, fip->ctlr_ka_time);
1076		}
1077		fcf->fka_period = new.fka_period;
1078		memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
1079	}
1080
1081	mtu_valid = fcoe_ctlr_mtu_valid(fcf);
1082	fcf->time = jiffies;
1083	if (!found)
1084		LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
1085				fcf->fabric_name, fcf->fcf_mac);
1086
1087	/*
1088	 * If this advertisement is not solicited and our max receive size
1089	 * hasn't been verified, send a solicited advertisement.
1090	 */
1091	if (!mtu_valid)
1092		fcoe_ctlr_solicit(fip, fcf);
1093
1094	/*
1095	 * If its been a while since we did a solicit, and this is
1096	 * the first advertisement we've received, do a multicast
1097	 * solicitation to gather as many advertisements as we can
1098	 * before selection occurs.
1099	 */
1100	if (first && time_after(jiffies, fip->sol_time + sol_tov))
1101		fcoe_ctlr_solicit(fip, NULL);
1102
1103	/*
1104	 * Put this FCF at the head of the list for priority among equals.
1105	 * This helps in the case of an NPV switch which insists we use
1106	 * the FCF that answers multicast solicitations, not the others that
1107	 * are sending periodic multicast advertisements.
1108	 */
1109	if (mtu_valid)
1110		list_move(&fcf->list, &fip->fcfs);
1111
1112	/*
1113	 * If this is the first validated FCF, note the time and
1114	 * set a timer to trigger selection.
1115	 */
1116	if (mtu_valid && !fip->sel_fcf && !fip->sel_time &&
1117	    fcoe_ctlr_fcf_usable(fcf)) {
1118		fip->sel_time = jiffies +
1119			msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1120		if (!timer_pending(&fip->timer) ||
1121		    time_before(fip->sel_time, fip->timer.expires))
1122			mod_timer(&fip->timer, fip->sel_time);
1123	}
1124
1125out:
1126	mutex_unlock(&fip->ctlr_mutex);
1127}
1128
1129/**
1130 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1131 * @fip: The FCoE controller which received the packet
1132 * @skb: The received FIP packet
1133 */
1134static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1135{
1136	struct fc_lport *lport = fip->lp;
1137	struct fip_header *fiph;
1138	struct fc_frame *fp = (struct fc_frame *)skb;
1139	struct fc_frame_header *fh = NULL;
1140	struct fip_desc *desc;
1141	struct fip_encaps *els;
1142	struct fcoe_fcf *sel;
1143	struct fc_stats *stats;
1144	enum fip_desc_type els_dtype = 0;
1145	u8 els_op;
1146	u8 sub;
1147	u8 granted_mac[ETH_ALEN] = { 0 };
1148	size_t els_len = 0;
1149	size_t rlen;
1150	size_t dlen;
1151	u32 desc_mask = 0;
1152	u32 desc_cnt = 0;
1153
1154	fiph = (struct fip_header *)skb->data;
1155	sub = fiph->fip_subcode;
1156	if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1157		goto drop;
1158
1159	rlen = ntohs(fiph->fip_dl_len) * 4;
1160	if (rlen + sizeof(*fiph) > skb->len)
1161		goto drop;
1162
1163	desc = (struct fip_desc *)(fiph + 1);
1164	while (rlen > 0) {
1165		desc_cnt++;
1166		dlen = desc->fip_dlen * FIP_BPW;
1167		if (dlen < sizeof(*desc) || dlen > rlen)
1168			goto drop;
1169		/* Drop ELS if there are duplicate critical descriptors */
1170		if (desc->fip_dtype < 32) {
1171			if ((desc->fip_dtype != FIP_DT_MAC) &&
1172			    (desc_mask & 1U << desc->fip_dtype)) {
1173				LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1174						"Descriptors in FIP ELS\n");
1175				goto drop;
1176			}
1177			desc_mask |= (1 << desc->fip_dtype);
1178		}
1179		switch (desc->fip_dtype) {
1180		case FIP_DT_MAC:
1181			sel = fip->sel_fcf;
1182			if (desc_cnt == 1) {
1183				LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1184						"received out of order\n");
1185				goto drop;
1186			}
1187			/*
1188			 * Some switch implementations send two MAC descriptors,
1189			 * with first MAC(granted_mac) being the FPMA, and the
1190			 * second one(fcoe_mac) is used as destination address
1191			 * for sending/receiving FCoE packets. FIP traffic is
1192			 * sent using fip_mac. For regular switches, both
1193			 * fip_mac and fcoe_mac would be the same.
1194			 */
1195			if (desc_cnt == 2)
1196				memcpy(granted_mac,
1197				       ((struct fip_mac_desc *)desc)->fd_mac,
1198				       ETH_ALEN);
1199
1200			if (dlen != sizeof(struct fip_mac_desc))
1201				goto len_err;
1202
1203			if ((desc_cnt == 3) && (sel))
1204				memcpy(sel->fcoe_mac,
1205				       ((struct fip_mac_desc *)desc)->fd_mac,
1206				       ETH_ALEN);
1207			break;
1208		case FIP_DT_FLOGI:
1209		case FIP_DT_FDISC:
1210		case FIP_DT_LOGO:
1211		case FIP_DT_ELP:
1212			if (desc_cnt != 1) {
1213				LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1214						"received out of order\n");
1215				goto drop;
1216			}
1217			if (fh)
1218				goto drop;
1219			if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1220				goto len_err;
1221			els_len = dlen - sizeof(*els);
1222			els = (struct fip_encaps *)desc;
1223			fh = (struct fc_frame_header *)(els + 1);
1224			els_dtype = desc->fip_dtype;
1225			break;
1226		default:
1227			LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1228					"in FIP adv\n", desc->fip_dtype);
1229			/* standard says ignore unknown descriptors >= 128 */
1230			if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1231				goto drop;
1232			if (desc_cnt <= 2) {
1233				LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1234						"received out of order\n");
1235				goto drop;
1236			}
1237			break;
1238		}
1239		desc = (struct fip_desc *)((char *)desc + dlen);
1240		rlen -= dlen;
1241	}
1242
1243	if (!fh)
1244		goto drop;
1245	els_op = *(u8 *)(fh + 1);
1246
1247	if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1248	    sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1249		if (els_op == ELS_LS_ACC) {
1250			if (!is_valid_ether_addr(granted_mac)) {
1251				LIBFCOE_FIP_DBG(fip,
1252					"Invalid MAC address %pM in FIP ELS\n",
1253					granted_mac);
1254				goto drop;
1255			}
1256			memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1257
1258			if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1259				fip->flogi_oxid = FC_XID_UNKNOWN;
1260				if (els_dtype == FIP_DT_FLOGI)
1261					fcoe_ctlr_announce(fip);
1262			}
1263		} else if (els_dtype == FIP_DT_FLOGI &&
1264			   !fcoe_ctlr_flogi_retry(fip))
1265			goto drop;	/* retrying FLOGI so drop reject */
1266	}
1267
1268	if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1269	    (!(1U << FIP_DT_MAC & desc_mask)))) {
1270		LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1271				"in FIP ELS\n");
1272		goto drop;
1273	}
1274
1275	/*
1276	 * Convert skb into an fc_frame containing only the ELS.
1277	 */
1278	skb_pull(skb, (u8 *)fh - skb->data);
1279	skb_trim(skb, els_len);
1280	fp = (struct fc_frame *)skb;
1281	fc_frame_init(fp);
1282	fr_sof(fp) = FC_SOF_I3;
1283	fr_eof(fp) = FC_EOF_T;
1284	fr_dev(fp) = lport;
1285	fr_encaps(fp) = els_dtype;
1286
1287	stats = per_cpu_ptr(lport->stats, get_cpu());
1288	stats->RxFrames++;
1289	stats->RxWords += skb->len / FIP_BPW;
1290	put_cpu();
1291
1292	fc_exch_recv(lport, fp);
1293	return;
1294
1295len_err:
1296	LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1297			desc->fip_dtype, dlen);
1298drop:
1299	kfree_skb(skb);
1300}
1301
1302/**
1303 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1304 * @fip: The FCoE controller that received the frame
1305 * @fh:	 The received FIP header
1306 *
1307 * There may be multiple VN_Port descriptors.
1308 * The overall length has already been checked.
1309 */
1310static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1311				     struct sk_buff *skb)
1312{
1313	struct fip_desc *desc;
1314	struct fip_mac_desc *mp;
1315	struct fip_wwn_desc *wp;
1316	struct fip_vn_desc *vp;
1317	size_t rlen;
1318	size_t dlen;
1319	struct fcoe_fcf *fcf = fip->sel_fcf;
1320	struct fc_lport *lport = fip->lp;
1321	struct fc_lport *vn_port = NULL;
1322	u32 desc_mask;
1323	int num_vlink_desc;
1324	int reset_phys_port = 0;
1325	struct fip_vn_desc **vlink_desc_arr = NULL;
1326	struct fip_header *fh = (struct fip_header *)skb->data;
1327	struct ethhdr *eh = eth_hdr(skb);
1328
1329	LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1330
1331	if (!fcf) {
1332		/*
1333		 * We are yet to select best FCF, but we got CVL in the
1334		 * meantime. reset the ctlr and let it rediscover the FCF
1335		 */
1336		LIBFCOE_FIP_DBG(fip, "Resetting fcoe_ctlr as FCF has not been "
1337		    "selected yet\n");
1338		mutex_lock(&fip->ctlr_mutex);
1339		fcoe_ctlr_reset(fip);
1340		mutex_unlock(&fip->ctlr_mutex);
1341		return;
1342	}
1343
1344	/*
1345	 * If we've selected an FCF check that the CVL is from there to avoid
1346	 * processing CVLs from an unexpected source.  If it is from an
1347	 * unexpected source drop it on the floor.
1348	 */
1349	if (!ether_addr_equal(eh->h_source, fcf->fcf_mac)) {
1350		LIBFCOE_FIP_DBG(fip, "Dropping CVL due to source address "
1351		    "mismatch with FCF src=%pM\n", eh->h_source);
1352		return;
1353	}
1354
1355	/*
1356	 * If we haven't logged into the fabric but receive a CVL we should
1357	 * reset everything and go back to solicitation.
1358	 */
1359	if (!lport->port_id) {
1360		LIBFCOE_FIP_DBG(fip, "lport not logged in, resoliciting\n");
1361		mutex_lock(&fip->ctlr_mutex);
1362		fcoe_ctlr_reset(fip);
1363		mutex_unlock(&fip->ctlr_mutex);
1364		fc_lport_reset(fip->lp);
1365		fcoe_ctlr_solicit(fip, NULL);
1366		return;
1367	}
1368
1369	/*
1370	 * mask of required descriptors.  Validating each one clears its bit.
1371	 */
1372	desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
1373
1374	rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1375	desc = (struct fip_desc *)(fh + 1);
1376
1377	/*
1378	 * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1379	 * before determining max Vx_Port descriptor but a buggy FCF could have
1380	 * omitted either or both MAC Address and Name Identifier descriptors
1381	 */
1382	num_vlink_desc = rlen / sizeof(*vp);
1383	if (num_vlink_desc)
1384		vlink_desc_arr = kmalloc_array(num_vlink_desc, sizeof(vp),
1385					       GFP_ATOMIC);
1386	if (!vlink_desc_arr)
1387		return;
1388	num_vlink_desc = 0;
1389
1390	while (rlen >= sizeof(*desc)) {
1391		dlen = desc->fip_dlen * FIP_BPW;
1392		if (dlen > rlen)
1393			goto err;
1394		/* Drop CVL if there are duplicate critical descriptors */
1395		if ((desc->fip_dtype < 32) &&
1396		    (desc->fip_dtype != FIP_DT_VN_ID) &&
1397		    !(desc_mask & 1U << desc->fip_dtype)) {
1398			LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1399					"Descriptors in FIP CVL\n");
1400			goto err;
1401		}
1402		switch (desc->fip_dtype) {
1403		case FIP_DT_MAC:
1404			mp = (struct fip_mac_desc *)desc;
1405			if (dlen < sizeof(*mp))
1406				goto err;
1407			if (!ether_addr_equal(mp->fd_mac, fcf->fcf_mac))
1408				goto err;
1409			desc_mask &= ~BIT(FIP_DT_MAC);
1410			break;
1411		case FIP_DT_NAME:
1412			wp = (struct fip_wwn_desc *)desc;
1413			if (dlen < sizeof(*wp))
1414				goto err;
1415			if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1416				goto err;
1417			desc_mask &= ~BIT(FIP_DT_NAME);
1418			break;
1419		case FIP_DT_VN_ID:
1420			vp = (struct fip_vn_desc *)desc;
1421			if (dlen < sizeof(*vp))
1422				goto err;
1423			vlink_desc_arr[num_vlink_desc++] = vp;
1424			vn_port = fc_vport_id_lookup(lport,
1425						      ntoh24(vp->fd_fc_id));
1426			if (vn_port && (vn_port == lport)) {
1427				mutex_lock(&fip->ctlr_mutex);
1428				per_cpu_ptr(lport->stats,
1429					    get_cpu())->VLinkFailureCount++;
1430				put_cpu();
1431				fcoe_ctlr_reset(fip);
1432				mutex_unlock(&fip->ctlr_mutex);
1433			}
1434			break;
1435		default:
1436			/* standard says ignore unknown descriptors >= 128 */
1437			if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1438				goto err;
1439			break;
1440		}
1441		desc = (struct fip_desc *)((char *)desc + dlen);
1442		rlen -= dlen;
1443	}
1444
1445	/*
1446	 * reset only if all required descriptors were present and valid.
1447	 */
1448	if (desc_mask)
1449		LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1450				desc_mask);
1451	else if (!num_vlink_desc) {
1452		LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1453		/*
1454		 * No Vx_Port description. Clear all NPIV ports,
1455		 * followed by physical port
1456		 */
 
 
 
 
 
1457		mutex_lock(&fip->ctlr_mutex);
1458		per_cpu_ptr(lport->stats, get_cpu())->VLinkFailureCount++;
 
1459		put_cpu();
1460		fcoe_ctlr_reset(fip);
1461		mutex_unlock(&fip->ctlr_mutex);
1462
1463		mutex_lock(&lport->lp_mutex);
1464		list_for_each_entry(vn_port, &lport->vports, list)
1465			fc_lport_reset(vn_port);
1466		mutex_unlock(&lport->lp_mutex);
1467
1468		fc_lport_reset(fip->lp);
1469		fcoe_ctlr_solicit(fip, NULL);
1470	} else {
1471		int i;
1472
1473		LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1474		for (i = 0; i < num_vlink_desc; i++) {
1475			vp = vlink_desc_arr[i];
1476			vn_port = fc_vport_id_lookup(lport,
1477						     ntoh24(vp->fd_fc_id));
1478			if (!vn_port)
1479				continue;
1480
1481			/*
1482			 * 'port_id' is already validated, check MAC address and
1483			 * wwpn
1484			 */
1485			if (!ether_addr_equal(fip->get_src_addr(vn_port),
1486					      vp->fd_mac) ||
1487				get_unaligned_be64(&vp->fd_wwpn) !=
1488							vn_port->wwpn)
1489				continue;
1490
1491			if (vn_port == lport)
1492				/*
1493				 * Physical port, defer processing till all
1494				 * listed NPIV ports are cleared
1495				 */
1496				reset_phys_port = 1;
1497			else    /* NPIV port */
1498				fc_lport_reset(vn_port);
1499		}
1500
1501		if (reset_phys_port) {
1502			fc_lport_reset(fip->lp);
1503			fcoe_ctlr_solicit(fip, NULL);
1504		}
1505	}
1506
1507err:
1508	kfree(vlink_desc_arr);
1509}
1510
1511/**
1512 * fcoe_ctlr_recv() - Receive a FIP packet
1513 * @fip: The FCoE controller that received the packet
1514 * @skb: The received FIP packet
1515 *
1516 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1517 */
1518void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1519{
1520	skb = skb_share_check(skb, GFP_ATOMIC);
1521	if (!skb)
1522		return;
1523	skb_queue_tail(&fip->fip_recv_list, skb);
1524	schedule_work(&fip->recv_work);
1525}
1526EXPORT_SYMBOL(fcoe_ctlr_recv);
1527
1528/**
1529 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1530 * @fip: The FCoE controller that received the frame
1531 * @skb: The received FIP frame
1532 *
1533 * Returns non-zero if the frame is dropped.
1534 */
1535static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1536{
1537	struct fip_header *fiph;
1538	struct ethhdr *eh;
1539	enum fip_state state;
1540	bool fip_vlan_resp = false;
1541	u16 op;
1542	u8 sub;
1543
1544	if (skb_linearize(skb))
1545		goto drop;
1546	if (skb->len < sizeof(*fiph))
1547		goto drop;
1548	eh = eth_hdr(skb);
1549	if (fip->mode == FIP_MODE_VN2VN) {
1550		if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1551		    !ether_addr_equal(eh->h_dest, fcoe_all_vn2vn) &&
1552		    !ether_addr_equal(eh->h_dest, fcoe_all_p2p))
1553			goto drop;
1554	} else if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1555		   !ether_addr_equal(eh->h_dest, fcoe_all_enode))
1556		goto drop;
1557	fiph = (struct fip_header *)skb->data;
1558	op = ntohs(fiph->fip_op);
1559	sub = fiph->fip_subcode;
1560
1561	if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1562		goto drop;
1563	if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1564		goto drop;
1565
1566	mutex_lock(&fip->ctlr_mutex);
1567	state = fip->state;
1568	if (state == FIP_ST_AUTO) {
1569		fip->map_dest = 0;
1570		fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1571		state = FIP_ST_ENABLED;
1572		LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1573	}
1574	fip_vlan_resp = fip->fip_resp;
1575	mutex_unlock(&fip->ctlr_mutex);
1576
1577	if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1578		return fcoe_ctlr_vn_recv(fip, skb);
1579
1580	if (fip_vlan_resp && op == FIP_OP_VLAN) {
1581		LIBFCOE_FIP_DBG(fip, "fip vlan discovery\n");
1582		return fcoe_ctlr_vlan_recv(fip, skb);
1583	}
1584
1585	if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1586	    state != FIP_ST_VNMP_CLAIM)
1587		goto drop;
1588
1589	if (op == FIP_OP_LS) {
1590		fcoe_ctlr_recv_els(fip, skb);	/* consumes skb */
1591		return 0;
1592	}
1593
1594	if (state != FIP_ST_ENABLED)
1595		goto drop;
1596
1597	if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1598		fcoe_ctlr_recv_adv(fip, skb);
1599	else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1600		fcoe_ctlr_recv_clr_vlink(fip, skb);
1601	kfree_skb(skb);
1602	return 0;
1603drop:
1604	kfree_skb(skb);
1605	return -1;
1606}
1607
1608/**
1609 * fcoe_ctlr_select() - Select the best FCF (if possible)
1610 * @fip: The FCoE controller
1611 *
1612 * Returns the selected FCF, or NULL if none are usable.
1613 *
1614 * If there are conflicting advertisements, no FCF can be chosen.
1615 *
1616 * If there is already a selected FCF, this will choose a better one or
1617 * an equivalent one that hasn't already been sent a FLOGI.
1618 *
1619 * Called with lock held.
1620 */
1621static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
1622{
1623	struct fcoe_fcf *fcf;
1624	struct fcoe_fcf *best = fip->sel_fcf;
 
 
 
1625
1626	list_for_each_entry(fcf, &fip->fcfs, list) {
1627		LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1628				"VFID %d mac %pM map %x val %d "
1629				"sent %u pri %u\n",
1630				fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1631				fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1632				fcf->flogi_sent, fcf->pri);
 
 
 
 
 
 
 
 
 
1633		if (!fcoe_ctlr_fcf_usable(fcf)) {
1634			LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1635					"map %x %svalid %savailable\n",
1636					fcf->fabric_name, fcf->fc_map,
1637					(fcf->flags & FIP_FL_SOL) ? "" : "in",
1638					(fcf->flags & FIP_FL_AVAIL) ?
1639					"" : "un");
1640			continue;
1641		}
1642		if (!best || fcf->pri < best->pri || best->flogi_sent)
1643			best = fcf;
1644		if (fcf->fabric_name != best->fabric_name ||
1645		    fcf->vfid != best->vfid ||
1646		    fcf->fc_map != best->fc_map) {
1647			LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1648					"or FC-MAP\n");
1649			return NULL;
1650		}
1651	}
1652	fip->sel_fcf = best;
1653	if (best) {
1654		LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
1655		fip->port_ka_time = jiffies +
1656			msecs_to_jiffies(FIP_VN_KA_PERIOD);
1657		fip->ctlr_ka_time = jiffies + best->fka_period;
1658		if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1659			mod_timer(&fip->timer, fip->ctlr_ka_time);
1660	}
1661	return best;
1662}
1663
1664/**
1665 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1666 * @fip: The FCoE controller
1667 *
1668 * Returns non-zero error if it could not be sent.
1669 *
1670 * Called with ctlr_mutex and ctlr_lock held.
1671 * Caller must verify that fip->sel_fcf is not NULL.
1672 */
1673static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1674{
1675	struct sk_buff *skb;
1676	struct sk_buff *skb_orig;
1677	struct fc_frame_header *fh;
1678	int error;
1679
1680	skb_orig = fip->flogi_req;
1681	if (!skb_orig)
1682		return -EINVAL;
1683
1684	/*
1685	 * Clone and send the FLOGI request.  If clone fails, use original.
1686	 */
1687	skb = skb_clone(skb_orig, GFP_ATOMIC);
1688	if (!skb) {
1689		skb = skb_orig;
1690		fip->flogi_req = NULL;
1691	}
1692	fh = (struct fc_frame_header *)skb->data;
1693	error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1694				 ntoh24(fh->fh_d_id));
1695	if (error) {
1696		kfree_skb(skb);
1697		return error;
1698	}
1699	fip->send(fip, skb);
1700	fip->sel_fcf->flogi_sent = 1;
1701	return 0;
1702}
1703
1704/**
1705 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1706 * @fip: The FCoE controller
1707 *
1708 * Returns non-zero error code if there's no FLOGI request to retry or
1709 * no alternate FCF available.
1710 */
1711static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1712{
1713	struct fcoe_fcf *fcf;
1714	int error;
1715
1716	mutex_lock(&fip->ctlr_mutex);
1717	spin_lock_bh(&fip->ctlr_lock);
1718	LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1719	fcf = fcoe_ctlr_select(fip);
1720	if (!fcf || fcf->flogi_sent) {
1721		kfree_skb(fip->flogi_req);
1722		fip->flogi_req = NULL;
1723		error = -ENOENT;
1724	} else {
1725		fcoe_ctlr_solicit(fip, NULL);
1726		error = fcoe_ctlr_flogi_send_locked(fip);
1727	}
1728	spin_unlock_bh(&fip->ctlr_lock);
1729	mutex_unlock(&fip->ctlr_mutex);
1730	return error;
1731}
1732
1733
1734/**
1735 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1736 * @fip: The FCoE controller that timed out
1737 *
1738 * Done here because fcoe_ctlr_els_send() can't get mutex.
1739 *
1740 * Called with ctlr_mutex held.  The caller must not hold ctlr_lock.
1741 */
1742static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1743{
1744	struct fcoe_fcf *fcf;
1745
1746	spin_lock_bh(&fip->ctlr_lock);
1747	fcf = fip->sel_fcf;
1748	if (!fcf || !fip->flogi_req_send)
1749		goto unlock;
1750
1751	LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1752
1753	/*
1754	 * If this FLOGI is being sent due to a timeout retry
1755	 * to the same FCF as before, select a different FCF if possible.
1756	 */
1757	if (fcf->flogi_sent) {
1758		LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1759		fcf = fcoe_ctlr_select(fip);
1760		if (!fcf || fcf->flogi_sent) {
1761			LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1762			list_for_each_entry(fcf, &fip->fcfs, list)
1763				fcf->flogi_sent = 0;
1764			fcf = fcoe_ctlr_select(fip);
1765		}
1766	}
1767	if (fcf) {
1768		fcoe_ctlr_flogi_send_locked(fip);
1769		fip->flogi_req_send = 0;
1770	} else /* XXX */
1771		LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1772unlock:
1773	spin_unlock_bh(&fip->ctlr_lock);
1774}
1775
1776/**
1777 * fcoe_ctlr_timeout() - FIP timeout handler
1778 * @arg: The FCoE controller that timed out
1779 */
1780static void fcoe_ctlr_timeout(struct timer_list *t)
1781{
1782	struct fcoe_ctlr *fip = from_timer(fip, t, timer);
1783
1784	schedule_work(&fip->timer_work);
1785}
1786
1787/**
1788 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1789 * @work: Handle to a FCoE controller
1790 *
1791 * Ages FCFs.  Triggers FCF selection if possible.
1792 * Sends keep-alives and resets.
1793 */
1794static void fcoe_ctlr_timer_work(struct work_struct *work)
1795{
1796	struct fcoe_ctlr *fip;
1797	struct fc_lport *vport;
1798	u8 *mac;
1799	u8 reset = 0;
1800	u8 send_ctlr_ka = 0;
1801	u8 send_port_ka = 0;
1802	struct fcoe_fcf *sel;
1803	struct fcoe_fcf *fcf;
1804	unsigned long next_timer;
1805
1806	fip = container_of(work, struct fcoe_ctlr, timer_work);
1807	if (fip->mode == FIP_MODE_VN2VN)
1808		return fcoe_ctlr_vn_timeout(fip);
1809	mutex_lock(&fip->ctlr_mutex);
1810	if (fip->state == FIP_ST_DISABLED) {
1811		mutex_unlock(&fip->ctlr_mutex);
1812		return;
1813	}
1814
1815	fcf = fip->sel_fcf;
1816	next_timer = fcoe_ctlr_age_fcfs(fip);
1817
1818	sel = fip->sel_fcf;
1819	if (!sel && fip->sel_time) {
1820		if (time_after_eq(jiffies, fip->sel_time)) {
1821			sel = fcoe_ctlr_select(fip);
1822			fip->sel_time = 0;
1823		} else if (time_after(next_timer, fip->sel_time))
1824			next_timer = fip->sel_time;
1825	}
1826
1827	if (sel && fip->flogi_req_send)
1828		fcoe_ctlr_flogi_send(fip);
1829	else if (!sel && fcf)
1830		reset = 1;
1831
1832	if (sel && !sel->fd_flags) {
1833		if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1834			fip->ctlr_ka_time = jiffies + sel->fka_period;
1835			send_ctlr_ka = 1;
1836		}
1837		if (time_after(next_timer, fip->ctlr_ka_time))
1838			next_timer = fip->ctlr_ka_time;
1839
1840		if (time_after_eq(jiffies, fip->port_ka_time)) {
1841			fip->port_ka_time = jiffies +
1842				msecs_to_jiffies(FIP_VN_KA_PERIOD);
1843			send_port_ka = 1;
1844		}
1845		if (time_after(next_timer, fip->port_ka_time))
1846			next_timer = fip->port_ka_time;
1847	}
1848	if (!list_empty(&fip->fcfs))
1849		mod_timer(&fip->timer, next_timer);
1850	mutex_unlock(&fip->ctlr_mutex);
1851
1852	if (reset) {
1853		fc_lport_reset(fip->lp);
1854		/* restart things with a solicitation */
1855		fcoe_ctlr_solicit(fip, NULL);
1856	}
1857
1858	if (send_ctlr_ka)
1859		fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1860
1861	if (send_port_ka) {
1862		mutex_lock(&fip->lp->lp_mutex);
1863		mac = fip->get_src_addr(fip->lp);
1864		fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1865		list_for_each_entry(vport, &fip->lp->vports, list) {
1866			mac = fip->get_src_addr(vport);
1867			fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1868		}
1869		mutex_unlock(&fip->lp->lp_mutex);
1870	}
1871}
1872
1873/**
1874 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1875 * @recv_work: Handle to a FCoE controller
1876 */
1877static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1878{
1879	struct fcoe_ctlr *fip;
1880	struct sk_buff *skb;
1881
1882	fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1883	while ((skb = skb_dequeue(&fip->fip_recv_list)))
1884		fcoe_ctlr_recv_handler(fip, skb);
1885}
1886
1887/**
1888 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1889 * @fip: The FCoE controller
1890 * @fp:	 The FC frame to snoop
1891 *
1892 * Snoop potential response to FLOGI or even incoming FLOGI.
1893 *
1894 * The caller has checked that we are waiting for login as indicated
1895 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1896 *
1897 * The caller is responsible for freeing the frame.
1898 * Fill in the granted_mac address.
1899 *
1900 * Return non-zero if the frame should not be delivered to libfc.
1901 */
1902int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1903			 struct fc_frame *fp)
1904{
1905	struct fc_frame_header *fh;
1906	u8 op;
1907	u8 *sa;
1908
1909	sa = eth_hdr(&fp->skb)->h_source;
1910	fh = fc_frame_header_get(fp);
1911	if (fh->fh_type != FC_TYPE_ELS)
1912		return 0;
1913
1914	op = fc_frame_payload_op(fp);
1915	if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1916	    fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1917
1918		mutex_lock(&fip->ctlr_mutex);
1919		if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1920			mutex_unlock(&fip->ctlr_mutex);
1921			return -EINVAL;
1922		}
1923		fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1924		LIBFCOE_FIP_DBG(fip,
1925				"received FLOGI LS_ACC using non-FIP mode\n");
1926
1927		/*
1928		 * FLOGI accepted.
1929		 * If the src mac addr is FC_OUI-based, then we mark the
1930		 * address_mode flag to use FC_OUI-based Ethernet DA.
1931		 * Otherwise we use the FCoE gateway addr
1932		 */
1933		if (ether_addr_equal(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1934			fcoe_ctlr_map_dest(fip);
1935		} else {
1936			memcpy(fip->dest_addr, sa, ETH_ALEN);
1937			fip->map_dest = 0;
1938		}
1939		fip->flogi_oxid = FC_XID_UNKNOWN;
1940		mutex_unlock(&fip->ctlr_mutex);
1941		fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1942	} else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1943		/*
1944		 * Save source MAC for point-to-point responses.
1945		 */
1946		mutex_lock(&fip->ctlr_mutex);
1947		if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1948			memcpy(fip->dest_addr, sa, ETH_ALEN);
1949			fip->map_dest = 0;
1950			if (fip->state == FIP_ST_AUTO)
1951				LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1952						"Setting non-FIP mode\n");
1953			fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1954		}
1955		mutex_unlock(&fip->ctlr_mutex);
1956	}
1957	return 0;
1958}
1959EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1960
1961/**
1962 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1963 * @mac:    The MAC address to convert
1964 * @scheme: The scheme to use when converting
1965 * @port:   The port indicator for converting
1966 *
1967 * Returns: u64 fc world wide name
1968 */
1969u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1970		      unsigned int scheme, unsigned int port)
1971{
1972	u64 wwn;
1973	u64 host_mac;
1974
1975	/* The MAC is in NO, so flip only the low 48 bits */
1976	host_mac = ((u64) mac[0] << 40) |
1977		((u64) mac[1] << 32) |
1978		((u64) mac[2] << 24) |
1979		((u64) mac[3] << 16) |
1980		((u64) mac[4] << 8) |
1981		(u64) mac[5];
1982
1983	WARN_ON(host_mac >= (1ULL << 48));
1984	wwn = host_mac | ((u64) scheme << 60);
1985	switch (scheme) {
1986	case 1:
1987		WARN_ON(port != 0);
1988		break;
1989	case 2:
1990		WARN_ON(port >= 0xfff);
1991		wwn |= (u64) port << 48;
1992		break;
1993	default:
1994		WARN_ON(1);
1995		break;
1996	}
1997
1998	return wwn;
1999}
2000EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
2001
2002/**
2003 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
2004 * @rdata: libfc remote port
2005 */
2006static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
2007{
2008	return container_of(rdata, struct fcoe_rport, rdata);
2009}
2010
2011/**
2012 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
2013 * @fip: The FCoE controller
2014 * @sub: sub-opcode for probe request, reply, or advertisement.
2015 * @dest: The destination Ethernet MAC address
2016 * @min_len: minimum size of the Ethernet payload to be sent
2017 */
2018static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
2019			      enum fip_vn2vn_subcode sub,
2020			      const u8 *dest, size_t min_len)
2021{
2022	struct sk_buff *skb;
2023	struct fip_vn2vn_probe_frame {
2024		struct ethhdr eth;
2025		struct fip_header fip;
2026		struct fip_mac_desc mac;
2027		struct fip_wwn_desc wwnn;
2028		struct fip_vn_desc vn;
2029	} __packed * frame;
2030	struct fip_fc4_feat *ff;
2031	struct fip_size_desc *size;
2032	u32 fcp_feat;
2033	size_t len;
2034	size_t dlen;
2035
2036	len = sizeof(*frame);
2037	dlen = 0;
2038	if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2039		dlen = sizeof(struct fip_fc4_feat) +
2040		       sizeof(struct fip_size_desc);
2041		len += dlen;
2042	}
2043	dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
2044	len = max(len, min_len + sizeof(struct ethhdr));
2045
2046	skb = dev_alloc_skb(len);
2047	if (!skb)
2048		return;
2049
2050	frame = (struct fip_vn2vn_probe_frame *)skb->data;
2051	memset(frame, 0, len);
2052	memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2053
2054	if (sub == FIP_SC_VN_BEACON) {
2055		hton24(frame->eth.h_source, FIP_VN_FC_MAP);
2056		hton24(frame->eth.h_source + 3, fip->port_id);
2057	} else {
2058		memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2059	}
2060	frame->eth.h_proto = htons(ETH_P_FIP);
2061
2062	frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2063	frame->fip.fip_op = htons(FIP_OP_VN2VN);
2064	frame->fip.fip_subcode = sub;
2065	frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2066
2067	frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2068	frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2069	memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2070
2071	frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
2072	frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
2073	put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
2074
2075	frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
2076	frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
2077	hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
2078	hton24(frame->vn.fd_mac + 3, fip->port_id);
2079	hton24(frame->vn.fd_fc_id, fip->port_id);
2080	put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
2081
2082	/*
2083	 * For claims, add FC-4 features.
2084	 * TBD: Add interface to get fc-4 types and features from libfc.
2085	 */
2086	if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2087		ff = (struct fip_fc4_feat *)(frame + 1);
2088		ff->fd_desc.fip_dtype = FIP_DT_FC4F;
2089		ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
2090		ff->fd_fts = fip->lp->fcts;
2091
2092		fcp_feat = 0;
2093		if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
2094			fcp_feat |= FCP_FEAT_INIT;
2095		if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
2096			fcp_feat |= FCP_FEAT_TARG;
2097		fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
2098		ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
2099
2100		size = (struct fip_size_desc *)(ff + 1);
2101		size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
2102		size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
2103		size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
2104	}
2105
2106	skb_put(skb, len);
2107	skb->protocol = htons(ETH_P_FIP);
2108	skb->priority = fip->priority;
2109	skb_reset_mac_header(skb);
2110	skb_reset_network_header(skb);
2111
2112	fip->send(fip, skb);
2113}
2114
2115/**
2116 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
2117 * @lport: The lport which is receiving the event
2118 * @rdata: remote port private data
2119 * @event: The event that occurred
2120 *
2121 * Locking Note:  The rport lock must not be held when calling this function.
2122 */
2123static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
2124					struct fc_rport_priv *rdata,
2125					enum fc_rport_event event)
2126{
2127	struct fcoe_ctlr *fip = lport->disc.priv;
2128	struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2129
2130	LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
2131			rdata->ids.port_id, event);
2132
2133	mutex_lock(&fip->ctlr_mutex);
2134	switch (event) {
2135	case RPORT_EV_READY:
2136		frport->login_count = 0;
2137		break;
2138	case RPORT_EV_LOGO:
2139	case RPORT_EV_FAILED:
2140	case RPORT_EV_STOP:
2141		frport->login_count++;
2142		if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
2143			LIBFCOE_FIP_DBG(fip,
2144					"rport FLOGI limited port_id %6.6x\n",
2145					rdata->ids.port_id);
2146			fc_rport_logoff(rdata);
2147		}
2148		break;
2149	default:
2150		break;
2151	}
2152	mutex_unlock(&fip->ctlr_mutex);
2153}
2154
2155static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
2156	.event_callback = fcoe_ctlr_vn_rport_callback,
2157};
2158
2159/**
2160 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
2161 * @fip: The FCoE controller
2162 *
2163 * Called with ctlr_mutex held.
2164 */
2165static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
2166{
2167	struct fc_rport_priv *rdata;
2168
2169	mutex_lock(&lport->disc.disc_mutex);
2170	list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2171		if (kref_get_unless_zero(&rdata->kref)) {
2172			fc_rport_logoff(rdata);
2173			kref_put(&rdata->kref, fc_rport_destroy);
2174		}
2175	}
2176	lport->disc.disc_callback = NULL;
2177	mutex_unlock(&lport->disc.disc_mutex);
2178}
2179
2180/**
2181 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2182 * @fip: The FCoE controller
2183 *
2184 * Called through the local port template for discovery.
2185 * Called without the ctlr_mutex held.
2186 */
2187static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2188{
2189	struct fcoe_ctlr *fip = lport->disc.priv;
2190
2191	mutex_lock(&fip->ctlr_mutex);
2192	fcoe_ctlr_disc_stop_locked(lport);
2193	mutex_unlock(&fip->ctlr_mutex);
2194}
2195
2196/**
2197 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2198 * @fip: The FCoE controller
2199 *
2200 * Called through the local port template for discovery.
2201 * Called without the ctlr_mutex held.
2202 */
2203static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2204{
2205	fcoe_ctlr_disc_stop(lport);
2206	fc_rport_flush_queue();
2207	synchronize_rcu();
2208}
2209
2210/**
2211 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2212 * @fip: The FCoE controller
2213 *
2214 * Called with fcoe_ctlr lock held.
2215 */
2216static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2217{
2218	unsigned long wait;
2219	u32 port_id;
2220
2221	fcoe_ctlr_disc_stop_locked(fip->lp);
2222
2223	/*
2224	 * Get proposed port ID.
2225	 * If this is the first try after link up, use any previous port_id.
2226	 * If there was none, use the low bits of the port_name.
2227	 * On subsequent tries, get the next random one.
2228	 * Don't use reserved IDs, use another non-zero value, just as random.
2229	 */
2230	port_id = fip->port_id;
2231	if (fip->probe_tries)
2232		port_id = prandom_u32_state(&fip->rnd_state) & 0xffff;
2233	else if (!port_id)
2234		port_id = fip->lp->wwpn & 0xffff;
2235	if (!port_id || port_id == 0xffff)
2236		port_id = 1;
2237	fip->port_id = port_id;
2238
2239	if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2240		fip->probe_tries++;
2241		wait = prandom_u32() % FIP_VN_PROBE_WAIT;
2242	} else
2243		wait = FIP_VN_RLIM_INT;
2244	mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2245	fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2246}
2247
2248/**
2249 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2250 * @fip: The FCoE controller
2251 *
2252 * Called with fcoe_ctlr lock held.
2253 */
2254static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2255{
2256	fip->probe_tries = 0;
2257	prandom_seed_state(&fip->rnd_state, fip->lp->wwpn);
2258	fcoe_ctlr_vn_restart(fip);
2259}
2260
2261/**
2262 * fcoe_ctlr_vn_parse - parse probe request or response
2263 * @fip: The FCoE controller
2264 * @skb: incoming packet
2265 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2266 *
2267 * Returns non-zero error number on error.
2268 * Does not consume the packet.
2269 */
2270static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2271			      struct sk_buff *skb,
2272			      struct fcoe_rport *frport)
2273{
2274	struct fip_header *fiph;
2275	struct fip_desc *desc = NULL;
2276	struct fip_mac_desc *macd = NULL;
2277	struct fip_wwn_desc *wwn = NULL;
2278	struct fip_vn_desc *vn = NULL;
2279	struct fip_size_desc *size = NULL;
 
2280	size_t rlen;
2281	size_t dlen;
2282	u32 desc_mask = 0;
2283	u32 dtype;
2284	u8 sub;
2285
 
 
 
2286	fiph = (struct fip_header *)skb->data;
2287	frport->flags = ntohs(fiph->fip_flags);
2288
2289	sub = fiph->fip_subcode;
2290	switch (sub) {
2291	case FIP_SC_VN_PROBE_REQ:
2292	case FIP_SC_VN_PROBE_REP:
2293	case FIP_SC_VN_BEACON:
2294		desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2295			    BIT(FIP_DT_VN_ID);
2296		break;
2297	case FIP_SC_VN_CLAIM_NOTIFY:
2298	case FIP_SC_VN_CLAIM_REP:
2299		desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2300			    BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2301			    BIT(FIP_DT_FCOE_SIZE);
2302		break;
2303	default:
2304		LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2305		return -EINVAL;
2306	}
2307
2308	rlen = ntohs(fiph->fip_dl_len) * 4;
2309	if (rlen + sizeof(*fiph) > skb->len)
2310		return -EINVAL;
2311
2312	desc = (struct fip_desc *)(fiph + 1);
2313	while (rlen > 0) {
2314		dlen = desc->fip_dlen * FIP_BPW;
2315		if (dlen < sizeof(*desc) || dlen > rlen)
2316			return -EINVAL;
2317
2318		dtype = desc->fip_dtype;
2319		if (dtype < 32) {
2320			if (!(desc_mask & BIT(dtype))) {
2321				LIBFCOE_FIP_DBG(fip,
2322						"unexpected or duplicated desc "
2323						"desc type %u in "
2324						"FIP VN2VN subtype %u\n",
2325						dtype, sub);
2326				return -EINVAL;
2327			}
2328			desc_mask &= ~BIT(dtype);
2329		}
2330
2331		switch (dtype) {
2332		case FIP_DT_MAC:
2333			if (dlen != sizeof(struct fip_mac_desc))
2334				goto len_err;
2335			macd = (struct fip_mac_desc *)desc;
2336			if (!is_valid_ether_addr(macd->fd_mac)) {
2337				LIBFCOE_FIP_DBG(fip,
2338					"Invalid MAC addr %pM in FIP VN2VN\n",
2339					 macd->fd_mac);
2340				return -EINVAL;
2341			}
2342			memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2343			break;
2344		case FIP_DT_NAME:
2345			if (dlen != sizeof(struct fip_wwn_desc))
2346				goto len_err;
2347			wwn = (struct fip_wwn_desc *)desc;
2348			frport->rdata.ids.node_name =
2349				get_unaligned_be64(&wwn->fd_wwn);
2350			break;
2351		case FIP_DT_VN_ID:
2352			if (dlen != sizeof(struct fip_vn_desc))
2353				goto len_err;
2354			vn = (struct fip_vn_desc *)desc;
2355			memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2356			frport->rdata.ids.port_id = ntoh24(vn->fd_fc_id);
2357			frport->rdata.ids.port_name =
2358				get_unaligned_be64(&vn->fd_wwpn);
2359			break;
2360		case FIP_DT_FC4F:
2361			if (dlen != sizeof(struct fip_fc4_feat))
2362				goto len_err;
2363			break;
2364		case FIP_DT_FCOE_SIZE:
2365			if (dlen != sizeof(struct fip_size_desc))
2366				goto len_err;
2367			size = (struct fip_size_desc *)desc;
2368			frport->fcoe_len = ntohs(size->fd_size);
2369			break;
2370		default:
2371			LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2372					"in FIP probe\n", dtype);
2373			/* standard says ignore unknown descriptors >= 128 */
2374			if (dtype < FIP_DT_NON_CRITICAL)
2375				return -EINVAL;
2376			break;
2377		}
2378		desc = (struct fip_desc *)((char *)desc + dlen);
2379		rlen -= dlen;
2380	}
2381	return 0;
2382
2383len_err:
2384	LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2385			dtype, dlen);
2386	return -EINVAL;
2387}
2388
2389/**
2390 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2391 * @fip: The FCoE controller
2392 *
2393 * Called with ctlr_mutex held.
2394 */
2395static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2396{
2397	fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2398	fip->sol_time = jiffies;
2399}
2400
2401/**
2402 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2403 * @fip: The FCoE controller
2404 * @frport: parsed FCoE rport from the probe request
2405 *
2406 * Called with ctlr_mutex held.
2407 */
2408static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2409				   struct fcoe_rport *frport)
2410{
2411	if (frport->rdata.ids.port_id != fip->port_id)
 
 
2412		return;
2413
2414	switch (fip->state) {
2415	case FIP_ST_VNMP_CLAIM:
2416	case FIP_ST_VNMP_UP:
2417		LIBFCOE_FIP_DBG(fip, "vn_probe_req: send reply, state %x\n",
2418				fip->state);
2419		fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2420				  frport->enode_mac, 0);
2421		break;
2422	case FIP_ST_VNMP_PROBE1:
2423	case FIP_ST_VNMP_PROBE2:
2424		/*
2425		 * Decide whether to reply to the Probe.
2426		 * Our selected address is never a "recorded" one, so
2427		 * only reply if our WWPN is greater and the
2428		 * Probe's REC bit is not set.
2429		 * If we don't reply, we will change our address.
2430		 */
2431		if (fip->lp->wwpn > frport->rdata.ids.port_name &&
2432		    !(frport->flags & FIP_FL_REC_OR_P2P)) {
2433			LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2434					"port_id collision\n");
2435			fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2436					  frport->enode_mac, 0);
2437			break;
2438		}
2439		/* fall through */
2440	case FIP_ST_VNMP_START:
2441		LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2442				"restart VN2VN negotiation\n");
2443		fcoe_ctlr_vn_restart(fip);
2444		break;
2445	default:
2446		LIBFCOE_FIP_DBG(fip, "vn_probe_req: ignore state %x\n",
2447				fip->state);
2448		break;
2449	}
2450}
2451
2452/**
2453 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2454 * @fip: The FCoE controller
2455 * @frport: parsed FCoE rport from the probe request
2456 *
2457 * Called with ctlr_mutex held.
2458 */
2459static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2460				     struct fcoe_rport *frport)
2461{
2462	if (frport->rdata.ids.port_id != fip->port_id)
2463		return;
2464	switch (fip->state) {
2465	case FIP_ST_VNMP_START:
2466	case FIP_ST_VNMP_PROBE1:
2467	case FIP_ST_VNMP_PROBE2:
2468	case FIP_ST_VNMP_CLAIM:
2469		LIBFCOE_FIP_DBG(fip, "vn_probe_reply: restart state %x\n",
2470				fip->state);
2471		fcoe_ctlr_vn_restart(fip);
2472		break;
2473	case FIP_ST_VNMP_UP:
2474		LIBFCOE_FIP_DBG(fip, "vn_probe_reply: send claim notify\n");
2475		fcoe_ctlr_vn_send_claim(fip);
2476		break;
2477	default:
2478		break;
2479	}
2480}
2481
2482/**
2483 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2484 * @fip: The FCoE controller
2485 * @new: newly-parsed FCoE rport as a template for new rdata
2486 *
2487 * Called with ctlr_mutex held.
2488 */
2489static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fcoe_rport *new)
2490{
2491	struct fc_lport *lport = fip->lp;
2492	struct fc_rport_priv *rdata;
2493	struct fc_rport_identifiers *ids;
2494	struct fcoe_rport *frport;
2495	u32 port_id;
2496
2497	port_id = new->rdata.ids.port_id;
2498	if (port_id == fip->port_id)
2499		return;
2500
2501	mutex_lock(&lport->disc.disc_mutex);
2502	rdata = fc_rport_create(lport, port_id);
2503	if (!rdata) {
2504		mutex_unlock(&lport->disc.disc_mutex);
2505		return;
2506	}
2507	mutex_lock(&rdata->rp_mutex);
2508	mutex_unlock(&lport->disc.disc_mutex);
2509
2510	rdata->ops = &fcoe_ctlr_vn_rport_ops;
2511	rdata->disc_id = lport->disc.disc_id;
2512
2513	ids = &rdata->ids;
2514	if ((ids->port_name != -1 &&
2515	     ids->port_name != new->rdata.ids.port_name) ||
2516	    (ids->node_name != -1 &&
2517	     ids->node_name != new->rdata.ids.node_name)) {
2518		mutex_unlock(&rdata->rp_mutex);
2519		LIBFCOE_FIP_DBG(fip, "vn_add rport logoff %6.6x\n", port_id);
2520		fc_rport_logoff(rdata);
2521		mutex_lock(&rdata->rp_mutex);
2522	}
2523	ids->port_name = new->rdata.ids.port_name;
2524	ids->node_name = new->rdata.ids.node_name;
2525	mutex_unlock(&rdata->rp_mutex);
2526
2527	frport = fcoe_ctlr_rport(rdata);
2528	LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s state %d\n",
2529			port_id, frport->fcoe_len ? "old" : "new",
2530			rdata->rp_state);
2531	frport->fcoe_len = new->fcoe_len;
2532	frport->flags = new->flags;
2533	frport->login_count = new->login_count;
2534	memcpy(frport->enode_mac, new->enode_mac, ETH_ALEN);
2535	memcpy(frport->vn_mac, new->vn_mac, ETH_ALEN);
2536	frport->time = 0;
2537}
2538
2539/**
2540 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2541 * @fip: The FCoE controller
2542 * @port_id:  The port_id of the remote VN_node
2543 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2544 *
2545 * Returns non-zero error if no remote port found.
2546 */
2547static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2548{
2549	struct fc_lport *lport = fip->lp;
2550	struct fc_rport_priv *rdata;
2551	struct fcoe_rport *frport;
2552	int ret = -1;
2553
2554	rdata = fc_rport_lookup(lport, port_id);
 
2555	if (rdata) {
2556		frport = fcoe_ctlr_rport(rdata);
2557		memcpy(mac, frport->enode_mac, ETH_ALEN);
2558		ret = 0;
2559		kref_put(&rdata->kref, fc_rport_destroy);
2560	}
 
2561	return ret;
2562}
2563
2564/**
2565 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2566 * @fip: The FCoE controller
2567 * @new: newly-parsed FCoE rport as a template for new rdata
2568 *
2569 * Called with ctlr_mutex held.
2570 */
2571static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2572				      struct fcoe_rport *new)
2573{
2574	if (new->flags & FIP_FL_REC_OR_P2P) {
2575		LIBFCOE_FIP_DBG(fip, "send probe req for P2P/REC\n");
 
2576		fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2577		return;
2578	}
2579	switch (fip->state) {
2580	case FIP_ST_VNMP_START:
2581	case FIP_ST_VNMP_PROBE1:
2582	case FIP_ST_VNMP_PROBE2:
2583		if (new->rdata.ids.port_id == fip->port_id) {
2584			LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2585					"restart, state %d\n",
2586					fip->state);
2587			fcoe_ctlr_vn_restart(fip);
2588		}
2589		break;
2590	case FIP_ST_VNMP_CLAIM:
2591	case FIP_ST_VNMP_UP:
2592		if (new->rdata.ids.port_id == fip->port_id) {
2593			if (new->rdata.ids.port_name > fip->lp->wwpn) {
2594				LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2595						"restart, port_id collision\n");
2596				fcoe_ctlr_vn_restart(fip);
2597				break;
2598			}
2599			LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2600					"send claim notify\n");
2601			fcoe_ctlr_vn_send_claim(fip);
2602			break;
2603		}
2604		LIBFCOE_FIP_DBG(fip, "vn_claim_notify: send reply to %x\n",
2605				new->rdata.ids.port_id);
2606		fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, new->enode_mac,
2607				  min((u32)new->fcoe_len,
2608				      fcoe_ctlr_fcoe_size(fip)));
2609		fcoe_ctlr_vn_add(fip, new);
2610		break;
2611	default:
2612		LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2613				"ignoring claim from %x\n",
2614				new->rdata.ids.port_id);
2615		break;
2616	}
2617}
2618
2619/**
2620 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2621 * @fip: The FCoE controller that received the frame
2622 * @new: newly-parsed FCoE rport from the Claim Response
2623 *
2624 * Called with ctlr_mutex held.
2625 */
2626static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2627				    struct fcoe_rport *new)
2628{
2629	LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2630			new->rdata.ids.port_id, fcoe_ctlr_state(fip->state));
2631	if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2632		fcoe_ctlr_vn_add(fip, new);
2633}
2634
2635/**
2636 * fcoe_ctlr_vn_beacon() - handle received beacon.
2637 * @fip: The FCoE controller that received the frame
2638 * @new: newly-parsed FCoE rport from the Beacon
2639 *
2640 * Called with ctlr_mutex held.
2641 */
2642static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2643				struct fcoe_rport *new)
2644{
2645	struct fc_lport *lport = fip->lp;
2646	struct fc_rport_priv *rdata;
2647	struct fcoe_rport *frport;
2648
2649	if (new->flags & FIP_FL_REC_OR_P2P) {
2650		LIBFCOE_FIP_DBG(fip, "p2p beacon while in vn2vn mode\n");
2651		fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2652		return;
2653	}
2654	rdata = fc_rport_lookup(lport, new->rdata.ids.port_id);
 
 
 
 
2655	if (rdata) {
2656		if (rdata->ids.node_name == new->rdata.ids.node_name &&
2657		    rdata->ids.port_name == new->rdata.ids.port_name) {
2658			frport = fcoe_ctlr_rport(rdata);
2659
2660			LIBFCOE_FIP_DBG(fip, "beacon from rport %x\n",
2661					rdata->ids.port_id);
2662			if (!frport->time && fip->state == FIP_ST_VNMP_UP) {
2663				LIBFCOE_FIP_DBG(fip, "beacon expired "
2664						"for rport %x\n",
2665						rdata->ids.port_id);
2666				fc_rport_login(rdata);
2667			}
2668			frport->time = jiffies;
2669		}
2670		kref_put(&rdata->kref, fc_rport_destroy);
2671		return;
2672	}
2673	if (fip->state != FIP_ST_VNMP_UP)
2674		return;
2675
2676	/*
2677	 * Beacon from a new neighbor.
2678	 * Send a claim notify if one hasn't been sent recently.
2679	 * Don't add the neighbor yet.
2680	 */
2681	LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2682			new->rdata.ids.port_id);
2683	if (time_after(jiffies,
2684		       fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2685		fcoe_ctlr_vn_send_claim(fip);
2686}
2687
2688/**
2689 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2690 * @fip: The FCoE controller
2691 *
2692 * Called with ctlr_mutex held.
2693 * Called only in state FIP_ST_VNMP_UP.
2694 * Returns the soonest time for next age-out or a time far in the future.
2695 */
2696static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2697{
2698	struct fc_lport *lport = fip->lp;
2699	struct fc_rport_priv *rdata;
2700	struct fcoe_rport *frport;
2701	unsigned long next_time;
2702	unsigned long deadline;
2703
2704	next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2705	mutex_lock(&lport->disc.disc_mutex);
2706	list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2707		if (!kref_get_unless_zero(&rdata->kref))
2708			continue;
2709		frport = fcoe_ctlr_rport(rdata);
2710		if (!frport->time) {
2711			kref_put(&rdata->kref, fc_rport_destroy);
2712			continue;
2713		}
2714		deadline = frport->time +
2715			   msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2716		if (time_after_eq(jiffies, deadline)) {
2717			frport->time = 0;
2718			LIBFCOE_FIP_DBG(fip,
2719				"port %16.16llx fc_id %6.6x beacon expired\n",
2720				rdata->ids.port_name, rdata->ids.port_id);
2721			fc_rport_logoff(rdata);
2722		} else if (time_before(deadline, next_time))
2723			next_time = deadline;
2724		kref_put(&rdata->kref, fc_rport_destroy);
2725	}
2726	mutex_unlock(&lport->disc.disc_mutex);
2727	return next_time;
2728}
2729
2730/**
2731 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2732 * @fip: The FCoE controller that received the frame
2733 * @skb: The received FIP frame
2734 *
2735 * Returns non-zero if the frame is dropped.
2736 * Always consumes the frame.
2737 */
2738static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2739{
2740	struct fip_header *fiph;
2741	enum fip_vn2vn_subcode sub;
2742	struct fcoe_rport frport = { };
2743	int rc, vlan_id = 0;
 
 
 
2744
2745	fiph = (struct fip_header *)skb->data;
2746	sub = fiph->fip_subcode;
2747
2748	if (fip->lp->vlan)
2749		vlan_id = skb_vlan_tag_get_id(skb);
2750
2751	if (vlan_id && vlan_id != fip->lp->vlan) {
2752		LIBFCOE_FIP_DBG(fip, "vn_recv drop frame sub %x vlan %d\n",
2753				sub, vlan_id);
2754		rc = -EAGAIN;
2755		goto drop;
2756	}
2757
2758	rc = fcoe_ctlr_vn_parse(fip, skb, &frport);
2759	if (rc) {
2760		LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2761		goto drop;
2762	}
2763
2764	mutex_lock(&fip->ctlr_mutex);
2765	switch (sub) {
2766	case FIP_SC_VN_PROBE_REQ:
2767		fcoe_ctlr_vn_probe_req(fip, &frport);
2768		break;
2769	case FIP_SC_VN_PROBE_REP:
2770		fcoe_ctlr_vn_probe_reply(fip, &frport);
2771		break;
2772	case FIP_SC_VN_CLAIM_NOTIFY:
2773		fcoe_ctlr_vn_claim_notify(fip, &frport);
2774		break;
2775	case FIP_SC_VN_CLAIM_REP:
2776		fcoe_ctlr_vn_claim_resp(fip, &frport);
2777		break;
2778	case FIP_SC_VN_BEACON:
2779		fcoe_ctlr_vn_beacon(fip, &frport);
2780		break;
2781	default:
2782		LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2783		rc = -1;
2784		break;
2785	}
2786	mutex_unlock(&fip->ctlr_mutex);
2787drop:
2788	kfree_skb(skb);
2789	return rc;
2790}
2791
2792/**
2793 * fcoe_ctlr_vlan_parse - parse vlan discovery request or response
2794 * @fip: The FCoE controller
2795 * @skb: incoming packet
2796 * @rdata: buffer for resulting parsed VLAN entry plus fcoe_rport
2797 *
2798 * Returns non-zero error number on error.
2799 * Does not consume the packet.
2800 */
2801static int fcoe_ctlr_vlan_parse(struct fcoe_ctlr *fip,
2802			      struct sk_buff *skb,
2803			      struct fcoe_rport *frport)
2804{
2805	struct fip_header *fiph;
2806	struct fip_desc *desc = NULL;
2807	struct fip_mac_desc *macd = NULL;
2808	struct fip_wwn_desc *wwn = NULL;
2809	size_t rlen;
2810	size_t dlen;
2811	u32 desc_mask = 0;
2812	u32 dtype;
2813	u8 sub;
2814
2815	fiph = (struct fip_header *)skb->data;
2816	frport->flags = ntohs(fiph->fip_flags);
2817
2818	sub = fiph->fip_subcode;
2819	switch (sub) {
2820	case FIP_SC_VL_REQ:
2821		desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
2822		break;
2823	default:
2824		LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2825		return -EINVAL;
2826	}
2827
2828	rlen = ntohs(fiph->fip_dl_len) * 4;
2829	if (rlen + sizeof(*fiph) > skb->len)
2830		return -EINVAL;
2831
2832	desc = (struct fip_desc *)(fiph + 1);
2833	while (rlen > 0) {
2834		dlen = desc->fip_dlen * FIP_BPW;
2835		if (dlen < sizeof(*desc) || dlen > rlen)
2836			return -EINVAL;
2837
2838		dtype = desc->fip_dtype;
2839		if (dtype < 32) {
2840			if (!(desc_mask & BIT(dtype))) {
2841				LIBFCOE_FIP_DBG(fip,
2842						"unexpected or duplicated desc "
2843						"desc type %u in "
2844						"FIP VN2VN subtype %u\n",
2845						dtype, sub);
2846				return -EINVAL;
2847			}
2848			desc_mask &= ~BIT(dtype);
2849		}
2850
2851		switch (dtype) {
2852		case FIP_DT_MAC:
2853			if (dlen != sizeof(struct fip_mac_desc))
2854				goto len_err;
2855			macd = (struct fip_mac_desc *)desc;
2856			if (!is_valid_ether_addr(macd->fd_mac)) {
2857				LIBFCOE_FIP_DBG(fip,
2858					"Invalid MAC addr %pM in FIP VN2VN\n",
2859					 macd->fd_mac);
2860				return -EINVAL;
2861			}
2862			memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2863			break;
2864		case FIP_DT_NAME:
2865			if (dlen != sizeof(struct fip_wwn_desc))
2866				goto len_err;
2867			wwn = (struct fip_wwn_desc *)desc;
2868			frport->rdata.ids.node_name =
2869				get_unaligned_be64(&wwn->fd_wwn);
2870			break;
2871		default:
2872			LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2873					"in FIP probe\n", dtype);
2874			/* standard says ignore unknown descriptors >= 128 */
2875			if (dtype < FIP_DT_NON_CRITICAL)
2876				return -EINVAL;
2877			break;
2878		}
2879		desc = (struct fip_desc *)((char *)desc + dlen);
2880		rlen -= dlen;
2881	}
2882	return 0;
2883
2884len_err:
2885	LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2886			dtype, dlen);
2887	return -EINVAL;
2888}
2889
2890/**
2891 * fcoe_ctlr_vlan_send() - Send a FIP VLAN Notification
2892 * @fip: The FCoE controller
2893 * @sub: sub-opcode for vlan notification or vn2vn vlan notification
2894 * @dest: The destination Ethernet MAC address
2895 * @min_len: minimum size of the Ethernet payload to be sent
2896 */
2897static void fcoe_ctlr_vlan_send(struct fcoe_ctlr *fip,
2898			      enum fip_vlan_subcode sub,
2899			      const u8 *dest)
2900{
2901	struct sk_buff *skb;
2902	struct fip_vlan_notify_frame {
2903		struct ethhdr eth;
2904		struct fip_header fip;
2905		struct fip_mac_desc mac;
2906		struct fip_vlan_desc vlan;
2907	} __packed * frame;
2908	size_t len;
2909	size_t dlen;
2910
2911	len = sizeof(*frame);
2912	dlen = sizeof(frame->mac) + sizeof(frame->vlan);
2913	len = max(len, sizeof(struct ethhdr));
2914
2915	skb = dev_alloc_skb(len);
2916	if (!skb)
2917		return;
2918
2919	LIBFCOE_FIP_DBG(fip, "fip %s vlan notification, vlan %d\n",
2920			fip->mode == FIP_MODE_VN2VN ? "vn2vn" : "fcf",
2921			fip->lp->vlan);
2922
2923	frame = (struct fip_vlan_notify_frame *)skb->data;
2924	memset(frame, 0, len);
2925	memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2926
2927	memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2928	frame->eth.h_proto = htons(ETH_P_FIP);
2929
2930	frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2931	frame->fip.fip_op = htons(FIP_OP_VLAN);
2932	frame->fip.fip_subcode = sub;
2933	frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2934
2935	frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2936	frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2937	memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2938
2939	frame->vlan.fd_desc.fip_dtype = FIP_DT_VLAN;
2940	frame->vlan.fd_desc.fip_dlen = sizeof(frame->vlan) / FIP_BPW;
2941	put_unaligned_be16(fip->lp->vlan, &frame->vlan.fd_vlan);
2942
2943	skb_put(skb, len);
2944	skb->protocol = htons(ETH_P_FIP);
2945	skb->priority = fip->priority;
2946	skb_reset_mac_header(skb);
2947	skb_reset_network_header(skb);
2948
2949	fip->send(fip, skb);
2950}
2951
2952/**
2953 * fcoe_ctlr_vlan_disk_reply() - send FIP VLAN Discovery Notification.
2954 * @fip: The FCoE controller
2955 * @frport: The newly-parsed FCoE rport from the Discovery Request
2956 *
2957 * Called with ctlr_mutex held.
2958 */
2959static void fcoe_ctlr_vlan_disc_reply(struct fcoe_ctlr *fip,
2960				      struct fcoe_rport *frport)
2961{
2962	enum fip_vlan_subcode sub = FIP_SC_VL_NOTE;
2963
2964	if (fip->mode == FIP_MODE_VN2VN)
2965		sub = FIP_SC_VL_VN2VN_NOTE;
2966
2967	fcoe_ctlr_vlan_send(fip, sub, frport->enode_mac);
2968}
2969
2970/**
2971 * fcoe_ctlr_vlan_recv - vlan request receive handler for VN2VN mode.
2972 * @lport: The local port
2973 * @fp: The received frame
2974 *
2975 */
2976static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2977{
2978	struct fip_header *fiph;
2979	enum fip_vlan_subcode sub;
2980	struct fcoe_rport frport = { };
2981	int rc;
2982
2983	fiph = (struct fip_header *)skb->data;
2984	sub = fiph->fip_subcode;
2985	rc = fcoe_ctlr_vlan_parse(fip, skb, &frport);
2986	if (rc) {
2987		LIBFCOE_FIP_DBG(fip, "vlan_recv vlan_parse error %d\n", rc);
2988		goto drop;
2989	}
2990	mutex_lock(&fip->ctlr_mutex);
2991	if (sub == FIP_SC_VL_REQ)
2992		fcoe_ctlr_vlan_disc_reply(fip, &frport);
2993	mutex_unlock(&fip->ctlr_mutex);
2994
2995drop:
2996	kfree_skb(skb);
2997	return rc;
2998}
2999
3000/**
3001 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
3002 * @lport: The local port
3003 * @fp: The received frame
3004 *
3005 * This should never be called since we don't see RSCNs or other
3006 * fabric-generated ELSes.
3007 */
3008static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
3009{
3010	struct fc_seq_els_data rjt_data;
3011
3012	rjt_data.reason = ELS_RJT_UNSUP;
3013	rjt_data.explan = ELS_EXPL_NONE;
3014	fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
3015	fc_frame_free(fp);
3016}
3017
3018/**
3019 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
3020 * @fip: The FCoE controller
3021 *
3022 * This sets a flag indicating that remote ports should be created
3023 * and started for the peers we discover.  We use the disc_callback
3024 * pointer as that flag.  Peers already discovered are created here.
3025 *
3026 * The lport lock is held during this call. The callback must be done
3027 * later, without holding either the lport or discovery locks.
3028 * The fcoe_ctlr lock may also be held during this call.
3029 */
3030static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
3031						  enum fc_disc_event),
3032				 struct fc_lport *lport)
3033{
3034	struct fc_disc *disc = &lport->disc;
3035	struct fcoe_ctlr *fip = disc->priv;
3036
3037	mutex_lock(&disc->disc_mutex);
3038	disc->disc_callback = callback;
3039	disc->disc_id = (disc->disc_id + 2) | 1;
3040	disc->pending = 1;
3041	schedule_work(&fip->timer_work);
3042	mutex_unlock(&disc->disc_mutex);
3043}
3044
3045/**
3046 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
3047 * @fip: The FCoE controller
3048 *
3049 * Starts the FLOGI and PLOGI login process to each discovered rport for which
3050 * we've received at least one beacon.
3051 * Performs the discovery complete callback.
3052 */
3053static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
3054{
3055	struct fc_lport *lport = fip->lp;
3056	struct fc_disc *disc = &lport->disc;
3057	struct fc_rport_priv *rdata;
3058	struct fcoe_rport *frport;
3059	void (*callback)(struct fc_lport *, enum fc_disc_event);
3060
3061	mutex_lock(&disc->disc_mutex);
3062	callback = disc->pending ? disc->disc_callback : NULL;
3063	disc->pending = 0;
3064	list_for_each_entry_rcu(rdata, &disc->rports, peers) {
3065		if (!kref_get_unless_zero(&rdata->kref))
3066			continue;
3067		frport = fcoe_ctlr_rport(rdata);
3068		if (frport->time)
3069			fc_rport_login(rdata);
3070		kref_put(&rdata->kref, fc_rport_destroy);
3071	}
3072	mutex_unlock(&disc->disc_mutex);
3073	if (callback)
3074		callback(lport, DISC_EV_SUCCESS);
3075}
3076
3077/**
3078 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
3079 * @fip: The FCoE controller
3080 */
3081static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
3082{
3083	unsigned long next_time;
3084	u8 mac[ETH_ALEN];
3085	u32 new_port_id = 0;
3086
3087	mutex_lock(&fip->ctlr_mutex);
3088	switch (fip->state) {
3089	case FIP_ST_VNMP_START:
3090		fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
3091		LIBFCOE_FIP_DBG(fip, "vn_timeout: send 1st probe request\n");
3092		fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3093		next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
3094		break;
3095	case FIP_ST_VNMP_PROBE1:
3096		fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
3097		LIBFCOE_FIP_DBG(fip, "vn_timeout: send 2nd probe request\n");
3098		fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3099		next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3100		break;
3101	case FIP_ST_VNMP_PROBE2:
3102		fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
3103		new_port_id = fip->port_id;
3104		hton24(mac, FIP_VN_FC_MAP);
3105		hton24(mac + 3, new_port_id);
3106		fcoe_ctlr_map_dest(fip);
3107		fip->update_mac(fip->lp, mac);
3108		LIBFCOE_FIP_DBG(fip, "vn_timeout: send claim notify\n");
3109		fcoe_ctlr_vn_send_claim(fip);
3110		next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3111		break;
3112	case FIP_ST_VNMP_CLAIM:
3113		/*
3114		 * This may be invoked either by starting discovery so don't
3115		 * go to the next state unless it's been long enough.
3116		 */
3117		next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3118		if (time_after_eq(jiffies, next_time)) {
3119			fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
3120			LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3121			fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3122					  fcoe_all_vn2vn, 0);
3123			next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3124			fip->port_ka_time = next_time;
3125		}
3126		fcoe_ctlr_vn_disc(fip);
3127		break;
3128	case FIP_ST_VNMP_UP:
3129		next_time = fcoe_ctlr_vn_age(fip);
3130		if (time_after_eq(jiffies, fip->port_ka_time)) {
3131			LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3132			fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3133					  fcoe_all_vn2vn, 0);
3134			fip->port_ka_time = jiffies +
3135				 msecs_to_jiffies(FIP_VN_BEACON_INT +
3136					(prandom_u32() % FIP_VN_BEACON_FUZZ));
3137		}
3138		if (time_before(fip->port_ka_time, next_time))
3139			next_time = fip->port_ka_time;
3140		break;
3141	case FIP_ST_LINK_WAIT:
3142		goto unlock;
3143	default:
3144		WARN(1, "unexpected state %d\n", fip->state);
3145		goto unlock;
3146	}
3147	mod_timer(&fip->timer, next_time);
3148unlock:
3149	mutex_unlock(&fip->ctlr_mutex);
3150
3151	/* If port ID is new, notify local port after dropping ctlr_mutex */
3152	if (new_port_id)
3153		fc_lport_set_local_id(fip->lp, new_port_id);
3154}
3155
3156/**
3157 * fcoe_ctlr_mode_set() - Set or reset the ctlr's mode
3158 * @lport: The local port to be (re)configured
3159 * @fip:   The FCoE controller whose mode is changing
3160 * @fip_mode: The new fip mode
3161 *
3162 * Note that the we shouldn't be changing the libfc discovery settings
3163 * (fc_disc_config) while an lport is going through the libfc state
3164 * machine. The mode can only be changed when a fcoe_ctlr device is
3165 * disabled, so that should ensure that this routine is only called
3166 * when nothing is happening.
3167 */
3168static void fcoe_ctlr_mode_set(struct fc_lport *lport, struct fcoe_ctlr *fip,
3169			       enum fip_mode fip_mode)
3170{
3171	void *priv;
3172
3173	WARN_ON(lport->state != LPORT_ST_RESET &&
3174		lport->state != LPORT_ST_DISABLED);
3175
3176	if (fip_mode == FIP_MODE_VN2VN) {
3177		lport->rport_priv_size = sizeof(struct fcoe_rport);
3178		lport->point_to_multipoint = 1;
3179		lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
3180		lport->tt.disc_start = fcoe_ctlr_disc_start;
3181		lport->tt.disc_stop = fcoe_ctlr_disc_stop;
3182		lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
3183		priv = fip;
3184	} else {
3185		lport->rport_priv_size = 0;
3186		lport->point_to_multipoint = 0;
3187		lport->tt.disc_recv_req = NULL;
3188		lport->tt.disc_start = NULL;
3189		lport->tt.disc_stop = NULL;
3190		lport->tt.disc_stop_final = NULL;
3191		priv = lport;
3192	}
3193
3194	fc_disc_config(lport, priv);
3195}
3196
3197/**
3198 * fcoe_libfc_config() - Sets up libfc related properties for local port
3199 * @lport:    The local port to configure libfc for
3200 * @fip:      The FCoE controller in use by the local port
3201 * @tt:       The libfc function template
3202 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
3203 *
3204 * Returns : 0 for success
3205 */
3206int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
3207		      const struct libfc_function_template *tt, int init_fcp)
3208{
3209	/* Set the function pointers set by the LLDD */
3210	memcpy(&lport->tt, tt, sizeof(*tt));
3211	if (init_fcp && fc_fcp_init(lport))
3212		return -ENOMEM;
3213	fc_exch_init(lport);
3214	fc_elsct_init(lport);
3215	fc_lport_init(lport);
3216	fc_disc_init(lport);
3217	fcoe_ctlr_mode_set(lport, fip, fip->mode);
 
 
 
 
 
 
 
 
 
 
 
 
 
3218	return 0;
3219}
3220EXPORT_SYMBOL_GPL(fcoe_libfc_config);
3221
3222void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
3223{
3224	struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
3225	struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
3226	struct fcoe_fcf *fcf;
3227
3228	mutex_lock(&fip->ctlr_mutex);
3229	mutex_lock(&ctlr_dev->lock);
3230
3231	fcf = fcoe_fcf_device_priv(fcf_dev);
3232	if (fcf)
3233		fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
3234	else
3235		fcf_dev->selected = 0;
3236
3237	mutex_unlock(&ctlr_dev->lock);
3238	mutex_unlock(&fip->ctlr_mutex);
3239}
3240EXPORT_SYMBOL(fcoe_fcf_get_selected);
3241
3242void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
3243{
3244	struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
3245	struct fc_lport *lport = ctlr->lp;
3246
3247	mutex_lock(&ctlr->ctlr_mutex);
3248	switch (ctlr_dev->mode) {
3249	case FIP_CONN_TYPE_VN2VN:
3250		ctlr->mode = FIP_MODE_VN2VN;
3251		break;
3252	case FIP_CONN_TYPE_FABRIC:
3253	default:
3254		ctlr->mode = FIP_MODE_FABRIC;
3255		break;
3256	}
3257
3258	mutex_unlock(&ctlr->ctlr_mutex);
3259
3260	fcoe_ctlr_mode_set(lport, ctlr, ctlr->mode);
3261}
3262EXPORT_SYMBOL(fcoe_ctlr_set_fip_mode);