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