Linux Audio

Check our new training course

Loading...
v4.17
 
   1/*
   2 * Copyright (C) 2003 - 2009 NetXen, Inc.
   3 * Copyright (C) 2009 - QLogic Corporation.
   4 * All rights reserved.
   5 *
   6 * This program is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU General Public License
   8 * as published by the Free Software Foundation; either version 2
   9 * of the License, or (at your option) any later version.
  10 *
  11 * This program is distributed in the hope that it will be useful, but
  12 * WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 * GNU General Public License for more details.
  15 *
  16 * You should have received a copy of the GNU General Public License
  17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18 *
  19 * The full GNU General Public License is included in this distribution
  20 * in the file called "COPYING".
  21 *
  22 */
  23
  24#include <linux/slab.h>
  25#include <linux/vmalloc.h>
  26#include <linux/interrupt.h>
  27#include "netxen_nic_hw.h"
  28
  29#include "netxen_nic.h"
  30
  31#include <linux/dma-mapping.h>
  32#include <linux/if_vlan.h>
  33#include <net/ip.h>
  34#include <linux/ipv6.h>
  35#include <linux/inetdevice.h>
  36#include <linux/sysfs.h>
  37#include <linux/aer.h>
  38
  39MODULE_DESCRIPTION("QLogic/NetXen (1/10) GbE Intelligent Ethernet Driver");
  40MODULE_LICENSE("GPL");
  41MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
  42MODULE_FIRMWARE(NX_UNIFIED_ROMIMAGE_NAME);
  43
  44char netxen_nic_driver_name[] = "netxen_nic";
  45static char netxen_nic_driver_string[] = "QLogic/NetXen Network Driver v"
  46    NETXEN_NIC_LINUX_VERSIONID;
  47
  48static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
  49
  50/* Default to restricted 1G auto-neg mode */
  51static int wol_port_mode = 5;
  52
  53static int use_msi = 1;
  54
  55static int use_msi_x = 1;
  56
  57static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
  58module_param(auto_fw_reset, int, 0644);
  59MODULE_PARM_DESC(auto_fw_reset,"Auto firmware reset (0=disabled, 1=enabled");
  60
  61static int netxen_nic_probe(struct pci_dev *pdev,
  62		const struct pci_device_id *ent);
  63static void netxen_nic_remove(struct pci_dev *pdev);
  64static int netxen_nic_open(struct net_device *netdev);
  65static int netxen_nic_close(struct net_device *netdev);
  66static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
  67					       struct net_device *);
  68static void netxen_tx_timeout(struct net_device *netdev);
  69static void netxen_tx_timeout_task(struct work_struct *work);
  70static void netxen_fw_poll_work(struct work_struct *work);
  71static void netxen_schedule_work(struct netxen_adapter *adapter,
  72		work_func_t func, int delay);
  73static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
  74static int netxen_nic_poll(struct napi_struct *napi, int budget);
  75#ifdef CONFIG_NET_POLL_CONTROLLER
  76static void netxen_nic_poll_controller(struct net_device *netdev);
  77#endif
  78
  79static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
  80static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
  81static void netxen_create_diag_entries(struct netxen_adapter *adapter);
  82static void netxen_remove_diag_entries(struct netxen_adapter *adapter);
  83static int nx_dev_request_aer(struct netxen_adapter *adapter);
  84static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
  85static int netxen_can_start_firmware(struct netxen_adapter *adapter);
  86
  87static irqreturn_t netxen_intr(int irq, void *data);
  88static irqreturn_t netxen_msi_intr(int irq, void *data);
  89static irqreturn_t netxen_msix_intr(int irq, void *data);
  90
  91static void netxen_free_ip_list(struct netxen_adapter *, bool);
  92static void netxen_restore_indev_addr(struct net_device *dev, unsigned long);
  93static void netxen_nic_get_stats(struct net_device *dev,
  94				 struct rtnl_link_stats64 *stats);
  95static int netxen_nic_set_mac(struct net_device *netdev, void *p);
  96
  97/*  PCI Device ID Table  */
  98#define ENTRY(device) \
  99	{PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
 100	.class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
 101
 102static const struct pci_device_id netxen_pci_tbl[] = {
 103	ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
 104	ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
 105	ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
 106	ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
 107	ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
 108	ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
 109	ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
 110	ENTRY(PCI_DEVICE_ID_NX3031),
 111	{0,}
 112};
 113
 114MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
 115
 116static uint32_t crb_cmd_producer[4] = {
 117	CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
 118	CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
 119};
 120
 121void
 122netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
 123		struct nx_host_tx_ring *tx_ring)
 124{
 125	NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
 126}
 127
 128static uint32_t crb_cmd_consumer[4] = {
 129	CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
 130	CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
 131};
 132
 133static inline void
 134netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
 135		struct nx_host_tx_ring *tx_ring)
 136{
 137	NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
 138}
 139
 140static uint32_t msi_tgt_status[8] = {
 141	ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
 142	ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
 143	ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
 144	ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
 145};
 146
 147static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
 148
 149static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
 150{
 151	struct netxen_adapter *adapter = sds_ring->adapter;
 152
 153	NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
 154}
 155
 156static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
 157{
 158	struct netxen_adapter *adapter = sds_ring->adapter;
 159
 160	NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
 161
 162	if (!NETXEN_IS_MSI_FAMILY(adapter))
 163		NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
 164}
 165
 166static int
 167netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
 168{
 169	int size = sizeof(struct nx_host_sds_ring) * count;
 170
 171	recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
 172
 173	return recv_ctx->sds_rings == NULL;
 174}
 175
 176static void
 177netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
 178{
 179	kfree(recv_ctx->sds_rings);
 180	recv_ctx->sds_rings = NULL;
 181}
 182
 183static int
 184netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
 185{
 186	int ring;
 187	struct nx_host_sds_ring *sds_ring;
 188	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 189
 190	if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
 191		return -ENOMEM;
 192
 193	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 194		sds_ring = &recv_ctx->sds_rings[ring];
 195		netif_napi_add(netdev, &sds_ring->napi,
 196				netxen_nic_poll, NAPI_POLL_WEIGHT);
 197	}
 198
 199	return 0;
 200}
 201
 202static void
 203netxen_napi_del(struct netxen_adapter *adapter)
 204{
 205	int ring;
 206	struct nx_host_sds_ring *sds_ring;
 207	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 208
 209	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 210		sds_ring = &recv_ctx->sds_rings[ring];
 211		netif_napi_del(&sds_ring->napi);
 212	}
 213
 214	netxen_free_sds_rings(&adapter->recv_ctx);
 215}
 216
 217static void
 218netxen_napi_enable(struct netxen_adapter *adapter)
 219{
 220	int ring;
 221	struct nx_host_sds_ring *sds_ring;
 222	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 223
 224	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 225		sds_ring = &recv_ctx->sds_rings[ring];
 226		napi_enable(&sds_ring->napi);
 227		netxen_nic_enable_int(sds_ring);
 228	}
 229}
 230
 231static void
 232netxen_napi_disable(struct netxen_adapter *adapter)
 233{
 234	int ring;
 235	struct nx_host_sds_ring *sds_ring;
 236	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 237
 238	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 239		sds_ring = &recv_ctx->sds_rings[ring];
 240		netxen_nic_disable_int(sds_ring);
 241		napi_synchronize(&sds_ring->napi);
 242		napi_disable(&sds_ring->napi);
 243	}
 244}
 245
 246static int nx_set_dma_mask(struct netxen_adapter *adapter)
 247{
 248	struct pci_dev *pdev = adapter->pdev;
 249	uint64_t mask, cmask;
 250
 251	adapter->pci_using_dac = 0;
 252
 253	mask = DMA_BIT_MASK(32);
 254	cmask = DMA_BIT_MASK(32);
 255
 256	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
 257#ifndef CONFIG_IA64
 258		mask = DMA_BIT_MASK(35);
 259#endif
 260	} else {
 261		mask = DMA_BIT_MASK(39);
 262		cmask = mask;
 263	}
 264
 265	if (pci_set_dma_mask(pdev, mask) == 0 &&
 266		pci_set_consistent_dma_mask(pdev, cmask) == 0) {
 267		adapter->pci_using_dac = 1;
 268		return 0;
 269	}
 270
 271	return -EIO;
 272}
 273
 274/* Update addressable range if firmware supports it */
 275static int
 276nx_update_dma_mask(struct netxen_adapter *adapter)
 277{
 278	int change, shift, err;
 279	uint64_t mask, old_mask, old_cmask;
 280	struct pci_dev *pdev = adapter->pdev;
 281
 282	change = 0;
 283
 284	shift = NXRD32(adapter, CRB_DMA_SHIFT);
 285	if (shift > 32)
 286		return 0;
 287
 288	if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
 289		change = 1;
 290	else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
 291		change = 1;
 292
 293	if (change) {
 294		old_mask = pdev->dma_mask;
 295		old_cmask = pdev->dev.coherent_dma_mask;
 296
 297		mask = DMA_BIT_MASK(32+shift);
 298
 299		err = pci_set_dma_mask(pdev, mask);
 300		if (err)
 301			goto err_out;
 302
 303		if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 304
 305			err = pci_set_consistent_dma_mask(pdev, mask);
 306			if (err)
 307				goto err_out;
 308		}
 309		dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
 310	}
 311
 312	return 0;
 313
 314err_out:
 315	pci_set_dma_mask(pdev, old_mask);
 316	pci_set_consistent_dma_mask(pdev, old_cmask);
 317	return err;
 318}
 319
 320static int
 321netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
 322{
 323	u32 val, timeout;
 324
 325	if (first_boot == 0x55555555) {
 326		/* This is the first boot after power up */
 327		NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
 328
 329		if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
 330			return 0;
 331
 332		/* PCI bus master workaround */
 333		first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
 334		if (!(first_boot & 0x4)) {
 335			first_boot |= 0x4;
 336			NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
 337			NXRD32(adapter, NETXEN_PCIE_REG(0x4));
 338		}
 339
 340		/* This is the first boot after power up */
 341		first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
 342		if (first_boot != 0x80000f) {
 343			/* clear the register for future unloads/loads */
 344			NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
 345			return -EIO;
 346		}
 347
 348		/* Start P2 boot loader */
 349		val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
 350		NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
 351		timeout = 0;
 352		do {
 353			msleep(1);
 354			val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
 355
 356			if (++timeout > 5000)
 357				return -EIO;
 358
 359		} while (val == NETXEN_BDINFO_MAGIC);
 360	}
 361	return 0;
 362}
 363
 364static void netxen_set_port_mode(struct netxen_adapter *adapter)
 365{
 366	u32 val, data;
 367
 368	val = adapter->ahw.board_type;
 369	if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
 370		(val == NETXEN_BRDTYPE_P3_XG_LOM)) {
 371		if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
 372			data = NETXEN_PORT_MODE_802_3_AP;
 373			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 374		} else if (port_mode == NETXEN_PORT_MODE_XG) {
 375			data = NETXEN_PORT_MODE_XG;
 376			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 377		} else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
 378			data = NETXEN_PORT_MODE_AUTO_NEG_1G;
 379			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 380		} else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
 381			data = NETXEN_PORT_MODE_AUTO_NEG_XG;
 382			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 383		} else {
 384			data = NETXEN_PORT_MODE_AUTO_NEG;
 385			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 386		}
 387
 388		if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
 389			(wol_port_mode != NETXEN_PORT_MODE_XG) &&
 390			(wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
 391			(wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
 392			wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
 393		}
 394		NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
 395	}
 396}
 397
 398#define PCI_CAP_ID_GEN  0x10
 399
 400static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
 401{
 402	u32 pdevfuncsave;
 403	u32 c8c9value = 0;
 404	u32 chicken = 0;
 405	u32 control = 0;
 406	int i, pos;
 407	struct pci_dev *pdev;
 408
 409	pdev = adapter->pdev;
 410
 411	chicken = NXRD32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3));
 412	/* clear chicken3.25:24 */
 413	chicken &= 0xFCFFFFFF;
 414	/*
 415	 * if gen1 and B0, set F1020 - if gen 2, do nothing
 416	 * if gen2 set to F1000
 417	 */
 418	pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
 419	if (pos == 0xC0) {
 420		pci_read_config_dword(pdev, pos + 0x10, &control);
 421		if ((control & 0x000F0000) != 0x00020000) {
 422			/*  set chicken3.24 if gen1 */
 423			chicken |= 0x01000000;
 424		}
 425		dev_info(&adapter->pdev->dev, "Gen2 strapping detected\n");
 426		c8c9value = 0xF1000;
 427	} else {
 428		/* set chicken3.24 if gen1 */
 429		chicken |= 0x01000000;
 430		dev_info(&adapter->pdev->dev, "Gen1 strapping detected\n");
 431		if (adapter->ahw.revision_id == NX_P3_B0)
 432			c8c9value = 0xF1020;
 433		else
 434			c8c9value = 0;
 435	}
 436
 437	NXWR32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3), chicken);
 438
 439	if (!c8c9value)
 440		return;
 441
 442	pdevfuncsave = pdev->devfn;
 443	if (pdevfuncsave & 0x07)
 444		return;
 445
 446	for (i = 0; i < 8; i++) {
 447		pci_read_config_dword(pdev, pos + 8, &control);
 448		pci_read_config_dword(pdev, pos + 8, &control);
 449		pci_write_config_dword(pdev, pos + 8, c8c9value);
 450		pdev->devfn++;
 451	}
 452	pdev->devfn = pdevfuncsave;
 453}
 454
 455static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
 456{
 457	u32 control;
 458
 459	if (pdev->msix_cap) {
 460		pci_read_config_dword(pdev, pdev->msix_cap, &control);
 461		if (enable)
 462			control |= PCI_MSIX_FLAGS_ENABLE;
 463		else
 464			control = 0;
 465		pci_write_config_dword(pdev, pdev->msix_cap, control);
 466	}
 467}
 468
 469static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
 470{
 471	int i;
 472
 473	for (i = 0; i < count; i++)
 474		adapter->msix_entries[i].entry = i;
 475}
 476
 477static int
 478netxen_read_mac_addr(struct netxen_adapter *adapter)
 479{
 480	int i;
 481	unsigned char *p;
 482	u64 mac_addr;
 483	struct net_device *netdev = adapter->netdev;
 484	struct pci_dev *pdev = adapter->pdev;
 
 485
 486	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 487		if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
 488			return -EIO;
 489	} else {
 490		if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
 491			return -EIO;
 492	}
 493
 494	p = (unsigned char *)&mac_addr;
 495	for (i = 0; i < 6; i++)
 496		netdev->dev_addr[i] = *(p + 5 - i);
 
 497
 498	memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
 499
 500	/* set station address */
 501
 502	if (!is_valid_ether_addr(netdev->dev_addr))
 503		dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
 504
 505	return 0;
 506}
 507
 508static int netxen_nic_set_mac(struct net_device *netdev, void *p)
 509{
 510	struct netxen_adapter *adapter = netdev_priv(netdev);
 511	struct sockaddr *addr = p;
 512
 513	if (!is_valid_ether_addr(addr->sa_data))
 514		return -EADDRNOTAVAIL;
 515
 516	if (netif_running(netdev)) {
 517		netif_device_detach(netdev);
 518		netxen_napi_disable(adapter);
 519	}
 520
 521	memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
 522	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
 523	adapter->macaddr_set(adapter, addr->sa_data);
 524
 525	if (netif_running(netdev)) {
 526		netif_device_attach(netdev);
 527		netxen_napi_enable(adapter);
 528	}
 529	return 0;
 530}
 531
 532static void netxen_set_multicast_list(struct net_device *dev)
 533{
 534	struct netxen_adapter *adapter = netdev_priv(dev);
 535
 536	adapter->set_multi(dev);
 537}
 538
 539static netdev_features_t netxen_fix_features(struct net_device *dev,
 540	netdev_features_t features)
 541{
 542	if (!(features & NETIF_F_RXCSUM)) {
 543		netdev_info(dev, "disabling LRO as RXCSUM is off\n");
 544
 545		features &= ~NETIF_F_LRO;
 546	}
 547
 548	return features;
 549}
 550
 551static int netxen_set_features(struct net_device *dev,
 552	netdev_features_t features)
 553{
 554	struct netxen_adapter *adapter = netdev_priv(dev);
 555	int hw_lro;
 556
 557	if (!((dev->features ^ features) & NETIF_F_LRO))
 558		return 0;
 559
 560	hw_lro = (features & NETIF_F_LRO) ? NETXEN_NIC_LRO_ENABLED
 561	         : NETXEN_NIC_LRO_DISABLED;
 562
 563	if (netxen_config_hw_lro(adapter, hw_lro))
 564		return -EIO;
 565
 566	if (!(features & NETIF_F_LRO) && netxen_send_lro_cleanup(adapter))
 567		return -EIO;
 568
 569	return 0;
 570}
 571
 572static const struct net_device_ops netxen_netdev_ops = {
 573	.ndo_open	   = netxen_nic_open,
 574	.ndo_stop	   = netxen_nic_close,
 575	.ndo_start_xmit    = netxen_nic_xmit_frame,
 576	.ndo_get_stats64   = netxen_nic_get_stats,
 577	.ndo_validate_addr = eth_validate_addr,
 578	.ndo_set_rx_mode   = netxen_set_multicast_list,
 579	.ndo_set_mac_address    = netxen_nic_set_mac,
 580	.ndo_change_mtu	   = netxen_nic_change_mtu,
 581	.ndo_tx_timeout	   = netxen_tx_timeout,
 582	.ndo_fix_features = netxen_fix_features,
 583	.ndo_set_features = netxen_set_features,
 584#ifdef CONFIG_NET_POLL_CONTROLLER
 585	.ndo_poll_controller = netxen_nic_poll_controller,
 586#endif
 587};
 588
 589static inline bool netxen_function_zero(struct pci_dev *pdev)
 590{
 591	return (PCI_FUNC(pdev->devfn) == 0) ? true : false;
 592}
 593
 594static inline void netxen_set_interrupt_mode(struct netxen_adapter *adapter,
 595					     u32 mode)
 596{
 597	NXWR32(adapter, NETXEN_INTR_MODE_REG, mode);
 598}
 599
 600static inline u32 netxen_get_interrupt_mode(struct netxen_adapter *adapter)
 601{
 602	return NXRD32(adapter, NETXEN_INTR_MODE_REG);
 603}
 604
 605static void
 606netxen_initialize_interrupt_registers(struct netxen_adapter *adapter)
 607{
 608	struct netxen_legacy_intr_set *legacy_intrp;
 609	u32 tgt_status_reg, int_state_reg;
 610
 611	if (adapter->ahw.revision_id >= NX_P3_B0)
 612		legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
 613	else
 614		legacy_intrp = &legacy_intr[0];
 615
 616	tgt_status_reg = legacy_intrp->tgt_status_reg;
 617	int_state_reg = ISR_INT_STATE_REG;
 618
 619	adapter->int_vec_bit = legacy_intrp->int_vec_bit;
 620	adapter->tgt_status_reg = netxen_get_ioaddr(adapter, tgt_status_reg);
 621	adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
 622						  legacy_intrp->tgt_mask_reg);
 623	adapter->pci_int_reg = netxen_get_ioaddr(adapter,
 624						 legacy_intrp->pci_int_reg);
 625	adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
 626
 627	if (adapter->ahw.revision_id >= NX_P3_B1)
 628		adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
 629							       int_state_reg);
 630	else
 631		adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
 632							       CRB_INT_VECTOR);
 633}
 634
 635static int netxen_setup_msi_interrupts(struct netxen_adapter *adapter,
 636				       int num_msix)
 637{
 638	struct pci_dev *pdev = adapter->pdev;
 639	u32 value;
 640	int err;
 641
 642	if (adapter->msix_supported) {
 643		netxen_init_msix_entries(adapter, num_msix);
 644		err = pci_enable_msix_range(pdev, adapter->msix_entries,
 645					    num_msix, num_msix);
 646		if (err > 0) {
 647			adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
 648			netxen_set_msix_bit(pdev, 1);
 649
 650			if (adapter->rss_supported)
 651				adapter->max_sds_rings = num_msix;
 652
 653			dev_info(&pdev->dev, "using msi-x interrupts\n");
 654			return 0;
 655		}
 656		/* fall through for msi */
 657	}
 658
 659	if (use_msi && !pci_enable_msi(pdev)) {
 660		value = msi_tgt_status[adapter->ahw.pci_func];
 661		adapter->flags |= NETXEN_NIC_MSI_ENABLED;
 662		adapter->tgt_status_reg = netxen_get_ioaddr(adapter, value);
 663		adapter->msix_entries[0].vector = pdev->irq;
 664		dev_info(&pdev->dev, "using msi interrupts\n");
 665		return 0;
 666	}
 667
 668	dev_err(&pdev->dev, "Failed to acquire MSI-X/MSI interrupt vector\n");
 669	return -EIO;
 670}
 671
 672static int netxen_setup_intr(struct netxen_adapter *adapter)
 673{
 674	struct pci_dev *pdev = adapter->pdev;
 675	int num_msix;
 676
 677	if (adapter->rss_supported)
 678		num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
 679			    MSIX_ENTRIES_PER_ADAPTER : 2;
 680	else
 681		num_msix = 1;
 682
 683	adapter->max_sds_rings = 1;
 684	adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
 685
 686	netxen_initialize_interrupt_registers(adapter);
 687	netxen_set_msix_bit(pdev, 0);
 688
 689	if (netxen_function_zero(pdev)) {
 690		if (!netxen_setup_msi_interrupts(adapter, num_msix))
 691			netxen_set_interrupt_mode(adapter, NETXEN_MSI_MODE);
 692		else
 693			netxen_set_interrupt_mode(adapter, NETXEN_INTX_MODE);
 694	} else {
 695		if (netxen_get_interrupt_mode(adapter) == NETXEN_MSI_MODE &&
 696		    netxen_setup_msi_interrupts(adapter, num_msix)) {
 697			dev_err(&pdev->dev, "Co-existence of MSI-X/MSI and INTx interrupts is not supported\n");
 698			return -EIO;
 699		}
 700	}
 701
 702	if (!NETXEN_IS_MSI_FAMILY(adapter)) {
 703		adapter->msix_entries[0].vector = pdev->irq;
 704		dev_info(&pdev->dev, "using legacy interrupts\n");
 705	}
 706	return 0;
 707}
 708
 709static void
 710netxen_teardown_intr(struct netxen_adapter *adapter)
 711{
 712	if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
 713		pci_disable_msix(adapter->pdev);
 714	if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
 715		pci_disable_msi(adapter->pdev);
 716}
 717
 718static void
 719netxen_cleanup_pci_map(struct netxen_adapter *adapter)
 720{
 721	if (adapter->ahw.db_base != NULL)
 722		iounmap(adapter->ahw.db_base);
 723	if (adapter->ahw.pci_base0 != NULL)
 724		iounmap(adapter->ahw.pci_base0);
 725	if (adapter->ahw.pci_base1 != NULL)
 726		iounmap(adapter->ahw.pci_base1);
 727	if (adapter->ahw.pci_base2 != NULL)
 728		iounmap(adapter->ahw.pci_base2);
 729}
 730
 731static int
 732netxen_setup_pci_map(struct netxen_adapter *adapter)
 733{
 734	void __iomem *db_ptr = NULL;
 735
 736	resource_size_t mem_base, db_base;
 737	unsigned long mem_len, db_len = 0;
 738
 739	struct pci_dev *pdev = adapter->pdev;
 740	int pci_func = adapter->ahw.pci_func;
 741	struct netxen_hardware_context *ahw = &adapter->ahw;
 742
 743	int err = 0;
 744
 745	/*
 746	 * Set the CRB window to invalid. If any register in window 0 is
 747	 * accessed it should set the window to 0 and then reset it to 1.
 748	 */
 749	adapter->ahw.crb_win = -1;
 750	adapter->ahw.ocm_win = -1;
 751
 752	/* remap phys address */
 753	mem_base = pci_resource_start(pdev, 0);	/* 0 is for BAR 0 */
 754	mem_len = pci_resource_len(pdev, 0);
 755
 756	/* 128 Meg of memory */
 757	if (mem_len == NETXEN_PCI_128MB_SIZE) {
 758
 759		ahw->pci_base0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
 760		ahw->pci_base1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
 761				SECOND_PAGE_GROUP_SIZE);
 762		ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
 763				THIRD_PAGE_GROUP_SIZE);
 764		if (ahw->pci_base0 == NULL || ahw->pci_base1 == NULL ||
 765						ahw->pci_base2 == NULL) {
 766			dev_err(&pdev->dev, "failed to map PCI bar 0\n");
 767			err = -EIO;
 768			goto err_out;
 769		}
 770
 771		ahw->pci_len0 = FIRST_PAGE_GROUP_SIZE;
 772
 773	} else if (mem_len == NETXEN_PCI_32MB_SIZE) {
 774
 775		ahw->pci_base1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
 776		ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
 777			SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
 778		if (ahw->pci_base1 == NULL || ahw->pci_base2 == NULL) {
 779			dev_err(&pdev->dev, "failed to map PCI bar 0\n");
 780			err = -EIO;
 781			goto err_out;
 782		}
 783
 784	} else if (mem_len == NETXEN_PCI_2MB_SIZE) {
 785
 786		ahw->pci_base0 = pci_ioremap_bar(pdev, 0);
 787		if (ahw->pci_base0 == NULL) {
 788			dev_err(&pdev->dev, "failed to map PCI bar 0\n");
 789			return -EIO;
 790		}
 791		ahw->pci_len0 = mem_len;
 792	} else {
 793		return -EIO;
 794	}
 795
 796	netxen_setup_hwops(adapter);
 797
 798	dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
 799
 800	if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
 801		adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
 802			NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
 803
 804	} else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 805		adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
 806			NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func)));
 807	}
 808
 809	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
 810		goto skip_doorbell;
 811
 812	db_base = pci_resource_start(pdev, 4);	/* doorbell is on bar 4 */
 813	db_len = pci_resource_len(pdev, 4);
 814
 815	if (db_len == 0) {
 816		printk(KERN_ERR "%s: doorbell is disabled\n",
 817				netxen_nic_driver_name);
 818		err = -EIO;
 819		goto err_out;
 820	}
 821
 822	db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
 823	if (!db_ptr) {
 824		printk(KERN_ERR "%s: Failed to allocate doorbell map.",
 825				netxen_nic_driver_name);
 826		err = -EIO;
 827		goto err_out;
 828	}
 829
 830skip_doorbell:
 831	adapter->ahw.db_base = db_ptr;
 832	adapter->ahw.db_len = db_len;
 833	return 0;
 834
 835err_out:
 836	netxen_cleanup_pci_map(adapter);
 837	return err;
 838}
 839
 840static void
 841netxen_check_options(struct netxen_adapter *adapter)
 842{
 843	u32 fw_major, fw_minor, fw_build, prev_fw_version;
 844	char brd_name[NETXEN_MAX_SHORT_NAME];
 845	char serial_num[32];
 846	int i, offset, val, err;
 847	__le32 *ptr32;
 848	struct pci_dev *pdev = adapter->pdev;
 849
 850	adapter->driver_mismatch = 0;
 851
 852	ptr32 = (__le32 *)&serial_num;
 853	offset = NX_FW_SERIAL_NUM_OFFSET;
 854	for (i = 0; i < 8; i++) {
 855		err = netxen_rom_fast_read(adapter, offset, &val);
 856		if (err) {
 857			dev_err(&pdev->dev, "error reading board info\n");
 858			adapter->driver_mismatch = 1;
 859			return;
 860		}
 861		ptr32[i] = cpu_to_le32(val);
 862		offset += sizeof(u32);
 863	}
 864
 865	fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
 866	fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
 867	fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
 868	prev_fw_version = adapter->fw_version;
 869	adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
 870
 871	/* Get FW Mini Coredump template and store it */
 872	 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 873		if (adapter->mdump.md_template == NULL ||
 874				adapter->fw_version > prev_fw_version) {
 875			kfree(adapter->mdump.md_template);
 876			adapter->mdump.md_template = NULL;
 877			err = netxen_setup_minidump(adapter);
 878			if (err)
 879				dev_err(&adapter->pdev->dev,
 880				"Failed to setup minidump rcode = %d\n", err);
 881		}
 882	}
 883
 884	if (adapter->portnum == 0) {
 885		if (netxen_nic_get_brd_name_by_type(adapter->ahw.board_type,
 886						    brd_name))
 887			strcpy(serial_num, "Unknown");
 888
 889		pr_info("%s: %s Board S/N %s  Chip rev 0x%x\n",
 890				module_name(THIS_MODULE),
 891				brd_name, serial_num, adapter->ahw.revision_id);
 892	}
 893
 894	if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
 895		adapter->driver_mismatch = 1;
 896		dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
 897				fw_major, fw_minor, fw_build);
 898		return;
 899	}
 900
 901	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 902		i = NXRD32(adapter, NETXEN_SRE_MISC);
 903		adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
 904	}
 905
 906	dev_info(&pdev->dev, "Driver v%s, firmware v%d.%d.%d [%s]\n",
 907		 NETXEN_NIC_LINUX_VERSIONID, fw_major, fw_minor, fw_build,
 908		 adapter->ahw.cut_through ? "cut-through" : "legacy");
 909
 910	if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
 911		adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
 912
 913	if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
 914		adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
 915		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
 916	} else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
 917		adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
 918		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
 919	}
 920
 921	adapter->msix_supported = 0;
 922	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 923		adapter->msix_supported = !!use_msi_x;
 924		adapter->rss_supported = !!use_msi_x;
 925	} else {
 926		u32 flashed_ver = 0;
 927		netxen_rom_fast_read(adapter,
 928				NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
 929		flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
 930
 931		if (flashed_ver >= NETXEN_VERSION_CODE(3, 4, 336)) {
 932			switch (adapter->ahw.board_type) {
 933			case NETXEN_BRDTYPE_P2_SB31_10G:
 934			case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
 935				adapter->msix_supported = !!use_msi_x;
 936				adapter->rss_supported = !!use_msi_x;
 937				break;
 938			default:
 939				break;
 940			}
 941		}
 942	}
 943
 944	adapter->num_txd = MAX_CMD_DESCRIPTORS;
 945
 946	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
 947		adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
 948		adapter->max_rds_rings = 3;
 949	} else {
 950		adapter->num_lro_rxd = 0;
 951		adapter->max_rds_rings = 2;
 952	}
 953}
 954
 955static int
 956netxen_start_firmware(struct netxen_adapter *adapter)
 957{
 958	int val, err, first_boot;
 959	struct pci_dev *pdev = adapter->pdev;
 960
 961	/* required for NX2031 dummy dma */
 962	err = nx_set_dma_mask(adapter);
 963	if (err)
 964		return err;
 965
 966	err = netxen_can_start_firmware(adapter);
 967
 968	if (err < 0)
 969		return err;
 970
 971	if (!err)
 972		goto wait_init;
 973
 974	first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
 975
 976	err = netxen_check_hw_init(adapter, first_boot);
 977	if (err) {
 978		dev_err(&pdev->dev, "error in init HW init sequence\n");
 979		return err;
 980	}
 981
 982	netxen_request_firmware(adapter);
 983
 984	err = netxen_need_fw_reset(adapter);
 985	if (err < 0)
 986		goto err_out;
 987	if (err == 0)
 988		goto pcie_strap_init;
 989
 990	if (first_boot != 0x55555555) {
 991		NXWR32(adapter, CRB_CMDPEG_STATE, 0);
 992		netxen_pinit_from_rom(adapter);
 993		msleep(1);
 994	}
 995
 996	NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
 997	NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
 998	NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
 999
1000	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1001		netxen_set_port_mode(adapter);
1002
1003	err = netxen_load_firmware(adapter);
1004	if (err)
1005		goto err_out;
1006
1007	netxen_release_firmware(adapter);
1008
1009	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1010
1011		/* Initialize multicast addr pool owners */
1012		val = 0x7654;
1013		if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
1014			val |= 0x0f000000;
1015		NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
1016
1017	}
1018
1019	err = netxen_init_dummy_dma(adapter);
1020	if (err)
1021		goto err_out;
1022
1023	/*
1024	 * Tell the hardware our version number.
1025	 */
1026	val = (_NETXEN_NIC_LINUX_MAJOR << 16)
1027		| ((_NETXEN_NIC_LINUX_MINOR << 8))
1028		| (_NETXEN_NIC_LINUX_SUBVERSION);
1029	NXWR32(adapter, CRB_DRIVER_VERSION, val);
1030
1031pcie_strap_init:
1032	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1033		netxen_pcie_strap_init(adapter);
1034
1035wait_init:
1036	/* Handshake with the card before we register the devices. */
1037	err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
1038	if (err) {
1039		netxen_free_dummy_dma(adapter);
1040		goto err_out;
1041	}
1042
1043	NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
1044
1045	nx_update_dma_mask(adapter);
1046
1047	netxen_check_options(adapter);
1048
1049	adapter->need_fw_reset = 0;
1050
1051	/* fall through and release firmware */
1052
1053err_out:
1054	netxen_release_firmware(adapter);
1055	return err;
1056}
1057
1058static int
1059netxen_nic_request_irq(struct netxen_adapter *adapter)
1060{
1061	irq_handler_t handler;
1062	struct nx_host_sds_ring *sds_ring;
1063	int err, ring;
1064
1065	unsigned long flags = 0;
1066	struct net_device *netdev = adapter->netdev;
1067	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1068
1069	if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1070		handler = netxen_msix_intr;
1071	else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
1072		handler = netxen_msi_intr;
1073	else {
1074		flags |= IRQF_SHARED;
1075		handler = netxen_intr;
1076	}
1077	adapter->irq = netdev->irq;
1078
1079	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1080		sds_ring = &recv_ctx->sds_rings[ring];
1081		sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1082		err = request_irq(sds_ring->irq, handler,
1083				  flags, sds_ring->name, sds_ring);
1084		if (err)
1085			return err;
1086	}
1087
1088	return 0;
1089}
1090
1091static void
1092netxen_nic_free_irq(struct netxen_adapter *adapter)
1093{
1094	int ring;
1095	struct nx_host_sds_ring *sds_ring;
1096
1097	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1098
1099	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1100		sds_ring = &recv_ctx->sds_rings[ring];
1101		free_irq(sds_ring->irq, sds_ring);
1102	}
1103}
1104
1105static void
1106netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
1107{
1108	adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
1109	adapter->coal.normal.data.rx_time_us =
1110		NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
1111	adapter->coal.normal.data.rx_packets =
1112		NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
1113	adapter->coal.normal.data.tx_time_us =
1114		NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
1115	adapter->coal.normal.data.tx_packets =
1116		NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
1117}
1118
1119/* with rtnl_lock */
1120static int
1121__netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1122{
1123	int err;
1124
1125	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1126		return -EIO;
1127
1128	err = adapter->init_port(adapter, adapter->physical_port);
1129	if (err) {
1130		printk(KERN_ERR "%s: Failed to initialize port %d\n",
1131				netxen_nic_driver_name, adapter->portnum);
1132		return err;
1133	}
1134	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
1135		adapter->macaddr_set(adapter, adapter->mac_addr);
1136
1137	adapter->set_multi(netdev);
1138	adapter->set_mtu(adapter, netdev->mtu);
1139
1140	adapter->ahw.linkup = 0;
1141
1142	if (adapter->max_sds_rings > 1)
1143		netxen_config_rss(adapter, 1);
1144
1145	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1146		netxen_config_intr_coalesce(adapter);
1147
1148	if (netdev->features & NETIF_F_LRO)
1149		netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
1150
1151	netxen_napi_enable(adapter);
1152
1153	if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1154		netxen_linkevent_request(adapter, 1);
1155	else
1156		netxen_nic_set_link_parameters(adapter);
1157
1158	set_bit(__NX_DEV_UP, &adapter->state);
1159	return 0;
1160}
1161
1162/* Usage: During resume and firmware recovery module.*/
1163
1164static inline int
1165netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1166{
1167	int err = 0;
1168
1169	rtnl_lock();
1170	if (netif_running(netdev))
1171		err = __netxen_nic_up(adapter, netdev);
1172	rtnl_unlock();
1173
1174	return err;
1175}
1176
1177/* with rtnl_lock */
1178static void
1179__netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1180{
1181	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1182		return;
1183
1184	if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state))
1185		return;
1186
1187	smp_mb();
1188	netif_carrier_off(netdev);
1189	netif_tx_disable(netdev);
1190
1191	if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1192		netxen_linkevent_request(adapter, 0);
1193
1194	if (adapter->stop_port)
1195		adapter->stop_port(adapter);
1196
1197	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1198		netxen_p3_free_mac_list(adapter);
1199
1200	adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1201
1202	netxen_napi_disable(adapter);
1203
1204	netxen_release_tx_buffers(adapter);
1205}
1206
1207/* Usage: During suspend and firmware recovery module */
1208
1209static inline void
1210netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1211{
1212	rtnl_lock();
1213	if (netif_running(netdev))
1214		__netxen_nic_down(adapter, netdev);
1215	rtnl_unlock();
1216
1217}
1218
1219static int
1220netxen_nic_attach(struct netxen_adapter *adapter)
1221{
1222	struct net_device *netdev = adapter->netdev;
1223	struct pci_dev *pdev = adapter->pdev;
1224	int err, ring;
1225	struct nx_host_rds_ring *rds_ring;
1226	struct nx_host_tx_ring *tx_ring;
1227	u32 capab2;
1228
1229	if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1230		return 0;
1231
1232	err = netxen_init_firmware(adapter);
1233	if (err)
1234		return err;
1235
1236	adapter->flags &= ~NETXEN_FW_MSS_CAP;
1237	if (adapter->capabilities & NX_FW_CAPABILITY_MORE_CAPS) {
1238		capab2 = NXRD32(adapter, CRB_FW_CAPABILITIES_2);
1239		if (capab2 & NX_FW_CAPABILITY_2_LRO_MAX_TCP_SEG)
1240			adapter->flags |= NETXEN_FW_MSS_CAP;
1241	}
1242
1243	err = netxen_napi_add(adapter, netdev);
1244	if (err)
1245		return err;
1246
1247	err = netxen_alloc_sw_resources(adapter);
1248	if (err) {
1249		printk(KERN_ERR "%s: Error in setting sw resources\n",
1250				netdev->name);
1251		return err;
1252	}
1253
1254	err = netxen_alloc_hw_resources(adapter);
1255	if (err) {
1256		printk(KERN_ERR "%s: Error in setting hw resources\n",
1257				netdev->name);
1258		goto err_out_free_sw;
1259	}
1260
1261	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1262		tx_ring = adapter->tx_ring;
1263		tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1264				crb_cmd_producer[adapter->portnum]);
1265		tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1266				crb_cmd_consumer[adapter->portnum]);
1267
1268		tx_ring->producer = 0;
1269		tx_ring->sw_consumer = 0;
1270
1271		netxen_nic_update_cmd_producer(adapter, tx_ring);
1272		netxen_nic_update_cmd_consumer(adapter, tx_ring);
1273	}
1274
1275	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1276		rds_ring = &adapter->recv_ctx.rds_rings[ring];
1277		netxen_post_rx_buffers(adapter, ring, rds_ring);
1278	}
1279
1280	err = netxen_nic_request_irq(adapter);
1281	if (err) {
1282		dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1283				netdev->name);
1284		goto err_out_free_rxbuf;
1285	}
1286
1287	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1288		netxen_nic_init_coalesce_defaults(adapter);
1289
1290	netxen_create_sysfs_entries(adapter);
1291
1292	adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1293	return 0;
1294
1295err_out_free_rxbuf:
1296	netxen_release_rx_buffers(adapter);
1297	netxen_free_hw_resources(adapter);
1298err_out_free_sw:
1299	netxen_free_sw_resources(adapter);
1300	return err;
1301}
1302
1303static void
1304netxen_nic_detach(struct netxen_adapter *adapter)
1305{
1306	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1307		return;
1308
1309	netxen_remove_sysfs_entries(adapter);
1310
1311	netxen_free_hw_resources(adapter);
1312	netxen_release_rx_buffers(adapter);
1313	netxen_nic_free_irq(adapter);
1314	netxen_napi_del(adapter);
1315	netxen_free_sw_resources(adapter);
1316
1317	adapter->is_up = 0;
1318}
1319
1320int
1321netxen_nic_reset_context(struct netxen_adapter *adapter)
1322{
1323	int err = 0;
1324	struct net_device *netdev = adapter->netdev;
1325
1326	if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1327		return -EBUSY;
1328
1329	if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1330
1331		netif_device_detach(netdev);
1332
1333		if (netif_running(netdev))
1334			__netxen_nic_down(adapter, netdev);
1335
1336		netxen_nic_detach(adapter);
1337
1338		if (netif_running(netdev)) {
1339			err = netxen_nic_attach(adapter);
1340			if (!err)
1341				err = __netxen_nic_up(adapter, netdev);
1342
1343			if (err)
1344				goto done;
1345		}
1346
1347		netif_device_attach(netdev);
1348	}
1349
1350done:
1351	clear_bit(__NX_RESETTING, &adapter->state);
1352	return err;
1353}
1354
1355static int
1356netxen_setup_netdev(struct netxen_adapter *adapter,
1357		struct net_device *netdev)
1358{
1359	int err = 0;
1360	struct pci_dev *pdev = adapter->pdev;
1361
1362	adapter->mc_enabled = 0;
1363	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1364		adapter->max_mc_count = 38;
1365	else
1366		adapter->max_mc_count = 16;
1367
1368	netdev->netdev_ops	   = &netxen_netdev_ops;
1369	netdev->watchdog_timeo     = 5*HZ;
1370
1371	netxen_nic_change_mtu(netdev, netdev->mtu);
1372
1373	netdev->ethtool_ops = &netxen_nic_ethtool_ops;
1374
1375	netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
1376	                      NETIF_F_RXCSUM;
1377
1378	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1379		netdev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
1380
1381	netdev->vlan_features |= netdev->hw_features;
1382
1383	if (adapter->pci_using_dac) {
1384		netdev->features |= NETIF_F_HIGHDMA;
1385		netdev->vlan_features |= NETIF_F_HIGHDMA;
1386	}
1387
1388	if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1389		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
1390
1391	if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1392		netdev->hw_features |= NETIF_F_LRO;
1393
1394	netdev->features |= netdev->hw_features;
1395
1396	netdev->irq = adapter->msix_entries[0].vector;
1397
1398	INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1399
1400	if (netxen_read_mac_addr(adapter))
1401		dev_warn(&pdev->dev, "failed to read mac addr\n");
1402
1403	netif_carrier_off(netdev);
1404
1405	err = register_netdev(netdev);
1406	if (err) {
1407		dev_err(&pdev->dev, "failed to register net device\n");
1408		return err;
1409	}
1410
1411	return 0;
1412}
1413
1414#define NETXEN_ULA_ADAPTER_KEY		(0xdaddad01)
1415#define NETXEN_NON_ULA_ADAPTER_KEY	(0xdaddad00)
1416
1417static void netxen_read_ula_info(struct netxen_adapter *adapter)
1418{
1419	u32 temp;
1420
1421	/* Print ULA info only once for an adapter */
1422	if (adapter->portnum != 0)
1423		return;
1424
1425	temp = NXRD32(adapter, NETXEN_ULA_KEY);
1426	switch (temp) {
1427	case NETXEN_ULA_ADAPTER_KEY:
1428		dev_info(&adapter->pdev->dev, "ULA adapter");
1429		break;
1430	case NETXEN_NON_ULA_ADAPTER_KEY:
1431		dev_info(&adapter->pdev->dev, "non ULA adapter");
1432		break;
1433	default:
1434		break;
1435	}
1436
1437	return;
1438}
1439
1440#ifdef CONFIG_PCIEAER
1441static void netxen_mask_aer_correctable(struct netxen_adapter *adapter)
1442{
1443	struct pci_dev *pdev = adapter->pdev;
1444	struct pci_dev *root = pdev->bus->self;
1445	u32 aer_pos;
1446
1447	/* root bus? */
1448	if (!root)
1449		return;
1450
1451	if (adapter->ahw.board_type != NETXEN_BRDTYPE_P3_4_GB_MM &&
1452		adapter->ahw.board_type != NETXEN_BRDTYPE_P3_10G_TP)
1453		return;
1454
1455	if (pci_pcie_type(root) != PCI_EXP_TYPE_ROOT_PORT)
1456		return;
1457
1458	aer_pos = pci_find_ext_capability(root, PCI_EXT_CAP_ID_ERR);
1459	if (!aer_pos)
1460		return;
1461
1462	pci_write_config_dword(root, aer_pos + PCI_ERR_COR_MASK, 0xffff);
1463}
1464#endif
1465
1466static int
1467netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1468{
1469	struct net_device *netdev = NULL;
1470	struct netxen_adapter *adapter = NULL;
1471	int i = 0, err;
1472	int pci_func_id = PCI_FUNC(pdev->devfn);
1473	uint8_t revision_id;
1474	u32 val;
1475
1476	if (pdev->revision >= NX_P3_A0 && pdev->revision <= NX_P3_B1) {
1477		pr_warn("%s: chip revisions between 0x%x-0x%x will not be enabled\n",
1478			module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
1479		return -ENODEV;
1480	}
1481
1482	if ((err = pci_enable_device(pdev)))
1483		return err;
1484
1485	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1486		err = -ENODEV;
1487		goto err_out_disable_pdev;
1488	}
1489
1490	if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1491		goto err_out_disable_pdev;
1492
1493	if (NX_IS_REVISION_P3(pdev->revision))
1494		pci_enable_pcie_error_reporting(pdev);
1495
1496	pci_set_master(pdev);
1497
1498	netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1499	if(!netdev) {
1500		err = -ENOMEM;
1501		goto err_out_free_res;
1502	}
1503
1504	SET_NETDEV_DEV(netdev, &pdev->dev);
1505
1506	adapter = netdev_priv(netdev);
1507	adapter->netdev  = netdev;
1508	adapter->pdev    = pdev;
1509	adapter->ahw.pci_func  = pci_func_id;
1510
1511	revision_id = pdev->revision;
1512	adapter->ahw.revision_id = revision_id;
1513
1514	rwlock_init(&adapter->ahw.crb_lock);
1515	spin_lock_init(&adapter->ahw.mem_lock);
1516
1517	spin_lock_init(&adapter->tx_clean_lock);
1518	INIT_LIST_HEAD(&adapter->mac_list);
1519	INIT_LIST_HEAD(&adapter->ip_list);
1520
1521	err = netxen_setup_pci_map(adapter);
1522	if (err)
1523		goto err_out_free_netdev;
1524
1525	/* This will be reset for mezz cards  */
1526	adapter->portnum = pci_func_id;
1527
1528	err = netxen_nic_get_board_info(adapter);
1529	if (err) {
1530		dev_err(&pdev->dev, "Error getting board config info.\n");
1531		goto err_out_iounmap;
1532	}
1533
1534#ifdef CONFIG_PCIEAER
1535	netxen_mask_aer_correctable(adapter);
1536#endif
1537
1538	/* Mezz cards have PCI function 0,2,3 enabled */
1539	switch (adapter->ahw.board_type) {
1540	case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1541	case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1542		if (pci_func_id >= 2)
1543			adapter->portnum = pci_func_id - 2;
1544		break;
1545	default:
1546		break;
1547	}
1548
1549	err = netxen_check_flash_fw_compatibility(adapter);
1550	if (err)
1551		goto err_out_iounmap;
1552
1553	if (adapter->portnum == 0) {
1554		val = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
1555		if (val != 0xffffffff && val != 0) {
1556			NXWR32(adapter, NX_CRB_DEV_REF_COUNT, 0);
1557			adapter->need_fw_reset = 1;
1558		}
1559	}
1560
1561	err = netxen_start_firmware(adapter);
1562	if (err)
1563		goto err_out_decr_ref;
1564
1565	/*
1566	 * See if the firmware gave us a virtual-physical port mapping.
1567	 */
1568	adapter->physical_port = adapter->portnum;
1569	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1570		i = NXRD32(adapter, CRB_V2P(adapter->portnum));
1571		if (i != 0x55555555)
1572			adapter->physical_port = i;
1573	}
1574
1575	/* MTU range: 0 - 8000 (P2) or 9600 (P3) */
1576	netdev->min_mtu = 0;
1577	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1578		netdev->max_mtu = P3_MAX_MTU;
1579	else
1580		netdev->max_mtu = P2_MAX_MTU;
1581
1582	netxen_nic_clear_stats(adapter);
1583
1584	err = netxen_setup_intr(adapter);
1585
1586	if (err) {
1587		dev_err(&adapter->pdev->dev,
1588			"Failed to setup interrupts, error = %d\n", err);
1589		goto err_out_disable_msi;
1590	}
1591
1592	netxen_read_ula_info(adapter);
1593
1594	err = netxen_setup_netdev(adapter, netdev);
1595	if (err)
1596		goto err_out_disable_msi;
1597
1598	pci_set_drvdata(pdev, adapter);
1599
1600	netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1601
1602	switch (adapter->ahw.port_type) {
1603	case NETXEN_NIC_GBE:
1604		dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1605				adapter->netdev->name);
1606		break;
1607	case NETXEN_NIC_XGBE:
1608		dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1609				adapter->netdev->name);
1610		break;
1611	}
1612
1613	netxen_create_diag_entries(adapter);
1614
1615	return 0;
1616
1617err_out_disable_msi:
1618	netxen_teardown_intr(adapter);
1619
1620	netxen_free_dummy_dma(adapter);
1621
1622err_out_decr_ref:
1623	nx_decr_dev_ref_cnt(adapter);
1624
1625err_out_iounmap:
1626	netxen_cleanup_pci_map(adapter);
1627
1628err_out_free_netdev:
1629	free_netdev(netdev);
1630
1631err_out_free_res:
 
 
1632	pci_release_regions(pdev);
1633
1634err_out_disable_pdev:
1635	pci_disable_device(pdev);
1636	return err;
1637}
1638
1639static
1640void netxen_cleanup_minidump(struct netxen_adapter *adapter)
1641{
1642	kfree(adapter->mdump.md_template);
1643	adapter->mdump.md_template = NULL;
1644
1645	if (adapter->mdump.md_capture_buff) {
1646		vfree(adapter->mdump.md_capture_buff);
1647		adapter->mdump.md_capture_buff = NULL;
1648	}
1649}
1650
1651static void netxen_nic_remove(struct pci_dev *pdev)
1652{
1653	struct netxen_adapter *adapter;
1654	struct net_device *netdev;
1655
1656	adapter = pci_get_drvdata(pdev);
1657	if (adapter == NULL)
1658		return;
1659
1660	netdev = adapter->netdev;
1661
1662	netxen_cancel_fw_work(adapter);
1663
1664	unregister_netdev(netdev);
1665
1666	cancel_work_sync(&adapter->tx_timeout_task);
1667
1668	netxen_free_ip_list(adapter, false);
1669	netxen_nic_detach(adapter);
1670
1671	nx_decr_dev_ref_cnt(adapter);
1672
1673	if (adapter->portnum == 0)
1674		netxen_free_dummy_dma(adapter);
1675
1676	clear_bit(__NX_RESETTING, &adapter->state);
1677
1678	netxen_teardown_intr(adapter);
1679	netxen_set_interrupt_mode(adapter, 0);
1680	netxen_remove_diag_entries(adapter);
1681
1682	netxen_cleanup_pci_map(adapter);
1683
1684	netxen_release_firmware(adapter);
1685
1686	if (NX_IS_REVISION_P3(pdev->revision)) {
1687		netxen_cleanup_minidump(adapter);
1688		pci_disable_pcie_error_reporting(pdev);
1689	}
1690
1691	pci_release_regions(pdev);
1692	pci_disable_device(pdev);
1693
1694	free_netdev(netdev);
1695}
1696
1697static void netxen_nic_detach_func(struct netxen_adapter *adapter)
1698{
1699	struct net_device *netdev = adapter->netdev;
1700
1701	netif_device_detach(netdev);
1702
1703	netxen_cancel_fw_work(adapter);
1704
1705	if (netif_running(netdev))
1706		netxen_nic_down(adapter, netdev);
1707
1708	cancel_work_sync(&adapter->tx_timeout_task);
1709
1710	netxen_nic_detach(adapter);
1711
1712	if (adapter->portnum == 0)
1713		netxen_free_dummy_dma(adapter);
1714
1715	nx_decr_dev_ref_cnt(adapter);
1716
1717	clear_bit(__NX_RESETTING, &adapter->state);
1718}
1719
1720static int netxen_nic_attach_func(struct pci_dev *pdev)
1721{
1722	struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1723	struct net_device *netdev = adapter->netdev;
1724	int err;
1725
1726	err = pci_enable_device(pdev);
1727	if (err)
1728		return err;
1729
1730	pci_set_power_state(pdev, PCI_D0);
1731	pci_set_master(pdev);
1732	pci_restore_state(pdev);
1733
1734	adapter->ahw.crb_win = -1;
1735	adapter->ahw.ocm_win = -1;
1736
1737	err = netxen_start_firmware(adapter);
1738	if (err) {
1739		dev_err(&pdev->dev, "failed to start firmware\n");
1740		return err;
1741	}
1742
1743	if (netif_running(netdev)) {
1744		err = netxen_nic_attach(adapter);
1745		if (err)
1746			goto err_out;
1747
1748		err = netxen_nic_up(adapter, netdev);
1749		if (err)
1750			goto err_out_detach;
1751
1752		netxen_restore_indev_addr(netdev, NETDEV_UP);
1753	}
1754
1755	netif_device_attach(netdev);
1756	netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1757	return 0;
1758
1759err_out_detach:
1760	netxen_nic_detach(adapter);
1761err_out:
1762	nx_decr_dev_ref_cnt(adapter);
1763	return err;
1764}
1765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1766static pci_ers_result_t netxen_io_error_detected(struct pci_dev *pdev,
1767						pci_channel_state_t state)
1768{
1769	struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1770
1771	if (state == pci_channel_io_perm_failure)
1772		return PCI_ERS_RESULT_DISCONNECT;
1773
1774	if (nx_dev_request_aer(adapter))
1775		return PCI_ERS_RESULT_RECOVERED;
1776
1777	netxen_nic_detach_func(adapter);
1778
1779	pci_disable_device(pdev);
1780
1781	return PCI_ERS_RESULT_NEED_RESET;
1782}
1783
1784static pci_ers_result_t netxen_io_slot_reset(struct pci_dev *pdev)
1785{
1786	int err = 0;
1787
1788	err = netxen_nic_attach_func(pdev);
1789
1790	return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
1791}
1792
1793static void netxen_io_resume(struct pci_dev *pdev)
1794{
1795	pci_cleanup_aer_uncorrect_error_status(pdev);
1796}
1797
1798static void netxen_nic_shutdown(struct pci_dev *pdev)
1799{
1800	struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1801
1802	netxen_nic_detach_func(adapter);
1803
1804	if (pci_save_state(pdev))
1805		return;
1806
1807	if (netxen_nic_wol_supported(adapter)) {
1808		pci_enable_wake(pdev, PCI_D3cold, 1);
1809		pci_enable_wake(pdev, PCI_D3hot, 1);
1810	}
1811
1812	pci_disable_device(pdev);
1813}
1814
1815#ifdef CONFIG_PM
1816static int
1817netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1818{
1819	struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1820	int retval;
1821
1822	netxen_nic_detach_func(adapter);
1823
1824	retval = pci_save_state(pdev);
1825	if (retval)
1826		return retval;
1827
1828	if (netxen_nic_wol_supported(adapter)) {
1829		pci_enable_wake(pdev, PCI_D3cold, 1);
1830		pci_enable_wake(pdev, PCI_D3hot, 1);
1831	}
1832
1833	pci_disable_device(pdev);
1834	pci_set_power_state(pdev, pci_choose_state(pdev, state));
1835
1836	return 0;
1837}
1838
1839static int
1840netxen_nic_resume(struct pci_dev *pdev)
1841{
1842	return netxen_nic_attach_func(pdev);
1843}
1844#endif
1845
1846static int netxen_nic_open(struct net_device *netdev)
1847{
1848	struct netxen_adapter *adapter = netdev_priv(netdev);
1849	int err = 0;
1850
1851	if (adapter->driver_mismatch)
1852		return -EIO;
1853
1854	err = netxen_nic_attach(adapter);
1855	if (err)
1856		return err;
1857
1858	err = __netxen_nic_up(adapter, netdev);
1859	if (err)
1860		goto err_out;
1861
1862	netif_start_queue(netdev);
1863
1864	return 0;
1865
1866err_out:
1867	netxen_nic_detach(adapter);
1868	return err;
1869}
1870
1871/*
1872 * netxen_nic_close - Disables a network interface entry point
1873 */
1874static int netxen_nic_close(struct net_device *netdev)
1875{
1876	struct netxen_adapter *adapter = netdev_priv(netdev);
1877
1878	__netxen_nic_down(adapter, netdev);
1879	return 0;
1880}
1881
1882static void
1883netxen_tso_check(struct net_device *netdev,
1884		struct nx_host_tx_ring *tx_ring,
1885		struct cmd_desc_type0 *first_desc,
1886		struct sk_buff *skb)
1887{
1888	u8 opcode = TX_ETHER_PKT;
1889	__be16 protocol = skb->protocol;
1890	u16 flags = 0, vid = 0;
1891	u32 producer;
1892	int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1893	struct cmd_desc_type0 *hwdesc;
1894	struct vlan_ethhdr *vh;
1895
1896	if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1897
1898		vh = (struct vlan_ethhdr *)skb->data;
1899		protocol = vh->h_vlan_encapsulated_proto;
1900		flags = FLAGS_VLAN_TAGGED;
1901
1902	} else if (skb_vlan_tag_present(skb)) {
1903		flags = FLAGS_VLAN_OOB;
1904		vid = skb_vlan_tag_get(skb);
1905		netxen_set_tx_vlan_tci(first_desc, vid);
1906		vlan_oob = 1;
1907	}
1908
1909	if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1910			skb_shinfo(skb)->gso_size > 0) {
1911
1912		hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1913
1914		first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1915		first_desc->total_hdr_length = hdr_len;
1916		if (vlan_oob) {
1917			first_desc->total_hdr_length += VLAN_HLEN;
1918			first_desc->tcp_hdr_offset = VLAN_HLEN;
1919			first_desc->ip_hdr_offset = VLAN_HLEN;
1920			/* Only in case of TSO on vlan device */
1921			flags |= FLAGS_VLAN_TAGGED;
1922		}
1923
1924		opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1925				TX_TCP_LSO6 : TX_TCP_LSO;
1926		tso = 1;
1927
1928	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1929		u8 l4proto;
1930
1931		if (protocol == cpu_to_be16(ETH_P_IP)) {
1932			l4proto = ip_hdr(skb)->protocol;
1933
1934			if (l4proto == IPPROTO_TCP)
1935				opcode = TX_TCP_PKT;
1936			else if(l4proto == IPPROTO_UDP)
1937				opcode = TX_UDP_PKT;
1938		} else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1939			l4proto = ipv6_hdr(skb)->nexthdr;
1940
1941			if (l4proto == IPPROTO_TCP)
1942				opcode = TX_TCPV6_PKT;
1943			else if(l4proto == IPPROTO_UDP)
1944				opcode = TX_UDPV6_PKT;
1945		}
1946	}
1947
1948	first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1949	first_desc->ip_hdr_offset += skb_network_offset(skb);
1950	netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1951
1952	if (!tso)
1953		return;
1954
1955	/* For LSO, we need to copy the MAC/IP/TCP headers into
1956	 * the descriptor ring
1957	 */
1958	producer = tx_ring->producer;
1959	copied = 0;
1960	offset = 2;
1961
1962	if (vlan_oob) {
1963		/* Create a TSO vlan header template for firmware */
1964
1965		hwdesc = &tx_ring->desc_head[producer];
1966		tx_ring->cmd_buf_arr[producer].skb = NULL;
1967
1968		copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1969				hdr_len + VLAN_HLEN);
1970
1971		vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1972		skb_copy_from_linear_data(skb, vh, 12);
1973		vh->h_vlan_proto = htons(ETH_P_8021Q);
1974		vh->h_vlan_TCI = htons(vid);
1975		skb_copy_from_linear_data_offset(skb, 12,
1976				(char *)vh + 16, copy_len - 16);
1977
1978		copied = copy_len - VLAN_HLEN;
1979		offset = 0;
1980
1981		producer = get_next_index(producer, tx_ring->num_desc);
1982	}
1983
1984	while (copied < hdr_len) {
1985
1986		copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1987				(hdr_len - copied));
1988
1989		hwdesc = &tx_ring->desc_head[producer];
1990		tx_ring->cmd_buf_arr[producer].skb = NULL;
1991
1992		skb_copy_from_linear_data_offset(skb, copied,
1993				 (char *)hwdesc + offset, copy_len);
1994
1995		copied += copy_len;
1996		offset = 0;
1997
1998		producer = get_next_index(producer, tx_ring->num_desc);
1999	}
2000
2001	tx_ring->producer = producer;
2002	barrier();
2003}
2004
2005static int
2006netxen_map_tx_skb(struct pci_dev *pdev,
2007		struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
2008{
2009	struct netxen_skb_frag *nf;
2010	struct skb_frag_struct *frag;
2011	int i, nr_frags;
2012	dma_addr_t map;
2013
2014	nr_frags = skb_shinfo(skb)->nr_frags;
2015	nf = &pbuf->frag_array[0];
2016
2017	map = pci_map_single(pdev, skb->data,
2018			skb_headlen(skb), PCI_DMA_TODEVICE);
2019	if (pci_dma_mapping_error(pdev, map))
2020		goto out_err;
2021
2022	nf->dma = map;
2023	nf->length = skb_headlen(skb);
2024
2025	for (i = 0; i < nr_frags; i++) {
2026		frag = &skb_shinfo(skb)->frags[i];
2027		nf = &pbuf->frag_array[i+1];
2028
2029		map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
2030				       DMA_TO_DEVICE);
2031		if (dma_mapping_error(&pdev->dev, map))
2032			goto unwind;
2033
2034		nf->dma = map;
2035		nf->length = skb_frag_size(frag);
2036	}
2037
2038	return 0;
2039
2040unwind:
2041	while (--i >= 0) {
2042		nf = &pbuf->frag_array[i+1];
2043		pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2044		nf->dma = 0ULL;
2045	}
2046
2047	nf = &pbuf->frag_array[0];
2048	pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2049	nf->dma = 0ULL;
2050
2051out_err:
2052	return -ENOMEM;
2053}
2054
2055static inline void
2056netxen_clear_cmddesc(u64 *desc)
2057{
2058	desc[0] = 0ULL;
2059	desc[2] = 0ULL;
2060}
2061
2062static netdev_tx_t
2063netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2064{
2065	struct netxen_adapter *adapter = netdev_priv(netdev);
2066	struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
2067	struct netxen_cmd_buffer *pbuf;
2068	struct netxen_skb_frag *buffrag;
2069	struct cmd_desc_type0 *hwdesc, *first_desc;
2070	struct pci_dev *pdev;
2071	int i, k;
2072	int delta = 0;
2073	struct skb_frag_struct *frag;
2074
2075	u32 producer;
2076	int frag_count, no_of_desc;
2077	u32 num_txd = tx_ring->num_desc;
2078
2079	frag_count = skb_shinfo(skb)->nr_frags + 1;
2080
2081	/* 14 frags supported for normal packet and
2082	 * 32 frags supported for TSO packet
2083	 */
2084	if (!skb_is_gso(skb) && frag_count > NETXEN_MAX_FRAGS_PER_TX) {
2085
2086		for (i = 0; i < (frag_count - NETXEN_MAX_FRAGS_PER_TX); i++) {
2087			frag = &skb_shinfo(skb)->frags[i];
2088			delta += skb_frag_size(frag);
2089		}
2090
2091		if (!__pskb_pull_tail(skb, delta))
2092			goto drop_packet;
2093
2094		frag_count = 1 + skb_shinfo(skb)->nr_frags;
2095	}
2096	/* 4 fragments per cmd des */
2097	no_of_desc = (frag_count + 3) >> 2;
2098
2099	if (unlikely(netxen_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
2100		netif_stop_queue(netdev);
2101		smp_mb();
2102		if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH)
2103			netif_start_queue(netdev);
2104		else
2105			return NETDEV_TX_BUSY;
2106	}
2107
2108	producer = tx_ring->producer;
2109	pbuf = &tx_ring->cmd_buf_arr[producer];
2110
2111	pdev = adapter->pdev;
2112
2113	if (netxen_map_tx_skb(pdev, skb, pbuf))
2114		goto drop_packet;
2115
2116	pbuf->skb = skb;
2117	pbuf->frag_count = frag_count;
2118
2119	first_desc = hwdesc = &tx_ring->desc_head[producer];
2120	netxen_clear_cmddesc((u64 *)hwdesc);
2121
2122	netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
2123	netxen_set_tx_port(first_desc, adapter->portnum);
2124
2125	for (i = 0; i < frag_count; i++) {
2126
2127		k = i % 4;
2128
2129		if ((k == 0) && (i > 0)) {
2130			/* move to next desc.*/
2131			producer = get_next_index(producer, num_txd);
2132			hwdesc = &tx_ring->desc_head[producer];
2133			netxen_clear_cmddesc((u64 *)hwdesc);
2134			tx_ring->cmd_buf_arr[producer].skb = NULL;
2135		}
2136
2137		buffrag = &pbuf->frag_array[i];
2138
2139		hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2140		switch (k) {
2141		case 0:
2142			hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2143			break;
2144		case 1:
2145			hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2146			break;
2147		case 2:
2148			hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2149			break;
2150		case 3:
2151			hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2152			break;
2153		}
2154	}
2155
2156	tx_ring->producer = get_next_index(producer, num_txd);
2157
2158	netxen_tso_check(netdev, tx_ring, first_desc, skb);
2159
2160	adapter->stats.txbytes += skb->len;
2161	adapter->stats.xmitcalled++;
2162
2163	netxen_nic_update_cmd_producer(adapter, tx_ring);
2164
2165	return NETDEV_TX_OK;
2166
2167drop_packet:
2168	adapter->stats.txdropped++;
2169	dev_kfree_skb_any(skb);
2170	return NETDEV_TX_OK;
2171}
2172
2173static int netxen_nic_check_temp(struct netxen_adapter *adapter)
2174{
2175	struct net_device *netdev = adapter->netdev;
2176	uint32_t temp, temp_state, temp_val;
2177	int rv = 0;
2178
2179	temp = NXRD32(adapter, CRB_TEMP_STATE);
2180
2181	temp_state = nx_get_temp_state(temp);
2182	temp_val = nx_get_temp_val(temp);
2183
2184	if (temp_state == NX_TEMP_PANIC) {
2185		printk(KERN_ALERT
2186		       "%s: Device temperature %d degrees C exceeds"
2187		       " maximum allowed. Hardware has been shut down.\n",
2188		       netdev->name, temp_val);
2189		rv = 1;
2190	} else if (temp_state == NX_TEMP_WARN) {
2191		if (adapter->temp == NX_TEMP_NORMAL) {
2192			printk(KERN_ALERT
2193			       "%s: Device temperature %d degrees C "
2194			       "exceeds operating range."
2195			       " Immediate action needed.\n",
2196			       netdev->name, temp_val);
2197		}
2198	} else {
2199		if (adapter->temp == NX_TEMP_WARN) {
2200			printk(KERN_INFO
2201			       "%s: Device temperature is now %d degrees C"
2202			       " in normal range.\n", netdev->name,
2203			       temp_val);
2204		}
2205	}
2206	adapter->temp = temp_state;
2207	return rv;
2208}
2209
2210void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
2211{
2212	struct net_device *netdev = adapter->netdev;
2213
2214	if (adapter->ahw.linkup && !linkup) {
2215		printk(KERN_INFO "%s: %s NIC Link is down\n",
2216		       netxen_nic_driver_name, netdev->name);
2217		adapter->ahw.linkup = 0;
2218		if (netif_running(netdev)) {
2219			netif_carrier_off(netdev);
2220			netif_stop_queue(netdev);
2221		}
2222		adapter->link_changed = !adapter->has_link_events;
2223	} else if (!adapter->ahw.linkup && linkup) {
2224		printk(KERN_INFO "%s: %s NIC Link is up\n",
2225		       netxen_nic_driver_name, netdev->name);
2226		adapter->ahw.linkup = 1;
2227		if (netif_running(netdev)) {
2228			netif_carrier_on(netdev);
2229			netif_wake_queue(netdev);
2230		}
2231		adapter->link_changed = !adapter->has_link_events;
2232	}
2233}
2234
2235static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
2236{
2237	u32 val, port, linkup;
2238
2239	port = adapter->physical_port;
2240
2241	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2242		val = NXRD32(adapter, CRB_XG_STATE_P3);
2243		val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
2244		linkup = (val == XG_LINK_UP_P3);
2245	} else {
2246		val = NXRD32(adapter, CRB_XG_STATE);
2247		val = (val >> port*8) & 0xff;
2248		linkup = (val == XG_LINK_UP);
2249	}
2250
2251	netxen_advert_link_change(adapter, linkup);
2252}
2253
2254static void netxen_tx_timeout(struct net_device *netdev)
2255{
2256	struct netxen_adapter *adapter = netdev_priv(netdev);
2257
2258	if (test_bit(__NX_RESETTING, &adapter->state))
2259		return;
2260
2261	dev_err(&netdev->dev, "transmit timeout, resetting.\n");
2262	schedule_work(&adapter->tx_timeout_task);
2263}
2264
2265static void netxen_tx_timeout_task(struct work_struct *work)
2266{
2267	struct netxen_adapter *adapter =
2268		container_of(work, struct netxen_adapter, tx_timeout_task);
2269
2270	if (!netif_running(adapter->netdev))
2271		return;
2272
2273	if (test_and_set_bit(__NX_RESETTING, &adapter->state))
2274		return;
2275
2276	if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
2277		goto request_reset;
2278
2279	rtnl_lock();
2280	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
2281		/* try to scrub interrupt */
2282		netxen_napi_disable(adapter);
2283
2284		netxen_napi_enable(adapter);
2285
2286		netif_wake_queue(adapter->netdev);
2287
2288		clear_bit(__NX_RESETTING, &adapter->state);
2289	} else {
2290		clear_bit(__NX_RESETTING, &adapter->state);
2291		if (netxen_nic_reset_context(adapter)) {
2292			rtnl_unlock();
2293			goto request_reset;
2294		}
2295	}
2296	netif_trans_update(adapter->netdev);
2297	rtnl_unlock();
2298	return;
2299
2300request_reset:
2301	adapter->need_fw_reset = 1;
2302	clear_bit(__NX_RESETTING, &adapter->state);
2303}
2304
2305static void netxen_nic_get_stats(struct net_device *netdev,
2306				 struct rtnl_link_stats64 *stats)
2307{
2308	struct netxen_adapter *adapter = netdev_priv(netdev);
2309
2310	stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2311	stats->tx_packets = adapter->stats.xmitfinished;
2312	stats->rx_bytes = adapter->stats.rxbytes;
2313	stats->tx_bytes = adapter->stats.txbytes;
2314	stats->rx_dropped = adapter->stats.rxdropped;
2315	stats->tx_dropped = adapter->stats.txdropped;
2316}
2317
2318static irqreturn_t netxen_intr(int irq, void *data)
2319{
2320	struct nx_host_sds_ring *sds_ring = data;
2321	struct netxen_adapter *adapter = sds_ring->adapter;
2322	u32 status = 0;
2323
2324	status = readl(adapter->isr_int_vec);
2325
2326	if (!(status & adapter->int_vec_bit))
2327		return IRQ_NONE;
2328
2329	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2330		/* check interrupt state machine, to be sure */
2331		status = readl(adapter->crb_int_state_reg);
2332		if (!ISR_LEGACY_INT_TRIGGERED(status))
2333			return IRQ_NONE;
2334
2335	} else {
2336		unsigned long our_int = 0;
2337
2338		our_int = readl(adapter->crb_int_state_reg);
2339
2340		/* not our interrupt */
2341		if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
2342			return IRQ_NONE;
2343
2344		/* claim interrupt */
2345		writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
2346
2347		/* clear interrupt */
2348		netxen_nic_disable_int(sds_ring);
2349	}
2350
2351	writel(0xffffffff, adapter->tgt_status_reg);
2352	/* read twice to ensure write is flushed */
2353	readl(adapter->isr_int_vec);
2354	readl(adapter->isr_int_vec);
2355
2356	napi_schedule(&sds_ring->napi);
2357
2358	return IRQ_HANDLED;
2359}
2360
2361static irqreturn_t netxen_msi_intr(int irq, void *data)
2362{
2363	struct nx_host_sds_ring *sds_ring = data;
2364	struct netxen_adapter *adapter = sds_ring->adapter;
2365
2366	/* clear interrupt */
2367	writel(0xffffffff, adapter->tgt_status_reg);
2368
2369	napi_schedule(&sds_ring->napi);
2370	return IRQ_HANDLED;
2371}
2372
2373static irqreturn_t netxen_msix_intr(int irq, void *data)
2374{
2375	struct nx_host_sds_ring *sds_ring = data;
2376
2377	napi_schedule(&sds_ring->napi);
2378	return IRQ_HANDLED;
2379}
2380
2381static int netxen_nic_poll(struct napi_struct *napi, int budget)
2382{
2383	struct nx_host_sds_ring *sds_ring =
2384		container_of(napi, struct nx_host_sds_ring, napi);
2385
2386	struct netxen_adapter *adapter = sds_ring->adapter;
2387
2388	int tx_complete;
2389	int work_done;
2390
2391	tx_complete = netxen_process_cmd_ring(adapter);
2392
2393	work_done = netxen_process_rcv_ring(sds_ring, budget);
2394
2395	if (!tx_complete)
2396		work_done = budget;
2397
2398	if (work_done < budget) {
2399		napi_complete_done(&sds_ring->napi, work_done);
2400		if (test_bit(__NX_DEV_UP, &adapter->state))
2401			netxen_nic_enable_int(sds_ring);
2402	}
2403
2404	return work_done;
2405}
2406
2407#ifdef CONFIG_NET_POLL_CONTROLLER
2408static void netxen_nic_poll_controller(struct net_device *netdev)
2409{
2410	int ring;
2411	struct nx_host_sds_ring *sds_ring;
2412	struct netxen_adapter *adapter = netdev_priv(netdev);
2413	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
2414
2415	disable_irq(adapter->irq);
2416	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2417		sds_ring = &recv_ctx->sds_rings[ring];
2418		netxen_intr(adapter->irq, sds_ring);
2419	}
2420	enable_irq(adapter->irq);
2421}
2422#endif
2423
2424static int
2425nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
2426{
2427	int count;
2428	if (netxen_api_lock(adapter))
2429		return -EIO;
2430
2431	count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2432
2433	NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2434
2435	netxen_api_unlock(adapter);
2436	return count;
2437}
2438
2439static int
2440nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
2441{
2442	int count, state;
2443	if (netxen_api_lock(adapter))
2444		return -EIO;
2445
2446	count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2447	WARN_ON(count == 0);
2448
2449	NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2450	state = NXRD32(adapter, NX_CRB_DEV_STATE);
2451
2452	if (count == 0 && state != NX_DEV_FAILED)
2453		NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2454
2455	netxen_api_unlock(adapter);
2456	return count;
2457}
2458
2459static int
2460nx_dev_request_aer(struct netxen_adapter *adapter)
2461{
2462	u32 state;
2463	int ret = -EINVAL;
2464
2465	if (netxen_api_lock(adapter))
2466		return ret;
2467
2468	state = NXRD32(adapter, NX_CRB_DEV_STATE);
2469
2470	if (state == NX_DEV_NEED_AER)
2471		ret = 0;
2472	else if (state == NX_DEV_READY) {
2473		NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_AER);
2474		ret = 0;
2475	}
2476
2477	netxen_api_unlock(adapter);
2478	return ret;
2479}
2480
2481int
2482nx_dev_request_reset(struct netxen_adapter *adapter)
2483{
2484	u32 state;
2485	int ret = -EINVAL;
2486
2487	if (netxen_api_lock(adapter))
2488		return ret;
2489
2490	state = NXRD32(adapter, NX_CRB_DEV_STATE);
2491
2492	if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED)
2493		ret = 0;
2494	else if (state != NX_DEV_INITALIZING && state != NX_DEV_NEED_AER) {
2495		NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
2496		adapter->flags |= NETXEN_FW_RESET_OWNER;
2497		ret = 0;
2498	}
2499
2500	netxen_api_unlock(adapter);
2501
2502	return ret;
2503}
2504
2505static int
2506netxen_can_start_firmware(struct netxen_adapter *adapter)
2507{
2508	int count;
2509	int can_start = 0;
2510
2511	if (netxen_api_lock(adapter)) {
2512		nx_incr_dev_ref_cnt(adapter);
2513		return -1;
2514	}
2515
2516	count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2517
2518	if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2519		count = 0;
2520
2521	if (count == 0) {
2522		can_start = 1;
2523		NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2524	}
2525
2526	NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2527
2528	netxen_api_unlock(adapter);
2529
2530	return can_start;
2531}
2532
2533static void
2534netxen_schedule_work(struct netxen_adapter *adapter,
2535		work_func_t func, int delay)
2536{
2537	INIT_DELAYED_WORK(&adapter->fw_work, func);
2538	schedule_delayed_work(&adapter->fw_work, delay);
2539}
2540
2541static void
2542netxen_cancel_fw_work(struct netxen_adapter *adapter)
2543{
2544	while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2545		msleep(10);
2546
2547	cancel_delayed_work_sync(&adapter->fw_work);
2548}
2549
2550static void
2551netxen_attach_work(struct work_struct *work)
2552{
2553	struct netxen_adapter *adapter = container_of(work,
2554				struct netxen_adapter, fw_work.work);
2555	struct net_device *netdev = adapter->netdev;
2556	int err = 0;
2557
2558	if (netif_running(netdev)) {
2559		err = netxen_nic_attach(adapter);
2560		if (err)
2561			goto done;
2562
2563		err = netxen_nic_up(adapter, netdev);
2564		if (err) {
2565			netxen_nic_detach(adapter);
2566			goto done;
2567		}
2568
2569		netxen_restore_indev_addr(netdev, NETDEV_UP);
2570	}
2571
2572	netif_device_attach(netdev);
2573
2574done:
2575	adapter->fw_fail_cnt = 0;
2576	clear_bit(__NX_RESETTING, &adapter->state);
2577	netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2578}
2579
2580static void
2581netxen_fwinit_work(struct work_struct *work)
2582{
2583	struct netxen_adapter *adapter = container_of(work,
2584				struct netxen_adapter, fw_work.work);
2585	int dev_state;
2586	int count;
2587	dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2588	if (adapter->flags & NETXEN_FW_RESET_OWNER) {
2589		count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2590		WARN_ON(count == 0);
2591		if (count == 1) {
2592			if (adapter->mdump.md_enabled) {
2593				rtnl_lock();
2594				netxen_dump_fw(adapter);
2595				rtnl_unlock();
2596			}
2597			adapter->flags &= ~NETXEN_FW_RESET_OWNER;
2598			if (netxen_api_lock(adapter)) {
2599				clear_bit(__NX_RESETTING, &adapter->state);
2600				NXWR32(adapter, NX_CRB_DEV_STATE,
2601						NX_DEV_FAILED);
2602				return;
2603			}
2604			count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2605			NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2606			NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2607			dev_state = NX_DEV_COLD;
2608			netxen_api_unlock(adapter);
2609		}
2610	}
2611
2612	switch (dev_state) {
2613	case NX_DEV_COLD:
2614	case NX_DEV_READY:
2615		if (!netxen_start_firmware(adapter)) {
2616			netxen_schedule_work(adapter, netxen_attach_work, 0);
2617			return;
2618		}
2619		break;
2620
2621	case NX_DEV_NEED_RESET:
2622	case NX_DEV_INITALIZING:
2623			netxen_schedule_work(adapter,
2624					netxen_fwinit_work, 2 * FW_POLL_DELAY);
2625			return;
2626
2627	case NX_DEV_FAILED:
2628	default:
2629		nx_incr_dev_ref_cnt(adapter);
2630		break;
2631	}
2632
2633	if (netxen_api_lock(adapter)) {
2634		clear_bit(__NX_RESETTING, &adapter->state);
2635		return;
2636	}
2637	NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_FAILED);
2638	netxen_api_unlock(adapter);
2639	dev_err(&adapter->pdev->dev, "%s: Device initialization Failed\n",
2640				adapter->netdev->name);
2641
2642	clear_bit(__NX_RESETTING, &adapter->state);
2643}
2644
2645static void
2646netxen_detach_work(struct work_struct *work)
2647{
2648	struct netxen_adapter *adapter = container_of(work,
2649				struct netxen_adapter, fw_work.work);
2650	struct net_device *netdev = adapter->netdev;
2651	int ref_cnt = 0, delay;
2652	u32 status;
2653
2654	netif_device_detach(netdev);
2655
2656	netxen_nic_down(adapter, netdev);
2657
2658	rtnl_lock();
2659	netxen_nic_detach(adapter);
2660	rtnl_unlock();
2661
2662	status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2663
2664	if (status & NX_RCODE_FATAL_ERROR)
2665		goto err_ret;
2666
2667	if (adapter->temp == NX_TEMP_PANIC)
2668		goto err_ret;
2669
2670	if (!(adapter->flags & NETXEN_FW_RESET_OWNER))
2671		ref_cnt = nx_decr_dev_ref_cnt(adapter);
2672
2673	if (ref_cnt == -EIO)
2674		goto err_ret;
2675
2676	delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2677
2678	adapter->fw_wait_cnt = 0;
2679	netxen_schedule_work(adapter, netxen_fwinit_work, delay);
2680
2681	return;
2682
2683err_ret:
2684	clear_bit(__NX_RESETTING, &adapter->state);
2685}
2686
2687static int
2688netxen_check_health(struct netxen_adapter *adapter)
2689{
2690	u32 state, heartbit;
2691	u32 peg_status;
2692	struct net_device *netdev = adapter->netdev;
2693
2694	state = NXRD32(adapter, NX_CRB_DEV_STATE);
2695	if (state == NX_DEV_NEED_AER)
2696		return 0;
2697
2698	if (netxen_nic_check_temp(adapter))
2699		goto detach;
2700
2701	if (adapter->need_fw_reset) {
2702		if (nx_dev_request_reset(adapter))
2703			return 0;
2704		goto detach;
2705	}
2706
2707	/* NX_DEV_NEED_RESET, this state can be marked in two cases
2708	 * 1. Tx timeout 2. Fw hang
2709	 * Send request to destroy context in case of tx timeout only
2710	 * and doesn't required in case of Fw hang
2711	 */
2712	if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED) {
2713		adapter->need_fw_reset = 1;
2714		if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2715			goto detach;
2716	}
2717
2718	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2719		return 0;
2720
2721	heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2722	if (heartbit != adapter->heartbit) {
2723		adapter->heartbit = heartbit;
2724		adapter->fw_fail_cnt = 0;
2725		if (adapter->need_fw_reset)
2726			goto detach;
2727		return 0;
2728	}
2729
2730	if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2731		return 0;
2732
2733	if (nx_dev_request_reset(adapter))
2734		return 0;
2735
2736	clear_bit(__NX_FW_ATTACHED, &adapter->state);
2737
2738	dev_err(&netdev->dev, "firmware hang detected\n");
2739	peg_status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2740	dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
2741			"PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
2742			"PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
2743			"PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
2744			"PEG_NET_4_PC: 0x%x\n",
2745			peg_status,
2746			NXRD32(adapter, NETXEN_PEG_HALT_STATUS2),
2747			NXRD32(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c),
2748			NXRD32(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c),
2749			NXRD32(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c),
2750			NXRD32(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c),
2751			NXRD32(adapter, NETXEN_CRB_PEG_NET_4 + 0x3c));
2752	if (NX_FWERROR_PEGSTAT1(peg_status) == 0x67)
2753		dev_err(&adapter->pdev->dev,
2754			"Firmware aborted with error code 0x00006700. "
2755				"Device is being reset.\n");
2756detach:
2757	if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
2758			!test_and_set_bit(__NX_RESETTING, &adapter->state))
2759		netxen_schedule_work(adapter, netxen_detach_work, 0);
2760	return 1;
2761}
2762
2763static void
2764netxen_fw_poll_work(struct work_struct *work)
2765{
2766	struct netxen_adapter *adapter = container_of(work,
2767				struct netxen_adapter, fw_work.work);
2768
2769	if (test_bit(__NX_RESETTING, &adapter->state))
2770		goto reschedule;
2771
2772	if (test_bit(__NX_DEV_UP, &adapter->state) &&
2773	    !(adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)) {
2774		if (!adapter->has_link_events) {
2775
2776			netxen_nic_handle_phy_intr(adapter);
2777
2778			if (adapter->link_changed)
2779				netxen_nic_set_link_parameters(adapter);
2780		}
2781	}
2782
2783	if (netxen_check_health(adapter))
2784		return;
2785
2786reschedule:
2787	netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2788}
2789
2790static ssize_t
2791netxen_store_bridged_mode(struct device *dev,
2792		struct device_attribute *attr, const char *buf, size_t len)
2793{
2794	struct net_device *net = to_net_dev(dev);
2795	struct netxen_adapter *adapter = netdev_priv(net);
2796	unsigned long new;
2797	int ret = -EINVAL;
2798
2799	if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2800		goto err_out;
2801
2802	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2803		goto err_out;
2804
2805	if (kstrtoul(buf, 2, &new))
2806		goto err_out;
2807
2808	if (!netxen_config_bridged_mode(adapter, !!new))
2809		ret = len;
2810
2811err_out:
2812	return ret;
2813}
2814
2815static ssize_t
2816netxen_show_bridged_mode(struct device *dev,
2817		struct device_attribute *attr, char *buf)
2818{
2819	struct net_device *net = to_net_dev(dev);
2820	struct netxen_adapter *adapter;
2821	int bridged_mode = 0;
2822
2823	adapter = netdev_priv(net);
2824
2825	if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2826		bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2827
2828	return sprintf(buf, "%d\n", bridged_mode);
2829}
2830
2831static const struct device_attribute dev_attr_bridged_mode = {
2832	.attr = { .name = "bridged_mode", .mode = 0644 },
2833	.show = netxen_show_bridged_mode,
2834	.store = netxen_store_bridged_mode,
2835};
2836
2837static ssize_t
2838netxen_store_diag_mode(struct device *dev,
2839		struct device_attribute *attr, const char *buf, size_t len)
2840{
2841	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2842	unsigned long new;
2843
2844	if (kstrtoul(buf, 2, &new))
2845		return -EINVAL;
2846
2847	if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2848		adapter->flags ^= NETXEN_NIC_DIAG_ENABLED;
2849
2850	return len;
2851}
2852
2853static ssize_t
2854netxen_show_diag_mode(struct device *dev,
2855		struct device_attribute *attr, char *buf)
2856{
2857	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2858
2859	return sprintf(buf, "%d\n",
2860			!!(adapter->flags & NETXEN_NIC_DIAG_ENABLED));
2861}
2862
2863static const struct device_attribute dev_attr_diag_mode = {
2864	.attr = { .name = "diag_mode", .mode = 0644 },
2865	.show = netxen_show_diag_mode,
2866	.store = netxen_store_diag_mode,
2867};
2868
2869static int
2870netxen_sysfs_validate_crb(struct netxen_adapter *adapter,
2871		loff_t offset, size_t size)
2872{
2873	size_t crb_size = 4;
2874
2875	if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2876		return -EIO;
2877
2878	if (offset < NETXEN_PCI_CRBSPACE) {
2879		if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2880			return -EINVAL;
2881
2882		if (ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2883						NETXEN_PCI_CAMQM_2M_END))
2884			crb_size = 8;
2885		else
2886			return -EINVAL;
2887	}
2888
2889	if ((size != crb_size) || (offset & (crb_size-1)))
2890		return  -EINVAL;
2891
2892	return 0;
2893}
2894
2895static ssize_t
2896netxen_sysfs_read_crb(struct file *filp, struct kobject *kobj,
2897		struct bin_attribute *attr,
2898		char *buf, loff_t offset, size_t size)
2899{
2900	struct device *dev = kobj_to_dev(kobj);
2901	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2902	u32 data;
2903	u64 qmdata;
2904	int ret;
2905
2906	ret = netxen_sysfs_validate_crb(adapter, offset, size);
2907	if (ret != 0)
2908		return ret;
2909
2910	if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
2911		ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2912					NETXEN_PCI_CAMQM_2M_END)) {
2913		netxen_pci_camqm_read_2M(adapter, offset, &qmdata);
2914		memcpy(buf, &qmdata, size);
2915	} else {
2916		data = NXRD32(adapter, offset);
2917		memcpy(buf, &data, size);
2918	}
2919
2920	return size;
2921}
2922
2923static ssize_t
2924netxen_sysfs_write_crb(struct file *filp, struct kobject *kobj,
2925		struct bin_attribute *attr,
2926		char *buf, loff_t offset, size_t size)
2927{
2928	struct device *dev = kobj_to_dev(kobj);
2929	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2930	u32 data;
2931	u64 qmdata;
2932	int ret;
2933
2934	ret = netxen_sysfs_validate_crb(adapter, offset, size);
2935	if (ret != 0)
2936		return ret;
2937
2938	if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
2939		ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2940					NETXEN_PCI_CAMQM_2M_END)) {
2941		memcpy(&qmdata, buf, size);
2942		netxen_pci_camqm_write_2M(adapter, offset, qmdata);
2943	} else {
2944		memcpy(&data, buf, size);
2945		NXWR32(adapter, offset, data);
2946	}
2947
2948	return size;
2949}
2950
2951static int
2952netxen_sysfs_validate_mem(struct netxen_adapter *adapter,
2953		loff_t offset, size_t size)
2954{
2955	if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2956		return -EIO;
2957
2958	if ((size != 8) || (offset & 0x7))
2959		return  -EIO;
2960
2961	return 0;
2962}
2963
2964static ssize_t
2965netxen_sysfs_read_mem(struct file *filp, struct kobject *kobj,
2966		struct bin_attribute *attr,
2967		char *buf, loff_t offset, size_t size)
2968{
2969	struct device *dev = kobj_to_dev(kobj);
2970	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2971	u64 data;
2972	int ret;
2973
2974	ret = netxen_sysfs_validate_mem(adapter, offset, size);
2975	if (ret != 0)
2976		return ret;
2977
2978	if (adapter->pci_mem_read(adapter, offset, &data))
2979		return -EIO;
2980
2981	memcpy(buf, &data, size);
2982
2983	return size;
2984}
2985
2986static ssize_t netxen_sysfs_write_mem(struct file *filp, struct kobject *kobj,
2987		struct bin_attribute *attr, char *buf,
2988		loff_t offset, size_t size)
2989{
2990	struct device *dev = kobj_to_dev(kobj);
2991	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2992	u64 data;
2993	int ret;
2994
2995	ret = netxen_sysfs_validate_mem(adapter, offset, size);
2996	if (ret != 0)
2997		return ret;
2998
2999	memcpy(&data, buf, size);
3000
3001	if (adapter->pci_mem_write(adapter, offset, data))
3002		return -EIO;
3003
3004	return size;
3005}
3006
3007
3008static const struct bin_attribute bin_attr_crb = {
3009	.attr = { .name = "crb", .mode = 0644 },
3010	.size = 0,
3011	.read = netxen_sysfs_read_crb,
3012	.write = netxen_sysfs_write_crb,
3013};
3014
3015static const struct bin_attribute bin_attr_mem = {
3016	.attr = { .name = "mem", .mode = 0644 },
3017	.size = 0,
3018	.read = netxen_sysfs_read_mem,
3019	.write = netxen_sysfs_write_mem,
3020};
3021
3022static ssize_t
3023netxen_sysfs_read_dimm(struct file *filp, struct kobject *kobj,
3024		struct bin_attribute *attr,
3025		char *buf, loff_t offset, size_t size)
3026{
3027	struct device *dev = kobj_to_dev(kobj);
3028	struct netxen_adapter *adapter = dev_get_drvdata(dev);
3029	struct net_device *netdev = adapter->netdev;
3030	struct netxen_dimm_cfg dimm;
3031	u8 dw, rows, cols, banks, ranks;
3032	u32 val;
3033
3034	if (size < attr->size) {
3035		netdev_err(netdev, "Invalid size\n");
3036		return -EINVAL;
3037	}
3038
3039	memset(&dimm, 0, sizeof(struct netxen_dimm_cfg));
3040	val = NXRD32(adapter, NETXEN_DIMM_CAPABILITY);
3041
3042	/* Checks if DIMM info is valid. */
3043	if (val & NETXEN_DIMM_VALID_FLAG) {
3044		netdev_err(netdev, "Invalid DIMM flag\n");
3045		dimm.presence = 0xff;
3046		goto out;
3047	}
3048
3049	rows = NETXEN_DIMM_NUMROWS(val);
3050	cols = NETXEN_DIMM_NUMCOLS(val);
3051	ranks = NETXEN_DIMM_NUMRANKS(val);
3052	banks = NETXEN_DIMM_NUMBANKS(val);
3053	dw = NETXEN_DIMM_DATAWIDTH(val);
3054
3055	dimm.presence = (val & NETXEN_DIMM_PRESENT);
3056
3057	/* Checks if DIMM info is present. */
3058	if (!dimm.presence) {
3059		netdev_err(netdev, "DIMM not present\n");
3060		goto out;
3061	}
3062
3063	dimm.dimm_type = NETXEN_DIMM_TYPE(val);
3064
3065	switch (dimm.dimm_type) {
3066	case NETXEN_DIMM_TYPE_RDIMM:
3067	case NETXEN_DIMM_TYPE_UDIMM:
3068	case NETXEN_DIMM_TYPE_SO_DIMM:
3069	case NETXEN_DIMM_TYPE_Micro_DIMM:
3070	case NETXEN_DIMM_TYPE_Mini_RDIMM:
3071	case NETXEN_DIMM_TYPE_Mini_UDIMM:
3072		break;
3073	default:
3074		netdev_err(netdev, "Invalid DIMM type %x\n", dimm.dimm_type);
3075		goto out;
3076	}
3077
3078	if (val & NETXEN_DIMM_MEMTYPE_DDR2_SDRAM)
3079		dimm.mem_type = NETXEN_DIMM_MEM_DDR2_SDRAM;
3080	else
3081		dimm.mem_type = NETXEN_DIMM_MEMTYPE(val);
3082
3083	if (val & NETXEN_DIMM_SIZE) {
3084		dimm.size = NETXEN_DIMM_STD_MEM_SIZE;
3085		goto out;
3086	}
3087
3088	if (!rows) {
3089		netdev_err(netdev, "Invalid no of rows %x\n", rows);
3090		goto out;
3091	}
3092
3093	if (!cols) {
3094		netdev_err(netdev, "Invalid no of columns %x\n", cols);
3095		goto out;
3096	}
3097
3098	if (!banks) {
3099		netdev_err(netdev, "Invalid no of banks %x\n", banks);
3100		goto out;
3101	}
3102
3103	ranks += 1;
3104
3105	switch (dw) {
3106	case 0x0:
3107		dw = 32;
3108		break;
3109	case 0x1:
3110		dw = 33;
3111		break;
3112	case 0x2:
3113		dw = 36;
3114		break;
3115	case 0x3:
3116		dw = 64;
3117		break;
3118	case 0x4:
3119		dw = 72;
3120		break;
3121	case 0x5:
3122		dw = 80;
3123		break;
3124	case 0x6:
3125		dw = 128;
3126		break;
3127	case 0x7:
3128		dw = 144;
3129		break;
3130	default:
3131		netdev_err(netdev, "Invalid data-width %x\n", dw);
3132		goto out;
3133	}
3134
3135	dimm.size = ((1 << rows) * (1 << cols) * dw * banks * ranks) / 8;
3136	/* Size returned in MB. */
3137	dimm.size = (dimm.size) / 0x100000;
3138out:
3139	memcpy(buf, &dimm, sizeof(struct netxen_dimm_cfg));
3140	return sizeof(struct netxen_dimm_cfg);
3141
3142}
3143
3144static const struct bin_attribute bin_attr_dimm = {
3145	.attr = { .name = "dimm", .mode = 0644 },
3146	.size = sizeof(struct netxen_dimm_cfg),
3147	.read = netxen_sysfs_read_dimm,
3148};
3149
3150
3151static void
3152netxen_create_sysfs_entries(struct netxen_adapter *adapter)
3153{
3154	struct device *dev = &adapter->pdev->dev;
3155
3156	if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
3157		/* bridged_mode control */
3158		if (device_create_file(dev, &dev_attr_bridged_mode)) {
3159			dev_warn(dev,
3160				"failed to create bridged_mode sysfs entry\n");
3161		}
3162	}
3163}
3164
3165static void
3166netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
3167{
3168	struct device *dev = &adapter->pdev->dev;
3169
3170	if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
3171		device_remove_file(dev, &dev_attr_bridged_mode);
3172}
3173
3174static void
3175netxen_create_diag_entries(struct netxen_adapter *adapter)
3176{
3177	struct pci_dev *pdev = adapter->pdev;
3178	struct device *dev;
3179
3180	dev = &pdev->dev;
3181	if (device_create_file(dev, &dev_attr_diag_mode))
3182		dev_info(dev, "failed to create diag_mode sysfs entry\n");
3183	if (device_create_bin_file(dev, &bin_attr_crb))
3184		dev_info(dev, "failed to create crb sysfs entry\n");
3185	if (device_create_bin_file(dev, &bin_attr_mem))
3186		dev_info(dev, "failed to create mem sysfs entry\n");
3187	if (device_create_bin_file(dev, &bin_attr_dimm))
3188		dev_info(dev, "failed to create dimm sysfs entry\n");
3189}
3190
3191
3192static void
3193netxen_remove_diag_entries(struct netxen_adapter *adapter)
3194{
3195	struct pci_dev *pdev = adapter->pdev;
3196	struct device *dev = &pdev->dev;
3197
3198	device_remove_file(dev, &dev_attr_diag_mode);
3199	device_remove_bin_file(dev, &bin_attr_crb);
3200	device_remove_bin_file(dev, &bin_attr_mem);
3201	device_remove_bin_file(dev, &bin_attr_dimm);
3202}
3203
3204#ifdef CONFIG_INET
3205
3206#define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
3207
3208static int
3209netxen_destip_supported(struct netxen_adapter *adapter)
3210{
3211	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
3212		return 0;
3213
3214	if (adapter->ahw.cut_through)
3215		return 0;
3216
3217	return 1;
3218}
3219
3220static void
3221netxen_free_ip_list(struct netxen_adapter *adapter, bool master)
3222{
3223	struct nx_ip_list  *cur, *tmp_cur;
3224
3225	list_for_each_entry_safe(cur, tmp_cur, &adapter->ip_list, list) {
3226		if (master) {
3227			if (cur->master) {
3228				netxen_config_ipaddr(adapter, cur->ip_addr,
3229						     NX_IP_DOWN);
3230				list_del(&cur->list);
3231				kfree(cur);
3232			}
3233		} else {
3234			netxen_config_ipaddr(adapter, cur->ip_addr, NX_IP_DOWN);
3235			list_del(&cur->list);
3236			kfree(cur);
3237		}
3238	}
3239}
3240
3241static bool
3242netxen_list_config_ip(struct netxen_adapter *adapter,
3243		struct in_ifaddr *ifa, unsigned long event)
3244{
3245	struct net_device *dev;
3246	struct nx_ip_list *cur, *tmp_cur;
3247	struct list_head *head;
3248	bool ret = false;
3249
3250	dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
3251
3252	if (dev == NULL)
3253		goto out;
3254
3255	switch (event) {
3256	case NX_IP_UP:
3257		list_for_each(head, &adapter->ip_list) {
3258			cur = list_entry(head, struct nx_ip_list, list);
3259
3260			if (cur->ip_addr == ifa->ifa_address)
3261				goto out;
3262		}
3263
3264		cur = kzalloc(sizeof(struct nx_ip_list), GFP_ATOMIC);
3265		if (cur == NULL)
3266			goto out;
3267		if (is_vlan_dev(dev))
3268			dev = vlan_dev_real_dev(dev);
3269		cur->master = !!netif_is_bond_master(dev);
3270		cur->ip_addr = ifa->ifa_address;
3271		list_add_tail(&cur->list, &adapter->ip_list);
3272		netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
3273		ret = true;
3274		break;
3275	case NX_IP_DOWN:
3276		list_for_each_entry_safe(cur, tmp_cur,
3277					&adapter->ip_list, list) {
3278			if (cur->ip_addr == ifa->ifa_address) {
3279				list_del(&cur->list);
3280				kfree(cur);
3281				netxen_config_ipaddr(adapter, ifa->ifa_address,
3282						     NX_IP_DOWN);
3283				ret = true;
3284				break;
3285			}
3286		}
3287	}
3288out:
3289	return ret;
3290}
3291
3292static void
3293netxen_config_indev_addr(struct netxen_adapter *adapter,
3294		struct net_device *dev, unsigned long event)
3295{
3296	struct in_device *indev;
 
3297
3298	if (!netxen_destip_supported(adapter))
3299		return;
3300
3301	indev = in_dev_get(dev);
3302	if (!indev)
3303		return;
3304
3305	for_ifa(indev) {
 
3306		switch (event) {
3307		case NETDEV_UP:
3308			netxen_list_config_ip(adapter, ifa, NX_IP_UP);
3309			break;
3310		case NETDEV_DOWN:
3311			netxen_list_config_ip(adapter, ifa, NX_IP_DOWN);
3312			break;
3313		default:
3314			break;
3315		}
3316	} endfor_ifa(indev);
3317
3318	in_dev_put(indev);
3319}
3320
3321static void
3322netxen_restore_indev_addr(struct net_device *netdev, unsigned long event)
3323
3324{
3325	struct netxen_adapter *adapter = netdev_priv(netdev);
3326	struct nx_ip_list *pos, *tmp_pos;
3327	unsigned long ip_event;
3328
3329	ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
3330	netxen_config_indev_addr(adapter, netdev, event);
3331
3332	list_for_each_entry_safe(pos, tmp_pos, &adapter->ip_list, list) {
3333		netxen_config_ipaddr(adapter, pos->ip_addr, ip_event);
3334	}
3335}
3336
3337static inline bool
3338netxen_config_checkdev(struct net_device *dev)
3339{
3340	struct netxen_adapter *adapter;
3341
3342	if (!is_netxen_netdev(dev))
3343		return false;
3344	adapter = netdev_priv(dev);
3345	if (!adapter)
3346		return false;
3347	if (!netxen_destip_supported(adapter))
3348		return false;
3349	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
3350		return false;
3351
3352	return true;
3353}
3354
3355/**
3356 * netxen_config_master - configure addresses based on master
3357 * @dev: netxen device
3358 * @event: netdev event
3359 */
3360static void netxen_config_master(struct net_device *dev, unsigned long event)
3361{
3362	struct net_device *master, *slave;
3363	struct netxen_adapter *adapter = netdev_priv(dev);
3364
3365	rcu_read_lock();
3366	master = netdev_master_upper_dev_get_rcu(dev);
3367	/*
3368	 * This is the case where the netxen nic is being
3369	 * enslaved and is dev_open()ed in bond_enslave()
3370	 * Now we should program the bond's (and its vlans')
3371	 * addresses in the netxen NIC.
3372	 */
3373	if (master && netif_is_bond_master(master) &&
3374	    !netif_is_bond_slave(dev)) {
3375		netxen_config_indev_addr(adapter, master, event);
3376		for_each_netdev_rcu(&init_net, slave)
3377			if (is_vlan_dev(slave) &&
3378			    vlan_dev_real_dev(slave) == master)
3379				netxen_config_indev_addr(adapter, slave, event);
3380	}
3381	rcu_read_unlock();
3382	/*
3383	 * This is the case where the netxen nic is being
3384	 * released and is dev_close()ed in bond_release()
3385	 * just before IFF_BONDING is stripped.
3386	 */
3387	if (!master && dev->priv_flags & IFF_BONDING)
3388		netxen_free_ip_list(adapter, true);
3389}
3390
3391static int netxen_netdev_event(struct notifier_block *this,
3392				 unsigned long event, void *ptr)
3393{
3394	struct netxen_adapter *adapter;
3395	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3396	struct net_device *orig_dev = dev;
3397	struct net_device *slave;
3398
3399recheck:
3400	if (dev == NULL)
3401		goto done;
3402
3403	if (is_vlan_dev(dev)) {
3404		dev = vlan_dev_real_dev(dev);
3405		goto recheck;
3406	}
3407	if (event == NETDEV_UP || event == NETDEV_DOWN) {
3408		/* If this is a bonding device, look for netxen-based slaves*/
3409		if (netif_is_bond_master(dev)) {
3410			rcu_read_lock();
3411			for_each_netdev_in_bond_rcu(dev, slave) {
3412				if (!netxen_config_checkdev(slave))
3413					continue;
3414				adapter = netdev_priv(slave);
3415				netxen_config_indev_addr(adapter,
3416							 orig_dev, event);
3417			}
3418			rcu_read_unlock();
3419		} else {
3420			if (!netxen_config_checkdev(dev))
3421				goto done;
3422			adapter = netdev_priv(dev);
3423			/* Act only if the actual netxen is the target */
3424			if (orig_dev == dev)
3425				netxen_config_master(dev, event);
3426			netxen_config_indev_addr(adapter, orig_dev, event);
3427		}
3428	}
3429done:
3430	return NOTIFY_DONE;
3431}
3432
3433static int
3434netxen_inetaddr_event(struct notifier_block *this,
3435		unsigned long event, void *ptr)
3436{
3437	struct netxen_adapter *adapter;
3438	struct net_device *dev, *slave;
3439	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
3440	unsigned long ip_event;
3441
3442	dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
3443	ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
3444recheck:
3445	if (dev == NULL)
3446		goto done;
3447
3448	if (is_vlan_dev(dev)) {
3449		dev = vlan_dev_real_dev(dev);
3450		goto recheck;
3451	}
3452	if (event == NETDEV_UP || event == NETDEV_DOWN) {
3453		/* If this is a bonding device, look for netxen-based slaves*/
3454		if (netif_is_bond_master(dev)) {
3455			rcu_read_lock();
3456			for_each_netdev_in_bond_rcu(dev, slave) {
3457				if (!netxen_config_checkdev(slave))
3458					continue;
3459				adapter = netdev_priv(slave);
3460				netxen_list_config_ip(adapter, ifa, ip_event);
3461			}
3462			rcu_read_unlock();
3463		} else {
3464			if (!netxen_config_checkdev(dev))
3465				goto done;
3466			adapter = netdev_priv(dev);
3467			netxen_list_config_ip(adapter, ifa, ip_event);
3468		}
3469	}
3470done:
3471	return NOTIFY_DONE;
3472}
3473
3474static struct notifier_block	netxen_netdev_cb = {
3475	.notifier_call = netxen_netdev_event,
3476};
3477
3478static struct notifier_block netxen_inetaddr_cb = {
3479	.notifier_call = netxen_inetaddr_event,
3480};
3481#else
3482static void
3483netxen_restore_indev_addr(struct net_device *dev, unsigned long event)
3484{ }
3485static void
3486netxen_free_ip_list(struct netxen_adapter *adapter, bool master)
3487{ }
3488#endif
3489
3490static const struct pci_error_handlers netxen_err_handler = {
3491	.error_detected = netxen_io_error_detected,
3492	.slot_reset = netxen_io_slot_reset,
3493	.resume = netxen_io_resume,
3494};
3495
 
 
 
 
3496static struct pci_driver netxen_driver = {
3497	.name = netxen_nic_driver_name,
3498	.id_table = netxen_pci_tbl,
3499	.probe = netxen_nic_probe,
3500	.remove = netxen_nic_remove,
3501#ifdef CONFIG_PM
3502	.suspend = netxen_nic_suspend,
3503	.resume = netxen_nic_resume,
3504#endif
3505	.shutdown = netxen_nic_shutdown,
3506	.err_handler = &netxen_err_handler
3507};
3508
3509static int __init netxen_init_module(void)
3510{
3511	printk(KERN_INFO "%s\n", netxen_nic_driver_string);
3512
3513#ifdef CONFIG_INET
3514	register_netdevice_notifier(&netxen_netdev_cb);
3515	register_inetaddr_notifier(&netxen_inetaddr_cb);
3516#endif
3517	return pci_register_driver(&netxen_driver);
3518}
3519
3520module_init(netxen_init_module);
3521
3522static void __exit netxen_exit_module(void)
3523{
3524	pci_unregister_driver(&netxen_driver);
3525
3526#ifdef CONFIG_INET
3527	unregister_inetaddr_notifier(&netxen_inetaddr_cb);
3528	unregister_netdevice_notifier(&netxen_netdev_cb);
3529#endif
3530}
3531
3532module_exit(netxen_exit_module);
v6.2
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Copyright (C) 2003 - 2009 NetXen, Inc.
   4 * Copyright (C) 2009 - QLogic Corporation.
   5 * All rights reserved.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   6 */
   7
   8#include <linux/slab.h>
   9#include <linux/vmalloc.h>
  10#include <linux/interrupt.h>
  11#include "netxen_nic_hw.h"
  12
  13#include "netxen_nic.h"
  14
  15#include <linux/dma-mapping.h>
  16#include <linux/if_vlan.h>
  17#include <net/ip.h>
  18#include <linux/ipv6.h>
  19#include <linux/inetdevice.h>
  20#include <linux/sysfs.h>
  21#include <linux/aer.h>
  22
  23MODULE_DESCRIPTION("QLogic/NetXen (1/10) GbE Intelligent Ethernet Driver");
  24MODULE_LICENSE("GPL");
  25MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
  26MODULE_FIRMWARE(NX_UNIFIED_ROMIMAGE_NAME);
  27
  28char netxen_nic_driver_name[] = "netxen_nic";
  29static char netxen_nic_driver_string[] = "QLogic/NetXen Network Driver v"
  30    NETXEN_NIC_LINUX_VERSIONID;
  31
  32static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
  33
  34/* Default to restricted 1G auto-neg mode */
  35static int wol_port_mode = 5;
  36
  37static int use_msi = 1;
  38
  39static int use_msi_x = 1;
  40
  41static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
  42module_param(auto_fw_reset, int, 0644);
  43MODULE_PARM_DESC(auto_fw_reset,"Auto firmware reset (0=disabled, 1=enabled");
  44
  45static int netxen_nic_probe(struct pci_dev *pdev,
  46		const struct pci_device_id *ent);
  47static void netxen_nic_remove(struct pci_dev *pdev);
  48static int netxen_nic_open(struct net_device *netdev);
  49static int netxen_nic_close(struct net_device *netdev);
  50static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
  51					       struct net_device *);
  52static void netxen_tx_timeout(struct net_device *netdev, unsigned int txqueue);
  53static void netxen_tx_timeout_task(struct work_struct *work);
  54static void netxen_fw_poll_work(struct work_struct *work);
  55static void netxen_schedule_work(struct netxen_adapter *adapter,
  56		work_func_t func, int delay);
  57static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
  58static int netxen_nic_poll(struct napi_struct *napi, int budget);
 
 
 
  59
  60static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
  61static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
  62static void netxen_create_diag_entries(struct netxen_adapter *adapter);
  63static void netxen_remove_diag_entries(struct netxen_adapter *adapter);
  64static int nx_dev_request_aer(struct netxen_adapter *adapter);
  65static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
  66static int netxen_can_start_firmware(struct netxen_adapter *adapter);
  67
  68static irqreturn_t netxen_intr(int irq, void *data);
  69static irqreturn_t netxen_msi_intr(int irq, void *data);
  70static irqreturn_t netxen_msix_intr(int irq, void *data);
  71
  72static void netxen_free_ip_list(struct netxen_adapter *, bool);
  73static void netxen_restore_indev_addr(struct net_device *dev, unsigned long);
  74static void netxen_nic_get_stats(struct net_device *dev,
  75				 struct rtnl_link_stats64 *stats);
  76static int netxen_nic_set_mac(struct net_device *netdev, void *p);
  77
  78/*  PCI Device ID Table  */
  79#define ENTRY(device) \
  80	{PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
  81	.class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
  82
  83static const struct pci_device_id netxen_pci_tbl[] = {
  84	ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
  85	ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
  86	ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
  87	ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
  88	ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
  89	ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
  90	ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
  91	ENTRY(PCI_DEVICE_ID_NX3031),
  92	{0,}
  93};
  94
  95MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
  96
  97static uint32_t crb_cmd_producer[4] = {
  98	CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
  99	CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
 100};
 101
 102void
 103netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
 104		struct nx_host_tx_ring *tx_ring)
 105{
 106	NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
 107}
 108
 109static uint32_t crb_cmd_consumer[4] = {
 110	CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
 111	CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
 112};
 113
 114static inline void
 115netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
 116		struct nx_host_tx_ring *tx_ring)
 117{
 118	NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
 119}
 120
 121static uint32_t msi_tgt_status[8] = {
 122	ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
 123	ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
 124	ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
 125	ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
 126};
 127
 128static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
 129
 130static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
 131{
 132	struct netxen_adapter *adapter = sds_ring->adapter;
 133
 134	NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
 135}
 136
 137static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
 138{
 139	struct netxen_adapter *adapter = sds_ring->adapter;
 140
 141	NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
 142
 143	if (!NETXEN_IS_MSI_FAMILY(adapter))
 144		NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
 145}
 146
 147static int
 148netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
 149{
 150	int size = sizeof(struct nx_host_sds_ring) * count;
 151
 152	recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
 153
 154	return recv_ctx->sds_rings == NULL;
 155}
 156
 157static void
 158netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
 159{
 160	kfree(recv_ctx->sds_rings);
 161	recv_ctx->sds_rings = NULL;
 162}
 163
 164static int
 165netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
 166{
 167	int ring;
 168	struct nx_host_sds_ring *sds_ring;
 169	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 170
 171	if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
 172		return -ENOMEM;
 173
 174	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 175		sds_ring = &recv_ctx->sds_rings[ring];
 176		netif_napi_add(netdev, &sds_ring->napi, netxen_nic_poll);
 
 177	}
 178
 179	return 0;
 180}
 181
 182static void
 183netxen_napi_del(struct netxen_adapter *adapter)
 184{
 185	int ring;
 186	struct nx_host_sds_ring *sds_ring;
 187	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 188
 189	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 190		sds_ring = &recv_ctx->sds_rings[ring];
 191		netif_napi_del(&sds_ring->napi);
 192	}
 193
 194	netxen_free_sds_rings(&adapter->recv_ctx);
 195}
 196
 197static void
 198netxen_napi_enable(struct netxen_adapter *adapter)
 199{
 200	int ring;
 201	struct nx_host_sds_ring *sds_ring;
 202	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 203
 204	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 205		sds_ring = &recv_ctx->sds_rings[ring];
 206		napi_enable(&sds_ring->napi);
 207		netxen_nic_enable_int(sds_ring);
 208	}
 209}
 210
 211static void
 212netxen_napi_disable(struct netxen_adapter *adapter)
 213{
 214	int ring;
 215	struct nx_host_sds_ring *sds_ring;
 216	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 217
 218	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 219		sds_ring = &recv_ctx->sds_rings[ring];
 220		netxen_nic_disable_int(sds_ring);
 221		napi_synchronize(&sds_ring->napi);
 222		napi_disable(&sds_ring->napi);
 223	}
 224}
 225
 226static int nx_set_dma_mask(struct netxen_adapter *adapter)
 227{
 228	struct pci_dev *pdev = adapter->pdev;
 229	uint64_t mask, cmask;
 230
 231	adapter->pci_using_dac = 0;
 232
 233	mask = DMA_BIT_MASK(32);
 234	cmask = DMA_BIT_MASK(32);
 235
 236	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
 237#ifndef CONFIG_IA64
 238		mask = DMA_BIT_MASK(35);
 239#endif
 240	} else {
 241		mask = DMA_BIT_MASK(39);
 242		cmask = mask;
 243	}
 244
 245	if (dma_set_mask(&pdev->dev, mask) == 0 &&
 246	    dma_set_coherent_mask(&pdev->dev, cmask) == 0) {
 247		adapter->pci_using_dac = 1;
 248		return 0;
 249	}
 250
 251	return -EIO;
 252}
 253
 254/* Update addressable range if firmware supports it */
 255static int
 256nx_update_dma_mask(struct netxen_adapter *adapter)
 257{
 258	int change, shift, err;
 259	uint64_t mask, old_mask, old_cmask;
 260	struct pci_dev *pdev = adapter->pdev;
 261
 262	change = 0;
 263
 264	shift = NXRD32(adapter, CRB_DMA_SHIFT);
 265	if (shift > 32)
 266		return 0;
 267
 268	if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
 269		change = 1;
 270	else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
 271		change = 1;
 272
 273	if (change) {
 274		old_mask = pdev->dma_mask;
 275		old_cmask = pdev->dev.coherent_dma_mask;
 276
 277		mask = DMA_BIT_MASK(32+shift);
 278
 279		err = dma_set_mask(&pdev->dev, mask);
 280		if (err)
 281			goto err_out;
 282
 283		if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 284
 285			err = dma_set_coherent_mask(&pdev->dev, mask);
 286			if (err)
 287				goto err_out;
 288		}
 289		dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
 290	}
 291
 292	return 0;
 293
 294err_out:
 295	dma_set_mask(&pdev->dev, old_mask);
 296	dma_set_coherent_mask(&pdev->dev, old_cmask);
 297	return err;
 298}
 299
 300static int
 301netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
 302{
 303	u32 val, timeout;
 304
 305	if (first_boot == 0x55555555) {
 306		/* This is the first boot after power up */
 307		NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
 308
 309		if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
 310			return 0;
 311
 312		/* PCI bus master workaround */
 313		first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
 314		if (!(first_boot & 0x4)) {
 315			first_boot |= 0x4;
 316			NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
 317			NXRD32(adapter, NETXEN_PCIE_REG(0x4));
 318		}
 319
 320		/* This is the first boot after power up */
 321		first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
 322		if (first_boot != 0x80000f) {
 323			/* clear the register for future unloads/loads */
 324			NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
 325			return -EIO;
 326		}
 327
 328		/* Start P2 boot loader */
 329		val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
 330		NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
 331		timeout = 0;
 332		do {
 333			msleep(1);
 334			val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
 335
 336			if (++timeout > 5000)
 337				return -EIO;
 338
 339		} while (val == NETXEN_BDINFO_MAGIC);
 340	}
 341	return 0;
 342}
 343
 344static void netxen_set_port_mode(struct netxen_adapter *adapter)
 345{
 346	u32 val, data;
 347
 348	val = adapter->ahw.board_type;
 349	if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
 350		(val == NETXEN_BRDTYPE_P3_XG_LOM)) {
 351		if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
 352			data = NETXEN_PORT_MODE_802_3_AP;
 353			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 354		} else if (port_mode == NETXEN_PORT_MODE_XG) {
 355			data = NETXEN_PORT_MODE_XG;
 356			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 357		} else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
 358			data = NETXEN_PORT_MODE_AUTO_NEG_1G;
 359			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 360		} else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
 361			data = NETXEN_PORT_MODE_AUTO_NEG_XG;
 362			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 363		} else {
 364			data = NETXEN_PORT_MODE_AUTO_NEG;
 365			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 366		}
 367
 368		if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
 369			(wol_port_mode != NETXEN_PORT_MODE_XG) &&
 370			(wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
 371			(wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
 372			wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
 373		}
 374		NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
 375	}
 376}
 377
 378#define PCI_CAP_ID_GEN  0x10
 379
 380static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
 381{
 382	u32 pdevfuncsave;
 383	u32 c8c9value = 0;
 384	u32 chicken = 0;
 385	u32 control = 0;
 386	int i, pos;
 387	struct pci_dev *pdev;
 388
 389	pdev = adapter->pdev;
 390
 391	chicken = NXRD32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3));
 392	/* clear chicken3.25:24 */
 393	chicken &= 0xFCFFFFFF;
 394	/*
 395	 * if gen1 and B0, set F1020 - if gen 2, do nothing
 396	 * if gen2 set to F1000
 397	 */
 398	pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
 399	if (pos == 0xC0) {
 400		pci_read_config_dword(pdev, pos + 0x10, &control);
 401		if ((control & 0x000F0000) != 0x00020000) {
 402			/*  set chicken3.24 if gen1 */
 403			chicken |= 0x01000000;
 404		}
 405		dev_info(&adapter->pdev->dev, "Gen2 strapping detected\n");
 406		c8c9value = 0xF1000;
 407	} else {
 408		/* set chicken3.24 if gen1 */
 409		chicken |= 0x01000000;
 410		dev_info(&adapter->pdev->dev, "Gen1 strapping detected\n");
 411		if (adapter->ahw.revision_id == NX_P3_B0)
 412			c8c9value = 0xF1020;
 413		else
 414			c8c9value = 0;
 415	}
 416
 417	NXWR32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3), chicken);
 418
 419	if (!c8c9value)
 420		return;
 421
 422	pdevfuncsave = pdev->devfn;
 423	if (pdevfuncsave & 0x07)
 424		return;
 425
 426	for (i = 0; i < 8; i++) {
 427		pci_read_config_dword(pdev, pos + 8, &control);
 428		pci_read_config_dword(pdev, pos + 8, &control);
 429		pci_write_config_dword(pdev, pos + 8, c8c9value);
 430		pdev->devfn++;
 431	}
 432	pdev->devfn = pdevfuncsave;
 433}
 434
 435static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
 436{
 437	u32 control;
 438
 439	if (pdev->msix_cap) {
 440		pci_read_config_dword(pdev, pdev->msix_cap, &control);
 441		if (enable)
 442			control |= PCI_MSIX_FLAGS_ENABLE;
 443		else
 444			control = 0;
 445		pci_write_config_dword(pdev, pdev->msix_cap, control);
 446	}
 447}
 448
 449static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
 450{
 451	int i;
 452
 453	for (i = 0; i < count; i++)
 454		adapter->msix_entries[i].entry = i;
 455}
 456
 457static int
 458netxen_read_mac_addr(struct netxen_adapter *adapter)
 459{
 460	int i;
 461	unsigned char *p;
 462	u64 mac_addr;
 463	struct net_device *netdev = adapter->netdev;
 464	struct pci_dev *pdev = adapter->pdev;
 465	u8 addr[ETH_ALEN];
 466
 467	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 468		if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
 469			return -EIO;
 470	} else {
 471		if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
 472			return -EIO;
 473	}
 474
 475	p = (unsigned char *)&mac_addr;
 476	for (i = 0; i < 6; i++)
 477		addr[i] = *(p + 5 - i);
 478	eth_hw_addr_set(netdev, addr);
 479
 480	memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
 481
 482	/* set station address */
 483
 484	if (!is_valid_ether_addr(netdev->dev_addr))
 485		dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
 486
 487	return 0;
 488}
 489
 490static int netxen_nic_set_mac(struct net_device *netdev, void *p)
 491{
 492	struct netxen_adapter *adapter = netdev_priv(netdev);
 493	struct sockaddr *addr = p;
 494
 495	if (!is_valid_ether_addr(addr->sa_data))
 496		return -EADDRNOTAVAIL;
 497
 498	if (netif_running(netdev)) {
 499		netif_device_detach(netdev);
 500		netxen_napi_disable(adapter);
 501	}
 502
 503	memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
 504	eth_hw_addr_set(netdev, addr->sa_data);
 505	adapter->macaddr_set(adapter, addr->sa_data);
 506
 507	if (netif_running(netdev)) {
 508		netif_device_attach(netdev);
 509		netxen_napi_enable(adapter);
 510	}
 511	return 0;
 512}
 513
 514static void netxen_set_multicast_list(struct net_device *dev)
 515{
 516	struct netxen_adapter *adapter = netdev_priv(dev);
 517
 518	adapter->set_multi(dev);
 519}
 520
 521static netdev_features_t netxen_fix_features(struct net_device *dev,
 522	netdev_features_t features)
 523{
 524	if (!(features & NETIF_F_RXCSUM)) {
 525		netdev_info(dev, "disabling LRO as RXCSUM is off\n");
 526
 527		features &= ~NETIF_F_LRO;
 528	}
 529
 530	return features;
 531}
 532
 533static int netxen_set_features(struct net_device *dev,
 534	netdev_features_t features)
 535{
 536	struct netxen_adapter *adapter = netdev_priv(dev);
 537	int hw_lro;
 538
 539	if (!((dev->features ^ features) & NETIF_F_LRO))
 540		return 0;
 541
 542	hw_lro = (features & NETIF_F_LRO) ? NETXEN_NIC_LRO_ENABLED
 543	         : NETXEN_NIC_LRO_DISABLED;
 544
 545	if (netxen_config_hw_lro(adapter, hw_lro))
 546		return -EIO;
 547
 548	if (!(features & NETIF_F_LRO) && netxen_send_lro_cleanup(adapter))
 549		return -EIO;
 550
 551	return 0;
 552}
 553
 554static const struct net_device_ops netxen_netdev_ops = {
 555	.ndo_open	   = netxen_nic_open,
 556	.ndo_stop	   = netxen_nic_close,
 557	.ndo_start_xmit    = netxen_nic_xmit_frame,
 558	.ndo_get_stats64   = netxen_nic_get_stats,
 559	.ndo_validate_addr = eth_validate_addr,
 560	.ndo_set_rx_mode   = netxen_set_multicast_list,
 561	.ndo_set_mac_address    = netxen_nic_set_mac,
 562	.ndo_change_mtu	   = netxen_nic_change_mtu,
 563	.ndo_tx_timeout	   = netxen_tx_timeout,
 564	.ndo_fix_features = netxen_fix_features,
 565	.ndo_set_features = netxen_set_features,
 
 
 
 566};
 567
 
 
 
 
 
 568static inline void netxen_set_interrupt_mode(struct netxen_adapter *adapter,
 569					     u32 mode)
 570{
 571	NXWR32(adapter, NETXEN_INTR_MODE_REG, mode);
 572}
 573
 574static inline u32 netxen_get_interrupt_mode(struct netxen_adapter *adapter)
 575{
 576	return NXRD32(adapter, NETXEN_INTR_MODE_REG);
 577}
 578
 579static void
 580netxen_initialize_interrupt_registers(struct netxen_adapter *adapter)
 581{
 582	struct netxen_legacy_intr_set *legacy_intrp;
 583	u32 tgt_status_reg, int_state_reg;
 584
 585	if (adapter->ahw.revision_id >= NX_P3_B0)
 586		legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
 587	else
 588		legacy_intrp = &legacy_intr[0];
 589
 590	tgt_status_reg = legacy_intrp->tgt_status_reg;
 591	int_state_reg = ISR_INT_STATE_REG;
 592
 593	adapter->int_vec_bit = legacy_intrp->int_vec_bit;
 594	adapter->tgt_status_reg = netxen_get_ioaddr(adapter, tgt_status_reg);
 595	adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
 596						  legacy_intrp->tgt_mask_reg);
 597	adapter->pci_int_reg = netxen_get_ioaddr(adapter,
 598						 legacy_intrp->pci_int_reg);
 599	adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
 600
 601	if (adapter->ahw.revision_id >= NX_P3_B1)
 602		adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
 603							       int_state_reg);
 604	else
 605		adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
 606							       CRB_INT_VECTOR);
 607}
 608
 609static int netxen_setup_msi_interrupts(struct netxen_adapter *adapter,
 610				       int num_msix)
 611{
 612	struct pci_dev *pdev = adapter->pdev;
 613	u32 value;
 614	int err;
 615
 616	if (adapter->msix_supported) {
 617		netxen_init_msix_entries(adapter, num_msix);
 618		err = pci_enable_msix_range(pdev, adapter->msix_entries,
 619					    num_msix, num_msix);
 620		if (err > 0) {
 621			adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
 622			netxen_set_msix_bit(pdev, 1);
 623
 624			if (adapter->rss_supported)
 625				adapter->max_sds_rings = num_msix;
 626
 627			dev_info(&pdev->dev, "using msi-x interrupts\n");
 628			return 0;
 629		}
 630		/* fall through for msi */
 631	}
 632
 633	if (use_msi && !pci_enable_msi(pdev)) {
 634		value = msi_tgt_status[adapter->ahw.pci_func];
 635		adapter->flags |= NETXEN_NIC_MSI_ENABLED;
 636		adapter->tgt_status_reg = netxen_get_ioaddr(adapter, value);
 637		adapter->msix_entries[0].vector = pdev->irq;
 638		dev_info(&pdev->dev, "using msi interrupts\n");
 639		return 0;
 640	}
 641
 642	dev_err(&pdev->dev, "Failed to acquire MSI-X/MSI interrupt vector\n");
 643	return -EIO;
 644}
 645
 646static int netxen_setup_intr(struct netxen_adapter *adapter)
 647{
 648	struct pci_dev *pdev = adapter->pdev;
 649	int num_msix;
 650
 651	if (adapter->rss_supported)
 652		num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
 653			    MSIX_ENTRIES_PER_ADAPTER : 2;
 654	else
 655		num_msix = 1;
 656
 657	adapter->max_sds_rings = 1;
 658	adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
 659
 660	netxen_initialize_interrupt_registers(adapter);
 661	netxen_set_msix_bit(pdev, 0);
 662
 663	if (adapter->portnum == 0) {
 664		if (!netxen_setup_msi_interrupts(adapter, num_msix))
 665			netxen_set_interrupt_mode(adapter, NETXEN_MSI_MODE);
 666		else
 667			netxen_set_interrupt_mode(adapter, NETXEN_INTX_MODE);
 668	} else {
 669		if (netxen_get_interrupt_mode(adapter) == NETXEN_MSI_MODE &&
 670		    netxen_setup_msi_interrupts(adapter, num_msix)) {
 671			dev_err(&pdev->dev, "Co-existence of MSI-X/MSI and INTx interrupts is not supported\n");
 672			return -EIO;
 673		}
 674	}
 675
 676	if (!NETXEN_IS_MSI_FAMILY(adapter)) {
 677		adapter->msix_entries[0].vector = pdev->irq;
 678		dev_info(&pdev->dev, "using legacy interrupts\n");
 679	}
 680	return 0;
 681}
 682
 683static void
 684netxen_teardown_intr(struct netxen_adapter *adapter)
 685{
 686	if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
 687		pci_disable_msix(adapter->pdev);
 688	if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
 689		pci_disable_msi(adapter->pdev);
 690}
 691
 692static void
 693netxen_cleanup_pci_map(struct netxen_adapter *adapter)
 694{
 695	if (adapter->ahw.db_base != NULL)
 696		iounmap(adapter->ahw.db_base);
 697	if (adapter->ahw.pci_base0 != NULL)
 698		iounmap(adapter->ahw.pci_base0);
 699	if (adapter->ahw.pci_base1 != NULL)
 700		iounmap(adapter->ahw.pci_base1);
 701	if (adapter->ahw.pci_base2 != NULL)
 702		iounmap(adapter->ahw.pci_base2);
 703}
 704
 705static int
 706netxen_setup_pci_map(struct netxen_adapter *adapter)
 707{
 708	void __iomem *db_ptr = NULL;
 709
 710	resource_size_t mem_base, db_base;
 711	unsigned long mem_len, db_len = 0;
 712
 713	struct pci_dev *pdev = adapter->pdev;
 714	int pci_func = adapter->ahw.pci_func;
 715	struct netxen_hardware_context *ahw = &adapter->ahw;
 716
 717	int err = 0;
 718
 719	/*
 720	 * Set the CRB window to invalid. If any register in window 0 is
 721	 * accessed it should set the window to 0 and then reset it to 1.
 722	 */
 723	adapter->ahw.crb_win = -1;
 724	adapter->ahw.ocm_win = -1;
 725
 726	/* remap phys address */
 727	mem_base = pci_resource_start(pdev, 0);	/* 0 is for BAR 0 */
 728	mem_len = pci_resource_len(pdev, 0);
 729
 730	/* 128 Meg of memory */
 731	if (mem_len == NETXEN_PCI_128MB_SIZE) {
 732
 733		ahw->pci_base0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
 734		ahw->pci_base1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
 735				SECOND_PAGE_GROUP_SIZE);
 736		ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
 737				THIRD_PAGE_GROUP_SIZE);
 738		if (ahw->pci_base0 == NULL || ahw->pci_base1 == NULL ||
 739						ahw->pci_base2 == NULL) {
 740			dev_err(&pdev->dev, "failed to map PCI bar 0\n");
 741			err = -EIO;
 742			goto err_out;
 743		}
 744
 745		ahw->pci_len0 = FIRST_PAGE_GROUP_SIZE;
 746
 747	} else if (mem_len == NETXEN_PCI_32MB_SIZE) {
 748
 749		ahw->pci_base1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
 750		ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
 751			SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
 752		if (ahw->pci_base1 == NULL || ahw->pci_base2 == NULL) {
 753			dev_err(&pdev->dev, "failed to map PCI bar 0\n");
 754			err = -EIO;
 755			goto err_out;
 756		}
 757
 758	} else if (mem_len == NETXEN_PCI_2MB_SIZE) {
 759
 760		ahw->pci_base0 = pci_ioremap_bar(pdev, 0);
 761		if (ahw->pci_base0 == NULL) {
 762			dev_err(&pdev->dev, "failed to map PCI bar 0\n");
 763			return -EIO;
 764		}
 765		ahw->pci_len0 = mem_len;
 766	} else {
 767		return -EIO;
 768	}
 769
 770	netxen_setup_hwops(adapter);
 771
 772	dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
 773
 774	if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
 775		adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
 776			NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
 777
 778	} else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 779		adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
 780			NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func)));
 781	}
 782
 783	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
 784		goto skip_doorbell;
 785
 786	db_base = pci_resource_start(pdev, 4);	/* doorbell is on bar 4 */
 787	db_len = pci_resource_len(pdev, 4);
 788
 789	if (db_len == 0) {
 790		printk(KERN_ERR "%s: doorbell is disabled\n",
 791				netxen_nic_driver_name);
 792		err = -EIO;
 793		goto err_out;
 794	}
 795
 796	db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
 797	if (!db_ptr) {
 798		printk(KERN_ERR "%s: Failed to allocate doorbell map.",
 799				netxen_nic_driver_name);
 800		err = -EIO;
 801		goto err_out;
 802	}
 803
 804skip_doorbell:
 805	adapter->ahw.db_base = db_ptr;
 806	adapter->ahw.db_len = db_len;
 807	return 0;
 808
 809err_out:
 810	netxen_cleanup_pci_map(adapter);
 811	return err;
 812}
 813
 814static void
 815netxen_check_options(struct netxen_adapter *adapter)
 816{
 817	u32 fw_major, fw_minor, fw_build, prev_fw_version;
 818	char brd_name[NETXEN_MAX_SHORT_NAME];
 819	char serial_num[32];
 820	int i, offset, val, err;
 821	__le32 *ptr32;
 822	struct pci_dev *pdev = adapter->pdev;
 823
 824	adapter->driver_mismatch = 0;
 825
 826	ptr32 = (__le32 *)&serial_num;
 827	offset = NX_FW_SERIAL_NUM_OFFSET;
 828	for (i = 0; i < 8; i++) {
 829		err = netxen_rom_fast_read(adapter, offset, &val);
 830		if (err) {
 831			dev_err(&pdev->dev, "error reading board info\n");
 832			adapter->driver_mismatch = 1;
 833			return;
 834		}
 835		ptr32[i] = cpu_to_le32(val);
 836		offset += sizeof(u32);
 837	}
 838
 839	fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
 840	fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
 841	fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
 842	prev_fw_version = adapter->fw_version;
 843	adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
 844
 845	/* Get FW Mini Coredump template and store it */
 846	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 847		if (adapter->mdump.md_template == NULL ||
 848				adapter->fw_version > prev_fw_version) {
 849			kfree(adapter->mdump.md_template);
 850			adapter->mdump.md_template = NULL;
 851			err = netxen_setup_minidump(adapter);
 852			if (err)
 853				dev_err(&adapter->pdev->dev,
 854				"Failed to setup minidump rcode = %d\n", err);
 855		}
 856	}
 857
 858	if (adapter->portnum == 0) {
 859		if (netxen_nic_get_brd_name_by_type(adapter->ahw.board_type,
 860						    brd_name))
 861			strcpy(serial_num, "Unknown");
 862
 863		pr_info("%s: %s Board S/N %s  Chip rev 0x%x\n",
 864				module_name(THIS_MODULE),
 865				brd_name, serial_num, adapter->ahw.revision_id);
 866	}
 867
 868	if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
 869		adapter->driver_mismatch = 1;
 870		dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
 871				fw_major, fw_minor, fw_build);
 872		return;
 873	}
 874
 875	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 876		i = NXRD32(adapter, NETXEN_SRE_MISC);
 877		adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
 878	}
 879
 880	dev_info(&pdev->dev, "Driver v%s, firmware v%d.%d.%d [%s]\n",
 881		 NETXEN_NIC_LINUX_VERSIONID, fw_major, fw_minor, fw_build,
 882		 adapter->ahw.cut_through ? "cut-through" : "legacy");
 883
 884	if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
 885		adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
 886
 887	if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
 888		adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
 889		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
 890	} else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
 891		adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
 892		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
 893	}
 894
 895	adapter->msix_supported = 0;
 896	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 897		adapter->msix_supported = !!use_msi_x;
 898		adapter->rss_supported = !!use_msi_x;
 899	} else {
 900		u32 flashed_ver = 0;
 901		netxen_rom_fast_read(adapter,
 902				NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
 903		flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
 904
 905		if (flashed_ver >= NETXEN_VERSION_CODE(3, 4, 336)) {
 906			switch (adapter->ahw.board_type) {
 907			case NETXEN_BRDTYPE_P2_SB31_10G:
 908			case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
 909				adapter->msix_supported = !!use_msi_x;
 910				adapter->rss_supported = !!use_msi_x;
 911				break;
 912			default:
 913				break;
 914			}
 915		}
 916	}
 917
 918	adapter->num_txd = MAX_CMD_DESCRIPTORS;
 919
 920	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
 921		adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
 922		adapter->max_rds_rings = 3;
 923	} else {
 924		adapter->num_lro_rxd = 0;
 925		adapter->max_rds_rings = 2;
 926	}
 927}
 928
 929static int
 930netxen_start_firmware(struct netxen_adapter *adapter)
 931{
 932	int val, err, first_boot;
 933	struct pci_dev *pdev = adapter->pdev;
 934
 935	/* required for NX2031 dummy dma */
 936	err = nx_set_dma_mask(adapter);
 937	if (err)
 938		return err;
 939
 940	err = netxen_can_start_firmware(adapter);
 941
 942	if (err < 0)
 943		return err;
 944
 945	if (!err)
 946		goto wait_init;
 947
 948	first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
 949
 950	err = netxen_check_hw_init(adapter, first_boot);
 951	if (err) {
 952		dev_err(&pdev->dev, "error in init HW init sequence\n");
 953		return err;
 954	}
 955
 956	netxen_request_firmware(adapter);
 957
 958	err = netxen_need_fw_reset(adapter);
 959	if (err < 0)
 960		goto err_out;
 961	if (err == 0)
 962		goto pcie_strap_init;
 963
 964	if (first_boot != 0x55555555) {
 965		NXWR32(adapter, CRB_CMDPEG_STATE, 0);
 966		netxen_pinit_from_rom(adapter);
 967		msleep(1);
 968	}
 969
 970	NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
 971	NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
 972	NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
 973
 974	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
 975		netxen_set_port_mode(adapter);
 976
 977	err = netxen_load_firmware(adapter);
 978	if (err)
 979		goto err_out;
 980
 981	netxen_release_firmware(adapter);
 982
 983	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
 984
 985		/* Initialize multicast addr pool owners */
 986		val = 0x7654;
 987		if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
 988			val |= 0x0f000000;
 989		NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
 990
 991	}
 992
 993	err = netxen_init_dummy_dma(adapter);
 994	if (err)
 995		goto err_out;
 996
 997	/*
 998	 * Tell the hardware our version number.
 999	 */
