PageRenderTime 152ms CodeModel.GetById 22ms app.highlight 117ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/net/qlcnic/qlcnic_main.c

https://bitbucket.org/ndreys/linux-sunxi
C | 4340 lines | 3416 code | 870 blank | 54 comment | 641 complexity | 250d9bc46cbfa9f00775b441e3f49b56 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

   1/*
   2 * QLogic qlcnic NIC Driver
   3 * Copyright (c)  2009-2010 QLogic Corporation
   4 *
   5 * See LICENSE.qlcnic for copyright and licensing details.
   6 */
   7
   8#include <linux/slab.h>
   9#include <linux/vmalloc.h>
  10#include <linux/interrupt.h>
  11
  12#include "qlcnic.h"
  13
  14#include <linux/swab.h>
  15#include <linux/dma-mapping.h>
  16#include <net/ip.h>
  17#include <linux/ipv6.h>
  18#include <linux/inetdevice.h>
  19#include <linux/sysfs.h>
  20#include <linux/aer.h>
  21#include <linux/log2.h>
  22
  23MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
  24MODULE_LICENSE("GPL");
  25MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
  26MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
  27
  28char qlcnic_driver_name[] = "qlcnic";
  29static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
  30	"Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
  31
  32static struct workqueue_struct *qlcnic_wq;
  33static int qlcnic_mac_learn;
  34module_param(qlcnic_mac_learn, int, 0444);
  35MODULE_PARM_DESC(qlcnic_mac_learn, "Mac Filter (0=disabled, 1=enabled)");
  36
  37static int use_msi = 1;
  38module_param(use_msi, int, 0444);
  39MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
  40
  41static int use_msi_x = 1;
  42module_param(use_msi_x, int, 0444);
  43MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
  44
  45static int auto_fw_reset = 1;
  46module_param(auto_fw_reset, int, 0644);
  47MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
  48
  49static int load_fw_file;
  50module_param(load_fw_file, int, 0444);
  51MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
  52
  53static int qlcnic_config_npars;
  54module_param(qlcnic_config_npars, int, 0444);
  55MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
  56
  57static int __devinit qlcnic_probe(struct pci_dev *pdev,
  58		const struct pci_device_id *ent);
  59static void __devexit qlcnic_remove(struct pci_dev *pdev);
  60static int qlcnic_open(struct net_device *netdev);
  61static int qlcnic_close(struct net_device *netdev);
  62static void qlcnic_tx_timeout(struct net_device *netdev);
  63static void qlcnic_attach_work(struct work_struct *work);
  64static void qlcnic_fwinit_work(struct work_struct *work);
  65static void qlcnic_fw_poll_work(struct work_struct *work);
  66static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
  67		work_func_t func, int delay);
  68static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
  69static int qlcnic_poll(struct napi_struct *napi, int budget);
  70static int qlcnic_rx_poll(struct napi_struct *napi, int budget);
  71#ifdef CONFIG_NET_POLL_CONTROLLER
  72static void qlcnic_poll_controller(struct net_device *netdev);
  73#endif
  74
  75static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
  76static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
  77static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
  78static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
  79
  80static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
  81static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8);
  82static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
  83
  84static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
  85static irqreturn_t qlcnic_intr(int irq, void *data);
  86static irqreturn_t qlcnic_msi_intr(int irq, void *data);
  87static irqreturn_t qlcnic_msix_intr(int irq, void *data);
  88
  89static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
  90static void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long);
  91static int qlcnic_start_firmware(struct qlcnic_adapter *);
  92
  93static void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter);
  94static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
  95static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
  96static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
  97static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
  98static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
  99static void qlcnic_set_netdev_features(struct qlcnic_adapter *,
 100				struct qlcnic_esw_func_cfg *);
 101static void qlcnic_vlan_rx_add(struct net_device *, u16);
 102static void qlcnic_vlan_rx_del(struct net_device *, u16);
 103
 104/*  PCI Device ID Table  */
 105#define ENTRY(device) \
 106	{PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
 107	.class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
 108
 109#define PCI_DEVICE_ID_QLOGIC_QLE824X  0x8020
 110
 111static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
 112	ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
 113	{0,}
 114};
 115
 116MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
 117
 118
 119inline void
 120qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
 121		struct qlcnic_host_tx_ring *tx_ring)
 122{
 123	writel(tx_ring->producer, tx_ring->crb_cmd_producer);
 124}
 125
 126static const u32 msi_tgt_status[8] = {
 127	ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
 128	ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
 129	ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
 130	ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
 131};
 132
 133static const
 134struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
 135
 136static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
 137{
 138	writel(0, sds_ring->crb_intr_mask);
 139}
 140
 141static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
 142{
 143	struct qlcnic_adapter *adapter = sds_ring->adapter;
 144
 145	writel(0x1, sds_ring->crb_intr_mask);
 146
 147	if (!QLCNIC_IS_MSI_FAMILY(adapter))
 148		writel(0xfbff, adapter->tgt_mask_reg);
 149}
 150
 151static int
 152qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
 153{
 154	int size = sizeof(struct qlcnic_host_sds_ring) * count;
 155
 156	recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
 157
 158	return recv_ctx->sds_rings == NULL;
 159}
 160
 161static void
 162qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
 163{
 164	if (recv_ctx->sds_rings != NULL)
 165		kfree(recv_ctx->sds_rings);
 166
 167	recv_ctx->sds_rings = NULL;
 168}
 169
 170static int
 171qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
 172{
 173	int ring;
 174	struct qlcnic_host_sds_ring *sds_ring;
 175	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
 176
 177	if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
 178		return -ENOMEM;
 179
 180	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 181		sds_ring = &recv_ctx->sds_rings[ring];
 182
 183		if (ring == adapter->max_sds_rings - 1)
 184			netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
 185				QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
 186		else
 187			netif_napi_add(netdev, &sds_ring->napi,
 188				qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
 189	}
 190
 191	return 0;
 192}
 193
 194static void
 195qlcnic_napi_del(struct qlcnic_adapter *adapter)
 196{
 197	int ring;
 198	struct qlcnic_host_sds_ring *sds_ring;
 199	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
 200
 201	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 202		sds_ring = &recv_ctx->sds_rings[ring];
 203		netif_napi_del(&sds_ring->napi);
 204	}
 205
 206	qlcnic_free_sds_rings(adapter->recv_ctx);
 207}
 208
 209static void
 210qlcnic_napi_enable(struct qlcnic_adapter *adapter)
 211{
 212	int ring;
 213	struct qlcnic_host_sds_ring *sds_ring;
 214	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
 215
 216	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
 217		return;
 218
 219	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 220		sds_ring = &recv_ctx->sds_rings[ring];
 221		napi_enable(&sds_ring->napi);
 222		qlcnic_enable_int(sds_ring);
 223	}
 224}
 225
 226static void
 227qlcnic_napi_disable(struct qlcnic_adapter *adapter)
 228{
 229	int ring;
 230	struct qlcnic_host_sds_ring *sds_ring;
 231	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
 232
 233	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
 234		return;
 235
 236	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 237		sds_ring = &recv_ctx->sds_rings[ring];
 238		qlcnic_disable_int(sds_ring);
 239		napi_synchronize(&sds_ring->napi);
 240		napi_disable(&sds_ring->napi);
 241	}
 242}
 243
 244static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
 245{
 246	memset(&adapter->stats, 0, sizeof(adapter->stats));
 247}
 248
 249static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
 250{
 251	u32 control;
 252	int pos;
 253
 254	pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
 255	if (pos) {
 256		pci_read_config_dword(pdev, pos, &control);
 257		if (enable)
 258			control |= PCI_MSIX_FLAGS_ENABLE;
 259		else
 260			control = 0;
 261		pci_write_config_dword(pdev, pos, control);
 262	}
 263}
 264
 265static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
 266{
 267	int i;
 268
 269	for (i = 0; i < count; i++)
 270		adapter->msix_entries[i].entry = i;
 271}
 272
 273static int
 274qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
 275{
 276	u8 mac_addr[ETH_ALEN];
 277	struct net_device *netdev = adapter->netdev;
 278	struct pci_dev *pdev = adapter->pdev;
 279
 280	if (qlcnic_get_mac_address(adapter, mac_addr) != 0)
 281		return -EIO;
 282
 283	memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
 284	memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
 285	memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
 286
 287	/* set station address */
 288
 289	if (!is_valid_ether_addr(netdev->perm_addr))
 290		dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
 291					netdev->dev_addr);
 292
 293	return 0;
 294}
 295
 296static int qlcnic_set_mac(struct net_device *netdev, void *p)
 297{
 298	struct qlcnic_adapter *adapter = netdev_priv(netdev);
 299	struct sockaddr *addr = p;
 300
 301	if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
 302		return -EOPNOTSUPP;
 303
 304	if (!is_valid_ether_addr(addr->sa_data))
 305		return -EINVAL;
 306
 307	if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
 308		netif_device_detach(netdev);
 309		qlcnic_napi_disable(adapter);
 310	}
 311
 312	memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
 313	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
 314	qlcnic_set_multi(adapter->netdev);
 315
 316	if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
 317		netif_device_attach(netdev);
 318		qlcnic_napi_enable(adapter);
 319	}
 320	return 0;
 321}
 322
 323static const struct net_device_ops qlcnic_netdev_ops = {
 324	.ndo_open	   = qlcnic_open,
 325	.ndo_stop	   = qlcnic_close,
 326	.ndo_start_xmit    = qlcnic_xmit_frame,
 327	.ndo_get_stats	   = qlcnic_get_stats,
 328	.ndo_validate_addr = eth_validate_addr,
 329	.ndo_set_multicast_list = qlcnic_set_multi,
 330	.ndo_set_mac_address    = qlcnic_set_mac,
 331	.ndo_change_mtu	   = qlcnic_change_mtu,
 332	.ndo_fix_features  = qlcnic_fix_features,
 333	.ndo_set_features  = qlcnic_set_features,
 334	.ndo_tx_timeout	   = qlcnic_tx_timeout,
 335	.ndo_vlan_rx_add_vid	= qlcnic_vlan_rx_add,
 336	.ndo_vlan_rx_kill_vid	= qlcnic_vlan_rx_del,
 337#ifdef CONFIG_NET_POLL_CONTROLLER
 338	.ndo_poll_controller = qlcnic_poll_controller,
 339#endif
 340};
 341
 342static struct qlcnic_nic_template qlcnic_ops = {
 343	.config_bridged_mode = qlcnic_config_bridged_mode,
 344	.config_led = qlcnic_config_led,
 345	.start_firmware = qlcnic_start_firmware
 346};
 347
 348static struct qlcnic_nic_template qlcnic_vf_ops = {
 349	.config_bridged_mode = qlcnicvf_config_bridged_mode,
 350	.config_led = qlcnicvf_config_led,
 351	.start_firmware = qlcnicvf_start_firmware
 352};
 353
 354static int qlcnic_enable_msix(struct qlcnic_adapter *adapter, u32 num_msix)
 355{
 356	struct pci_dev *pdev = adapter->pdev;
 357	int err = -1;
 358
 359	adapter->max_sds_rings = 1;
 360	adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
 361	qlcnic_set_msix_bit(pdev, 0);
 362
 363	if (adapter->msix_supported) {
 364 enable_msix:
 365		qlcnic_init_msix_entries(adapter, num_msix);
 366		err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
 367		if (err == 0) {
 368			adapter->flags |= QLCNIC_MSIX_ENABLED;
 369			qlcnic_set_msix_bit(pdev, 1);
 370
 371			adapter->max_sds_rings = num_msix;
 372
 373			dev_info(&pdev->dev, "using msi-x interrupts\n");
 374			return err;
 375		}
 376		if (err > 0) {
 377			num_msix = rounddown_pow_of_two(err);
 378			if (num_msix)
 379				goto enable_msix;
 380		}
 381	}
 382	return err;
 383}
 384
 385
 386static void qlcnic_enable_msi_legacy(struct qlcnic_adapter *adapter)
 387{
 388	const struct qlcnic_legacy_intr_set *legacy_intrp;
 389	struct pci_dev *pdev = adapter->pdev;
 390
 391	if (use_msi && !pci_enable_msi(pdev)) {
 392		adapter->flags |= QLCNIC_MSI_ENABLED;
 393		adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
 394				msi_tgt_status[adapter->ahw->pci_func]);
 395		dev_info(&pdev->dev, "using msi interrupts\n");
 396		adapter->msix_entries[0].vector = pdev->irq;
 397		return;
 398	}
 399
 400	legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
 401
 402	adapter->int_vec_bit = legacy_intrp->int_vec_bit;
 403	adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
 404			legacy_intrp->tgt_status_reg);
 405	adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
 406			legacy_intrp->tgt_mask_reg);
 407	adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
 408
 409	adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
 410			ISR_INT_STATE_REG);
 411	dev_info(&pdev->dev, "using legacy interrupts\n");
 412	adapter->msix_entries[0].vector = pdev->irq;
 413}
 414
 415static void
 416qlcnic_setup_intr(struct qlcnic_adapter *adapter)
 417{
 418	int num_msix;
 419
 420	if (adapter->msix_supported) {
 421		num_msix = (num_online_cpus() >=
 422			QLCNIC_DEF_NUM_STS_DESC_RINGS) ?
 423			QLCNIC_DEF_NUM_STS_DESC_RINGS :
 424			QLCNIC_MIN_NUM_RSS_RINGS;
 425	} else
 426		num_msix = 1;
 427
 428	if (!qlcnic_enable_msix(adapter, num_msix))
 429		return;
 430
 431	qlcnic_enable_msi_legacy(adapter);
 432}
 433
 434static void
 435qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
 436{
 437	if (adapter->flags & QLCNIC_MSIX_ENABLED)
 438		pci_disable_msix(adapter->pdev);
 439	if (adapter->flags & QLCNIC_MSI_ENABLED)
 440		pci_disable_msi(adapter->pdev);
 441}
 442
 443static void
 444qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
 445{
 446	if (adapter->ahw->pci_base0 != NULL)
 447		iounmap(adapter->ahw->pci_base0);
 448}
 449
 450static int
 451qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
 452{
 453	struct qlcnic_pci_info *pci_info;
 454	int i, ret = 0;
 455	u8 pfn;
 456
 457	pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
 458	if (!pci_info)
 459		return -ENOMEM;
 460
 461	adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
 462				QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
 463	if (!adapter->npars) {
 464		ret = -ENOMEM;
 465		goto err_pci_info;
 466	}
 467
 468	adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
 469				QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
 470	if (!adapter->eswitch) {
 471		ret = -ENOMEM;
 472		goto err_npars;
 473	}
 474
 475	ret = qlcnic_get_pci_info(adapter, pci_info);
 476	if (ret)
 477		goto err_eswitch;
 478
 479	for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
 480		pfn = pci_info[i].id;
 481		if (pfn > QLCNIC_MAX_PCI_FUNC) {
 482			ret = QL_STATUS_INVALID_PARAM;
 483			goto err_eswitch;
 484		}
 485		adapter->npars[pfn].active = (u8)pci_info[i].active;
 486		adapter->npars[pfn].type = (u8)pci_info[i].type;
 487		adapter->npars[pfn].phy_port = (u8)pci_info[i].default_port;
 488		adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
 489		adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
 490	}
 491
 492	for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
 493		adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
 494
 495	kfree(pci_info);
 496	return 0;
 497
 498err_eswitch:
 499	kfree(adapter->eswitch);
 500	adapter->eswitch = NULL;
 501err_npars:
 502	kfree(adapter->npars);
 503	adapter->npars = NULL;
 504err_pci_info:
 505	kfree(pci_info);
 506
 507	return ret;
 508}
 509
 510static int
 511qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
 512{
 513	u8 id;
 514	u32 ref_count;
 515	int i, ret = 1;
 516	u32 data = QLCNIC_MGMT_FUNC;
 517	void __iomem *priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
 518
 519	/* If other drivers are not in use set their privilege level */
 520	ref_count = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
 521	ret = qlcnic_api_lock(adapter);
 522	if (ret)
 523		goto err_lock;
 524
 525	if (qlcnic_config_npars) {
 526		for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
 527			id = i;
 528			if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
 529				id == adapter->ahw->pci_func)
 530				continue;
 531			data |= (qlcnic_config_npars &
 532					QLC_DEV_SET_DRV(0xf, id));
 533		}
 534	} else {
 535		data = readl(priv_op);
 536		data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw->pci_func)) |
 537			(QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
 538			adapter->ahw->pci_func));
 539	}
 540	writel(data, priv_op);
 541	qlcnic_api_unlock(adapter);
 542err_lock:
 543	return ret;
 544}
 545
 546static void
 547qlcnic_check_vf(struct qlcnic_adapter *adapter)
 548{
 549	void __iomem *msix_base_addr;
 550	void __iomem *priv_op;
 551	u32 func;
 552	u32 msix_base;
 553	u32 op_mode, priv_level;
 554
 555	/* Determine FW API version */
 556	adapter->fw_hal_version = readl(adapter->ahw->pci_base0 +
 557					QLCNIC_FW_API);
 558
 559	/* Find PCI function number */
 560	pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
 561	msix_base_addr = adapter->ahw->pci_base0 + QLCNIC_MSIX_BASE;
 562	msix_base = readl(msix_base_addr);
 563	func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
 564	adapter->ahw->pci_func = func;
 565
 566	/* Determine function privilege level */
 567	priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
 568	op_mode = readl(priv_op);
 569	if (op_mode == QLC_DEV_DRV_DEFAULT)
 570		priv_level = QLCNIC_MGMT_FUNC;
 571	else
 572		priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
 573
 574	if (priv_level == QLCNIC_NON_PRIV_FUNC) {
 575		adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
 576		dev_info(&adapter->pdev->dev,
 577			"HAL Version: %d Non Privileged function\n",
 578			adapter->fw_hal_version);
 579		adapter->nic_ops = &qlcnic_vf_ops;
 580	} else
 581		adapter->nic_ops = &qlcnic_ops;
 582}
 583
 584static int
 585qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
 586{
 587	void __iomem *mem_ptr0 = NULL;
 588	resource_size_t mem_base;
 589	unsigned long mem_len, pci_len0 = 0;
 590
 591	struct pci_dev *pdev = adapter->pdev;
 592
 593	/* remap phys address */
 594	mem_base = pci_resource_start(pdev, 0);	/* 0 is for BAR 0 */
 595	mem_len = pci_resource_len(pdev, 0);
 596
 597	if (mem_len == QLCNIC_PCI_2MB_SIZE) {
 598
 599		mem_ptr0 = pci_ioremap_bar(pdev, 0);
 600		if (mem_ptr0 == NULL) {
 601			dev_err(&pdev->dev, "failed to map PCI bar 0\n");
 602			return -EIO;
 603		}
 604		pci_len0 = mem_len;
 605	} else {
 606		return -EIO;
 607	}
 608
 609	dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
 610
 611	adapter->ahw->pci_base0 = mem_ptr0;
 612	adapter->ahw->pci_len0 = pci_len0;
 613
 614	qlcnic_check_vf(adapter);
 615
 616	adapter->ahw->ocm_win_crb = qlcnic_get_ioaddr(adapter,
 617		QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(
 618			adapter->ahw->pci_func)));
 619
 620	return 0;
 621}
 622
 623static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
 624{
 625	struct pci_dev *pdev = adapter->pdev;
 626	int i, found = 0;
 627
 628	for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
 629		if (qlcnic_boards[i].vendor == pdev->vendor &&
 630			qlcnic_boards[i].device == pdev->device &&
 631			qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
 632			qlcnic_boards[i].sub_device == pdev->subsystem_device) {
 633				sprintf(name, "%pM: %s" ,
 634					adapter->mac_addr,
 635					qlcnic_boards[i].short_name);
 636				found = 1;
 637				break;
 638		}
 639
 640	}
 641
 642	if (!found)
 643		sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
 644}
 645
 646static void
 647qlcnic_check_options(struct qlcnic_adapter *adapter)
 648{
 649	u32 fw_major, fw_minor, fw_build;
 650	struct pci_dev *pdev = adapter->pdev;
 651
 652	fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
 653	fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
 654	fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
 655
 656	adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
 657
 658	dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
 659			fw_major, fw_minor, fw_build);
 660	if (adapter->ahw->port_type == QLCNIC_XGBE) {
 661		if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
 662			adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
 663			adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
 664		} else {
 665			adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
 666			adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
 667		}
 668
 669		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
 670		adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
 671
 672	} else if (adapter->ahw->port_type == QLCNIC_GBE) {
 673		adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
 674		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
 675		adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
 676		adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
 677	}
 678
 679	adapter->msix_supported = !!use_msi_x;
 680
 681	adapter->num_txd = MAX_CMD_DESCRIPTORS;
 682
 683	adapter->max_rds_rings = MAX_RDS_RINGS;
 684}
 685
 686static int
 687qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
 688{
 689	int err;
 690	struct qlcnic_info nic_info;
 691
 692	err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
 693	if (err)
 694		return err;
 695
 696	adapter->physical_port = (u8)nic_info.phys_port;
 697	adapter->switch_mode = nic_info.switch_mode;
 698	adapter->max_tx_ques = nic_info.max_tx_ques;
 699	adapter->max_rx_ques = nic_info.max_rx_ques;
 700	adapter->capabilities = nic_info.capabilities;
 701	adapter->max_mac_filters = nic_info.max_mac_filters;
 702	adapter->max_mtu = nic_info.max_mtu;
 703
 704	if (adapter->capabilities & BIT_6)
 705		adapter->flags |= QLCNIC_ESWITCH_ENABLED;
 706	else
 707		adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
 708
 709	return err;
 710}
 711
 712static void
 713qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
 714		struct qlcnic_esw_func_cfg *esw_cfg)
 715{
 716	if (esw_cfg->discard_tagged)
 717		adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
 718	else
 719		adapter->flags |= QLCNIC_TAGGING_ENABLED;
 720
 721	if (esw_cfg->vlan_id)
 722		adapter->pvid = esw_cfg->vlan_id;
 723	else
 724		adapter->pvid = 0;
 725}
 726
 727static void
 728qlcnic_vlan_rx_add(struct net_device *netdev, u16 vid)
 729{
 730	struct qlcnic_adapter *adapter = netdev_priv(netdev);
 731	set_bit(vid, adapter->vlans);
 732}
 733
 734static void
 735qlcnic_vlan_rx_del(struct net_device *netdev, u16 vid)
 736{
 737	struct qlcnic_adapter *adapter = netdev_priv(netdev);
 738
 739	qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
 740	clear_bit(vid, adapter->vlans);
 741}
 742
 743static void
 744qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
 745		struct qlcnic_esw_func_cfg *esw_cfg)
 746{
 747	adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
 748				QLCNIC_PROMISC_DISABLED);
 749
 750	if (esw_cfg->mac_anti_spoof)
 751		adapter->flags |= QLCNIC_MACSPOOF;
 752
 753	if (!esw_cfg->mac_override)
 754		adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
 755
 756	if (!esw_cfg->promisc_mode)
 757		adapter->flags |= QLCNIC_PROMISC_DISABLED;
 758
 759	qlcnic_set_netdev_features(adapter, esw_cfg);
 760}
 761
 762static int
 763qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
 764{
 765	struct qlcnic_esw_func_cfg esw_cfg;
 766
 767	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
 768		return 0;
 769
 770	esw_cfg.pci_func = adapter->ahw->pci_func;
 771	if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
 772			return -EIO;
 773	qlcnic_set_vlan_config(adapter, &esw_cfg);
 774	qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
 775
 776	return 0;
 777}
 778
 779static void
 780qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
 781		struct qlcnic_esw_func_cfg *esw_cfg)
 782{
 783	struct net_device *netdev = adapter->netdev;
 784	unsigned long features, vlan_features;
 785
 786	features = (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
 787			NETIF_F_IPV6_CSUM | NETIF_F_GRO);
 788	vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
 789			NETIF_F_IPV6_CSUM | NETIF_F_HW_VLAN_FILTER);
 790
 791	if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
 792		features |= (NETIF_F_TSO | NETIF_F_TSO6);
 793		vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
 794	}
 795
 796	if (netdev->features & NETIF_F_LRO)
 797		features |= NETIF_F_LRO;
 798
 799	if (esw_cfg->offload_flags & BIT_0) {
 800		netdev->features |= features;
 801		if (!(esw_cfg->offload_flags & BIT_1))
 802			netdev->features &= ~NETIF_F_TSO;
 803		if (!(esw_cfg->offload_flags & BIT_2))
 804			netdev->features &= ~NETIF_F_TSO6;
 805	} else {
 806		netdev->features &= ~features;
 807	}
 808
 809	netdev->vlan_features = (features & vlan_features);
 810}
 811
 812static int
 813qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
 814{
 815	void __iomem *priv_op;
 816	u32 op_mode, priv_level;
 817	int err = 0;
 818
 819	err = qlcnic_initialize_nic(adapter);
 820	if (err)
 821		return err;
 822
 823	if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
 824		return 0;
 825
 826	priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
 827	op_mode = readl(priv_op);
 828	priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
 829
 830	if (op_mode == QLC_DEV_DRV_DEFAULT)
 831		priv_level = QLCNIC_MGMT_FUNC;
 832	else
 833		priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
 834
 835	if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
 836		if (priv_level == QLCNIC_MGMT_FUNC) {
 837			adapter->op_mode = QLCNIC_MGMT_FUNC;
 838			err = qlcnic_init_pci_info(adapter);
 839			if (err)
 840				return err;
 841			/* Set privilege level for other functions */
 842			qlcnic_set_function_modes(adapter);
 843			dev_info(&adapter->pdev->dev,
 844				"HAL Version: %d, Management function\n",
 845				adapter->fw_hal_version);
 846		} else if (priv_level == QLCNIC_PRIV_FUNC) {
 847			adapter->op_mode = QLCNIC_PRIV_FUNC;
 848			dev_info(&adapter->pdev->dev,
 849				"HAL Version: %d, Privileged function\n",
 850				adapter->fw_hal_version);
 851		}
 852	}
 853
 854	adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
 855
 856	return err;
 857}
 858
 859static int
 860qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
 861{
 862	struct qlcnic_esw_func_cfg esw_cfg;
 863	struct qlcnic_npar_info *npar;
 864	u8 i;
 865
 866	if (adapter->need_fw_reset)
 867		return 0;
 868
 869	for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
 870		if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
 871			continue;
 872		memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
 873		esw_cfg.pci_func = i;
 874		esw_cfg.offload_flags = BIT_0;
 875		esw_cfg.mac_override = BIT_0;
 876		esw_cfg.promisc_mode = BIT_0;
 877		if (adapter->capabilities  & QLCNIC_FW_CAPABILITY_TSO)
 878			esw_cfg.offload_flags |= (BIT_1 | BIT_2);
 879		if (qlcnic_config_switch_port(adapter, &esw_cfg))
 880			return -EIO;
 881		npar = &adapter->npars[i];
 882		npar->pvid = esw_cfg.vlan_id;
 883		npar->mac_override = esw_cfg.mac_override;
 884		npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
 885		npar->discard_tagged = esw_cfg.discard_tagged;
 886		npar->promisc_mode = esw_cfg.promisc_mode;
 887		npar->offload_flags = esw_cfg.offload_flags;
 888	}
 889
 890	return 0;
 891}
 892
 893static int
 894qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
 895			struct qlcnic_npar_info *npar, int pci_func)
 896{
 897	struct qlcnic_esw_func_cfg esw_cfg;
 898	esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
 899	esw_cfg.pci_func = pci_func;
 900	esw_cfg.vlan_id = npar->pvid;
 901	esw_cfg.mac_override = npar->mac_override;
 902	esw_cfg.discard_tagged = npar->discard_tagged;
 903	esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
 904	esw_cfg.offload_flags = npar->offload_flags;
 905	esw_cfg.promisc_mode = npar->promisc_mode;
 906	if (qlcnic_config_switch_port(adapter, &esw_cfg))
 907		return -EIO;
 908
 909	esw_cfg.op_mode = QLCNIC_ADD_VLAN;
 910	if (qlcnic_config_switch_port(adapter, &esw_cfg))
 911		return -EIO;
 912
 913	return 0;
 914}
 915
 916static int
 917qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
 918{
 919	int i, err;
 920	struct qlcnic_npar_info *npar;
 921	struct qlcnic_info nic_info;
 922
 923	if (!adapter->need_fw_reset)
 924		return 0;
 925
 926	/* Set the NPAR config data after FW reset */
 927	for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
 928		npar = &adapter->npars[i];
 929		if (npar->type != QLCNIC_TYPE_NIC)
 930			continue;
 931		err = qlcnic_get_nic_info(adapter, &nic_info, i);
 932		if (err)
 933			return err;
 934		nic_info.min_tx_bw = npar->min_bw;
 935		nic_info.max_tx_bw = npar->max_bw;
 936		err = qlcnic_set_nic_info(adapter, &nic_info);
 937		if (err)
 938			return err;
 939
 940		if (npar->enable_pm) {
 941			err = qlcnic_config_port_mirroring(adapter,
 942							npar->dest_npar, 1, i);
 943			if (err)
 944				return err;
 945		}
 946		err = qlcnic_reset_eswitch_config(adapter, npar, i);
 947		if (err)
 948			return err;
 949	}
 950	return 0;
 951}
 952
 953static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
 954{
 955	u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
 956	u32 npar_state;
 957
 958	if (adapter->op_mode == QLCNIC_MGMT_FUNC)
 959		return 0;
 960
 961	npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
 962	while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
 963		msleep(1000);
 964		npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
 965	}
 966	if (!npar_opt_timeo) {
 967		dev_err(&adapter->pdev->dev,
 968			"Waiting for NPAR state to opertional timeout\n");
 969		return -EIO;
 970	}
 971	return 0;
 972}
 973
 974static int
 975qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
 976{
 977	int err;
 978
 979	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
 980		    adapter->op_mode != QLCNIC_MGMT_FUNC)
 981		return 0;
 982
 983	err = qlcnic_set_default_offload_settings(adapter);
 984	if (err)
 985		return err;
 986
 987	err = qlcnic_reset_npar_config(adapter);
 988	if (err)
 989		return err;
 990
 991	qlcnic_dev_set_npar_ready(adapter);
 992
 993	return err;
 994}
 995
 996static int
 997qlcnic_start_firmware(struct qlcnic_adapter *adapter)
 998{
 999	int err;
1000
1001	err = qlcnic_can_start_firmware(adapter);
1002	if (err < 0)
1003		return err;
1004	else if (!err)
1005		goto check_fw_status;
1006
1007	if (load_fw_file)
1008		qlcnic_request_firmware(adapter);
1009	else {
1010		err = qlcnic_check_flash_fw_ver(adapter);
1011		if (err)
1012			goto err_out;
1013
1014		adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
1015	}
1016
1017	err = qlcnic_need_fw_reset(adapter);
1018	if (err == 0)
1019		goto check_fw_status;
1020
1021	err = qlcnic_pinit_from_rom(adapter);
1022	if (err)
1023		goto err_out;
1024
1025	err = qlcnic_load_firmware(adapter);
1026	if (err)
1027		goto err_out;
1028
1029	qlcnic_release_firmware(adapter);
1030	QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
1031
1032check_fw_status:
1033	err = qlcnic_check_fw_status(adapter);
1034	if (err)
1035		goto err_out;
1036
1037	QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
1038	qlcnic_idc_debug_info(adapter, 1);
1039
1040	err = qlcnic_check_eswitch_mode(adapter);
1041	if (err) {
1042		dev_err(&adapter->pdev->dev,
1043			"Memory allocation failed for eswitch\n");
1044		goto err_out;
1045	}
1046	err = qlcnic_set_mgmt_operations(adapter);
1047	if (err)
1048		goto err_out;
1049
1050	qlcnic_check_options(adapter);
1051	adapter->need_fw_reset = 0;
1052
1053	qlcnic_release_firmware(adapter);
1054	return 0;
1055
1056err_out:
1057	QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1058	dev_err(&adapter->pdev->dev, "Device state set to failed\n");
1059
1060	qlcnic_release_firmware(adapter);
1061	return err;
1062}
1063
1064static int
1065qlcnic_request_irq(struct qlcnic_adapter *adapter)
1066{
1067	irq_handler_t handler;
1068	struct qlcnic_host_sds_ring *sds_ring;
1069	int err, ring;
1070
1071	unsigned long flags = 0;
1072	struct net_device *netdev = adapter->netdev;
1073	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1074
1075	if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1076		handler = qlcnic_tmp_intr;
1077		if (!QLCNIC_IS_MSI_FAMILY(adapter))
1078			flags |= IRQF_SHARED;
1079
1080	} else {
1081		if (adapter->flags & QLCNIC_MSIX_ENABLED)
1082			handler = qlcnic_msix_intr;
1083		else if (adapter->flags & QLCNIC_MSI_ENABLED)
1084			handler = qlcnic_msi_intr;
1085		else {
1086			flags |= IRQF_SHARED;
1087			handler = qlcnic_intr;
1088		}
1089	}
1090	adapter->irq = netdev->irq;
1091
1092	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1093		sds_ring = &recv_ctx->sds_rings[ring];
1094		sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1095		err = request_irq(sds_ring->irq, handler,
1096				  flags, sds_ring->name, sds_ring);
1097		if (err)
1098			return err;
1099	}
1100
1101	return 0;
1102}
1103
1104static void
1105qlcnic_free_irq(struct qlcnic_adapter *adapter)
1106{
1107	int ring;
1108	struct qlcnic_host_sds_ring *sds_ring;
1109
1110	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1111
1112	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1113		sds_ring = &recv_ctx->sds_rings[ring];
1114		free_irq(sds_ring->irq, sds_ring);
1115	}
1116}
1117
1118static int
1119__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1120{
1121	int ring;
1122	struct qlcnic_host_rds_ring *rds_ring;
1123
1124	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1125		return -EIO;
1126
1127	if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1128		return 0;
1129	if (qlcnic_set_eswitch_port_config(adapter))
1130		return -EIO;
1131
1132	if (qlcnic_fw_create_ctx(adapter))
1133		return -EIO;
1134
1135	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1136		rds_ring = &adapter->recv_ctx->rds_rings[ring];
1137		qlcnic_post_rx_buffers(adapter, rds_ring);
1138	}
1139
1140	qlcnic_set_multi(netdev);
1141	qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1142
1143	adapter->ahw->linkup = 0;
1144
1145	if (adapter->max_sds_rings > 1)
1146		qlcnic_config_rss(adapter, 1);
1147
1148	qlcnic_config_intr_coalesce(adapter);
1149
1150	if (netdev->features & NETIF_F_LRO)
1151		qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1152
1153	qlcnic_napi_enable(adapter);
1154
1155	qlcnic_linkevent_request(adapter, 1);
1156
1157	adapter->reset_context = 0;
1158	set_bit(__QLCNIC_DEV_UP, &adapter->state);
1159	return 0;
1160}
1161
1162/* Usage: During resume and firmware recovery module.*/
1163
1164static int
1165qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1166{
1167	int err = 0;
1168
1169	rtnl_lock();
1170	if (netif_running(netdev))
1171		err = __qlcnic_up(adapter, netdev);
1172	rtnl_unlock();
1173
1174	return err;
1175}
1176
1177static void
1178__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1179{
1180	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1181		return;
1182
1183	if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1184		return;
1185
1186	smp_mb();
1187	spin_lock(&adapter->tx_clean_lock);
1188	netif_carrier_off(netdev);
1189	netif_tx_disable(netdev);
1190
1191	qlcnic_free_mac_list(adapter);
1192
1193	if (adapter->fhash.fnum)
1194		qlcnic_delete_lb_filters(adapter);
1195
1196	qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1197
1198	qlcnic_napi_disable(adapter);
1199
1200	qlcnic_fw_destroy_ctx(adapter);
1201
1202	qlcnic_reset_rx_buffers_list(adapter);
1203	qlcnic_release_tx_buffers(adapter);
1204	spin_unlock(&adapter->tx_clean_lock);
1205}
1206
1207/* Usage: During suspend and firmware recovery module */
1208
1209static void
1210qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1211{
1212	rtnl_lock();
1213	if (netif_running(netdev))
1214		__qlcnic_down(adapter, netdev);
1215	rtnl_unlock();
1216
1217}
1218
1219static int
1220qlcnic_attach(struct qlcnic_adapter *adapter)
1221{
1222	struct net_device *netdev = adapter->netdev;
1223	struct pci_dev *pdev = adapter->pdev;
1224	int err;
1225
1226	if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1227		return 0;
1228
1229	err = qlcnic_napi_add(adapter, netdev);
1230	if (err)
1231		return err;
1232
1233	err = qlcnic_alloc_sw_resources(adapter);
1234	if (err) {
1235		dev_err(&pdev->dev, "Error in setting sw resources\n");
1236		goto err_out_napi_del;
1237	}
1238
1239	err = qlcnic_alloc_hw_resources(adapter);
1240	if (err) {
1241		dev_err(&pdev->dev, "Error in setting hw resources\n");
1242		goto err_out_free_sw;
1243	}
1244
1245	err = qlcnic_request_irq(adapter);
1246	if (err) {
1247		dev_err(&pdev->dev, "failed to setup interrupt\n");
1248		goto err_out_free_hw;
1249	}
1250
1251	qlcnic_create_sysfs_entries(adapter);
1252
1253	adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1254	return 0;
1255
1256err_out_free_hw:
1257	qlcnic_free_hw_resources(adapter);
1258err_out_free_sw:
1259	qlcnic_free_sw_resources(adapter);
1260err_out_napi_del:
1261	qlcnic_napi_del(adapter);
1262	return err;
1263}
1264
1265static void
1266qlcnic_detach(struct qlcnic_adapter *adapter)
1267{
1268	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1269		return;
1270
1271	qlcnic_remove_sysfs_entries(adapter);
1272
1273	qlcnic_free_hw_resources(adapter);
1274	qlcnic_release_rx_buffers(adapter);
1275	qlcnic_free_irq(adapter);
1276	qlcnic_napi_del(adapter);
1277	qlcnic_free_sw_resources(adapter);
1278
1279	adapter->is_up = 0;
1280}
1281
1282void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1283{
1284	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1285	struct qlcnic_host_sds_ring *sds_ring;
1286	int ring;
1287
1288	clear_bit(__QLCNIC_DEV_UP, &adapter->state);
1289	if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1290		for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1291			sds_ring = &adapter->recv_ctx->sds_rings[ring];
1292			qlcnic_disable_int(sds_ring);
1293		}
1294	}
1295
1296	qlcnic_fw_destroy_ctx(adapter);
1297
1298	qlcnic_detach(adapter);
1299
1300	adapter->diag_test = 0;
1301	adapter->max_sds_rings = max_sds_rings;
1302
1303	if (qlcnic_attach(adapter))
1304		goto out;
1305
1306	if (netif_running(netdev))
1307		__qlcnic_up(adapter, netdev);
1308out:
1309	netif_device_attach(netdev);
1310}
1311
1312static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1313{
1314	int err = 0;
1315	adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1316				GFP_KERNEL);
1317	if (!adapter->ahw) {
1318		dev_err(&adapter->pdev->dev,
1319			"Failed to allocate recv ctx resources for adapter\n");
1320		err = -ENOMEM;
1321		goto err_out;
1322	}
1323	adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1324				GFP_KERNEL);
1325	if (!adapter->recv_ctx) {
1326		dev_err(&adapter->pdev->dev,
1327			"Failed to allocate recv ctx resources for adapter\n");
1328		kfree(adapter->ahw);
1329		adapter->ahw = NULL;
1330		err = -ENOMEM;
1331		goto err_out;
1332	}
1333	/* Initialize interrupt coalesce parameters */
1334	adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1335	adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1336	adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
1337err_out:
1338	return err;
1339}
1340
1341static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1342{
1343	kfree(adapter->recv_ctx);
1344	adapter->recv_ctx = NULL;
1345
1346	if (adapter->ahw->fw_dump.tmpl_hdr) {
1347		vfree(adapter->ahw->fw_dump.tmpl_hdr);
1348		adapter->ahw->fw_dump.tmpl_hdr = NULL;
1349	}
1350	kfree(adapter->ahw);
1351	adapter->ahw = NULL;
1352}
1353
1354int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1355{
1356	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1357	struct qlcnic_host_sds_ring *sds_ring;
1358	struct qlcnic_host_rds_ring *rds_ring;
1359	int ring;
1360	int ret;
1361
1362	netif_device_detach(netdev);
1363
1364	if (netif_running(netdev))
1365		__qlcnic_down(adapter, netdev);
1366
1367	qlcnic_detach(adapter);
1368
1369	adapter->max_sds_rings = 1;
1370	adapter->diag_test = test;
1371
1372	ret = qlcnic_attach(adapter);
1373	if (ret) {
1374		netif_device_attach(netdev);
1375		return ret;
1376	}
1377
1378	ret = qlcnic_fw_create_ctx(adapter);
1379	if (ret) {
1380		qlcnic_detach(adapter);
1381		netif_device_attach(netdev);
1382		return ret;
1383	}
1384
1385	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1386		rds_ring = &adapter->recv_ctx->rds_rings[ring];
1387		qlcnic_post_rx_buffers(adapter, rds_ring);
1388	}
1389
1390	if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1391		for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1392			sds_ring = &adapter->recv_ctx->sds_rings[ring];
1393			qlcnic_enable_int(sds_ring);
1394		}
1395	}
1396	set_bit(__QLCNIC_DEV_UP, &adapter->state);
1397
1398	return 0;
1399}
1400
1401/* Reset context in hardware only */
1402static int
1403qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1404{
1405	struct net_device *netdev = adapter->netdev;
1406
1407	if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1408		return -EBUSY;
1409
1410	netif_device_detach(netdev);
1411
1412	qlcnic_down(adapter, netdev);
1413
1414	qlcnic_up(adapter, netdev);
1415
1416	netif_device_attach(netdev);
1417
1418	clear_bit(__QLCNIC_RESETTING, &adapter->state);
1419	return 0;
1420}
1421
1422int
1423qlcnic_reset_context(struct qlcnic_adapter *adapter)
1424{
1425	int err = 0;
1426	struct net_device *netdev = adapter->netdev;
1427
1428	if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1429		return -EBUSY;
1430
1431	if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1432
1433		netif_device_detach(netdev);
1434
1435		if (netif_running(netdev))
1436			__qlcnic_down(adapter, netdev);
1437
1438		qlcnic_detach(adapter);
1439
1440		if (netif_running(netdev)) {
1441			err = qlcnic_attach(adapter);
1442			if (!err)
1443				__qlcnic_up(adapter, netdev);
1444		}
1445
1446		netif_device_attach(netdev);
1447	}
1448
1449	clear_bit(__QLCNIC_RESETTING, &adapter->state);
1450	return err;
1451}
1452
1453static int
1454qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
1455		struct net_device *netdev, u8 pci_using_dac)
1456{
1457	int err;
1458	struct pci_dev *pdev = adapter->pdev;
1459
1460	adapter->mc_enabled = 0;
1461	adapter->max_mc_count = 38;
1462
1463	netdev->netdev_ops	   = &qlcnic_netdev_ops;
1464	netdev->watchdog_timeo     = 5*HZ;
1465
1466	qlcnic_change_mtu(netdev, netdev->mtu);
1467
1468	SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1469
1470	netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1471		NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
1472
1473	if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
1474		netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1475	if (pci_using_dac)
1476		netdev->hw_features |= NETIF_F_HIGHDMA;
1477
1478	netdev->vlan_features = netdev->hw_features;
1479
1480	if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1481		netdev->hw_features |= NETIF_F_HW_VLAN_TX;
1482	if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1483		netdev->hw_features |= NETIF_F_LRO;
1484
1485	netdev->features |= netdev->hw_features |
1486		NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
1487
1488	netdev->irq = adapter->msix_entries[0].vector;
1489
1490	netif_carrier_off(netdev);
1491
1492	err = register_netdev(netdev);
1493	if (err) {
1494		dev_err(&pdev->dev, "failed to register net device\n");
1495		return err;
1496	}
1497
1498	return 0;
1499}
1500
1501static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1502{
1503	if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1504			!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1505		*pci_using_dac = 1;
1506	else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1507			!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1508		*pci_using_dac = 0;
1509	else {
1510		dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1511		return -EIO;
1512	}
1513
1514	return 0;
1515}
1516
1517static int
1518qlcnic_alloc_msix_entries(struct qlcnic_adapter *adapter, u16 count)
1519{
1520	adapter->msix_entries = kcalloc(count, sizeof(struct msix_entry),
1521					GFP_KERNEL);
1522
1523	if (adapter->msix_entries)
1524		return 0;
1525
1526	dev_err(&adapter->pdev->dev, "failed allocating msix_entries\n");
1527	return -ENOMEM;
1528}
1529
1530static int __devinit
1531qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1532{
1533	struct net_device *netdev = NULL;
1534	struct qlcnic_adapter *adapter = NULL;
1535	int err;
1536	uint8_t revision_id;
1537	uint8_t pci_using_dac;
1538	char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
1539
1540	err = pci_enable_device(pdev);
1541	if (err)
1542		return err;
1543
1544	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1545		err = -ENODEV;
1546		goto err_out_disable_pdev;
1547	}
1548
1549	err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1550	if (err)
1551		goto err_out_disable_pdev;
1552
1553	err = pci_request_regions(pdev, qlcnic_driver_name);
1554	if (err)
1555		goto err_out_disable_pdev;
1556
1557	pci_set_master(pdev);
1558	pci_enable_pcie_error_reporting(pdev);
1559
1560	netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1561	if (!netdev) {
1562		dev_err(&pdev->dev, "failed to allocate net_device\n");
1563		err = -ENOMEM;
1564		goto err_out_free_res;
1565	}
1566
1567	SET_NETDEV_DEV(netdev, &pdev->dev);
1568
1569	adapter = netdev_priv(netdev);
1570	adapter->netdev  = netdev;
1571	adapter->pdev    = pdev;
1572
1573	if (qlcnic_alloc_adapter_resources(adapter))
1574		goto err_out_free_netdev;
1575
1576	adapter->dev_rst_time = jiffies;
1577	revision_id = pdev->revision;
1578	adapter->ahw->revision_id = revision_id;
1579
1580	rwlock_init(&adapter->ahw->crb_lock);
1581	mutex_init(&adapter->ahw->mem_lock);
1582
1583	spin_lock_init(&adapter->tx_clean_lock);
1584	INIT_LIST_HEAD(&adapter->mac_list);
1585
1586	err = qlcnic_setup_pci_map(adapter);
1587	if (err)
1588		goto err_out_free_hw;
1589
1590	/* This will be reset for mezz cards  */
1591	adapter->portnum = adapter->ahw->pci_func;
1592
1593	/* Get FW dump template and store it */
1594	if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC)
1595		qlcnic_fw_cmd_get_minidump_temp(adapter);
1596
1597	err = qlcnic_get_board_info(adapter);
1598	if (err) {
1599		dev_err(&pdev->dev, "Error getting board config info.\n");
1600		goto err_out_iounmap;
1601	}
1602
1603	err = qlcnic_setup_idc_param(adapter);
1604	if (err)
1605		goto err_out_iounmap;
1606
1607	adapter->flags |= QLCNIC_NEED_FLR;
1608
1609	err = adapter->nic_ops->start_firmware(adapter);
1610	if (err) {
1611		dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
1612		goto err_out_decr_ref;
1613	}
1614
1615	if (qlcnic_read_mac_addr(adapter))
1616		dev_warn(&pdev->dev, "failed to read mac addr\n");
1617
1618	if (adapter->portnum == 0) {
1619		get_brd_name(adapter, brd_name);
1620
1621		pr_info("%s: %s Board Chip rev 0x%x\n",
1622				module_name(THIS_MODULE),
1623				brd_name, adapter->ahw->revision_id);
1624	}
1625
1626	qlcnic_clear_stats(adapter);
1627
1628	err = qlcnic_alloc_msix_entries(adapter, adapter->max_rx_ques);
1629	if (err)
1630		goto err_out_decr_ref;
1631
1632	qlcnic_setup_intr(adapter);
1633
1634	err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
1635	if (err)
1636		goto err_out_disable_msi;
1637
1638	pci_set_drvdata(pdev, adapter);
1639
1640	qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1641
1642	switch (adapter->ahw->port_type) {
1643	case QLCNIC_GBE:
1644		dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1645				adapter->netdev->name);
1646		break;
1647	case QLCNIC_XGBE:
1648		dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1649				adapter->netdev->name);
1650		break;
1651	}
1652
1653	qlcnic_alloc_lb_filters_mem(adapter);
1654	qlcnic_create_diag_entries(adapter);
1655
1656	return 0;
1657
1658err_out_disable_msi:
1659	qlcnic_teardown_intr(adapter);
1660	kfree(adapter->msix_entries);
1661
1662err_out_decr_ref:
1663	qlcnic_clr_all_drv_state(adapter, 0);
1664
1665err_out_iounmap:
1666	qlcnic_cleanup_pci_map(adapter);
1667
1668err_out_free_hw:
1669	qlcnic_free_adapter_resources(adapter);
1670
1671err_out_free_netdev:
1672	free_netdev(netdev);
1673
1674err_out_free_res:
1675	pci_release_regions(pdev);
1676
1677err_out_disable_pdev:
1678	pci_set_drvdata(pdev, NULL);
1679	pci_disable_device(pdev);
1680	return err;
1681}
1682
1683static void __devexit qlcnic_remove(struct pci_dev *pdev)
1684{
1685	struct qlcnic_adapter *adapter;
1686	struct net_device *netdev;
1687
1688	adapter = pci_get_drvdata(pdev);
1689	if (adapter == NULL)
1690		return;
1691
1692	netdev = adapter->netdev;
1693
1694	qlcnic_cancel_fw_work(adapter);
1695
1696	unregister_netdev(netdev);
1697
1698	qlcnic_detach(adapter);
1699
1700	if (adapter->npars != NULL)
1701		kfree(adapter->npars);
1702	if (adapter->eswitch != NULL)
1703		kfree(adapter->eswitch);
1704
1705	qlcnic_clr_all_drv_state(adapter, 0);
1706
1707	clear_bit(__QLCNIC_RESETTING, &adapter->state);
1708
1709	qlcnic_free_lb_filters_mem(adapter);
1710
1711	qlcnic_teardown_intr(adapter);
1712	kfree(adapter->msix_entries);
1713
1714	qlcnic_remove_diag_entries(adapter);
1715
1716	qlcnic_cleanup_pci_map(adapter);
1717
1718	qlcnic_release_firmware(adapter);
1719
1720	pci_disable_pcie_error_reporting(pdev);
1721	pci_release_regions(pdev);
1722	pci_disable_device(pdev);
1723	pci_set_drvdata(pdev, NULL);
1724
1725	qlcnic_free_adapter_resources(adapter);
1726	free_netdev(netdev);
1727}
1728static int __qlcnic_shutdown(struct pci_dev *pdev)
1729{
1730	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1731	struct net_device *netdev = adapter->netdev;
1732	int retval;
1733
1734	netif_device_detach(netdev);
1735
1736	qlcnic_cancel_fw_work(adapter);
1737
1738	if (netif_running(netdev))
1739		qlcnic_down(adapter, netdev);
1740
1741	qlcnic_clr_all_drv_state(adapter, 0);
1742
1743	clear_bit(__QLCNIC_RESETTING, &adapter->state);
1744
1745	retval = pci_save_state(pdev);
1746	if (retval)
1747		return retval;
1748
1749	if (qlcnic_wol_supported(adapter)) {
1750		pci_enable_wake(pdev, PCI_D3cold, 1);
1751		pci_enable_wake(pdev, PCI_D3hot, 1);
1752	}
1753
1754	return 0;
1755}
1756
1757static void qlcnic_shutdown(struct pci_dev *pdev)
1758{
1759	if (__qlcnic_shutdown(pdev))
1760		return;
1761
1762	pci_disable_device(pdev);
1763}
1764
1765#ifdef CONFIG_PM
1766static int
1767qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1768{
1769	int retval;
1770
1771	retval = __qlcnic_shutdown(pdev);
1772	if (retval)
1773		return retval;
1774
1775	pci_set_power_state(pdev, pci_choose_state(pdev, state));
1776	return 0;
1777}
1778
1779static int
1780qlcnic_resume(struct pci_dev *pdev)
1781{
1782	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1783	struct net_device *netdev = adapter->netdev;
1784	int err;
1785
1786	err = pci_enable_device(pdev);
1787	if (err)
1788		return err;
1789
1790	pci_set_power_state(pdev, PCI_D0);
1791	pci_set_master(pdev);
1792	pci_restore_state(pdev);
1793
1794	err = adapter->nic_ops->start_firmware(adapter);
1795	if (err) {
1796		dev_err(&pdev->dev, "failed to start firmware\n");
1797		return err;
1798	}
1799
1800	if (netif_running(netdev)) {
1801		err = qlcnic_up(adapter, netdev);
1802		if (err)
1803			goto done;
1804
1805		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
1806	}
1807done:
1808	netif_device_attach(netdev);
1809	qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1810	return 0;
1811}
1812#endif
1813
1814static int qlcnic_open(struct net_device *netdev)
1815{
1816	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1817	int err;
1818
1819	err = qlcnic_attach(adapter);
1820	if (err)
1821		return err;
1822
1823	err = __qlcnic_up(adapter, netdev);
1824	if (err)
1825		goto err_out;
1826
1827	netif_start_queue(netdev);
1828
1829	return 0;
1830
1831err_out:
1832	qlcnic_detach(adapter);
1833	return err;
1834}
1835
1836/*
1837 * qlcnic_close - Disables a network interface entry point
1838 */
1839static int qlcnic_close(struct net_device *netdev)
1840{
1841	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1842
1843	__qlcnic_down(adapter, netdev);
1844	return 0;
1845}
1846
1847static void
1848qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
1849{
1850	void *head;
1851	int i;
1852
1853	if (!qlcnic_mac_learn)
1854		return;
1855
1856	spin_lock_init(&adapter->mac_learn_lock);
1857
1858	head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1859								GFP_KERNEL);
1860	if (!head)
1861		return;
1862
1863	adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
1864	adapter->fhash.fhead = (struct hlist_head *)head;
1865
1866	for (i = 0; i < adapter->fhash.fmax; i++)
1867		INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1868}
1869
1870static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1871{
1872	if (adapter->fhash.fmax && adapter->fhash.fhead)
1873		kfree(adapter->fhash.fhead);
1874
1875	adapter->fhash.fhead = NULL;
1876	adapter->fhash.fmax = 0;
1877}
1878
1879static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
1880		u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
1881{
1882	struct cmd_desc_type0 *hwdesc;
1883	struct qlcnic_nic_req *req;
1884	struct qlcnic_mac_req *mac_req;
1885	struct qlcnic_vlan_req *vlan_req;
1886	u32 producer;
1887	u64 word;
1888
1889	producer = tx_ring->producer;
1890	hwdesc = &tx_ring->desc_head[tx_ring->producer];
1891
1892	req = (struct qlcnic_nic_req *)hwdesc;
1893	memset(req, 0, sizeof(struct qlcnic_nic_req));
1894	req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1895
1896	word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1897	req->req_hdr = cpu_to_le64(word);
1898
1899	mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
1900	mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
1901	memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1902
1903	vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1904	vlan_req->vlan_id = vlan_id;
1905
1906	tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
1907	smp_mb();
1908}
1909
1910#define QLCNIC_MAC_HASH(MAC)\
1911	((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1912
1913static void
1914qlcnic_send_filter(struct qlcnic_adapter *adapter,
1915		struct qlcnic_host_tx_ring *tx_ring,
1916		struct cmd_desc_type0 *first_desc,
1917		struct sk_buff *skb)
1918{
1919	struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1920	struct qlcnic_filter *fil, *tmp_fil;
1921	struct hlist_node *tmp_hnode, *n;
1922	struct hlist_head *head;
1923	u64 src_addr = 0;
1924	__le16 vlan_id = 0;
1925	u8 hindex;
1926
1927	if (!compare_ether_addr(phdr->h_source, adapter->mac_addr))
1928		return;
1929
1930	if (adapter->fhash.fnum >= adapter->fhash.fmax)
1931		return;
1932
1933	/* Only NPAR capable devices support vlan based learning*/
1934	if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1935		vlan_id = first_desc->vlan_TCI;
1936	memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1937	hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1938	head = &(adapter->fhash.fhead[hindex]);
1939
1940	hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
1941		if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1942			    tmp_fil->vlan_id == vlan_id) {
1943
1944			if (jiffies >
1945			    (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1946				qlcnic_change_filter(adapter, src_addr, vlan_id,
1947								tx_ring);
1948			tmp_fil->ftime = jiffies;
1949			return;
1950		}
1951	}
1952
1953	fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
1954	if (!fil)
1955		return;
1956
1957	qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
1958
1959	fil->ftime = jiffies;
1960	fil->vlan_id = vlan_id;
1961	memcpy(fil->faddr, &src_addr, ETH_ALEN);
1962	spin_lock(&adapter->mac_learn_lock);
1963	hlist_add_head(&(fil->fnode), head);
1964	adapter->fhash.fnum++;
1965	spin_unlock(&adapter->mac_learn_lock);
1966}
1967
1968static int
1969qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
1970		struct cmd_desc_type0 *first_desc,
1971		struct sk_buff *skb)
1972{
1973	u8 opcode = 0, hdr_len = 0;
1974	u16 flags = 0, vlan_tci = 0;
1975	int copied, offset, copy_len;
1976	struct cmd_desc_type0 *hwdesc;
1977	struct vlan_ethhdr *vh;
1978	struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1979	u16 protocol = ntohs(skb->protocol);
1980	u32 producer = tx_ring->producer;
1981
1982	if (protocol == ETH_P_8021Q) {
1983		vh = (struct vlan_ethhdr *)skb->data;
1984		flags = FLAGS_VLAN_TAGGED;
1985		vlan_tci = vh->h_vlan_TCI;
1986	} else if (vlan_tx_tag_present(skb)) {
1987		flags = FLAGS_VLAN_OOB;
1988		vlan_tci = vlan_tx_tag_get(skb);
1989	}
1990	if (unlikely(adapter->pvid)) {
1991		if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
1992			return -EIO;
1993		if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
1994			goto set_flags;
1995
1996		flags = FLAGS_VLAN_OOB;
1997		vlan_tci = adapter->pvid;
1998	}
1999set_flags:
2000	qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
2001	qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2002
2003	if (*(skb->data) & BIT_0) {
2004		flags |= BIT_0;
2005		memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
2006	}
2007	opcode = TX_ETHER_PKT;
2008	if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
2009			skb_shinfo(skb)->gso_s

Large files files are truncated, but you can click here to view the full file