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