1000	val = (_NETXEN_NIC_LINUX_MAJOR << 16)
1001		| ((_NETXEN_NIC_LINUX_MINOR << 8))
1002		| (_NETXEN_NIC_LINUX_SUBVERSION);
1003	NXWR32(adapter, CRB_DRIVER_VERSION, val);
1004
1005pcie_strap_init:
1006	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1007		netxen_pcie_strap_init(adapter);
1008
1009wait_init:
1010	/* Handshake with the card before we register the devices. */
1011	err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
1012	if (err) {
1013		netxen_free_dummy_dma(adapter);
1014		goto err_out;
1015	}
1016
1017	NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
1018
1019	nx_update_dma_mask(adapter);
1020
1021	netxen_check_options(adapter);
1022
1023	adapter->need_fw_reset = 0;
1024
1025	/* fall through and release firmware */
1026
1027err_out:
1028	netxen_release_firmware(adapter);
1029	return err;
1030}
1031
1032static int
1033netxen_nic_request_irq(struct netxen_adapter *adapter)
1034{
1035	irq_handler_t handler;
1036	struct nx_host_sds_ring *sds_ring;
1037	int err, ring;
1038
1039	unsigned long flags = 0;
1040	struct net_device *netdev = adapter->netdev;
1041	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1042
1043	if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1044		handler = netxen_msix_intr;
1045	else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
1046		handler = netxen_msi_intr;
1047	else {
1048		flags |= IRQF_SHARED;
1049		handler = netxen_intr;
1050	}
1051	adapter->irq = netdev->irq;
1052
1053	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1054		sds_ring = &recv_ctx->sds_rings[ring];
1055		sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1056		err = request_irq(sds_ring->irq, handler,
1057				  flags, sds_ring->name, sds_ring);
1058		if (err)
1059			return err;
1060	}
1061
1062	return 0;
1063}
1064
1065static void
1066netxen_nic_free_irq(struct netxen_adapter *adapter)
1067{
1068	int ring;
1069	struct nx_host_sds_ring *sds_ring;
1070
1071	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1072
1073	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1074		sds_ring = &recv_ctx->sds_rings[ring];
1075		free_irq(sds_ring->irq, sds_ring);
1076	}
1077}
1078
1079static void
1080netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
1081{
1082	adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
1083	adapter->coal.normal.data.rx_time_us =
1084		NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
1085	adapter->coal.normal.data.rx_packets =
1086		NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
1087	adapter->coal.normal.data.tx_time_us =
1088		NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
1089	adapter->coal.normal.data.tx_packets =
1090		NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
1091}
1092
1093/* with rtnl_lock */
1094static int
1095__netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1096{
1097	int err;
1098
1099	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1100		return -EIO;
1101
1102	err = adapter->init_port(adapter, adapter->physical_port);
1103	if (err) {
1104		printk(KERN_ERR "%s: Failed to initialize port %d\n",
1105				netxen_nic_driver_name, adapter->portnum);
1106		return err;
1107	}
1108	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
1109		adapter->macaddr_set(adapter, adapter->mac_addr);
1110
1111	adapter->set_multi(netdev);
1112	adapter->set_mtu(adapter, netdev->mtu);
1113
1114	adapter->ahw.linkup = 0;
1115
1116	if (adapter->max_sds_rings > 1)
1117		netxen_config_rss(adapter, 1);
1118
1119	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1120		netxen_config_intr_coalesce(adapter);
1121
1122	if (netdev->features & NETIF_F_LRO)
1123		netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
1124
1125	netxen_napi_enable(adapter);
1126
1127	if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1128		netxen_linkevent_request(adapter, 1);
1129	else
1130		netxen_nic_set_link_parameters(adapter);
1131
1132	set_bit(__NX_DEV_UP, &adapter->state);
1133	return 0;
1134}
1135
1136/* Usage: During resume and firmware recovery module.*/
1137
1138static inline int
1139netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1140{
1141	int err = 0;
1142
1143	rtnl_lock();
1144	if (netif_running(netdev))
1145		err = __netxen_nic_up(adapter, netdev);
1146	rtnl_unlock();
1147
1148	return err;
1149}
1150
1151/* with rtnl_lock */
1152static void
1153__netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1154{
1155	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1156		return;
1157
1158	if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state))
1159		return;
1160
1161	smp_mb();
1162	netif_carrier_off(netdev);
1163	netif_tx_disable(netdev);
1164
1165	if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1166		netxen_linkevent_request(adapter, 0);
1167
1168	if (adapter->stop_port)
1169		adapter->stop_port(adapter);
1170
1171	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1172		netxen_p3_free_mac_list(adapter);
1173
1174	adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1175
1176	netxen_napi_disable(adapter);
1177
1178	netxen_release_tx_buffers(adapter);
1179}
1180
1181/* Usage: During suspend and firmware recovery module */
1182
1183static inline void
1184netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1185{
1186	rtnl_lock();
1187	if (netif_running(netdev))
1188		__netxen_nic_down(adapter, netdev);
1189	rtnl_unlock();
1190
1191}
1192
1193static int
1194netxen_nic_attach(struct netxen_adapter *adapter)
1195{
1196	struct net_device *netdev = adapter->netdev;
1197	struct pci_dev *pdev = adapter->pdev;
1198	int err, ring;
1199	struct nx_host_rds_ring *rds_ring;
1200	struct nx_host_tx_ring *tx_ring;
1201	u32 capab2;
1202
1203	if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1204		return 0;
1205
1206	err = netxen_init_firmware(adapter);
1207	if (err)
1208		return err;
1209
1210	adapter->flags &= ~NETXEN_FW_MSS_CAP;
1211	if (adapter->capabilities & NX_FW_CAPABILITY_MORE_CAPS) {
1212		capab2 = NXRD32(adapter, CRB_FW_CAPABILITIES_2);
1213		if (capab2 & NX_FW_CAPABILITY_2_LRO_MAX_TCP_SEG)
1214			adapter->flags |= NETXEN_FW_MSS_CAP;
1215	}
1216
1217	err = netxen_napi_add(adapter, netdev);
1218	if (err)
1219		return err;
1220
1221	err = netxen_alloc_sw_resources(adapter);
1222	if (err) {
1223		printk(KERN_ERR "%s: Error in setting sw resources\n",
1224				netdev->name);
1225		return err;
1226	}
1227
1228	err = netxen_alloc_hw_resources(adapter);
1229	if (err) {
1230		printk(KERN_ERR "%s: Error in setting hw resources\n",
1231				netdev->name);
1232		goto err_out_free_sw;
1233	}
1234
1235	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1236		tx_ring = adapter->tx_ring;
1237		tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1238				crb_cmd_producer[adapter->portnum]);
1239		tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1240				crb_cmd_consumer[adapter->portnum]);
1241
1242		tx_ring->producer = 0;
1243		tx_ring->sw_consumer = 0;
1244
1245		netxen_nic_update_cmd_producer(adapter, tx_ring);
1246		netxen_nic_update_cmd_consumer(adapter, tx_ring);
1247	}
1248
1249	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1250		rds_ring = &adapter->recv_ctx.rds_rings[ring];
1251		netxen_post_rx_buffers(adapter, ring, rds_ring);
1252	}
1253
1254	err = netxen_nic_request_irq(adapter);
1255	if (err) {
1256		dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1257				netdev->name);
1258		goto err_out_free_rxbuf;
1259	}
1260
1261	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1262		netxen_nic_init_coalesce_defaults(adapter);
1263
1264	netxen_create_sysfs_entries(adapter);
1265
1266	adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1267	return 0;
1268
1269err_out_free_rxbuf:
1270	netxen_release_rx_buffers(adapter);
1271	netxen_free_hw_resources(adapter);
1272err_out_free_sw:
1273	netxen_free_sw_resources(adapter);
1274	return err;
1275}
1276
1277static void
1278netxen_nic_detach(struct netxen_adapter *adapter)
1279{
1280	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1281		return;
1282
1283	netxen_remove_sysfs_entries(adapter);
1284
1285	netxen_free_hw_resources(adapter);
1286	netxen_release_rx_buffers(adapter);
1287	netxen_nic_free_irq(adapter);
1288	netxen_napi_del(adapter);
1289	netxen_free_sw_resources(adapter);
1290
1291	adapter->is_up = 0;
1292}
1293
1294int
1295netxen_nic_reset_context(struct netxen_adapter *adapter)
1296{
1297	int err = 0;
1298	struct net_device *netdev = adapter->netdev;
1299
1300	if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1301		return -EBUSY;
1302
1303	if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1304
1305		netif_device_detach(netdev);
1306
1307		if (netif_running(netdev))
1308			__netxen_nic_down(adapter, netdev);
1309
1310		netxen_nic_detach(adapter);
1311
1312		if (netif_running(netdev)) {
1313			err = netxen_nic_attach(adapter);
1314			if (!err)
1315				err = __netxen_nic_up(adapter, netdev);
1316
1317			if (err)
1318				goto done;
1319		}
1320
1321		netif_device_attach(netdev);
1322	}
1323
1324done:
1325	clear_bit(__NX_RESETTING, &adapter->state);
1326	return err;
1327}
1328
1329static int
1330netxen_setup_netdev(struct netxen_adapter *adapter,
1331		struct net_device *netdev)
1332{
1333	int err = 0;
1334	struct pci_dev *pdev = adapter->pdev;
1335
1336	adapter->mc_enabled = 0;
1337	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1338		adapter->max_mc_count = 38;
1339	else
1340		adapter->max_mc_count = 16;
1341
1342	netdev->netdev_ops	   = &netxen_netdev_ops;
1343	netdev->watchdog_timeo     = 5*HZ;
1344
1345	netxen_nic_change_mtu(netdev, netdev->mtu);
1346
1347	netdev->ethtool_ops = &netxen_nic_ethtool_ops;
1348
1349	netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
1350	                      NETIF_F_RXCSUM;
1351
1352	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1353		netdev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
1354
1355	netdev->vlan_features |= netdev->hw_features;
1356
1357	if (adapter->pci_using_dac) {
1358		netdev->features |= NETIF_F_HIGHDMA;
1359		netdev->vlan_features |= NETIF_F_HIGHDMA;
1360	}
1361
1362	if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1363		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
1364
1365	if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1366		netdev->hw_features |= NETIF_F_LRO;
1367
1368	netdev->features |= netdev->hw_features;
1369
1370	netdev->irq = adapter->msix_entries[0].vector;
1371
1372	INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1373
1374	if (netxen_read_mac_addr(adapter))
1375		dev_warn(&pdev->dev, "failed to read mac addr\n");
1376
1377	netif_carrier_off(netdev);
1378
1379	err = register_netdev(netdev);
1380	if (err) {
1381		dev_err(&pdev->dev, "failed to register net device\n");
1382		return err;
1383	}
1384
1385	return 0;
1386}
1387
1388#define NETXEN_ULA_ADAPTER_KEY		(0xdaddad01)
1389#define NETXEN_NON_ULA_ADAPTER_KEY	(0xdaddad00)
1390
1391static void netxen_read_ula_info(struct netxen_adapter *adapter)
1392{
1393	u32 temp;
1394
1395	/* Print ULA info only once for an adapter */
1396	if (adapter->portnum != 0)
1397		return;
1398
1399	temp = NXRD32(adapter, NETXEN_ULA_KEY);
1400	switch (temp) {
1401	case NETXEN_ULA_ADAPTER_KEY:
1402		dev_info(&adapter->pdev->dev, "ULA adapter");
1403		break;
1404	case NETXEN_NON_ULA_ADAPTER_KEY:
1405		dev_info(&adapter->pdev->dev, "non ULA adapter");
1406		break;
1407	default:
1408		break;
1409	}
1410
1411	return;
1412}
1413
1414#ifdef CONFIG_PCIEAER
1415static void netxen_mask_aer_correctable(struct netxen_adapter *adapter)
1416{
1417	struct pci_dev *pdev = adapter->pdev;
1418	struct pci_dev *root = pdev->bus->self;
1419	u32 aer_pos;
1420
1421	/* root bus? */
1422	if (!root)
1423		return;
1424
1425	if (adapter->ahw.board_type != NETXEN_BRDTYPE_P3_4_GB_MM &&
1426		adapter->ahw.board_type != NETXEN_BRDTYPE_P3_10G_TP)
1427		return;
1428
1429	if (pci_pcie_type(root) != PCI_EXP_TYPE_ROOT_PORT)
1430		return;
1431
1432	aer_pos = pci_find_ext_capability(root, PCI_EXT_CAP_ID_ERR);
1433	if (!aer_pos)
1434		return;
1435
1436	pci_write_config_dword(root, aer_pos + PCI_ERR_COR_MASK, 0xffff);
1437}
1438#endif
1439
1440static int
1441netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1442{
1443	struct net_device *netdev = NULL;
1444	struct netxen_adapter *adapter = NULL;
1445	int i = 0, err;
1446	int pci_func_id = PCI_FUNC(pdev->devfn);
1447	uint8_t revision_id;
1448	u32 val;
1449
1450	if (pdev->revision >= NX_P3_A0 && pdev->revision <= NX_P3_B1) {
1451		pr_warn("%s: chip revisions between 0x%x-0x%x will not be enabled\n",
1452			module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
1453		return -ENODEV;
1454	}
1455
1456	if ((err = pci_enable_device(pdev)))
1457		return err;
1458
1459	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1460		err = -ENODEV;
1461		goto err_out_disable_pdev;
1462	}
1463
1464	if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1465		goto err_out_disable_pdev;
1466
1467	if (NX_IS_REVISION_P3(pdev->revision))
1468		pci_enable_pcie_error_reporting(pdev);
1469
1470	pci_set_master(pdev);
1471
1472	netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1473	if(!netdev) {
1474		err = -ENOMEM;
1475		goto err_out_free_res;
1476	}
1477
1478	SET_NETDEV_DEV(netdev, &pdev->dev);
1479
1480	adapter = netdev_priv(netdev);
1481	adapter->netdev  = netdev;
1482	adapter->pdev    = pdev;
1483	adapter->ahw.pci_func  = pci_func_id;
1484
1485	revision_id = pdev->revision;
1486	adapter->ahw.revision_id = revision_id;
1487
1488	rwlock_init(&adapter->ahw.crb_lock);
1489	spin_lock_init(&adapter->ahw.mem_lock);
1490
1491	spin_lock_init(&adapter->tx_clean_lock);
1492	INIT_LIST_HEAD(&adapter->mac_list);
1493	INIT_LIST_HEAD(&adapter->ip_list);
1494
1495	err = netxen_setup_pci_map(adapter);
1496	if (err)
1497		goto err_out_free_netdev;
1498
1499	/* This will be reset for mezz cards  */
1500	adapter->portnum = pci_func_id;
1501
1502	err = netxen_nic_get_board_info(adapter);
1503	if (err) {
1504		dev_err(&pdev->dev, "Error getting board config info.\n");
1505		goto err_out_iounmap;
1506	}
1507
1508#ifdef CONFIG_PCIEAER
1509	netxen_mask_aer_correctable(adapter);
1510#endif
1511
1512	/* Mezz cards have PCI function 0,2,3 enabled */
1513	switch (adapter->ahw.board_type) {
1514	case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1515	case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1516		if (pci_func_id >= 2)
1517			adapter->portnum = pci_func_id - 2;
1518		break;
1519	default:
1520		break;
1521	}
1522
1523	err = netxen_check_flash_fw_compatibility(adapter);
1524	if (err)
1525		goto err_out_iounmap;
1526
1527	if (adapter->portnum == 0) {
1528		val = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
1529		if (val != 0xffffffff && val != 0) {
1530			NXWR32(adapter, NX_CRB_DEV_REF_COUNT, 0);
1531			adapter->need_fw_reset = 1;
1532		}
1533	}
1534
1535	err = netxen_start_firmware(adapter);
1536	if (err)
1537		goto err_out_decr_ref;
1538
1539	/*
1540	 * See if the firmware gave us a virtual-physical port mapping.
1541	 */
1542	adapter->physical_port = adapter->portnum;
1543	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1544		i = NXRD32(adapter, CRB_V2P(adapter->portnum));
1545		if (i != 0x55555555)
1546			adapter->physical_port = i;
1547	}
1548
1549	/* MTU range: 0 - 8000 (P2) or 9600 (P3) */
1550	netdev->min_mtu = 0;
1551	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1552		netdev->max_mtu = P3_MAX_MTU;
1553	else
1554		netdev->max_mtu = P2_MAX_MTU;
1555
1556	netxen_nic_clear_stats(adapter);
1557
1558	err = netxen_setup_intr(adapter);
1559
1560	if (err) {
1561		dev_err(&adapter->pdev->dev,
1562			"Failed to setup interrupts, error = %d\n", err);
1563		goto err_out_disable_msi;
1564	}
1565
1566	netxen_read_ula_info(adapter);
1567
1568	err = netxen_setup_netdev(adapter, netdev);
1569	if (err)
1570		goto err_out_disable_msi;
1571
1572	pci_set_drvdata(pdev, adapter);
1573
1574	netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1575
1576	switch (adapter->ahw.port_type) {
1577	case NETXEN_NIC_GBE:
1578		dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1579				adapter->netdev->name);
1580		break;
1581	case NETXEN_NIC_XGBE:
1582		dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1583				adapter->netdev->name);
1584		break;
1585	}
1586
1587	netxen_create_diag_entries(adapter);
1588
1589	return 0;
1590
1591err_out_disable_msi:
1592	netxen_teardown_intr(adapter);
1593
1594	netxen_free_dummy_dma(adapter);
1595
1596err_out_decr_ref:
1597	nx_decr_dev_ref_cnt(adapter);
1598
1599err_out_iounmap:
1600	netxen_cleanup_pci_map(adapter);
1601
1602err_out_free_netdev:
1603	free_netdev(netdev);
1604
1605err_out_free_res:
1606	if (NX_IS_REVISION_P3(pdev->revision))
1607		pci_disable_pcie_error_reporting(pdev);
1608	pci_release_regions(pdev);
1609
1610err_out_disable_pdev:
1611	pci_disable_device(pdev);
1612	return err;
1613}
1614
1615static
1616void netxen_cleanup_minidump(struct netxen_adapter *adapter)
1617{
1618	kfree(adapter->mdump.md_template);
1619	adapter->mdump.md_template = NULL;
1620
1621	if (adapter->mdump.md_capture_buff) {
1622		vfree(adapter->mdump.md_capture_buff);
1623		adapter->mdump.md_capture_buff = NULL;
1624	}
1625}
1626
1627static void netxen_nic_remove(struct pci_dev *pdev)
1628{
1629	struct netxen_adapter *adapter;
1630	struct net_device *netdev;
1631
1632	adapter = pci_get_drvdata(pdev);
1633	if (adapter == NULL)
1634		return;
1635
1636	netdev = adapter->netdev;
1637
1638	netxen_cancel_fw_work(adapter);
1639
1640	unregister_netdev(netdev);
1641
1642	cancel_work_sync(&adapter->tx_timeout_task);
1643
1644	netxen_free_ip_list(adapter, false);
1645	netxen_nic_detach(adapter);
1646
1647	nx_decr_dev_ref_cnt(adapter);
1648
1649	if (adapter->portnum == 0)
1650		netxen_free_dummy_dma(adapter);
1651
1652	clear_bit(__NX_RESETTING, &adapter->state);
1653
1654	netxen_teardown_intr(adapter);
1655	netxen_set_interrupt_mode(adapter, 0);
1656	netxen_remove_diag_entries(adapter);
1657
1658	netxen_cleanup_pci_map(adapter);
1659
1660	netxen_release_firmware(adapter);
1661
1662	if (NX_IS_REVISION_P3(pdev->revision)) {
1663		netxen_cleanup_minidump(adapter);
1664		pci_disable_pcie_error_reporting(pdev);
1665	}
1666
1667	pci_release_regions(pdev);
1668	pci_disable_device(pdev);
1669
1670	free_netdev(netdev);
1671}
1672
1673static void netxen_nic_detach_func(struct netxen_adapter *adapter)
1674{
1675	struct net_device *netdev = adapter->netdev;
1676
1677	netif_device_detach(netdev);
1678
1679	netxen_cancel_fw_work(adapter);
1680
1681	if (netif_running(netdev))
1682		netxen_nic_down(adapter, netdev);
1683
1684	cancel_work_sync(&adapter->tx_timeout_task);
1685
1686	netxen_nic_detach(adapter);
1687
1688	if (adapter->portnum == 0)
1689		netxen_free_dummy_dma(adapter);
1690
1691	nx_decr_dev_ref_cnt(adapter);
1692
1693	clear_bit(__NX_RESETTING, &adapter->state);
1694}
1695
1696static int netxen_nic_attach_late_func(struct pci_dev *pdev)
1697{
1698	struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1699	struct net_device *netdev = adapter->netdev;
1700	int err;
1701
 
 
 
 
 
1702	pci_set_master(pdev);
 
1703
1704	adapter->ahw.crb_win = -1;
1705	adapter->ahw.ocm_win = -1;
1706
1707	err = netxen_start_firmware(adapter);
1708	if (err) {
1709		dev_err(&pdev->dev, "failed to start firmware\n");
1710		return err;
1711	}
1712
1713	if (netif_running(netdev)) {
1714		err = netxen_nic_attach(adapter);
1715		if (err)
1716			goto err_out;
1717
1718		err = netxen_nic_up(adapter, netdev);
1719		if (err)
1720			goto err_out_detach;
1721
1722		netxen_restore_indev_addr(netdev, NETDEV_UP);
1723	}
1724
1725	netif_device_attach(netdev);
1726	netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1727	return 0;
1728
1729err_out_detach:
1730	netxen_nic_detach(adapter);
1731err_out:
1732	nx_decr_dev_ref_cnt(adapter);
1733	return err;
1734}
1735
1736static int netxen_nic_attach_func(struct pci_dev *pdev)
1737{
1738	int err;
1739
1740	err = pci_enable_device(pdev);
1741	if (err)
1742		return err;
1743
1744	pci_set_power_state(pdev, PCI_D0);
1745	pci_restore_state(pdev);
1746
1747	return netxen_nic_attach_late_func(pdev);
1748}
1749
1750static pci_ers_result_t netxen_io_error_detected(struct pci_dev *pdev,
1751						pci_channel_state_t state)
1752{
1753	struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1754
1755	if (state == pci_channel_io_perm_failure)
1756		return PCI_ERS_RESULT_DISCONNECT;
1757
1758	if (nx_dev_request_aer(adapter))
1759		return PCI_ERS_RESULT_RECOVERED;
1760
1761	netxen_nic_detach_func(adapter);
1762
1763	pci_disable_device(pdev);
1764
1765	return PCI_ERS_RESULT_NEED_RESET;
1766}
1767
1768static pci_ers_result_t netxen_io_slot_reset(struct pci_dev *pdev)
1769{
1770	int err = 0;
1771
1772	err = netxen_nic_attach_func(pdev);
1773
1774	return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
1775}
1776
 
 
 
 
 
