PageRenderTime 123ms CodeModel.GetById 53ms app.highlight 58ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/net/netxen/netxen_nic_main.c

https://bitbucket.org/ndreys/linux-sunxi
C | 3012 lines | 2266 code | 663 blank | 83 comment | 458 complexity | 5c09f269f24caa803a47e9b15d64ccd7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

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

   1/*
   2 * 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, write to the Free Software
  18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  19 * MA  02111-1307, USA.
  20 *
  21 * The full GNU General Public License is included in this distribution
  22 * in the file called "COPYING".
  23 *
  24 */
  25
  26#include <linux/slab.h>
  27#include <linux/vmalloc.h>
  28#include <linux/interrupt.h>
  29#include "netxen_nic_hw.h"
  30
  31#include "netxen_nic.h"
  32
  33#include <linux/dma-mapping.h>
  34#include <linux/if_vlan.h>
  35#include <net/ip.h>
  36#include <linux/ipv6.h>
  37#include <linux/inetdevice.h>
  38#include <linux/sysfs.h>
  39#include <linux/aer.h>
  40
  41MODULE_DESCRIPTION("QLogic/NetXen (1/10) GbE Intelligent Ethernet Driver");
  42MODULE_LICENSE("GPL");
  43MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
  44MODULE_FIRMWARE(NX_UNIFIED_ROMIMAGE_NAME);
  45
  46char netxen_nic_driver_name[] = "netxen_nic";
  47static char netxen_nic_driver_string[] = "QLogic/NetXen Network Driver v"
  48    NETXEN_NIC_LINUX_VERSIONID;
  49
  50static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
  51
  52/* Default to restricted 1G auto-neg mode */
  53static int wol_port_mode = 5;
  54
  55static int use_msi = 1;
  56
  57static int use_msi_x = 1;
  58
  59static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
  60module_param(auto_fw_reset, int, 0644);
  61MODULE_PARM_DESC(auto_fw_reset,"Auto firmware reset (0=disabled, 1=enabled");
  62
  63static int __devinit netxen_nic_probe(struct pci_dev *pdev,
  64		const struct pci_device_id *ent);
  65static void __devexit netxen_nic_remove(struct pci_dev *pdev);
  66static int netxen_nic_open(struct net_device *netdev);
  67static int netxen_nic_close(struct net_device *netdev);
  68static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
  69					       struct net_device *);
  70static void netxen_tx_timeout(struct net_device *netdev);
  71static void netxen_tx_timeout_task(struct work_struct *work);
  72static void netxen_fw_poll_work(struct work_struct *work);
  73static void netxen_schedule_work(struct netxen_adapter *adapter,
  74		work_func_t func, int delay);
  75static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
  76static int netxen_nic_poll(struct napi_struct *napi, int budget);
  77#ifdef CONFIG_NET_POLL_CONTROLLER
  78static void netxen_nic_poll_controller(struct net_device *netdev);
  79#endif
  80
  81static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
  82static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
  83static void netxen_create_diag_entries(struct netxen_adapter *adapter);
  84static void netxen_remove_diag_entries(struct netxen_adapter *adapter);
  85
  86static int nx_dev_request_aer(struct netxen_adapter *adapter);
  87static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
  88static int netxen_can_start_firmware(struct netxen_adapter *adapter);
  89
  90static irqreturn_t netxen_intr(int irq, void *data);
  91static irqreturn_t netxen_msi_intr(int irq, void *data);
  92static irqreturn_t netxen_msix_intr(int irq, void *data);
  93
  94static void netxen_config_indev_addr(struct net_device *dev, unsigned long);
  95static struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev);
  96static int netxen_nic_set_mac(struct net_device *netdev, void *p);
  97
  98/*  PCI Device ID Table  */
  99#define ENTRY(device) \
 100	{PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
 101	.class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
 102
 103static DEFINE_PCI_DEVICE_TABLE(netxen_pci_tbl) = {
 104	ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
 105	ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
 106	ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
 107	ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
 108	ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
 109	ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
 110	ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
 111	ENTRY(PCI_DEVICE_ID_NX3031),
 112	{0,}
 113};
 114
 115MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
 116
 117static uint32_t crb_cmd_producer[4] = {
 118	CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
 119	CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
 120};
 121
 122void
 123netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
 124		struct nx_host_tx_ring *tx_ring)
 125{
 126	NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
 127}
 128
 129static uint32_t crb_cmd_consumer[4] = {
 130	CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
 131	CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
 132};
 133
 134static inline void
 135netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
 136		struct nx_host_tx_ring *tx_ring)
 137{
 138	NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
 139}
 140
 141static uint32_t msi_tgt_status[8] = {
 142	ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
 143	ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
 144	ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
 145	ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
 146};
 147
 148static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
 149
 150static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
 151{
 152	struct netxen_adapter *adapter = sds_ring->adapter;
 153
 154	NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
 155}
 156
 157static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
 158{
 159	struct netxen_adapter *adapter = sds_ring->adapter;
 160
 161	NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
 162
 163	if (!NETXEN_IS_MSI_FAMILY(adapter))
 164		NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
 165}
 166
 167static int
 168netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
 169{
 170	int size = sizeof(struct nx_host_sds_ring) * count;
 171
 172	recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
 173
 174	return recv_ctx->sds_rings == NULL;
 175}
 176
 177static void
 178netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
 179{
 180	if (recv_ctx->sds_rings != NULL)
 181		kfree(recv_ctx->sds_rings);
 182
 183	recv_ctx->sds_rings = NULL;
 184}
 185
 186static int
 187netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
 188{
 189	int ring;
 190	struct nx_host_sds_ring *sds_ring;
 191	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 192
 193	if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
 194		return -ENOMEM;
 195
 196	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 197		sds_ring = &recv_ctx->sds_rings[ring];
 198		netif_napi_add(netdev, &sds_ring->napi,
 199				netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
 200	}
 201
 202	return 0;
 203}
 204
 205static void
 206netxen_napi_del(struct netxen_adapter *adapter)
 207{
 208	int ring;
 209	struct nx_host_sds_ring *sds_ring;
 210	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 211
 212	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 213		sds_ring = &recv_ctx->sds_rings[ring];
 214		netif_napi_del(&sds_ring->napi);
 215	}
 216
 217	netxen_free_sds_rings(&adapter->recv_ctx);
 218}
 219
 220static void
 221netxen_napi_enable(struct netxen_adapter *adapter)
 222{
 223	int ring;
 224	struct nx_host_sds_ring *sds_ring;
 225	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 226
 227	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 228		sds_ring = &recv_ctx->sds_rings[ring];
 229		napi_enable(&sds_ring->napi);
 230		netxen_nic_enable_int(sds_ring);
 231	}
 232}
 233
 234static void
 235netxen_napi_disable(struct netxen_adapter *adapter)
 236{
 237	int ring;
 238	struct nx_host_sds_ring *sds_ring;
 239	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 240
 241	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 242		sds_ring = &recv_ctx->sds_rings[ring];
 243		netxen_nic_disable_int(sds_ring);
 244		napi_synchronize(&sds_ring->napi);
 245		napi_disable(&sds_ring->napi);
 246	}
 247}
 248
 249static int nx_set_dma_mask(struct netxen_adapter *adapter)
 250{
 251	struct pci_dev *pdev = adapter->pdev;
 252	uint64_t mask, cmask;
 253
 254	adapter->pci_using_dac = 0;
 255
 256	mask = DMA_BIT_MASK(32);
 257	cmask = DMA_BIT_MASK(32);
 258
 259	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
 260#ifndef CONFIG_IA64
 261		mask = DMA_BIT_MASK(35);
 262#endif
 263	} else {
 264		mask = DMA_BIT_MASK(39);
 265		cmask = mask;
 266	}
 267
 268	if (pci_set_dma_mask(pdev, mask) == 0 &&
 269		pci_set_consistent_dma_mask(pdev, cmask) == 0) {
 270		adapter->pci_using_dac = 1;
 271		return 0;
 272	}
 273
 274	return -EIO;
 275}
 276
 277/* Update addressable range if firmware supports it */
 278static int
 279nx_update_dma_mask(struct netxen_adapter *adapter)
 280{
 281	int change, shift, err;
 282	uint64_t mask, old_mask, old_cmask;
 283	struct pci_dev *pdev = adapter->pdev;
 284
 285	change = 0;
 286
 287	shift = NXRD32(adapter, CRB_DMA_SHIFT);
 288	if (shift > 32)
 289		return 0;
 290
 291	if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
 292		change = 1;
 293	else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
 294		change = 1;
 295
 296	if (change) {
 297		old_mask = pdev->dma_mask;
 298		old_cmask = pdev->dev.coherent_dma_mask;
 299
 300		mask = DMA_BIT_MASK(32+shift);
 301
 302		err = pci_set_dma_mask(pdev, mask);
 303		if (err)
 304			goto err_out;
 305
 306		if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 307
 308			err = pci_set_consistent_dma_mask(pdev, mask);
 309			if (err)
 310				goto err_out;
 311		}
 312		dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
 313	}
 314
 315	return 0;
 316
 317err_out:
 318	pci_set_dma_mask(pdev, old_mask);
 319	pci_set_consistent_dma_mask(pdev, old_cmask);
 320	return err;
 321}
 322
 323static int
 324netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
 325{
 326	u32 val, timeout;
 327
 328	if (first_boot == 0x55555555) {
 329		/* This is the first boot after power up */
 330		NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
 331
 332		if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
 333			return 0;
 334
 335		/* PCI bus master workaround */
 336		first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
 337		if (!(first_boot & 0x4)) {
 338			first_boot |= 0x4;
 339			NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
 340			NXRD32(adapter, NETXEN_PCIE_REG(0x4));
 341		}
 342
 343		/* This is the first boot after power up */
 344		first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
 345		if (first_boot != 0x80000f) {
 346			/* clear the register for future unloads/loads */
 347			NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
 348			return -EIO;
 349		}
 350
 351		/* Start P2 boot loader */
 352		val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
 353		NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
 354		timeout = 0;
 355		do {
 356			msleep(1);
 357			val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
 358
 359			if (++timeout > 5000)
 360				return -EIO;
 361
 362		} while (val == NETXEN_BDINFO_MAGIC);
 363	}
 364	return 0;
 365}
 366
 367static void netxen_set_port_mode(struct netxen_adapter *adapter)
 368{
 369	u32 val, data;
 370
 371	val = adapter->ahw.board_type;
 372	if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
 373		(val == NETXEN_BRDTYPE_P3_XG_LOM)) {
 374		if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
 375			data = NETXEN_PORT_MODE_802_3_AP;
 376			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 377		} else if (port_mode == NETXEN_PORT_MODE_XG) {
 378			data = NETXEN_PORT_MODE_XG;
 379			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 380		} else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
 381			data = NETXEN_PORT_MODE_AUTO_NEG_1G;
 382			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 383		} else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
 384			data = NETXEN_PORT_MODE_AUTO_NEG_XG;
 385			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 386		} else {
 387			data = NETXEN_PORT_MODE_AUTO_NEG;
 388			NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
 389		}
 390
 391		if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
 392			(wol_port_mode != NETXEN_PORT_MODE_XG) &&
 393			(wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
 394			(wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
 395			wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
 396		}
 397		NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
 398	}
 399}
 400
 401static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
 402{
 403	u32 control;
 404	int pos;
 405
 406	pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
 407	if (pos) {
 408		pci_read_config_dword(pdev, pos, &control);
 409		if (enable)
 410			control |= PCI_MSIX_FLAGS_ENABLE;
 411		else
 412			control = 0;
 413		pci_write_config_dword(pdev, pos, control);
 414	}
 415}
 416
 417static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
 418{
 419	int i;
 420
 421	for (i = 0; i < count; i++)
 422		adapter->msix_entries[i].entry = i;
 423}
 424
 425static int
 426netxen_read_mac_addr(struct netxen_adapter *adapter)
 427{
 428	int i;
 429	unsigned char *p;
 430	u64 mac_addr;
 431	struct net_device *netdev = adapter->netdev;
 432	struct pci_dev *pdev = adapter->pdev;
 433
 434	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 435		if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
 436			return -EIO;
 437	} else {
 438		if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
 439			return -EIO;
 440	}
 441
 442	p = (unsigned char *)&mac_addr;
 443	for (i = 0; i < 6; i++)
 444		netdev->dev_addr[i] = *(p + 5 - i);
 445
 446	memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
 447	memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
 448
 449	/* set station address */
 450
 451	if (!is_valid_ether_addr(netdev->perm_addr))
 452		dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
 453
 454	return 0;
 455}
 456
 457static int netxen_nic_set_mac(struct net_device *netdev, void *p)
 458{
 459	struct netxen_adapter *adapter = netdev_priv(netdev);
 460	struct sockaddr *addr = p;
 461
 462	if (!is_valid_ether_addr(addr->sa_data))
 463		return -EINVAL;
 464
 465	if (netif_running(netdev)) {
 466		netif_device_detach(netdev);
 467		netxen_napi_disable(adapter);
 468	}
 469
 470	memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
 471	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
 472	adapter->macaddr_set(adapter, addr->sa_data);
 473
 474	if (netif_running(netdev)) {
 475		netif_device_attach(netdev);
 476		netxen_napi_enable(adapter);
 477	}
 478	return 0;
 479}
 480
 481static void netxen_set_multicast_list(struct net_device *dev)
 482{
 483	struct netxen_adapter *adapter = netdev_priv(dev);
 484
 485	adapter->set_multi(dev);
 486}
 487
 488static u32 netxen_fix_features(struct net_device *dev, u32 features)
 489{
 490	if (!(features & NETIF_F_RXCSUM)) {
 491		netdev_info(dev, "disabling LRO as RXCSUM is off\n");
 492
 493		features &= ~NETIF_F_LRO;
 494	}
 495
 496	return features;
 497}
 498
 499static int netxen_set_features(struct net_device *dev, u32 features)
 500{
 501	struct netxen_adapter *adapter = netdev_priv(dev);
 502	int hw_lro;
 503
 504	if (!((dev->features ^ features) & NETIF_F_LRO))
 505		return 0;
 506
 507	hw_lro = (features & NETIF_F_LRO) ? NETXEN_NIC_LRO_ENABLED
 508	         : NETXEN_NIC_LRO_DISABLED;
 509
 510	if (netxen_config_hw_lro(adapter, hw_lro))
 511		return -EIO;
 512
 513	if (!(features & NETIF_F_LRO) && netxen_send_lro_cleanup(adapter))
 514		return -EIO;
 515
 516	return 0;
 517}
 518
 519static const struct net_device_ops netxen_netdev_ops = {
 520	.ndo_open	   = netxen_nic_open,
 521	.ndo_stop	   = netxen_nic_close,
 522	.ndo_start_xmit    = netxen_nic_xmit_frame,
 523	.ndo_get_stats	   = netxen_nic_get_stats,
 524	.ndo_validate_addr = eth_validate_addr,
 525	.ndo_set_multicast_list = netxen_set_multicast_list,
 526	.ndo_set_mac_address    = netxen_nic_set_mac,
 527	.ndo_change_mtu	   = netxen_nic_change_mtu,
 528	.ndo_tx_timeout	   = netxen_tx_timeout,
 529	.ndo_fix_features = netxen_fix_features,
 530	.ndo_set_features = netxen_set_features,
 531#ifdef CONFIG_NET_POLL_CONTROLLER
 532	.ndo_poll_controller = netxen_nic_poll_controller,
 533#endif
 534};
 535
 536static void
 537netxen_setup_intr(struct netxen_adapter *adapter)
 538{
 539	struct netxen_legacy_intr_set *legacy_intrp;
 540	struct pci_dev *pdev = adapter->pdev;
 541	int err, num_msix;
 542
 543	if (adapter->rss_supported) {
 544		num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
 545			MSIX_ENTRIES_PER_ADAPTER : 2;
 546	} else
 547		num_msix = 1;
 548
 549	adapter->max_sds_rings = 1;
 550
 551	adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
 552
 553	if (adapter->ahw.revision_id >= NX_P3_B0)
 554		legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
 555	else
 556		legacy_intrp = &legacy_intr[0];
 557
 558	adapter->int_vec_bit = legacy_intrp->int_vec_bit;
 559	adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
 560			legacy_intrp->tgt_status_reg);
 561	adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
 562			legacy_intrp->tgt_mask_reg);
 563	adapter->pci_int_reg = netxen_get_ioaddr(adapter,
 564			legacy_intrp->pci_int_reg);
 565	adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
 566
 567	if (adapter->ahw.revision_id >= NX_P3_B1)
 568		adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
 569			ISR_INT_STATE_REG);
 570	else
 571		adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
 572			CRB_INT_VECTOR);
 573
 574	netxen_set_msix_bit(pdev, 0);
 575
 576	if (adapter->msix_supported) {
 577
 578		netxen_init_msix_entries(adapter, num_msix);
 579		err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
 580		if (err == 0) {
 581			adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
 582			netxen_set_msix_bit(pdev, 1);
 583
 584			if (adapter->rss_supported)
 585				adapter->max_sds_rings = num_msix;
 586
 587			dev_info(&pdev->dev, "using msi-x interrupts\n");
 588			return;
 589		}
 590
 591		if (err > 0)
 592			pci_disable_msix(pdev);
 593
 594		/* fall through for msi */
 595	}
 596
 597	if (use_msi && !pci_enable_msi(pdev)) {
 598		adapter->flags |= NETXEN_NIC_MSI_ENABLED;
 599		adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
 600				msi_tgt_status[adapter->ahw.pci_func]);
 601		dev_info(&pdev->dev, "using msi interrupts\n");
 602		adapter->msix_entries[0].vector = pdev->irq;
 603		return;
 604	}
 605
 606	dev_info(&pdev->dev, "using legacy interrupts\n");
 607	adapter->msix_entries[0].vector = pdev->irq;
 608}
 609
 610static void
 611netxen_teardown_intr(struct netxen_adapter *adapter)
 612{
 613	if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
 614		pci_disable_msix(adapter->pdev);
 615	if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
 616		pci_disable_msi(adapter->pdev);
 617}
 618
 619static void
 620netxen_cleanup_pci_map(struct netxen_adapter *adapter)
 621{
 622	if (adapter->ahw.db_base != NULL)
 623		iounmap(adapter->ahw.db_base);
 624	if (adapter->ahw.pci_base0 != NULL)
 625		iounmap(adapter->ahw.pci_base0);
 626	if (adapter->ahw.pci_base1 != NULL)
 627		iounmap(adapter->ahw.pci_base1);
 628	if (adapter->ahw.pci_base2 != NULL)
 629		iounmap(adapter->ahw.pci_base2);
 630}
 631
 632static int
 633netxen_setup_pci_map(struct netxen_adapter *adapter)
 634{
 635	void __iomem *db_ptr = NULL;
 636
 637	resource_size_t mem_base, db_base;
 638	unsigned long mem_len, db_len = 0;
 639
 640	struct pci_dev *pdev = adapter->pdev;
 641	int pci_func = adapter->ahw.pci_func;
 642	struct netxen_hardware_context *ahw = &adapter->ahw;
 643
 644	int err = 0;
 645
 646	/*
 647	 * Set the CRB window to invalid. If any register in window 0 is
 648	 * accessed it should set the window to 0 and then reset it to 1.
 649	 */
 650	adapter->ahw.crb_win = -1;
 651	adapter->ahw.ocm_win = -1;
 652
 653	/* remap phys address */
 654	mem_base = pci_resource_start(pdev, 0);	/* 0 is for BAR 0 */
 655	mem_len = pci_resource_len(pdev, 0);
 656
 657	/* 128 Meg of memory */
 658	if (mem_len == NETXEN_PCI_128MB_SIZE) {
 659
 660		ahw->pci_base0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
 661		ahw->pci_base1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
 662				SECOND_PAGE_GROUP_SIZE);
 663		ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
 664				THIRD_PAGE_GROUP_SIZE);
 665		if (ahw->pci_base0 == NULL || ahw->pci_base1 == NULL ||
 666						ahw->pci_base2 == NULL) {
 667			dev_err(&pdev->dev, "failed to map PCI bar 0\n");
 668			err = -EIO;
 669			goto err_out;
 670		}
 671
 672		ahw->pci_len0 = FIRST_PAGE_GROUP_SIZE;
 673
 674	} else if (mem_len == NETXEN_PCI_32MB_SIZE) {
 675
 676		ahw->pci_base1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
 677		ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
 678			SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
 679		if (ahw->pci_base1 == NULL || ahw->pci_base2 == NULL) {
 680			dev_err(&pdev->dev, "failed to map PCI bar 0\n");
 681			err = -EIO;
 682			goto err_out;
 683		}
 684
 685	} else if (mem_len == NETXEN_PCI_2MB_SIZE) {
 686
 687		ahw->pci_base0 = pci_ioremap_bar(pdev, 0);
 688		if (ahw->pci_base0 == NULL) {
 689			dev_err(&pdev->dev, "failed to map PCI bar 0\n");
 690			return -EIO;
 691		}
 692		ahw->pci_len0 = mem_len;
 693	} else {
 694		return -EIO;
 695	}
 696
 697	netxen_setup_hwops(adapter);
 698
 699	dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
 700
 701	if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
 702		adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
 703			NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
 704
 705	} else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 706		adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
 707			NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func)));
 708	}
 709
 710	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
 711		goto skip_doorbell;
 712
 713	db_base = pci_resource_start(pdev, 4);	/* doorbell is on bar 4 */
 714	db_len = pci_resource_len(pdev, 4);
 715
 716	if (db_len == 0) {
 717		printk(KERN_ERR "%s: doorbell is disabled\n",
 718				netxen_nic_driver_name);
 719		err = -EIO;
 720		goto err_out;
 721	}
 722
 723	db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
 724	if (!db_ptr) {
 725		printk(KERN_ERR "%s: Failed to allocate doorbell map.",
 726				netxen_nic_driver_name);
 727		err = -EIO;
 728		goto err_out;
 729	}
 730
 731skip_doorbell:
 732	adapter->ahw.db_base = db_ptr;
 733	adapter->ahw.db_len = db_len;
 734	return 0;
 735
 736err_out:
 737	netxen_cleanup_pci_map(adapter);
 738	return err;
 739}
 740
 741static void
 742netxen_check_options(struct netxen_adapter *adapter)
 743{
 744	u32 fw_major, fw_minor, fw_build;
 745	char brd_name[NETXEN_MAX_SHORT_NAME];
 746	char serial_num[32];
 747	int i, offset, val;
 748	int *ptr32;
 749	struct pci_dev *pdev = adapter->pdev;
 750
 751	adapter->driver_mismatch = 0;
 752
 753	ptr32 = (int *)&serial_num;
 754	offset = NX_FW_SERIAL_NUM_OFFSET;
 755	for (i = 0; i < 8; i++) {
 756		if (netxen_rom_fast_read(adapter, offset, &val) == -1) {
 757			dev_err(&pdev->dev, "error reading board info\n");
 758			adapter->driver_mismatch = 1;
 759			return;
 760		}
 761		ptr32[i] = cpu_to_le32(val);
 762		offset += sizeof(u32);
 763	}
 764
 765	fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
 766	fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
 767	fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
 768
 769	adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
 770
 771	if (adapter->portnum == 0) {
 772		get_brd_name_by_type(adapter->ahw.board_type, brd_name);
 773
 774		pr_info("%s: %s Board S/N %s  Chip rev 0x%x\n",
 775				module_name(THIS_MODULE),
 776				brd_name, serial_num, adapter->ahw.revision_id);
 777	}
 778
 779	if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
 780		adapter->driver_mismatch = 1;
 781		dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
 782				fw_major, fw_minor, fw_build);
 783		return;
 784	}
 785
 786	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 787		i = NXRD32(adapter, NETXEN_SRE_MISC);
 788		adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
 789	}
 790
 791	dev_info(&pdev->dev, "firmware v%d.%d.%d [%s]\n",
 792			fw_major, fw_minor, fw_build,
 793			adapter->ahw.cut_through ? "cut-through" : "legacy");
 794
 795	if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
 796		adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
 797
 798	if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
 799		adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
 800		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
 801	} else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
 802		adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
 803		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
 804	}
 805
 806	adapter->msix_supported = 0;
 807	if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
 808		adapter->msix_supported = !!use_msi_x;
 809		adapter->rss_supported = !!use_msi_x;
 810	} else {
 811		u32 flashed_ver = 0;
 812		netxen_rom_fast_read(adapter,
 813				NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
 814		flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
 815
 816		if (flashed_ver >= NETXEN_VERSION_CODE(3, 4, 336)) {
 817			switch (adapter->ahw.board_type) {
 818			case NETXEN_BRDTYPE_P2_SB31_10G:
 819			case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
 820				adapter->msix_supported = !!use_msi_x;
 821				adapter->rss_supported = !!use_msi_x;
 822				break;
 823			default:
 824				break;
 825			}
 826		}
 827	}
 828
 829	adapter->num_txd = MAX_CMD_DESCRIPTORS;
 830
 831	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
 832		adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
 833		adapter->max_rds_rings = 3;
 834	} else {
 835		adapter->num_lro_rxd = 0;
 836		adapter->max_rds_rings = 2;
 837	}
 838}
 839
 840static int
 841netxen_start_firmware(struct netxen_adapter *adapter)
 842{
 843	int val, err, first_boot;
 844	struct pci_dev *pdev = adapter->pdev;
 845
 846	/* required for NX2031 dummy dma */
 847	err = nx_set_dma_mask(adapter);
 848	if (err)
 849		return err;
 850
 851	if (!netxen_can_start_firmware(adapter))
 852		goto wait_init;
 853
 854	first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
 855
 856	err = netxen_check_hw_init(adapter, first_boot);
 857	if (err) {
 858		dev_err(&pdev->dev, "error in init HW init sequence\n");
 859		return err;
 860	}
 861
 862	netxen_request_firmware(adapter);
 863
 864	err = netxen_need_fw_reset(adapter);
 865	if (err < 0)
 866		goto err_out;
 867	if (err == 0)
 868		goto wait_init;
 869
 870	if (first_boot != 0x55555555) {
 871		NXWR32(adapter, CRB_CMDPEG_STATE, 0);
 872		netxen_pinit_from_rom(adapter);
 873		msleep(1);
 874	}
 875
 876	NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
 877	NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
 878	NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
 879
 880	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
 881		netxen_set_port_mode(adapter);
 882
 883	err = netxen_load_firmware(adapter);
 884	if (err)
 885		goto err_out;
 886
 887	netxen_release_firmware(adapter);
 888
 889	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
 890
 891		/* Initialize multicast addr pool owners */
 892		val = 0x7654;
 893		if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
 894			val |= 0x0f000000;
 895		NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
 896
 897	}
 898
 899	err = netxen_init_dummy_dma(adapter);
 900	if (err)
 901		goto err_out;
 902
 903	/*
 904	 * Tell the hardware our version number.
 905	 */
 906	val = (_NETXEN_NIC_LINUX_MAJOR << 16)
 907		| ((_NETXEN_NIC_LINUX_MINOR << 8))
 908		| (_NETXEN_NIC_LINUX_SUBVERSION);
 909	NXWR32(adapter, CRB_DRIVER_VERSION, val);
 910
 911wait_init:
 912	/* Handshake with the card before we register the devices. */
 913	err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
 914	if (err) {
 915		netxen_free_dummy_dma(adapter);
 916		goto err_out;
 917	}
 918
 919	NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
 920
 921	nx_update_dma_mask(adapter);
 922
 923	netxen_check_options(adapter);
 924
 925	adapter->need_fw_reset = 0;
 926
 927	/* fall through and release firmware */
 928
 929err_out:
 930	netxen_release_firmware(adapter);
 931	return err;
 932}
 933
 934static int
 935netxen_nic_request_irq(struct netxen_adapter *adapter)
 936{
 937	irq_handler_t handler;
 938	struct nx_host_sds_ring *sds_ring;
 939	int err, ring;
 940
 941	unsigned long flags = 0;
 942	struct net_device *netdev = adapter->netdev;
 943	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 944
 945	if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
 946		handler = netxen_msix_intr;
 947	else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
 948		handler = netxen_msi_intr;
 949	else {
 950		flags |= IRQF_SHARED;
 951		handler = netxen_intr;
 952	}
 953	adapter->irq = netdev->irq;
 954
 955	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 956		sds_ring = &recv_ctx->sds_rings[ring];
 957		sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
 958		err = request_irq(sds_ring->irq, handler,
 959				  flags, sds_ring->name, sds_ring);
 960		if (err)
 961			return err;
 962	}
 963
 964	return 0;
 965}
 966
 967static void
 968netxen_nic_free_irq(struct netxen_adapter *adapter)
 969{
 970	int ring;
 971	struct nx_host_sds_ring *sds_ring;
 972
 973	struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
 974
 975	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 976		sds_ring = &recv_ctx->sds_rings[ring];
 977		free_irq(sds_ring->irq, sds_ring);
 978	}
 979}
 980
 981static void
 982netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
 983{
 984	adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
 985	adapter->coal.normal.data.rx_time_us =
 986		NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
 987	adapter->coal.normal.data.rx_packets =
 988		NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
 989	adapter->coal.normal.data.tx_time_us =
 990		NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
 991	adapter->coal.normal.data.tx_packets =
 992		NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
 993}
 994
 995/* with rtnl_lock */
 996static int
 997__netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
 998{
 999	int err;
1000
1001	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1002		return -EIO;
1003
1004	err = adapter->init_port(adapter, adapter->physical_port);
1005	if (err) {
1006		printk(KERN_ERR "%s: Failed to initialize port %d\n",
1007				netxen_nic_driver_name, adapter->portnum);
1008		return err;
1009	}
1010	if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
1011		adapter->macaddr_set(adapter, adapter->mac_addr);
1012
1013	adapter->set_multi(netdev);
1014	adapter->set_mtu(adapter, netdev->mtu);
1015
1016	adapter->ahw.linkup = 0;
1017
1018	if (adapter->max_sds_rings > 1)
1019		netxen_config_rss(adapter, 1);
1020
1021	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1022		netxen_config_intr_coalesce(adapter);
1023
1024	if (netdev->features & NETIF_F_LRO)
1025		netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
1026
1027	netxen_napi_enable(adapter);
1028
1029	if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1030		netxen_linkevent_request(adapter, 1);
1031	else
1032		netxen_nic_set_link_parameters(adapter);
1033
1034	set_bit(__NX_DEV_UP, &adapter->state);
1035	return 0;
1036}
1037
1038/* Usage: During resume and firmware recovery module.*/
1039
1040static inline int
1041netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1042{
1043	int err = 0;
1044
1045	rtnl_lock();
1046	if (netif_running(netdev))
1047		err = __netxen_nic_up(adapter, netdev);
1048	rtnl_unlock();
1049
1050	return err;
1051}
1052
1053/* with rtnl_lock */
1054static void
1055__netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1056{
1057	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1058		return;
1059
1060	if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state))
1061		return;
1062
1063	smp_mb();
1064	spin_lock(&adapter->tx_clean_lock);
1065	netif_carrier_off(netdev);
1066	netif_tx_disable(netdev);
1067
1068	if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1069		netxen_linkevent_request(adapter, 0);
1070
1071	if (adapter->stop_port)
1072		adapter->stop_port(adapter);
1073
1074	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1075		netxen_p3_free_mac_list(adapter);
1076
1077	adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1078
1079	netxen_napi_disable(adapter);
1080
1081	netxen_release_tx_buffers(adapter);
1082	spin_unlock(&adapter->tx_clean_lock);
1083}
1084
1085/* Usage: During suspend and firmware recovery module */
1086
1087static inline void
1088netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1089{
1090	rtnl_lock();
1091	if (netif_running(netdev))
1092		__netxen_nic_down(adapter, netdev);
1093	rtnl_unlock();
1094
1095}
1096
1097static int
1098netxen_nic_attach(struct netxen_adapter *adapter)
1099{
1100	struct net_device *netdev = adapter->netdev;
1101	struct pci_dev *pdev = adapter->pdev;
1102	int err, ring;
1103	struct nx_host_rds_ring *rds_ring;
1104	struct nx_host_tx_ring *tx_ring;
1105
1106	if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1107		return 0;
1108
1109	err = netxen_init_firmware(adapter);
1110	if (err)
1111		return err;
1112
1113	err = netxen_napi_add(adapter, netdev);
1114	if (err)
1115		return err;
1116
1117	err = netxen_alloc_sw_resources(adapter);
1118	if (err) {
1119		printk(KERN_ERR "%s: Error in setting sw resources\n",
1120				netdev->name);
1121		return err;
1122	}
1123
1124	err = netxen_alloc_hw_resources(adapter);
1125	if (err) {
1126		printk(KERN_ERR "%s: Error in setting hw resources\n",
1127				netdev->name);
1128		goto err_out_free_sw;
1129	}
1130
1131	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1132		tx_ring = adapter->tx_ring;
1133		tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1134				crb_cmd_producer[adapter->portnum]);
1135		tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1136				crb_cmd_consumer[adapter->portnum]);
1137
1138		tx_ring->producer = 0;
1139		tx_ring->sw_consumer = 0;
1140
1141		netxen_nic_update_cmd_producer(adapter, tx_ring);
1142		netxen_nic_update_cmd_consumer(adapter, tx_ring);
1143	}
1144
1145	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1146		rds_ring = &adapter->recv_ctx.rds_rings[ring];
1147		netxen_post_rx_buffers(adapter, ring, rds_ring);
1148	}
1149
1150	err = netxen_nic_request_irq(adapter);
1151	if (err) {
1152		dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1153				netdev->name);
1154		goto err_out_free_rxbuf;
1155	}
1156
1157	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1158		netxen_nic_init_coalesce_defaults(adapter);
1159
1160	netxen_create_sysfs_entries(adapter);
1161
1162	adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1163	return 0;
1164
1165err_out_free_rxbuf:
1166	netxen_release_rx_buffers(adapter);
1167	netxen_free_hw_resources(adapter);
1168err_out_free_sw:
1169	netxen_free_sw_resources(adapter);
1170	return err;
1171}
1172
1173static void
1174netxen_nic_detach(struct netxen_adapter *adapter)
1175{
1176	if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1177		return;
1178
1179	netxen_remove_sysfs_entries(adapter);
1180
1181	netxen_free_hw_resources(adapter);
1182	netxen_release_rx_buffers(adapter);
1183	netxen_nic_free_irq(adapter);
1184	netxen_napi_del(adapter);
1185	netxen_free_sw_resources(adapter);
1186
1187	adapter->is_up = 0;
1188}
1189
1190int
1191netxen_nic_reset_context(struct netxen_adapter *adapter)
1192{
1193	int err = 0;
1194	struct net_device *netdev = adapter->netdev;
1195
1196	if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1197		return -EBUSY;
1198
1199	if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1200
1201		netif_device_detach(netdev);
1202
1203		if (netif_running(netdev))
1204			__netxen_nic_down(adapter, netdev);
1205
1206		netxen_nic_detach(adapter);
1207
1208		if (netif_running(netdev)) {
1209			err = netxen_nic_attach(adapter);
1210			if (!err)
1211				err = __netxen_nic_up(adapter, netdev);
1212
1213			if (err)
1214				goto done;
1215		}
1216
1217		netif_device_attach(netdev);
1218	}
1219
1220done:
1221	clear_bit(__NX_RESETTING, &adapter->state);
1222	return err;
1223}
1224
1225static int
1226netxen_setup_netdev(struct netxen_adapter *adapter,
1227		struct net_device *netdev)
1228{
1229	int err = 0;
1230	struct pci_dev *pdev = adapter->pdev;
1231
1232	adapter->mc_enabled = 0;
1233	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1234		adapter->max_mc_count = 38;
1235	else
1236		adapter->max_mc_count = 16;
1237
1238	netdev->netdev_ops	   = &netxen_netdev_ops;
1239	netdev->watchdog_timeo     = 5*HZ;
1240
1241	netxen_nic_change_mtu(netdev, netdev->mtu);
1242
1243	SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
1244
1245	netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
1246	                      NETIF_F_RXCSUM;
1247
1248	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1249		netdev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
1250
1251	netdev->vlan_features |= netdev->hw_features;
1252
1253	if (adapter->pci_using_dac) {
1254		netdev->features |= NETIF_F_HIGHDMA;
1255		netdev->vlan_features |= NETIF_F_HIGHDMA;
1256	}
1257
1258	if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1259		netdev->hw_features |= NETIF_F_HW_VLAN_TX;
1260
1261	if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1262		netdev->hw_features |= NETIF_F_LRO;
1263
1264	netdev->features |= netdev->hw_features;
1265
1266	netdev->irq = adapter->msix_entries[0].vector;
1267
1268	INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1269
1270	if (netxen_read_mac_addr(adapter))
1271		dev_warn(&pdev->dev, "failed to read mac addr\n");
1272
1273	netif_carrier_off(netdev);
1274
1275	err = register_netdev(netdev);
1276	if (err) {
1277		dev_err(&pdev->dev, "failed to register net device\n");
1278		return err;
1279	}
1280
1281	return 0;
1282}
1283
1284#ifdef CONFIG_PCIEAER
1285static void netxen_mask_aer_correctable(struct netxen_adapter *adapter)
1286{
1287	struct pci_dev *pdev = adapter->pdev;
1288	struct pci_dev *root = pdev->bus->self;
1289	u32 aer_pos;
1290
1291	/* root bus? */
1292	if (!root)
1293		return;
1294
1295	if (adapter->ahw.board_type != NETXEN_BRDTYPE_P3_4_GB_MM &&
1296		adapter->ahw.board_type != NETXEN_BRDTYPE_P3_10G_TP)
1297		return;
1298
1299	if (root->pcie_type != PCI_EXP_TYPE_ROOT_PORT)
1300		return;
1301
1302	aer_pos = pci_find_ext_capability(root, PCI_EXT_CAP_ID_ERR);
1303	if (!aer_pos)
1304		return;
1305
1306	pci_write_config_dword(root, aer_pos + PCI_ERR_COR_MASK, 0xffff);
1307}
1308#endif
1309
1310static int __devinit
1311netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1312{
1313	struct net_device *netdev = NULL;
1314	struct netxen_adapter *adapter = NULL;
1315	int i = 0, err;
1316	int pci_func_id = PCI_FUNC(pdev->devfn);
1317	uint8_t revision_id;
1318	u32 val;
1319
1320	if (pdev->revision >= NX_P3_A0 && pdev->revision <= NX_P3_B1) {
1321		pr_warning("%s: chip revisions between 0x%x-0x%x "
1322				"will not be enabled.\n",
1323				module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
1324		return -ENODEV;
1325	}
1326
1327	if ((err = pci_enable_device(pdev)))
1328		return err;
1329
1330	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1331		err = -ENODEV;
1332		goto err_out_disable_pdev;
1333	}
1334
1335	if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1336		goto err_out_disable_pdev;
1337
1338	if (NX_IS_REVISION_P3(pdev->revision))
1339		pci_enable_pcie_error_reporting(pdev);
1340
1341	pci_set_master(pdev);
1342
1343	netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1344	if(!netdev) {
1345		dev_err(&pdev->dev, "failed to allocate net_device\n");
1346		err = -ENOMEM;
1347		goto err_out_free_res;
1348	}
1349
1350	SET_NETDEV_DEV(netdev, &pdev->dev);
1351
1352	adapter = netdev_priv(netdev);
1353	adapter->netdev  = netdev;
1354	adapter->pdev    = pdev;
1355	adapter->ahw.pci_func  = pci_func_id;
1356
1357	revision_id = pdev->revision;
1358	adapter->ahw.revision_id = revision_id;
1359
1360	rwlock_init(&adapter->ahw.crb_lock);
1361	spin_lock_init(&adapter->ahw.mem_lock);
1362
1363	spin_lock_init(&adapter->tx_clean_lock);
1364	INIT_LIST_HEAD(&adapter->mac_list);
1365
1366	err = netxen_setup_pci_map(adapter);
1367	if (err)
1368		goto err_out_free_netdev;
1369
1370	/* This will be reset for mezz cards  */
1371	adapter->portnum = pci_func_id;
1372
1373	err = netxen_nic_get_board_info(adapter);
1374	if (err) {
1375		dev_err(&pdev->dev, "Error getting board config info.\n");
1376		goto err_out_iounmap;
1377	}
1378
1379#ifdef CONFIG_PCIEAER
1380	netxen_mask_aer_correctable(adapter);
1381#endif
1382
1383	/* Mezz cards have PCI function 0,2,3 enabled */
1384	switch (adapter->ahw.board_type) {
1385	case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1386	case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1387		if (pci_func_id >= 2)
1388			adapter->portnum = pci_func_id - 2;
1389		break;
1390	default:
1391		break;
1392	}
1393
1394	if (adapter->portnum == 0) {
1395		val = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
1396		if (val != 0xffffffff && val != 0) {
1397			NXWR32(adapter, NX_CRB_DEV_REF_COUNT, 0);
1398			adapter->need_fw_reset = 1;
1399		}
1400	}
1401
1402	err = netxen_start_firmware(adapter);
1403	if (err)
1404		goto err_out_decr_ref;
1405
1406	/*
1407	 * See if the firmware gave us a virtual-physical port mapping.
1408	 */
1409	adapter->physical_port = adapter->portnum;
1410	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1411		i = NXRD32(adapter, CRB_V2P(adapter->portnum));
1412		if (i != 0x55555555)
1413			adapter->physical_port = i;
1414	}
1415
1416	netxen_nic_clear_stats(adapter);
1417
1418	netxen_setup_intr(adapter);
1419
1420	err = netxen_setup_netdev(adapter, netdev);
1421	if (err)
1422		goto err_out_disable_msi;
1423
1424	pci_set_drvdata(pdev, adapter);
1425
1426	netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1427
1428	switch (adapter->ahw.port_type) {
1429	case NETXEN_NIC_GBE:
1430		dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1431				adapter->netdev->name);
1432		break;
1433	case NETXEN_NIC_XGBE:
1434		dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1435				adapter->netdev->name);
1436		break;
1437	}
1438
1439	netxen_create_diag_entries(adapter);
1440
1441	return 0;
1442
1443err_out_disable_msi:
1444	netxen_teardown_intr(adapter);
1445
1446	netxen_free_dummy_dma(adapter);
1447
1448err_out_decr_ref:
1449	nx_decr_dev_ref_cnt(adapter);
1450
1451err_out_iounmap:
1452	netxen_cleanup_pci_map(adapter);
1453
1454err_out_free_netdev:
1455	free_netdev(netdev);
1456
1457err_out_free_res:
1458	pci_release_regions(pdev);
1459
1460err_out_disable_pdev:
1461	pci_set_drvdata(pdev, NULL);
1462	pci_disable_device(pdev);
1463	return err;
1464}
1465
1466static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1467{
1468	struct netxen_adapter *adapter;
1469	struct net_device *netdev;
1470
1471	adapter = pci_get_drvdata(pdev);
1472	if (adapter == NULL)
1473		return;
1474
1475	netdev = adapter->netdev;
1476
1477	netxen_cancel_fw_work(adapter);
1478
1479	unregister_netdev(netdev);
1480
1481	cancel_work_sync(&adapter->tx_timeout_task);
1482
1483	netxen_nic_detach(adapter);
1484
1485	nx_decr_dev_ref_cnt(adapter);
1486
1487	if (adapter->portnum == 0)
1488		netxen_free_dummy_dma(adapter);
1489
1490	clear_bit(__NX_RESETTING, &adapter->state);
1491
1492	netxen_teardown_intr(adapter);
1493
1494	netxen_remove_diag_entries(adapter);
1495
1496	netxen_cleanup_pci_map(adapter);
1497
1498	netxen_release_firmware(adapter);
1499
1500	if (NX_IS_REVISION_P3(pdev->revision))
1501		pci_disable_pcie_error_reporting(pdev);
1502
1503	pci_release_regions(pdev);
1504	pci_disable_device(pdev);
1505	pci_set_drvdata(pdev, NULL);
1506
1507	free_netdev(netdev);
1508}
1509
1510static void netxen_nic_detach_func(struct netxen_adapter *adapter)
1511{
1512	struct net_device *netdev = adapter->netdev;
1513
1514	netif_device_detach(netdev);
1515
1516	netxen_cancel_fw_work(adapter);
1517
1518	if (netif_running(netdev))
1519		netxen_nic_down(adapter, netdev);
1520
1521	cancel_work_sync(&adapter->tx_timeout_task);
1522
1523	netxen_nic_detach(adapter);
1524
1525	if (adapter->portnum == 0)
1526		netxen_free_dummy_dma(adapter);
1527
1528	nx_decr_dev_ref_cnt(adapter);
1529
1530	clear_bit(__NX_RESETTING, &adapter->state);
1531}
1532
1533static int netxen_nic_attach_func(struct pci_dev *pdev)
1534{
1535	struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1536	struct net_device *netdev = adapter->netdev;
1537	int err;
1538
1539	err = pci_enable_device(pdev);
1540	if (err)
1541		return err;
1542
1543	pci_set_power_state(pdev, PCI_D0);
1544	pci_set_master(pdev);
1545	pci_restore_state(pdev);
1546
1547	adapter->ahw.crb_win = -1;
1548	adapter->ahw.ocm_win = -1;
1549
1550	err = netxen_start_firmware(adapter);
1551	if (err) {
1552		dev_err(&pdev->dev, "failed to start firmware\n");
1553		return err;
1554	}
1555
1556	if (netif_running(netdev)) {
1557		err = netxen_nic_attach(adapter);
1558		if (err)
1559			goto err_out;
1560
1561		err = netxen_nic_up(adapter, netdev);
1562		if (err)
1563			goto err_out_detach;
1564
1565		netxen_config_indev_addr(netdev, NETDEV_UP);
1566	}
1567
1568	netif_device_attach(netdev);
1569	netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1570	return 0;
1571
1572err_out_detach:
1573	netxen_nic_detach(adapter);
1574err_out:
1575	nx_decr_dev_ref_cnt(adapter);
1576	return err;
1577}
1578
1579static pci_ers_result_t netxen_io_error_detected(struct pci_dev *pdev,
1580						pci_channel_state_t state)
1581{
1582	struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1583
1584	if (state == pci_channel_io_perm_failure)
1585		return PCI_ERS_RESULT_DISCONNECT;
1586
1587	if (nx_dev_request_aer(adapter))
1588		return PCI_ERS_RESULT_RECOVERED;
1589
1590	netxen_nic_detach_func(adapter);
1591
1592	pci_disable_device(pdev);
1593
1594	return PCI_ERS_RESULT_NEED_RESET;
1595}
1596
1597static pci_ers_result_t netxen_io_slot_reset(struct pci_dev *pdev)
1598{
1599	int err = 0;
1600
1601	err = netxen_nic_attach_func(pdev);
1602
1603	return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
1604}
1605
1606static void netxen_io_resume(struct pci_dev *pdev)
1607{
1608	pci_cleanup_aer_uncorrect_error_status(pdev);
1609}
1610
1611static void netxen_nic_shutdown(struct pci_dev *pdev)
1612{
1613	struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1614
1615	netxen_nic_detach_func(adapter);
1616
1617	if (pci_save_state(pdev))
1618		return;
1619
1620	if (netxen_nic_wol_supported(adapter)) {
1621		pci_enable_wake(pdev, PCI_D3cold, 1);
1622		pci_enable_wake(pdev, PCI_D3hot, 1);
1623	}
1624
1625	pci_disable_device(pdev);
1626}
1627
1628#ifdef CONFIG_PM
1629static int
1630netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1631{
1632	struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1633	int retval;
1634
1635	netxen_nic_detach_func(adapter);
1636
1637	retval = pci_save_state(pdev);
1638	if (retval)
1639		return retval;
1640
1641	if (netxen_nic_wol_supported(adapter)) {
1642		pci_enable_wake(pdev, PCI_D3cold, 1);
1643		pci_enable_wake(pdev, PCI_D3hot, 1);
1644	}
1645
1646	pci_disable_device(pdev);
1647	pci_set_power_state(pdev, pci_choose_state(pdev, state));
1648
1649	return 0;
1650}
1651
1652static int
1653netxen_nic_resume(struct pci_dev *pdev)
1654{
1655	return netxen_nic_attach_func(pdev);
1656}
1657#endif
1658
1659static int netxen_nic_open(struct net_device *netdev)
1660{
1661	struct netxen_adapter *adapter = netdev_priv(netdev);
1662	int err = 0;
1663
1664	if (adapter->driver_mismatch)
1665		return -EIO;
1666
1667	err = netxen_nic_attach(adapter);
1668	if (err)
1669		return err;
1670
1671	err = __netxen_nic_up(adapter, netdev);
1672	if (err)
1673		goto err_out;
1674
1675	netif_start_queue(netdev);
1676
1677	return 0;
1678
1679err_out:
1680	netxen_nic_detach(adapter);
1681	return err;
1682}
1683
1684/*
1685 * netxen_nic_close - Disables a network interface entry point
1686 */
1687static int netxen_nic_close(struct net_device *netdev)
1688{
1689	struct netxen_adapter *adapter = netdev_priv(netdev);
1690
1691	__netxen_nic_down(adapter, netdev);
1692	return 0;
1693}
1694
1695static void
1696netxen_tso_check(struct net_device *netdev,
1697		struct nx_host_tx_ring *tx_ring,
1698		struct cmd_desc_type0 *first_desc,
1699		struct sk_buff *skb)
1700{
1701	u8 opcode = TX_ETHER_PKT;
1702	__be16 protocol = skb->protocol;
1703	u16 flags = 0, vid = 0;
1704	u32 producer;
1705	int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1706	struct cmd_desc_type0 *hwdesc;
1707	struct vlan_ethhdr *vh;
1708
1709	if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1710
1711		vh = (struct vlan_ethhdr *)skb->data;
1712		protocol = vh->h_vlan_encapsulated_proto;
1713		flags = FLAGS_VLAN_TAGGED;
1714
1715	} else if (vlan_tx_tag_present(skb)) {
1716
1717		flags = FLAGS_VLAN_OOB;
1718		vid = vlan_tx_tag_get(skb);
1719		netxen_set_tx_vlan_tci(first_desc, vid);
1720		vlan_oob = 1;
1721	}
1722
1723	if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1724			skb_shinfo(skb)->gso_size > 0) {
1725
1726		hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1727
1728		first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1729		first_desc->total_hdr_length = hdr_len;
1730		if (vlan_oob) {
1731			first_desc->total_hdr_length += VLAN_HLEN;
1732			first_desc->tcp_hdr_offset = VLAN_HLEN;
1733			first_desc->ip_hdr_offset = VLAN_HLEN;
1734			/* Only in case of TSO on vlan device */
1735			flags |= FLAGS_VLAN_TAGGED;
1736		}
1737
1738		opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1739				TX_TCP_LSO6 : TX_TCP_LSO;
1740		tso = 1;
1741
1742	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1743		u8 l4proto;
1744
1745		if (protocol == cpu_to_be16(ETH_P_IP)) {
1746			l4proto = ip_hdr(skb)->protocol;
1747
1748			if (l4proto == IPPROTO_TCP)
1749				opcode = TX_TCP_PKT;
1750			else if(l4proto == IPPROTO_UDP)
1751				opcode = TX_UDP_PKT;
1752		} else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1753			l4proto = ipv6_hdr(skb)->nexthdr;
1754
1755			if (l4proto == IPPROTO_TCP)
1756				opcode = TX_TCPV6_PKT;
1757			else if(l4proto == IPPROTO_UDP)
1758				opcode = TX_UDPV6_PKT;
1759		}
1760	}
1761
1762	first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1763	first_desc->ip_hdr_offset += skb_network_offset(skb);
1764	netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1765
1766	if (!tso)
1767		return;
1768
1769	/* For LSO, we need to copy the MAC/IP/TCP headers into
1770	 * the descriptor ring
1771	 */
1772	producer = tx_ring->producer;
1773	copied = 0;
1774	offset = 2;
1775
1776	if (vlan_oob) {
1777		/* Create a TSO vlan header template for firmware */
1778
1779		hwdesc = &tx_ring->desc_head[producer];
1780		tx_ring->cmd_buf_arr[producer].skb = NULL;
1781
1782		copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1783				hdr_len + VLAN_HLEN);
1784
1785		vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1786		skb_copy_from_linear_data(skb, vh, 12);
1787		vh->h_vlan_proto = htons(ETH_P_8021Q);
1788		vh->h_vlan_TCI = htons(vid);
1789		skb_copy_from_linear_data_offset(skb, 12,
1790				(char *)vh + 16, copy_len - 16);
1791
1792		copied = copy_len - VLAN_HLEN;
1793		offset = 0;
1794
1795		producer = get_next_index(producer, tx_ring->num_desc);
1796	}
1797
1798	while (copied < hdr_len) {
1799
1800		copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1801				(hdr_len - copied));
1802
1803		hwdesc = &tx_ring->desc_head[producer];
1804		tx_ring->cmd_buf_arr[producer].skb = NULL;
1805
1806		skb_copy_from_linear_data_offset(skb, copied,
1807				 (char *)hwdesc + offset, copy_len);
1808
1809		copied += copy_len;
1810		offset = 0;
1811
1812		producer = get_next_index(producer, tx_ring->num_desc);
1813	}
1814
1815	tx_ring->producer = producer;
1816	barrier();
1817}
1818
1819static int
1820netxen_map_tx_skb(struct pci_dev *pdev,
1821		struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
1822{
1823	struct netxen_skb_frag *nf;
1824	struct skb_frag_struct *frag;
1825	int i, nr_frags;
1826	dma_addr_t map;
1827
1828	nr_frags = skb_shinfo(skb)->nr_frags;
1829	nf = &pbuf->frag_array[0];
1830
1831	map = pci_map_single(pdev, skb->data,
1832			skb_headlen(skb), PCI_DMA_TODEVICE);
1833	if (pci_dma_mapping_error(pdev, map))
1834		goto out_err;
1835
1836	nf->dma = map;
1837	nf->length = skb_headlen(skb);
1838
1839	for (i = 0; i < nr_frags; i++) {
1840		frag = &skb_shinfo(skb)->frags[i];
1841		nf = &pbuf->frag_array[i+1];
1842
1843		map = pci_map_page(pdev, frag->page, frag->page_offset,
1844				frag->size, PCI_DMA_TODEVICE);
1845		if (pci_dma_mapping_error(pdev, map))
1846			goto unwind;
1847
1848		nf->dma = map;
1849		nf->length = frag->size;
1850	}
1851
1852	return 0;
1853
1854unwind:
1855	while (--i >= 0) {
1856		nf = &pbuf->frag_array[i+1];
1857		pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1858	}
1859
1860	nf = &pbuf->frag_array[0];
1861	pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1862
1863out_err:
1864	return -ENOMEM;
1865}
1866
1867static inline void
1868netxen_clear_cmddesc(u64 *desc)
1869{
1870	desc[0] = 0ULL;
1871	desc[2] = 0ULL;
1872}
1873
1874static netdev_tx_t
1875netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1876{
1877	struct netxen_adapter *adapter = netdev_priv(netdev);
1878	struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
1879	struct netxen_cmd_buffer *pbuf;
1880	struct netxen_skb_frag *buffrag;
1881	struct cmd_desc_type0 *hwdesc, *first_desc;
1882	struct pci_dev *pdev;
1883	int i, k;
1884	int delta = 0;
1885	struct skb_frag_struct *frag;
1886
1887	u32 producer;
1888	int frag_count, no_of_desc;
1889	u32 num_txd = tx_ring->num_desc;
1890
1891	frag_count = skb_shinfo(skb)->nr_frags + 1;
1892
1893	/* 14 frags supported for normal packet and
1894	 * 32 frags supported for TSO packet
1895	 */
1896	if (!skb_is_gso(skb) && frag_count > NETXEN_MAX_FRAGS_PER_TX) {
1897
1898		for (i = 0; i < (frag_count - NETXEN_MAX_FRAGS_PER_TX); i++) {
1899			frag = &skb_shinfo(skb)->frags[i];
1900			delta += frag->size;
1901		}
1902
1903		if (!__pskb_pull_tail(skb, delta))
1904			goto drop_packet;
1905
1906		frag_count = 1 + skb_shinfo(skb)->nr_frags;
1907	}
1908	/* 4 fragments per cmd des */
1909	no_of_desc = (frag_count + 3) >> 2;
1910
1911	if (unlikely(netxen_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
1912		netif_stop_queue(netdev);
1913		smp_mb();
1914		if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH)
1915			netif_start_queue(netdev);
1916		else
1917			return NETDEV_TX_BUSY;
1918	}
1919
1920	producer = tx_ring->producer;
1921	pbuf = &tx_ring->cmd_buf_arr[producer];
1922
1923	pdev = adapter->pdev;
1924
1925	if (netxen_map_tx_skb(pdev, skb, pbuf))
1926		goto drop_packet;
1927
1928	pbuf->skb = skb;
1929	pbuf->frag_count = frag_count;
1930
1931	first_desc = hwdesc = &tx_ring->desc_head[producer];
1932	netxen_clear_cmddesc((u64 *)hwdesc);
1933
1934	netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
1935	netxen_set_tx_port(first_desc, adapter->portnum);
1936
1937	for (i = 0; i < frag_count; i++) {
1938
1939		k = i % 4;
1940
1941		if ((k == 0) && (i > 0)) {
1942			/* move to next desc.*/
1943			producer = get_next_index(producer, num_txd);
1944			hwdesc = &tx_ring->desc_head[producer];
1945			netxen_clear_cmddesc((u64 *)hwdesc);
1946			tx_ring->cmd_buf_arr[producer].skb = NULL;
1947		}
1948
1949		buffrag = &pbuf->frag_array[i];
1950
1951		hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
1952		switch (k) {
1953		case 0:
1954			hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1955			break;
1956		case 1:
1957			hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
1958			break;
1959		case 2:
1960			hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
1961			break;
1962		case 3:
1963			hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
1964			break;
1965		}
1966	}
1967
1968	tx_ring->producer = get_next_index(producer, num_txd);
1969
1970	netxen_tso_check(netdev, tx_ring, first_desc, skb);
1971
1972	adapter->stats.txbytes += skb->len;
1973	adapter->stats.xmitcalled++;
1974
1975	netxen_nic_update_cmd_producer(adapter, tx_ring);
1976
1977	return NETDEV_TX_OK;
1978
1979drop_packet:
1980	adapter->stats.txdropped++;
1981	dev_kfree_skb_any(skb);
1982	return NETDEV_TX_OK;
1983}
1984
1985static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1986{
1987	struct net_device *netdev = adapter->netdev;
1988	uint32_t temp, temp_state, temp_val;
1989	int rv = 0;
1990
1991	temp = NXRD32(adapter, CRB_TEMP_STATE);
1992
1993	temp_state = nx_get_temp_state(temp);
1994	temp_val = nx_get_temp_val(temp);
1995
1996	if (temp_state == NX_TEMP_PANIC) {
1997		printk(KERN_ALERT
1998		       "%s: Device temperature %d degrees C exceeds"
1999		       " maximum allowed. Hardware has been shut down.\n",
2000		       netdev->name, temp_val);
2001		rv = 1;
2002	} else if (temp_state == NX_TEMP_WARN) {
2003		if (adapter->temp == NX_TEMP_NORMAL) {
2004			printk(KERN_ALERT
2005			       "%s: Device temperature %d degrees C "
2006			       "exceeds operating range."
2007			       " Immediate action needed.\n",
2008			       netdev->name, temp_val);
2009		}
2010	} else {
2011		if (adapter->temp == NX_TEMP_WARN) {
2012			printk(KERN_INFO
2013			       "%s: Device temperature is now %d degrees C"
2014			       " in normal range.\n", netdev->name,
2015			       temp_val);
2016		}
2017	}
2018	adapter->temp = temp_

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