1777static void netxen_nic_shutdown(struct pci_dev *pdev)
1778{
1779	struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1780
1781	netxen_nic_detach_func(adapter);
1782
1783	if (pci_save_state(pdev))
1784		return;
1785
1786	if (netxen_nic_wol_supported(adapter)) {
1787		pci_enable_wake(pdev, PCI_D3cold, 1);
1788		pci_enable_wake(pdev, PCI_D3hot, 1);
1789	}
1790
1791	pci_disable_device(pdev);
1792}
1793
1794static int __maybe_unused
1795netxen_nic_suspend(struct device *dev_d)
 
1796{
1797	struct netxen_adapter *adapter = dev_get_drvdata(dev_d);
 
1798
1799	netxen_nic_detach_func(adapter);
1800
1801	if (netxen_nic_wol_supported(adapter))
1802		device_wakeup_enable(dev_d);
 
 
 
 
 
 
 
 
 
1803
1804	return 0;
1805}
1806
1807static int __maybe_unused
1808netxen_nic_resume(struct device *dev_d)
1809{
1810	return netxen_nic_attach_late_func(to_pci_dev(dev_d));
1811}
 
1812
1813static int netxen_nic_open(struct net_device *netdev)
1814{
1815	struct netxen_adapter *adapter = netdev_priv(netdev);
1816	int err = 0;
1817
1818	if (adapter->driver_mismatch)
1819		return -EIO;
1820
1821	err = netxen_nic_attach(adapter);
1822	if (err)
1823		return err;
1824
1825	err = __netxen_nic_up(adapter, netdev);
1826	if (err)
1827		goto err_out;
1828
1829	netif_start_queue(netdev);
1830
1831	return 0;
1832
1833err_out:
1834	netxen_nic_detach(adapter);
1835	return err;
1836}
1837
1838/*
1839 * netxen_nic_close - Disables a network interface entry point
1840 */
1841static int netxen_nic_close(struct net_device *netdev)
1842{
1843	struct netxen_adapter *adapter = netdev_priv(netdev);
1844
1845	__netxen_nic_down(adapter, netdev);
1846	return 0;
1847}
1848
1849static void
1850netxen_tso_check(struct net_device *netdev,
1851		struct nx_host_tx_ring *tx_ring,
1852		struct cmd_desc_type0 *first_desc,
1853		struct sk_buff *skb)
1854{
1855	u8 opcode = TX_ETHER_PKT;
1856	__be16 protocol = skb->protocol;
1857	u16 flags = 0, vid = 0;
1858	u32 producer;
1859	int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1860	struct cmd_desc_type0 *hwdesc;
1861	struct vlan_ethhdr *vh;
1862
1863	if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1864
1865		vh = (struct vlan_ethhdr *)skb->data;
1866		protocol = vh->h_vlan_encapsulated_proto;
1867		flags = FLAGS_VLAN_TAGGED;
1868
1869	} else if (skb_vlan_tag_present(skb)) {
1870		flags = FLAGS_VLAN_OOB;
1871		vid = skb_vlan_tag_get(skb);
1872		netxen_set_tx_vlan_tci(first_desc, vid);
1873		vlan_oob = 1;
1874	}
1875
1876	if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1877			skb_shinfo(skb)->gso_size > 0) {
1878
1879		hdr_len = skb_tcp_all_headers(skb);
1880
1881		first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1882		first_desc->total_hdr_length = hdr_len;
1883		if (vlan_oob) {
1884			first_desc->total_hdr_length += VLAN_HLEN;
1885			first_desc->tcp_hdr_offset = VLAN_HLEN;
1886			first_desc->ip_hdr_offset = VLAN_HLEN;
1887			/* Only in case of TSO on vlan device */
1888			flags |= FLAGS_VLAN_TAGGED;
1889		}
1890
1891		opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1892				TX_TCP_LSO6 : TX_TCP_LSO;
1893		tso = 1;
1894
1895	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1896		u8 l4proto;
1897
1898		if (protocol == cpu_to_be16(ETH_P_IP)) {
1899			l4proto = ip_hdr(skb)->protocol;
1900
1901			if (l4proto == IPPROTO_TCP)
1902				opcode = TX_TCP_PKT;
1903			else if(l4proto == IPPROTO_UDP)
1904				opcode = TX_UDP_PKT;
1905		} else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1906			l4proto = ipv6_hdr(skb)->nexthdr;
1907
1908			if (l4proto == IPPROTO_TCP)
1909				opcode = TX_TCPV6_PKT;
1910			else if(l4proto == IPPROTO_UDP)
1911				opcode = TX_UDPV6_PKT;
1912		}
1913	}
1914
1915	first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1916	first_desc->ip_hdr_offset += skb_network_offset(skb);
1917	netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1918
1919	if (!tso)
1920		return;
1921
1922	/* For LSO, we need to copy the MAC/IP/TCP headers into
1923	 * the descriptor ring
1924	 */
1925	producer = tx_ring->producer;
1926	copied = 0;
1927	offset = 2;
1928
1929	if (vlan_oob) {
1930		/* Create a TSO vlan header template for firmware */
1931
1932		hwdesc = &tx_ring->desc_head[producer];
1933		tx_ring->cmd_buf_arr[producer].skb = NULL;
1934
1935		copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1936				hdr_len + VLAN_HLEN);
1937
1938		vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1939		skb_copy_from_linear_data(skb, vh, 12);
1940		vh->h_vlan_proto = htons(ETH_P_8021Q);
1941		vh->h_vlan_TCI = htons(vid);
1942		skb_copy_from_linear_data_offset(skb, 12,
1943				(char *)vh + 16, copy_len - 16);
1944
1945		copied = copy_len - VLAN_HLEN;
1946		offset = 0;
1947
1948		producer = get_next_index(producer, tx_ring->num_desc);
1949	}
1950
1951	while (copied < hdr_len) {
1952
1953		copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1954				(hdr_len - copied));
1955
1956		hwdesc = &tx_ring->desc_head[producer];
1957		tx_ring->cmd_buf_arr[producer].skb = NULL;
1958
1959		skb_copy_from_linear_data_offset(skb, copied,
1960				 (char *)hwdesc + offset, copy_len);
1961
1962		copied += copy_len;
1963		offset = 0;
1964
1965		producer = get_next_index(producer, tx_ring->num_desc);
1966	}
1967
1968	tx_ring->producer = producer;
1969	barrier();
1970}
1971
1972static int
1973netxen_map_tx_skb(struct pci_dev *pdev,
1974		struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
1975{
1976	struct netxen_skb_frag *nf;
1977	skb_frag_t *frag;
1978	int i, nr_frags;
1979	dma_addr_t map;
1980
1981	nr_frags = skb_shinfo(skb)->nr_frags;
1982	nf = &pbuf->frag_array[0];
1983
1984	map = dma_map_single(&pdev->dev, skb->data, skb_headlen(skb),
1985			     DMA_TO_DEVICE);
1986	if (dma_mapping_error(&pdev->dev, map))
1987		goto out_err;
1988
1989	nf->dma = map;
1990	nf->length = skb_headlen(skb);
1991
1992	for (i = 0; i < nr_frags; i++) {
1993		frag = &skb_shinfo(skb)->frags[i];
1994		nf = &pbuf->frag_array[i+1];
1995
1996		map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
1997				       DMA_TO_DEVICE);
1998		if (dma_mapping_error(&pdev->dev, map))
1999			goto unwind;
2000
2001		nf->dma = map;
2002		nf->length = skb_frag_size(frag);
2003	}
2004
2005	return 0;
2006
2007unwind:
2008	while (--i >= 0) {
2009		nf = &pbuf->frag_array[i+1];
2010		dma_unmap_page(&pdev->dev, nf->dma, nf->length, DMA_TO_DEVICE);
2011		nf->dma = 0ULL;
2012	}
2013
2014	nf = &pbuf->frag_array[0];
2015	dma_unmap_single(&pdev->dev, nf->dma, skb_headlen(skb), DMA_TO_DEVICE);
2016	nf->dma = 0ULL;
2017
2018out_err:
2019	return -ENOMEM;
2020}
2021
2022static inline void
2023netxen_clear_cmddesc(u64 *desc)
2024{
2025	desc[0] = 0ULL;
2026	desc[2] = 0ULL;
2027}
2028
2029static netdev_tx_t
2030netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2031{
2032	struct netxen_adapter *adapter = netdev_priv(netdev);
2033	struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
2034	struct netxen_cmd_buffer *pbuf;
2035	struct netxen_skb_frag *buffrag;
2036	struct cmd_desc_type0 *hwdesc, *first_desc;
2037	struct pci_dev *pdev;
2038	int i, k;
2039	int delta = 0;
2040	skb_frag_t *frag;
2041
2042	u32 producer;
2043	int frag_count;
2044	u32 num_txd = tx_ring->num_desc;
2045
2046	frag_count = skb_shinfo(skb)->nr_frags + 1;
2047
2048	/* 14 frags supported for normal packet and
2049	 * 32 frags supported for TSO packet
2050	 */
2051	if (!skb_is_gso(skb) && frag_count > NETXEN_MAX_FRAGS_PER_TX) {
2052
2053		for (i = 0; i < (frag_count - NETXEN_MAX_FRAGS_PER_TX); i++) {
2054			frag = &skb_shinfo(skb)->frags[i];
2055			delta += skb_frag_size(frag);
2056		}
2057
2058		if (!__pskb_pull_tail(skb, delta))
2059			goto drop_packet;
2060
2061		frag_count = 1 + skb_shinfo(skb)->nr_frags;
2062	}
 
 
2063
2064	if (unlikely(netxen_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
2065		netif_stop_queue(netdev);
2066		smp_mb();
2067		if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH)
2068			netif_start_queue(netdev);
2069		else
2070			return NETDEV_TX_BUSY;
2071	}
2072
2073	producer = tx_ring->producer;
2074	pbuf = &tx_ring->cmd_buf_arr[producer];
2075
2076	pdev = adapter->pdev;
2077
2078	if (netxen_map_tx_skb(pdev, skb, pbuf))
2079		goto drop_packet;
2080
2081	pbuf->skb = skb;
2082	pbuf->frag_count = frag_count;
2083
2084	first_desc = hwdesc = &tx_ring->desc_head[producer];
2085	netxen_clear_cmddesc((u64 *)hwdesc);
2086
2087	netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
2088	netxen_set_tx_port(first_desc, adapter->portnum);
2089
2090	for (i = 0; i < frag_count; i++) {
2091
2092		k = i % 4;
2093
2094		if ((k == 0) && (i > 0)) {
2095			/* move to next desc.*/
2096			producer = get_next_index(producer, num_txd);
2097			hwdesc = &tx_ring->desc_head[producer];
2098			netxen_clear_cmddesc((u64 *)hwdesc);
2099			tx_ring->cmd_buf_arr[producer].skb = NULL;
2100		}
2101
2102		buffrag = &pbuf->frag_array[i];
2103
2104		hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2105		switch (k) {
2106		case 0:
2107			hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2108			break;
2109		case 1:
2110			hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2111			break;
2112		case 2:
2113			hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2114			break;
2115		case 3:
2116			hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2117			break;
2118		}
2119	}
2120
2121	tx_ring->producer = get_next_index(producer, num_txd);
2122
2123	netxen_tso_check(netdev, tx_ring, first_desc, skb);
2124
2125	adapter->stats.txbytes += skb->len;
2126	adapter->stats.xmitcalled++;
2127
2128	netxen_nic_update_cmd_producer(adapter, tx_ring);
2129
2130	return NETDEV_TX_OK;
2131
2132drop_packet:
2133	adapter->stats.txdropped++;
2134	dev_kfree_skb_any(skb);
2135	return NETDEV_TX_OK;
2136}
2137
2138static int netxen_nic_check_temp(struct netxen_adapter *adapter)
2139{
2140	struct net_device *netdev = adapter->netdev;
2141	uint32_t temp, temp_state, temp_val;
2142	int rv = 0;
2143
2144	temp = NXRD32(adapter, CRB_TEMP_STATE);
2145
2146	temp_state = nx_get_temp_state(temp);
2147	temp_val = nx_get_temp_val(temp);
2148
2149	if (temp_state == NX_TEMP_PANIC) {
2150		printk(KERN_ALERT
2151		       "%s: Device temperature %d degrees C exceeds"
2152		       " maximum allowed. Hardware has been shut down.\n",
2153		       netdev->name, temp_val);
2154		rv = 1;
2155	} else if (temp_state == NX_TEMP_WARN) {
2156		if (adapter->temp == NX_TEMP_NORMAL) {
2157			printk(KERN_ALERT
2158			       "%s: Device temperature %d degrees C "
2159			       "exceeds operating range."
2160			       " Immediate action needed.\n",
2161			       netdev->name, temp_val);
2162		}
2163	} else {
2164		if (adapter->temp == NX_TEMP_WARN) {
2165			printk(KERN_INFO
2166			       "%s: Device temperature is now %d degrees C"
2167			       " in normal range.\n", netdev->name,
2168			       temp_val);
2169		}
2170	}
2171	adapter->temp = temp_state;
2172	return rv;
2173}
2174
2175void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
2176{
2177	struct net_device *netdev = adapter->netdev;
2178
2179	if (adapter->ahw.linkup && !linkup) {
2180		printk(KERN_INFO "%s: %s NIC Link is down\n",
2181		       netxen_nic_driver_name, netdev->name);
2182		adapter->ahw.linkup = 0;
2183		if (netif_running(netdev)) {
2184			netif_carrier_off(netdev);
2185			netif_stop_queue(netdev);
2186		}
2187		adapter->link_changed = !adapter->has_link_events;
2188	} else if (!adapter->ahw.linkup && linkup) {
2189		printk(KERN_INFO "%s: %s NIC Link is up\n",
2190		       netxen_nic_driver_name, netdev->name);
2191		adapter->ahw.linkup = 1;
2192		if (netif_running(netdev)) {
2193			netif_carrier_on(netdev);
2194			netif_wake_queue(netdev);
2195		}
2196		adapter->link_changed = !adapter->has_link_events;
2197	}
2198}
2199
2200static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
2201{
2202	u32 val, port, linkup;
2203
2204	port = adapter->physical_port;
2205
2206	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2207		val = NXRD32(adapter, CRB_XG_STATE_P3);
2208		val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
2209		linkup = (val == XG_LINK_UP_P3);
2210	} else {
2211		val = NXRD32(adapter, CRB_XG_STATE);
2212		val = (val >> port*8) & 0xff;
2213		linkup = (val == XG_LINK_UP);
2214	}
2215
2216	netxen_advert_link_change(adapter, linkup);
2217}
2218
2219static void netxen_tx_timeout(struct net_device *netdev, unsigned int txqueue)
2220{
2221	struct netxen_adapter *adapter = netdev_priv(netdev);
2222
2223	if (test_bit(__NX_RESETTING, &adapter->state))
2224		return;
2225
2226	dev_err(&netdev->dev, "transmit timeout, resetting.\n");
2227	schedule_work(&adapter->tx_timeout_task);
2228}
2229
2230static void netxen_tx_timeout_task(struct work_struct *work)
2231{
2232	struct netxen_adapter *adapter =
2233		container_of(work, struct netxen_adapter, tx_timeout_task);
2234
2235	if (!netif_running(adapter->netdev))
2236		return;
2237
2238	if (test_and_set_bit(__NX_RESETTING, &adapter->state))
2239		return;
2240
2241	if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
2242		goto request_reset;
2243
2244	rtnl_lock();
2245	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
2246		/* try to scrub interrupt */
2247		netxen_napi_disable(adapter);
2248
2249		netxen_napi_enable(adapter);
2250
2251		netif_wake_queue(adapter->netdev);
2252
2253		clear_bit(__NX_RESETTING, &adapter->state);
2254	} else {
2255		clear_bit(__NX_RESETTING, &adapter->state);
2256		if (netxen_nic_reset_context(adapter)) {
2257			rtnl_unlock();
2258			goto request_reset;
2259		}
2260	}
2261	netif_trans_update(adapter->netdev);
2262	rtnl_unlock();
2263	return;
2264
2265request_reset:
2266	adapter->need_fw_reset = 1;
2267	clear_bit(__NX_RESETTING, &adapter->state);
2268}
2269
2270static void netxen_nic_get_stats(struct net_device *netdev,
2271				 struct rtnl_link_stats64 *stats)
2272{
2273	struct netxen_adapter *adapter = netdev_priv(netdev);
2274
2275	stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2276	stats->tx_packets = adapter->stats.xmitfinished;
2277	stats->rx_bytes = adapter->stats.rxbytes;
2278	stats->tx_bytes = adapter->stats.txbytes;
2279	stats->rx_dropped = adapter->stats.rxdropped;
2280	stats->tx_dropped = adapter->stats.txdropped;
2281}
2282
2283static irqreturn_t netxen_intr(int irq, void *data)
2284{
2285	struct nx_host_sds_ring *sds_ring = data;
2286	struct netxen_adapter *adapter = sds_ring->adapter;
2287	u32 status = 0;
2288
2289	status = readl(adapter->isr_int_vec);
2290
2291	if (!(status & adapter->int_vec_bit))
2292		return IRQ_NONE;
2293
2294	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2295		/* check interrupt state machine, to be sure */
2296		status = readl(adapter->crb_int_state_reg);
2297		if (!ISR_LEGACY_INT_TRIGGERED(status))
2298			return IRQ_NONE;
2299
2300	} else {
2301		unsigned long our_int = 0;
2302
2303		our_int = readl(adapter->crb_int_state_reg);
2304
2305		/* not our interrupt */
2306		if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
2307			return IRQ_NONE;
2308
2309		/* claim interrupt */
2310		writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
2311
2312		/* clear interrupt */
2313		netxen_nic_disable_int(sds_ring);
2314	}
2315
2316	writel(0xffffffff, adapter->tgt_status_reg);
2317	/* read twice to ensure write is flushed */
2318	readl(adapter->isr_int_vec);
2319	readl(adapter->isr_int_vec);
2320
2321	napi_schedule(&sds_ring->napi);
2322
2323	return IRQ_HANDLED;
2324}
2325
2326static irqreturn_t netxen_msi_intr(int irq, void *data)
2327{
2328	struct nx_host_sds_ring *sds_ring = data;
2329	struct netxen_adapter *adapter = sds_ring->adapter;
2330
2331	/* clear interrupt */
2332	writel(0xffffffff, adapter->tgt_status_reg);
2333
2334	napi_schedule(&sds_ring->napi);
2335	return IRQ_HANDLED;
2336}
2337
2338static irqreturn_t netxen_msix_intr(int irq, void *data)
2339{
2340	struct nx_host_sds_ring *sds_ring = data;
2341
2342	napi_schedule(&sds_ring->napi);
2343	return IRQ_HANDLED;
2344}
2345
2346static int netxen_nic_poll(struct napi_struct *napi, int budget)
2347{
2348	struct nx_host_sds_ring *sds_ring =
2349		container_of(napi, struct nx_host_sds_ring, napi);
2350
2351	struct netxen_adapter *adapter = sds_ring->adapter;
2352
2353	int tx_complete;
2354	int work_done;
2355
2356	tx_complete = netxen_process_cmd_ring(adapter);
2357
2358	work_done = netxen_process_rcv_ring(sds_ring, budget);
2359
2360	if (!tx_complete)
2361		work_done = budget;
2362
2363	if (work_done < budget) {
2364		napi_complete_done(&sds_ring->napi, work_done);
2365		if (test_bit(__NX_DEV_UP, &adapter->state))
2366			netxen_nic_enable_int(sds_ring);
2367	}
2368
2369	return work_done;
2370}
2371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2372static int
2373nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
2374{
2375	int count;
2376	if (netxen_api_lock(adapter))
2377		return -EIO;
2378
2379	count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2380
2381	NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2382
2383	netxen_api_unlock(adapter);
2384	return count;
2385}
2386
2387static int
2388nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
2389{
2390	int count, state;
2391	if (netxen_api_lock(adapter))
2392		return -EIO;
2393
2394	count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2395	WARN_ON(count == 0);
2396
2397	NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2398	state = NXRD32(adapter, NX_CRB_DEV_STATE);
2399
2400	if (count == 0 && state != NX_DEV_FAILED)
2401		NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2402
2403	netxen_api_unlock(adapter);
2404	return count;
2405}
2406
2407static int
2408nx_dev_request_aer(struct netxen_adapter *adapter)
2409{
2410	u32 state;
2411	int ret = -EINVAL;
2412
2413	if (netxen_api_lock(adapter))
2414		return ret;
2415
2416	state = NXRD32(adapter, NX_CRB_DEV_STATE);
2417
2418	if (state == NX_DEV_NEED_AER)
2419		ret = 0;
2420	else if (state == NX_DEV_READY) {
2421		NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_AER);
2422		ret = 0;
2423	}
2424
2425	netxen_api_unlock(adapter);
2426	return ret;
2427}
2428
2429int
2430nx_dev_request_reset(struct netxen_adapter *adapter)
2431{
2432	u32 state;
2433	int ret = -EINVAL;
2434
2435	if (netxen_api_lock(adapter))
2436		return ret;
2437
2438	state = NXRD32(adapter, NX_CRB_DEV_STATE);
2439
2440	if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED)
2441		ret = 0;
2442	else if (state != NX_DEV_INITALIZING && state != NX_DEV_NEED_AER) {
2443		NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
2444		adapter->flags |= NETXEN_FW_RESET_OWNER;
2445		ret = 0;
2446	}
2447
2448	netxen_api_unlock(adapter);
2449
2450	return ret;
2451}
2452
2453static int
2454netxen_can_start_firmware(struct netxen_adapter *adapter)
2455{
2456	int count;
2457	int can_start = 0;
2458
2459	if (netxen_api_lock(adapter)) {
2460		nx_incr_dev_ref_cnt(adapter);
2461		return -1;
2462	}
2463
2464	count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2465
2466	if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2467		count = 0;
2468
2469	if (count == 0) {
2470		can_start = 1;
2471		NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2472	}
2473
2474	NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2475
2476	netxen_api_unlock(adapter);
2477
2478	return can_start;
2479}
2480
2481static void
2482netxen_schedule_work(struct netxen_adapter *adapter,
2483		work_func_t func, int delay)
2484{
2485	INIT_DELAYED_WORK(&adapter->fw_work, func);
2486	schedule_delayed_work(&adapter->fw_work, delay);
2487}
2488
2489static void
2490netxen_cancel_fw_work(struct netxen_adapter *adapter)
2491{
2492	while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2493		msleep(10);
2494
2495	cancel_delayed_work_sync(&adapter->fw_work);
2496}
2497
2498static void
2499netxen_attach_work(struct work_struct *work)
2500{
2501	struct netxen_adapter *adapter = container_of(work,
2502				struct netxen_adapter, fw_work.work);
2503	struct net_device *netdev = adapter->netdev;
2504	int err = 0;
2505
2506	if (netif_running(netdev)) {
2507		err = netxen_nic_attach(adapter);
2508		if (err)
2509			goto done;
2510
2511		err = netxen_nic_up(adapter, netdev);
2512		if (err) {
2513			netxen_nic_detach(adapter);
2514			goto done;
2515		}
2516
2517		netxen_restore_indev_addr(netdev, NETDEV_UP);
2518	}
2519
2520	netif_device_attach(netdev);
2521
2522done:
2523	adapter->fw_fail_cnt = 0;
2524	clear_bit(__NX_RESETTING, &adapter->state);
2525	netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2526}
2527
2528static void
2529netxen_fwinit_work(struct work_struct *work)
2530{
2531	struct netxen_adapter *adapter = container_of(work,
2532				struct netxen_adapter, fw_work.work);
2533	int dev_state;
2534	int count;
2535	dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2536	if (adapter->flags & NETXEN_FW_RESET_OWNER) {
2537		count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2538		WARN_ON(count == 0);
2539		if (count == 1) {
2540			if (adapter->mdump.md_enabled) {
2541				rtnl_lock();
2542				netxen_dump_fw(adapter);
2543				rtnl_unlock();
2544			}
2545			adapter->flags &= ~NETXEN_FW_RESET_OWNER;
2546			if (netxen_api_lock(adapter)) {
2547				clear_bit(__NX_RESETTING, &adapter->state);
2548				NXWR32(adapter, NX_CRB_DEV_STATE,
2549						NX_DEV_FAILED);
2550				return;
2551			}
2552			count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2553			NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2554			NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2555			dev_state = NX_DEV_COLD;
2556			netxen_api_unlock(adapter);
2557		}
2558	}
2559
2560	switch (dev_state) {
2561	case NX_DEV_COLD:
2562	case NX_DEV_READY:
2563		if (!netxen_start_firmware(adapter)) {
2564			netxen_schedule_work(adapter, netxen_attach_work, 0);
2565			return;
2566		}
2567		break;
2568
2569	case NX_DEV_NEED_RESET:
2570	case NX_DEV_INITALIZING:
2571			netxen_schedule_work(adapter,
2572					netxen_fwinit_work, 2 * FW_POLL_DELAY);
2573			return;
2574
2575	case NX_DEV_FAILED:
2576	default:
2577		nx_incr_dev_ref_cnt(adapter);
2578		break;
2579	}
2580
2581	if (netxen_api_lock(adapter)) {
2582		clear_bit(__NX_RESETTING, &adapter->state);
2583		return;
2584	}
2585	NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_FAILED);
2586	netxen_api_unlock(adapter);
2587	dev_err(&adapter->pdev->dev, "%s: Device initialization Failed\n",
2588				adapter->netdev->name);
2589
2590	clear_bit(__NX_RESETTING, &adapter->state);
2591}
2592
2593static void
2594netxen_detach_work(struct work_struct *work)
2595{
2596	struct netxen_adapter *adapter = container_of(work,
2597				struct netxen_adapter, fw_work.work);
2598	struct net_device *netdev = adapter->netdev;
2599	int ref_cnt = 0, delay;
2600	u32 status;
2601
2602	netif_device_detach(netdev);
2603
2604	netxen_nic_down(adapter, netdev);
2605
2606	rtnl_lock();
2607	netxen_nic_detach(adapter);
2608	rtnl_unlock();
2609
2610	status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2611
2612	if (status & NX_RCODE_FATAL_ERROR)
2613		goto err_ret;
2614
2615	if (adapter->temp == NX_TEMP_PANIC)
2616		goto err_ret;
2617
2618	if (!(adapter->flags & NETXEN_FW_RESET_OWNER))
2619		ref_cnt = nx_decr_dev_ref_cnt(adapter);
2620
2621	if (ref_cnt == -EIO)
2622		goto err_ret;
2623
2624	delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2625
2626	adapter->fw_wait_cnt = 0;
2627	netxen_schedule_work(adapter, netxen_fwinit_work, delay);
2628
2629	return;
2630
2631err_ret:
2632	clear_bit(__NX_RESETTING, &adapter->state);
2633}
2634
2635static int
2636netxen_check_health(struct netxen_adapter *adapter)
2637{
2638	u32 state, heartbit;
2639	u32 peg_status;
2640	struct net_device *netdev = adapter->netdev;
2641
2642	state = NXRD32(adapter, NX_CRB_DEV_STATE);
2643	if (state == NX_DEV_NEED_AER)
2644		return 0;
2645
2646	if (netxen_nic_check_temp(adapter))
2647		goto detach;
2648
2649	if (adapter->need_fw_reset) {
2650		if (nx_dev_request_reset(adapter))
2651			return 0;
2652		goto detach;
2653	}
2654
2655	/* NX_DEV_NEED_RESET, this state can be marked in two cases
2656	 * 1. Tx timeout 2. Fw hang
2657	 * Send request to destroy context in case of tx timeout only
2658	 * and doesn't required in case of Fw hang
2659	 */
2660	if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED) {
2661		adapter->need_fw_reset = 1;
2662		if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2663			goto detach;
2664	}
2665
2666	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2667		return 0;
2668
2669	heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2670	if (heartbit != adapter->heartbit) {
2671		adapter->heartbit = heartbit;
2672		adapter->fw_fail_cnt = 0;
2673		if (adapter->need_fw_reset)
2674			goto detach;
2675		return 0;
2676	}
2677
2678	if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2679		return 0;
2680
2681	if (nx_dev_request_reset(adapter))
2682		return 0;
2683
2684	clear_bit(__NX_FW_ATTACHED, &adapter->state);
2685
2686	dev_err(&netdev->dev, "firmware hang detected\n");
2687	peg_status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2688	dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
2689			"PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
2690			"PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
2691			"PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
2692			"PEG_NET_4_PC: 0x%x\n",
2693			peg_status,
2694			NXRD32(adapter, NETXEN_PEG_HALT_STATUS2),
2695			NXRD32(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c),
2696			NXRD32(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c),
2697			NXRD32(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c),
2698			NXRD32(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c),
2699			NXRD32(adapter, NETXEN_CRB_PEG_NET_4 + 0x3c));
2700	if (NX_FWERROR_PEGSTAT1(peg_status) == 0x67)
2701		dev_err(&adapter->pdev->dev,
2702			"Firmware aborted with error code 0x00006700. "
2703				"Device is being reset.\n");
2704detach:
2705	if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
2706			!test_and_set_bit(__NX_RESETTING, &adapter->state))
2707		netxen_schedule_work(adapter, netxen_detach_work, 0);
2708	return 1;
2709}
2710
2711static void
2712netxen_fw_poll_work(struct work_struct *work)
2713{
2714	struct netxen_adapter *adapter = container_of(work,
2715				struct netxen_adapter, fw_work.work);
2716
2717	if (test_bit(__NX_RESETTING, &adapter->state))
2718		goto reschedule;
2719
2720	if (test_bit(__NX_DEV_UP, &adapter->state) &&
2721	    !(adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)) {
2722		if (!adapter->has_link_events) {
2723
2724			netxen_nic_handle_phy_intr(adapter);
2725
2726			if (adapter->link_changed)
2727				netxen_nic_set_link_parameters(adapter);
2728		}
2729	}
2730
2731	if (netxen_check_health(adapter))
2732		return;
2733
2734reschedule:
2735	netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2736}
2737
2738static ssize_t
2739netxen_store_bridged_mode(struct device *dev,
2740		struct device_attribute *attr, const char *buf, size_t len)
2741{
2742	struct net_device *net = to_net_dev(dev);
2743	struct netxen_adapter *adapter = netdev_priv(net);
2744	unsigned long new;
2745	int ret = -EINVAL;
2746
2747	if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2748		goto err_out;
2749
2750	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2751		goto err_out;
2752
2753	if (kstrtoul(buf, 2, &new))
2754		goto err_out;
2755
2756	if (!netxen_config_bridged_mode(adapter, !!new))
2757		ret = len;
2758
2759err_out:
2760	return ret;
2761}
2762
2763static ssize_t
2764netxen_show_bridged_mode(struct device *dev,
2765		struct device_attribute *attr, char *buf)
2766{
2767	struct net_device *net = to_net_dev(dev);
2768	struct netxen_adapter *adapter;
2769	int bridged_mode = 0;
2770
2771	adapter = netdev_priv(net);
2772
2773	if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2774		bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2775
2776	return sprintf(buf, "%d\n", bridged_mode);
2777}
2778
2779static const struct device_attribute dev_attr_bridged_mode = {
2780	.attr = { .name = "bridged_mode", .mode = 0644 },
2781	.show = netxen_show_bridged_mode,
2782	.store = netxen_store_bridged_mode,
2783};
2784
2785static ssize_t
2786netxen_store_diag_mode(struct device *dev,
2787		struct device_attribute *attr, const char *buf, size_t len)
2788{
2789	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2790	unsigned long new;
2791
2792	if (kstrtoul(buf, 2, &new))
2793		return -EINVAL;
2794
2795	if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2796		adapter->flags ^= NETXEN_NIC_DIAG_ENABLED;
2797
2798	return len;
2799}
2800
2801static ssize_t
2802netxen_show_diag_mode(struct device *dev,
2803		struct device_attribute *attr, char *buf)
2804{
2805	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2806
2807	return sprintf(buf, "%d\n",
2808			!!(adapter->flags & NETXEN_NIC_DIAG_ENABLED));
2809}
2810
2811static const struct device_attribute dev_attr_diag_mode = {
2812	.attr = { .name = "diag_mode", .mode = 0644 },
2813	.show = netxen_show_diag_mode,
2814	.store = netxen_store_diag_mode,
2815};
2816
2817static int
2818netxen_sysfs_validate_crb(struct netxen_adapter *adapter,
2819		loff_t offset, size_t size)
2820{
2821	size_t crb_size = 4;
2822
2823	if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2824		return -EIO;
2825
2826	if (offset < NETXEN_PCI_CRBSPACE) {
2827		if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2828			return -EINVAL;
2829
2830		if (ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2831						NETXEN_PCI_CAMQM_2M_END))
2832			crb_size = 8;
2833		else
2834			return -EINVAL;
2835	}
2836
2837	if ((size != crb_size) || (offset & (crb_size-1)))
2838		return  -EINVAL;
2839
2840	return 0;
2841}
2842
2843static ssize_t
2844netxen_sysfs_read_crb(struct file *filp, struct kobject *kobj,
2845		struct bin_attribute *attr,
2846		char *buf, loff_t offset, size_t size)
2847{
2848	struct device *dev = kobj_to_dev(kobj);
2849	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2850	u32 data;
2851	u64 qmdata;
2852	int ret;
2853
2854	ret = netxen_sysfs_validate_crb(adapter, offset, size);
2855	if (ret != 0)
2856		return ret;
2857
2858	if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
2859		ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2860					NETXEN_PCI_CAMQM_2M_END)) {
2861		netxen_pci_camqm_read_2M(adapter, offset, &qmdata);
2862		memcpy(buf, &qmdata, size);
2863	} else {
2864		data = NXRD32(adapter, offset);
2865		memcpy(buf, &data, size);
2866	}
2867
2868	return size;
2869}
2870
2871static ssize_t
2872netxen_sysfs_write_crb(struct file *filp, struct kobject *kobj,
2873		struct bin_attribute *attr,
2874		char *buf, loff_t offset, size_t size)
2875{
2876	struct device *dev = kobj_to_dev(kobj);
2877	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2878	u32 data;
2879	u64 qmdata;
2880	int ret;
2881
2882	ret = netxen_sysfs_validate_crb(adapter, offset, size);
2883	if (ret != 0)
2884		return ret;
2885
2886	if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
2887		ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2888					NETXEN_PCI_CAMQM_2M_END)) {
2889		memcpy(&qmdata, buf, size);
2890		netxen_pci_camqm_write_2M(adapter, offset, qmdata);
2891	} else {
2892		memcpy(&data, buf, size);
2893		NXWR32(adapter, offset, data);
2894	}
2895
2896	return size;
2897}
2898
2899static int
2900netxen_sysfs_validate_mem(struct netxen_adapter *adapter,
2901		loff_t offset, size_t size)
2902{
2903	if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2904		return -EIO;
2905
2906	if ((size != 8) || (offset & 0x7))
2907		return  -EIO;
2908
2909	return 0;
2910}
2911
2912static ssize_t
2913netxen_sysfs_read_mem(struct file *filp, struct kobject *kobj,
2914		struct bin_attribute *attr,
2915		char *buf, loff_t offset, size_t size)
2916{
2917	struct device *dev = kobj_to_dev(kobj);
2918	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2919	u64 data;
2920	int ret;
2921
2922	ret = netxen_sysfs_validate_mem(adapter, offset, size);
2923	if (ret != 0)
2924		return ret;
2925
2926	if (adapter->pci_mem_read(adapter, offset, &data))
2927		return -EIO;
2928
2929	memcpy(buf, &data, size);
2930
2931	return size;
2932}
2933
2934static ssize_t netxen_sysfs_write_mem(struct file *filp, struct kobject *kobj,
2935		struct bin_attribute *attr, char *buf,
2936		loff_t offset, size_t size)
2937{
2938	struct device *dev = kobj_to_dev(kobj);
2939	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2940	u64 data;
2941	int ret;
2942
2943	ret = netxen_sysfs_validate_mem(adapter, offset, size);
2944	if (ret != 0)
2945		return ret;
2946
2947	memcpy(&data, buf, size);
2948
2949	if (adapter->pci_mem_write(adapter, offset, data))
2950		return -EIO;
2951
2952	return size;
2953}
2954
2955
2956static const struct bin_attribute bin_attr_crb = {
2957	.attr = { .name = "crb", .mode = 0644 },
2958	.size = 0,
2959	.read = netxen_sysfs_read_crb,
2960	.write = netxen_sysfs_write_crb,
2961};
2962
2963static const struct bin_attribute bin_attr_mem = {
2964	.attr = { .name = "mem", .mode = 0644 },
2965	.size = 0,
2966	.read = netxen_sysfs_read_mem,
2967	.write = netxen_sysfs_write_mem,
2968};
2969
2970static ssize_t
2971netxen_sysfs_read_dimm(struct file *filp, struct kobject *kobj,
2972		struct bin_attribute *attr,
2973		char *buf, loff_t offset, size_t size)
2974{
2975	struct device *dev = kobj_to_dev(kobj);
2976	struct netxen_adapter *adapter = dev_get_drvdata(dev);
2977	struct net_device *netdev = adapter->netdev;
2978	struct netxen_dimm_cfg dimm;
2979	u8 dw, rows, cols, banks, ranks;
2980	u32 val;
2981
2982	if (size < attr->size) {
2983		netdev_err(netdev, "Invalid size\n");
2984		return -EINVAL;
2985	}
2986
2987	memset(&dimm, 0, sizeof(struct netxen_dimm_cfg));
2988	val = NXRD32(adapter, NETXEN_DIMM_CAPABILITY);
2989
2990	/* Checks if DIMM info is valid. */
2991	if (val & NETXEN_DIMM_VALID_FLAG) {
2992		netdev_err(netdev, "Invalid DIMM flag\n");
2993		dimm.presence = 0xff;
2994		goto out;
2995	}
2996
2997	rows = NETXEN_DIMM_NUMROWS(val);
2998	cols = NETXEN_DIMM_NUMCOLS(val);
2999	ranks = NETXEN_DIMM_NUMRANKS(val);
3000	banks = NETXEN_DIMM_NUMBANKS(val);
3001	dw = NETXEN_DIMM_DATAWIDTH(val);
3002
3003	dimm.presence = (val & NETXEN_DIMM_PRESENT);
3004
3005	/* Checks if DIMM info is present. */
3006	if (!dimm.presence) {
3007		netdev_err(netdev, "DIMM not present\n");
3008		goto out;
3009	}
3010
3011	dimm.dimm_type = NETXEN_DIMM_TYPE(val);
3012
3013	switch (dimm.dimm_type) {
3014	case NETXEN_DIMM_TYPE_RDIMM:
3015	case NETXEN_DIMM_TYPE_UDIMM:
3016	case NETXEN_DIMM_TYPE_SO_DIMM:
3017	case NETXEN_DIMM_TYPE_Micro_DIMM:
3018	case NETXEN_DIMM_TYPE_Mini_RDIMM:
3019	case NETXEN_DIMM_TYPE_Mini_UDIMM:
3020		break;
3021	default:
3022		netdev_err(netdev, "Invalid DIMM type %x\n", dimm.dimm_type);
3023		goto out;
3024	}
3025
3026	if (val & NETXEN_DIMM_MEMTYPE_DDR2_SDRAM)
3027		dimm.mem_type = NETXEN_DIMM_MEM_DDR2_SDRAM;
3028	else
3029		dimm.mem_type = NETXEN_DIMM_MEMTYPE(val);
3030
3031	if (val & NETXEN_DIMM_SIZE) {
3032		dimm.size = NETXEN_DIMM_STD_MEM_SIZE;
3033		goto out;
3034	}
3035
3036	if (!rows) {
3037		netdev_err(netdev, "Invalid no of rows %x\n", rows);
3038		goto out;
3039	}
3040
3041	if (!cols) {
3042		netdev_err(netdev, "Invalid no of columns %x\n", cols);
3043		goto out;
3044	}
3045
3046	if (!banks) {
3047		netdev_err(netdev, "Invalid no of banks %x\n", banks);
3048		goto out;
3049	}
3050
3051	ranks += 1;
3052
3053	switch (dw) {
3054	case 0x0:
3055		dw = 32;
3056		break;
3057	case 0x1:
3058		dw = 33;
3059		break;
3060	case 0x2:
3061		dw = 36;
3062		break;
3063	case 0x3:
3064		dw = 64;
3065		break;
3066	case 0x4:
3067		dw = 72;
3068		break;
3069	case 0x5:
3070		dw = 80;
3071		break;
3072	case 0x6:
3073		dw = 128;
3074		break;
3075	case 0x7:
3076		dw = 144;
3077		break;
3078	default:
3079		netdev_err(netdev, "Invalid data-width %x\n", dw);
3080		goto out;
3081	}
3082
3083	dimm.size = ((1 << rows) * (1 << cols) * dw * banks * ranks) / 8;
3084	/* Size returned in MB. */
3085	dimm.size = (dimm.size) / 0x100000;
3086out:
3087	memcpy(buf, &dimm, sizeof(struct netxen_dimm_cfg));
3088	return sizeof(struct netxen_dimm_cfg);
3089
3090}
3091
3092static const struct bin_attribute bin_attr_dimm = {
3093	.attr = { .name = "dimm", .mode = 0644 },
3094	.size = sizeof(struct netxen_dimm_cfg),
3095	.read = netxen_sysfs_read_dimm,
3096};
3097
3098
3099static void
3100netxen_create_sysfs_entries(struct netxen_adapter *adapter)
3101{
3102	struct device *dev = &adapter->pdev->dev;
3103
3104	if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
3105		/* bridged_mode control */
3106		if (device_create_file(dev, &dev_attr_bridged_mode)) {
3107			dev_warn(dev,
3108				"failed to create bridged_mode sysfs entry\n");
3109		}
3110	}
3111}
3112
3113static void
3114netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
3115{
3116	struct device *dev = &adapter->pdev->dev;
3117
3118	if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
3119		device_remove_file(dev, &dev_attr_bridged_mode);
3120}
3121
3122static void
3123netxen_create_diag_entries(struct netxen_adapter *adapter)
3124{
3125	struct pci_dev *pdev = adapter->pdev;
3126	struct device *dev;
3127
3128	dev = &pdev->dev;
3129	if (device_create_file(dev, &dev_attr_diag_mode))
3130		dev_info(dev, "failed to create diag_mode sysfs entry\n");
3131	if (device_create_bin_file(dev, &bin_attr_crb))
3132		dev_info(dev, "failed to create crb sysfs entry\n");
3133	if (device_create_bin_file(dev, &bin_attr_mem))
3134		dev_info(dev, "failed to create mem sysfs entry\n");
3135	if (device_create_bin_file(dev, &bin_attr_dimm))
3136		dev_info(dev, "failed to create dimm sysfs entry\n");
3137}
3138
3139
3140static void
3141netxen_remove_diag_entries(struct netxen_adapter *adapter)
3142{
3143	struct pci_dev *pdev = adapter->pdev;
3144	struct device *dev = &pdev->dev;
3145
3146	device_remove_file(dev, &dev_attr_diag_mode);
3147	device_remove_bin_file(dev, &bin_attr_crb);
3148	device_remove_bin_file(dev, &bin_attr_mem);
3149	device_remove_bin_file(dev, &bin_attr_dimm);
3150}
3151
3152#ifdef CONFIG_INET
3153
3154#define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
3155
3156static int
3157netxen_destip_supported(struct netxen_adapter *adapter)
3158{
3159	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
3160		return 0;
3161
3162	if (adapter->ahw.cut_through)
3163		return 0;
3164
3165	return 1;
3166}
3167
3168static void
3169netxen_free_ip_list(struct netxen_adapter *adapter, bool master)
3170{
3171	struct nx_ip_list  *cur, *tmp_cur;
3172
3173	list_for_each_entry_safe(cur, tmp_cur, &adapter->ip_list, list) {
3174		if (master) {
3175			if (cur->master) {
3176				netxen_config_ipaddr(adapter, cur->ip_addr,
3177						     NX_IP_DOWN);
3178				list_del(&cur->list);
3179				kfree(cur);
3180			}
3181		} else {
3182			netxen_config_ipaddr(adapter, cur->ip_addr, NX_IP_DOWN);
3183			list_del(&cur->list);
3184			kfree(cur);
3185		}
3186	}
3187}
3188
3189static bool
3190netxen_list_config_ip(struct netxen_adapter *adapter,
3191		struct in_ifaddr *ifa, unsigned long event)
3192{
3193	struct net_device *dev;
3194	struct nx_ip_list *cur, *tmp_cur;
3195	struct list_head *head;
3196	bool ret = false;
3197
3198	dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
3199
3200	if (dev == NULL)
3201		goto out;
3202
3203	switch (event) {
3204	case NX_IP_UP:
3205		list_for_each(head, &adapter->ip_list) {
3206			cur = list_entry(head, struct nx_ip_list, list);
3207
3208			if (cur->ip_addr == ifa->ifa_address)
3209				goto out;
3210		}
3211
3212		cur = kzalloc(sizeof(struct nx_ip_list), GFP_ATOMIC);
3213		if (cur == NULL)
3214			goto out;
3215		if (is_vlan_dev(dev))
3216			dev = vlan_dev_real_dev(dev);
3217		cur->master = !!netif_is_bond_master(dev);
3218		cur->ip_addr = ifa->ifa_address;
3219		list_add_tail(&cur->list, &adapter->ip_list);
3220		netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
3221		ret = true;
3222		break;
3223	case NX_IP_DOWN:
3224		list_for_each_entry_safe(cur, tmp_cur,
3225					&adapter->ip_list, list) {
3226			if (cur->ip_addr == ifa->ifa_address) {
3227				list_del(&cur->list);
3228				kfree(cur);
3229				netxen_config_ipaddr(adapter, ifa->ifa_address,
3230						     NX_IP_DOWN);
3231				ret = true;
3232				break;
3233			}
3234		}
3235	}
3236out:
3237	return ret;
3238}
3239
3240static void
3241netxen_config_indev_addr(struct netxen_adapter *adapter,
3242		struct net_device *dev, unsigned long event)
3243{
3244	struct in_device *indev;
3245	struct in_ifaddr *ifa;
3246
3247	if (!netxen_destip_supported(adapter))
3248		return;
3249
3250	indev = in_dev_get(dev);
3251	if (!indev)
3252		return;
3253
3254	rcu_read_lock();
3255	in_dev_for_each_ifa_rcu(ifa, indev) {
3256		switch (event) {
3257		case NETDEV_UP:
3258			netxen_list_config_ip(adapter, ifa, NX_IP_UP);
3259			break;
3260		case NETDEV_DOWN:
3261			netxen_list_config_ip(adapter, ifa, NX_IP_DOWN);
3262			break;
3263		default:
3264			break;
3265		}
3266	}
3267	rcu_read_unlock();
3268	in_dev_put(indev);
3269}
3270
3271static void
3272netxen_restore_indev_addr(struct net_device *netdev, unsigned long event)
3273
3274{
3275	struct netxen_adapter *adapter = netdev_priv(netdev);
3276	struct nx_ip_list *pos, *tmp_pos;
3277	unsigned long ip_event;
3278
3279	ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
3280	netxen_config_indev_addr(adapter, netdev, event);
3281
3282	list_for_each_entry_safe(pos, tmp_pos, &adapter->ip_list, list) {
3283		netxen_config_ipaddr(adapter, pos->ip_addr, ip_event);
3284	}
3285}
3286
3287static inline bool
3288netxen_config_checkdev(struct net_device *dev)
3289{
3290	struct netxen_adapter *adapter;
3291
3292	if (!is_netxen_netdev(dev))
3293		return false;
3294	adapter = netdev_priv(dev);
3295	if (!adapter)
3296		return false;
3297	if (!netxen_destip_supported(adapter))
3298		return false;
3299	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
3300		return false;
3301
3302	return true;
3303}
3304
3305/**
3306 * netxen_config_master - configure addresses based on master
3307 * @dev: netxen device
3308 * @event: netdev event
3309 */
3310static void netxen_config_master(struct net_device *dev, unsigned long event)
3311{
3312	struct net_device *master, *slave;
3313	struct netxen_adapter *adapter = netdev_priv(dev);
3314
3315	rcu_read_lock();
3316	master = netdev_master_upper_dev_get_rcu(dev);
3317	/*
3318	 * This is the case where the netxen nic is being
3319	 * enslaved and is dev_open()ed in bond_enslave()
3320	 * Now we should program the bond's (and its vlans')
3321	 * addresses in the netxen NIC.
3322	 */
3323	if (master && netif_is_bond_master(master) &&
3324	    !netif_is_bond_slave(dev)) {
3325		netxen_config_indev_addr(adapter, master, event);
3326		for_each_netdev_rcu(&init_net, slave)
3327			if (is_vlan_dev(slave) &&
3328			    vlan_dev_real_dev(slave) == master)
3329				netxen_config_indev_addr(adapter, slave, event);
3330	}
3331	rcu_read_unlock();
3332	/*
3333	 * This is the case where the netxen nic is being
3334	 * released and is dev_close()ed in bond_release()
3335	 * just before IFF_BONDING is stripped.
3336	 */
3337	if (!master && dev->priv_flags & IFF_BONDING)
3338		netxen_free_ip_list(adapter, true);
3339}
3340
3341static int netxen_netdev_event(struct notifier_block *this,
3342				 unsigned long event, void *ptr)
3343{
3344	struct netxen_adapter *adapter;
3345	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3346	struct net_device *orig_dev = dev;
3347	struct net_device *slave;
3348
3349recheck:
3350	if (dev == NULL)
3351		goto done;
3352
3353	if (is_vlan_dev(dev)) {
3354		dev = vlan_dev_real_dev(dev);
3355		goto recheck;
3356	}
3357	if (event == NETDEV_UP || event == NETDEV_DOWN) {
3358		/* If this is a bonding device, look for netxen-based slaves*/
3359		if (netif_is_bond_master(dev)) {
3360			rcu_read_lock();
3361			for_each_netdev_in_bond_rcu(dev, slave) {
3362				if (!netxen_config_checkdev(slave))
3363					continue;
3364				adapter = netdev_priv(slave);
3365				netxen_config_indev_addr(adapter,
3366							 orig_dev, event);
3367			}
3368			rcu_read_unlock();
3369		} else {
3370			if (!netxen_config_checkdev(dev))
3371				goto done;
3372			adapter = netdev_priv(dev);
3373			/* Act only if the actual netxen is the target */
3374			if (orig_dev == dev)
3375				netxen_config_master(dev, event);
3376			netxen_config_indev_addr(adapter, orig_dev, event);
3377		}
3378	}
3379done:
3380	return NOTIFY_DONE;
3381}
3382
3383static int
3384netxen_inetaddr_event(struct notifier_block *this,
3385		unsigned long event, void *ptr)
3386{
3387	struct netxen_adapter *adapter;
3388	struct net_device *dev, *slave;
3389	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
3390	unsigned long ip_event;
3391
3392	dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
3393	ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
3394recheck:
3395	if (dev == NULL)
3396		goto done;
3397
3398	if (is_vlan_dev(dev)) {
3399		dev = vlan_dev_real_dev(dev);
3400		goto recheck;
3401	}
3402	if (event == NETDEV_UP || event == NETDEV_DOWN) {
3403		/* If this is a bonding device, look for netxen-based slaves*/
3404		if (netif_is_bond_master(dev)) {
3405			rcu_read_lock();
3406			for_each_netdev_in_bond_rcu(dev, slave) {
3407				if (!netxen_config_checkdev(slave))
3408					continue;
3409				adapter = netdev_priv(slave);
3410				netxen_list_config_ip(adapter, ifa, ip_event);
3411			}
3412			rcu_read_unlock();
3413		} else {
3414			if (!netxen_config_checkdev(dev))
3415				goto done;
3416			adapter = netdev_priv(dev);
3417			netxen_list_config_ip(adapter, ifa, ip_event);
3418		}
3419	}
3420done:
3421	return NOTIFY_DONE;
3422}
3423
3424static struct notifier_block	netxen_netdev_cb = {
3425	.notifier_call = netxen_netdev_event,
3426};
3427
3428static struct notifier_block netxen_inetaddr_cb = {
3429	.notifier_call = netxen_inetaddr_event,
3430};
3431#else
3432static void
3433netxen_restore_indev_addr(struct net_device *dev, unsigned long event)
3434{ }
3435static void
3436netxen_free_ip_list(struct netxen_adapter *adapter, bool master)
3437{ }
3438#endif
3439
3440static const struct pci_error_handlers netxen_err_handler = {
3441	.error_detected = netxen_io_error_detected,
3442	.slot_reset = netxen_io_slot_reset,
 
3443};
3444
3445static SIMPLE_DEV_PM_OPS(netxen_nic_pm_ops,
3446			 netxen_nic_suspend,
3447			 netxen_nic_resume);
3448
3449static struct pci_driver netxen_driver = {
3450	.name = netxen_nic_driver_name,
3451	.id_table = netxen_pci_tbl,
3452	.probe = netxen_nic_probe,
3453	.remove = netxen_nic_remove,
3454	.driver.pm = &netxen_nic_pm_ops,
 
 
 
3455	.shutdown = netxen_nic_shutdown,
3456	.err_handler = &netxen_err_handler
3457};
3458
3459static int __init netxen_init_module(void)
3460{
3461	printk(KERN_INFO "%s\n", netxen_nic_driver_string);
3462
3463#ifdef CONFIG_INET
3464	register_netdevice_notifier(&netxen_netdev_cb);
3465	register_inetaddr_notifier(&netxen_inetaddr_cb);
3466#endif
3467	return pci_register_driver(&netxen_driver);
3468}
3469
3470module_init(netxen_init_module);
3471
3472static void __exit netxen_exit_module(void)
3473{
3474	pci_unregister_driver(&netxen_driver);
3475
3476#ifdef CONFIG_INET
3477	unregister_inetaddr_notifier(&netxen_inetaddr_cb);
3478	unregister_netdevice_notifier(&netxen_netdev_cb);
3479#endif
3480}
3481
3482module_exit(netxen_exit_module);