linux/drivers/net/ethernet/qlogic/qlcnic/qlcnic_main.c
<<
>>
Prefs
   1/*
   2 * QLogic qlcnic NIC Driver
   3 * Copyright (c)  2009-2010 QLogic Corporation
   4 *
   5 * See LICENSE.qlcnic for copyright and licensing details.
   6 */
   7
   8#include <linux/slab.h>
   9#include <linux/vmalloc.h>
  10#include <linux/interrupt.h>
  11
  12#include "qlcnic.h"
  13
  14#include <linux/swab.h>
  15#include <linux/dma-mapping.h>
  16#include <net/ip.h>
  17#include <linux/ipv6.h>
  18#include <linux/inetdevice.h>
  19#include <linux/sysfs.h>
  20#include <linux/aer.h>
  21#include <linux/log2.h>
  22
  23MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
  24MODULE_LICENSE("GPL");
  25MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
  26MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
  27
  28char qlcnic_driver_name[] = "qlcnic";
  29static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
  30        "Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
  31
  32static struct workqueue_struct *qlcnic_wq;
  33static int qlcnic_mac_learn;
  34module_param(qlcnic_mac_learn, int, 0444);
  35MODULE_PARM_DESC(qlcnic_mac_learn, "Mac Filter (0=disabled, 1=enabled)");
  36
  37static int use_msi = 1;
  38module_param(use_msi, int, 0444);
  39MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
  40
  41static int use_msi_x = 1;
  42module_param(use_msi_x, int, 0444);
  43MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
  44
  45static int auto_fw_reset = 1;
  46module_param(auto_fw_reset, int, 0644);
  47MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
  48
  49static int load_fw_file;
  50module_param(load_fw_file, int, 0444);
  51MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
  52
  53static int qlcnic_config_npars;
  54module_param(qlcnic_config_npars, int, 0444);
  55MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
  56
  57static int __devinit qlcnic_probe(struct pci_dev *pdev,
  58                const struct pci_device_id *ent);
  59static void __devexit qlcnic_remove(struct pci_dev *pdev);
  60static int qlcnic_open(struct net_device *netdev);
  61static int qlcnic_close(struct net_device *netdev);
  62static void qlcnic_tx_timeout(struct net_device *netdev);
  63static void qlcnic_attach_work(struct work_struct *work);
  64static void qlcnic_fwinit_work(struct work_struct *work);
  65static void qlcnic_fw_poll_work(struct work_struct *work);
  66static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
  67                work_func_t func, int delay);
  68static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
  69static int qlcnic_poll(struct napi_struct *napi, int budget);
  70static int qlcnic_rx_poll(struct napi_struct *napi, int budget);
  71#ifdef CONFIG_NET_POLL_CONTROLLER
  72static void qlcnic_poll_controller(struct net_device *netdev);
  73#endif
  74
  75static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
  76static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
  77static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
  78static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
  79
  80static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
  81static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8);
  82static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
  83
  84static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
  85static irqreturn_t qlcnic_intr(int irq, void *data);
  86static irqreturn_t qlcnic_msi_intr(int irq, void *data);
  87static irqreturn_t qlcnic_msix_intr(int irq, void *data);
  88
  89static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
  90static void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long);
  91static int qlcnic_start_firmware(struct qlcnic_adapter *);
  92
  93static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
  94static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
  95static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
  96static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
  97static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
  98static void qlcnic_set_netdev_features(struct qlcnic_adapter *,
  99                                struct qlcnic_esw_func_cfg *);
 100static void qlcnic_vlan_rx_add(struct net_device *, u16);
 101static void qlcnic_vlan_rx_del(struct net_device *, u16);
 102
 103/*  PCI Device ID Table  */
 104#define ENTRY(device) \
 105        {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
 106        .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
 107
 108#define PCI_DEVICE_ID_QLOGIC_QLE824X  0x8020
 109
 110static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
 111        ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
 112        {0,}
 113};
 114
 115MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
 116
 117
 118inline void
 119qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
 120                struct qlcnic_host_tx_ring *tx_ring)
 121{
 122        writel(tx_ring->producer, tx_ring->crb_cmd_producer);
 123}
 124
 125static const u32 msi_tgt_status[8] = {
 126        ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
 127        ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
 128        ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
 129        ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
 130};
 131
 132static const
 133struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
 134
 135static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
 136{
 137        writel(0, sds_ring->crb_intr_mask);
 138}
 139
 140static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
 141{
 142        struct qlcnic_adapter *adapter = sds_ring->adapter;
 143
 144        writel(0x1, sds_ring->crb_intr_mask);
 145
 146        if (!QLCNIC_IS_MSI_FAMILY(adapter))
 147                writel(0xfbff, adapter->tgt_mask_reg);
 148}
 149
 150static int
 151qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
 152{
 153        int size = sizeof(struct qlcnic_host_sds_ring) * count;
 154
 155        recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
 156
 157        return recv_ctx->sds_rings == NULL;
 158}
 159
 160static void
 161qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
 162{
 163        if (recv_ctx->sds_rings != NULL)
 164                kfree(recv_ctx->sds_rings);
 165
 166        recv_ctx->sds_rings = NULL;
 167}
 168
 169static int
 170qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
 171{
 172        int ring;
 173        struct qlcnic_host_sds_ring *sds_ring;
 174        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
 175
 176        if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
 177                return -ENOMEM;
 178
 179        for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 180                sds_ring = &recv_ctx->sds_rings[ring];
 181
 182                if (ring == adapter->max_sds_rings - 1)
 183                        netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
 184                                QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
 185                else
 186                        netif_napi_add(netdev, &sds_ring->napi,
 187                                qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
 188        }
 189
 190        return 0;
 191}
 192
 193static void
 194qlcnic_napi_del(struct qlcnic_adapter *adapter)
 195{
 196        int ring;
 197        struct qlcnic_host_sds_ring *sds_ring;
 198        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
 199
 200        for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 201                sds_ring = &recv_ctx->sds_rings[ring];
 202                netif_napi_del(&sds_ring->napi);
 203        }
 204
 205        qlcnic_free_sds_rings(adapter->recv_ctx);
 206}
 207
 208static void
 209qlcnic_napi_enable(struct qlcnic_adapter *adapter)
 210{
 211        int ring;
 212        struct qlcnic_host_sds_ring *sds_ring;
 213        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
 214
 215        if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
 216                return;
 217
 218        for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 219                sds_ring = &recv_ctx->sds_rings[ring];
 220                napi_enable(&sds_ring->napi);
 221                qlcnic_enable_int(sds_ring);
 222        }
 223}
 224
 225static void
 226qlcnic_napi_disable(struct qlcnic_adapter *adapter)
 227{
 228        int ring;
 229        struct qlcnic_host_sds_ring *sds_ring;
 230        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
 231
 232        if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
 233                return;
 234
 235        for (ring = 0; ring < adapter->max_sds_rings; ring++) {
 236                sds_ring = &recv_ctx->sds_rings[ring];
 237                qlcnic_disable_int(sds_ring);
 238                napi_synchronize(&sds_ring->napi);
 239                napi_disable(&sds_ring->napi);
 240        }
 241}
 242
 243static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
 244{
 245        memset(&adapter->stats, 0, sizeof(adapter->stats));
 246}
 247
 248static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
 249{
 250        u32 control;
 251        int pos;
 252
 253        pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
 254        if (pos) {
 255                pci_read_config_dword(pdev, pos, &control);
 256                if (enable)
 257                        control |= PCI_MSIX_FLAGS_ENABLE;
 258                else
 259                        control = 0;
 260                pci_write_config_dword(pdev, pos, control);
 261        }
 262}
 263
 264static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
 265{
 266        int i;
 267
 268        for (i = 0; i < count; i++)
 269                adapter->msix_entries[i].entry = i;
 270}
 271
 272static int
 273qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
 274{
 275        u8 mac_addr[ETH_ALEN];
 276        struct net_device *netdev = adapter->netdev;
 277        struct pci_dev *pdev = adapter->pdev;
 278
 279        if (qlcnic_get_mac_address(adapter, mac_addr) != 0)
 280                return -EIO;
 281
 282        memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
 283        memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
 284        memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
 285
 286        /* set station address */
 287
 288        if (!is_valid_ether_addr(netdev->perm_addr))
 289                dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
 290                                        netdev->dev_addr);
 291
 292        return 0;
 293}
 294
 295static int qlcnic_set_mac(struct net_device *netdev, void *p)
 296{
 297        struct qlcnic_adapter *adapter = netdev_priv(netdev);
 298        struct sockaddr *addr = p;
 299
 300        if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
 301                return -EOPNOTSUPP;
 302
 303        if (!is_valid_ether_addr(addr->sa_data))
 304                return -EINVAL;
 305
 306        if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
 307                netif_device_detach(netdev);
 308                qlcnic_napi_disable(adapter);
 309        }
 310
 311        memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
 312        memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
 313        qlcnic_set_multi(adapter->netdev);
 314
 315        if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
 316                netif_device_attach(netdev);
 317                qlcnic_napi_enable(adapter);
 318        }
 319        return 0;
 320}
 321
 322static const struct net_device_ops qlcnic_netdev_ops = {
 323        .ndo_open          = qlcnic_open,
 324        .ndo_stop          = qlcnic_close,
 325        .ndo_start_xmit    = qlcnic_xmit_frame,
 326        .ndo_get_stats     = qlcnic_get_stats,
 327        .ndo_validate_addr = eth_validate_addr,
 328        .ndo_set_rx_mode   = qlcnic_set_multi,
 329        .ndo_set_mac_address    = qlcnic_set_mac,
 330        .ndo_change_mtu    = qlcnic_change_mtu,
 331        .ndo_fix_features  = qlcnic_fix_features,
 332        .ndo_set_features  = qlcnic_set_features,
 333        .ndo_tx_timeout    = qlcnic_tx_timeout,
 334        .ndo_vlan_rx_add_vid    = qlcnic_vlan_rx_add,
 335        .ndo_vlan_rx_kill_vid   = qlcnic_vlan_rx_del,
 336#ifdef CONFIG_NET_POLL_CONTROLLER
 337        .ndo_poll_controller = qlcnic_poll_controller,
 338#endif
 339};
 340
 341static struct qlcnic_nic_template qlcnic_ops = {
 342        .config_bridged_mode = qlcnic_config_bridged_mode,
 343        .config_led = qlcnic_config_led,
 344        .start_firmware = qlcnic_start_firmware
 345};
 346
 347static struct qlcnic_nic_template qlcnic_vf_ops = {
 348        .config_bridged_mode = qlcnicvf_config_bridged_mode,
 349        .config_led = qlcnicvf_config_led,
 350        .start_firmware = qlcnicvf_start_firmware
 351};
 352
 353static int qlcnic_enable_msix(struct qlcnic_adapter *adapter, u32 num_msix)
 354{
 355        struct pci_dev *pdev = adapter->pdev;
 356        int err = -1;
 357
 358        adapter->max_sds_rings = 1;
 359        adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
 360        qlcnic_set_msix_bit(pdev, 0);
 361
 362        if (adapter->msix_supported) {
 363 enable_msix:
 364                qlcnic_init_msix_entries(adapter, num_msix);
 365                err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
 366                if (err == 0) {
 367                        adapter->flags |= QLCNIC_MSIX_ENABLED;
 368                        qlcnic_set_msix_bit(pdev, 1);
 369
 370                        adapter->max_sds_rings = num_msix;
 371
 372                        dev_info(&pdev->dev, "using msi-x interrupts\n");
 373                        return err;
 374                }
 375                if (err > 0) {
 376                        num_msix = rounddown_pow_of_two(err);
 377                        if (num_msix)
 378                                goto enable_msix;
 379                }
 380        }
 381        return err;
 382}
 383
 384
 385static void qlcnic_enable_msi_legacy(struct qlcnic_adapter *adapter)
 386{
 387        const struct qlcnic_legacy_intr_set *legacy_intrp;
 388        struct pci_dev *pdev = adapter->pdev;
 389
 390        if (use_msi && !pci_enable_msi(pdev)) {
 391                adapter->flags |= QLCNIC_MSI_ENABLED;
 392                adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
 393                                msi_tgt_status[adapter->ahw->pci_func]);
 394                dev_info(&pdev->dev, "using msi interrupts\n");
 395                adapter->msix_entries[0].vector = pdev->irq;
 396                return;
 397        }
 398
 399        legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
 400
 401        adapter->int_vec_bit = legacy_intrp->int_vec_bit;
 402        adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
 403                        legacy_intrp->tgt_status_reg);
 404        adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
 405                        legacy_intrp->tgt_mask_reg);
 406        adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
 407
 408        adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
 409                        ISR_INT_STATE_REG);
 410        dev_info(&pdev->dev, "using legacy interrupts\n");
 411        adapter->msix_entries[0].vector = pdev->irq;
 412}
 413
 414static void
 415qlcnic_setup_intr(struct qlcnic_adapter *adapter)
 416{
 417        int num_msix;
 418
 419        if (adapter->msix_supported) {
 420                num_msix = rounddown_pow_of_two(min_t(int, num_online_cpus(),
 421                                QLCNIC_DEF_NUM_STS_DESC_RINGS));
 422        } else
 423                num_msix = 1;
 424
 425        if (!qlcnic_enable_msix(adapter, num_msix))
 426                return;
 427
 428        qlcnic_enable_msi_legacy(adapter);
 429}
 430
 431static void
 432qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
 433{
 434        if (adapter->flags & QLCNIC_MSIX_ENABLED)
 435                pci_disable_msix(adapter->pdev);
 436        if (adapter->flags & QLCNIC_MSI_ENABLED)
 437                pci_disable_msi(adapter->pdev);
 438}
 439
 440static void
 441qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
 442{
 443        if (adapter->ahw->pci_base0 != NULL)
 444                iounmap(adapter->ahw->pci_base0);
 445}
 446
 447static int
 448qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
 449{
 450        struct qlcnic_pci_info *pci_info;
 451        int i, ret = 0;
 452        u8 pfn;
 453
 454        pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
 455        if (!pci_info)
 456                return -ENOMEM;
 457
 458        adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
 459                                QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
 460        if (!adapter->npars) {
 461                ret = -ENOMEM;
 462                goto err_pci_info;
 463        }
 464
 465        adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
 466                                QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
 467        if (!adapter->eswitch) {
 468                ret = -ENOMEM;
 469                goto err_npars;
 470        }
 471
 472        ret = qlcnic_get_pci_info(adapter, pci_info);
 473        if (ret)
 474                goto err_eswitch;
 475
 476        for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
 477                pfn = pci_info[i].id;
 478                if (pfn > QLCNIC_MAX_PCI_FUNC) {
 479                        ret = QL_STATUS_INVALID_PARAM;
 480                        goto err_eswitch;
 481                }
 482                adapter->npars[pfn].active = (u8)pci_info[i].active;
 483                adapter->npars[pfn].type = (u8)pci_info[i].type;
 484                adapter->npars[pfn].phy_port = (u8)pci_info[i].default_port;
 485                adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
 486                adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
 487        }
 488
 489        for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
 490                adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
 491
 492        kfree(pci_info);
 493        return 0;
 494
 495err_eswitch:
 496        kfree(adapter->eswitch);
 497        adapter->eswitch = NULL;
 498err_npars:
 499        kfree(adapter->npars);
 500        adapter->npars = NULL;
 501err_pci_info:
 502        kfree(pci_info);
 503
 504        return ret;
 505}
 506
 507static int
 508qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
 509{
 510        u8 id;
 511        u32 ref_count;
 512        int i, ret = 1;
 513        u32 data = QLCNIC_MGMT_FUNC;
 514        void __iomem *priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
 515
 516        /* If other drivers are not in use set their privilege level */
 517        ref_count = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
 518        ret = qlcnic_api_lock(adapter);
 519        if (ret)
 520                goto err_lock;
 521
 522        if (qlcnic_config_npars) {
 523                for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
 524                        id = i;
 525                        if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
 526                                id == adapter->ahw->pci_func)
 527                                continue;
 528                        data |= (qlcnic_config_npars &
 529                                        QLC_DEV_SET_DRV(0xf, id));
 530                }
 531        } else {
 532                data = readl(priv_op);
 533                data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw->pci_func)) |
 534                        (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
 535                        adapter->ahw->pci_func));
 536        }
 537        writel(data, priv_op);
 538        qlcnic_api_unlock(adapter);
 539err_lock:
 540        return ret;
 541}
 542
 543static void
 544qlcnic_check_vf(struct qlcnic_adapter *adapter)
 545{
 546        void __iomem *msix_base_addr;
 547        void __iomem *priv_op;
 548        u32 func;
 549        u32 msix_base;
 550        u32 op_mode, priv_level;
 551
 552        /* Determine FW API version */
 553        adapter->fw_hal_version = readl(adapter->ahw->pci_base0 +
 554                                        QLCNIC_FW_API);
 555
 556        /* Find PCI function number */
 557        pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
 558        msix_base_addr = adapter->ahw->pci_base0 + QLCNIC_MSIX_BASE;
 559        msix_base = readl(msix_base_addr);
 560        func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
 561        adapter->ahw->pci_func = func;
 562
 563        /* Determine function privilege level */
 564        priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
 565        op_mode = readl(priv_op);
 566        if (op_mode == QLC_DEV_DRV_DEFAULT)
 567                priv_level = QLCNIC_MGMT_FUNC;
 568        else
 569                priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
 570
 571        if (priv_level == QLCNIC_NON_PRIV_FUNC) {
 572                adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
 573                dev_info(&adapter->pdev->dev,
 574                        "HAL Version: %d Non Privileged function\n",
 575                        adapter->fw_hal_version);
 576                adapter->nic_ops = &qlcnic_vf_ops;
 577        } else
 578                adapter->nic_ops = &qlcnic_ops;
 579}
 580
 581static int
 582qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
 583{
 584        void __iomem *mem_ptr0 = NULL;
 585        resource_size_t mem_base;
 586        unsigned long mem_len, pci_len0 = 0;
 587
 588        struct pci_dev *pdev = adapter->pdev;
 589
 590        /* remap phys address */
 591        mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
 592        mem_len = pci_resource_len(pdev, 0);
 593
 594        if (mem_len == QLCNIC_PCI_2MB_SIZE) {
 595
 596                mem_ptr0 = pci_ioremap_bar(pdev, 0);
 597                if (mem_ptr0 == NULL) {
 598                        dev_err(&pdev->dev, "failed to map PCI bar 0\n");
 599                        return -EIO;
 600                }
 601                pci_len0 = mem_len;
 602        } else {
 603                return -EIO;
 604        }
 605
 606        dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
 607
 608        adapter->ahw->pci_base0 = mem_ptr0;
 609        adapter->ahw->pci_len0 = pci_len0;
 610
 611        qlcnic_check_vf(adapter);
 612
 613        adapter->ahw->ocm_win_crb = qlcnic_get_ioaddr(adapter,
 614                QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(
 615                        adapter->ahw->pci_func)));
 616
 617        return 0;
 618}
 619
 620static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
 621{
 622        struct pci_dev *pdev = adapter->pdev;
 623        int i, found = 0;
 624
 625        for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
 626                if (qlcnic_boards[i].vendor == pdev->vendor &&
 627                        qlcnic_boards[i].device == pdev->device &&
 628                        qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
 629                        qlcnic_boards[i].sub_device == pdev->subsystem_device) {
 630                                sprintf(name, "%pM: %s" ,
 631                                        adapter->mac_addr,
 632                                        qlcnic_boards[i].short_name);
 633                                found = 1;
 634                                break;
 635                }
 636
 637        }
 638
 639        if (!found)
 640                sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
 641}
 642
 643static void
 644qlcnic_check_options(struct qlcnic_adapter *adapter)
 645{
 646        u32 fw_major, fw_minor, fw_build, prev_fw_version;
 647        struct pci_dev *pdev = adapter->pdev;
 648        struct qlcnic_fw_dump *fw_dump = &adapter->ahw->fw_dump;
 649
 650        prev_fw_version = adapter->fw_version;
 651
 652        fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
 653        fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
 654        fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
 655
 656        adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
 657
 658        if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC) {
 659                if (fw_dump->tmpl_hdr == NULL ||
 660                                adapter->fw_version > prev_fw_version) {
 661                        if (fw_dump->tmpl_hdr)
 662                                vfree(fw_dump->tmpl_hdr);
 663                        if (!qlcnic_fw_cmd_get_minidump_temp(adapter))
 664                                dev_info(&pdev->dev,
 665                                        "Supports FW dump capability\n");
 666                }
 667        }
 668
 669        dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
 670                        fw_major, fw_minor, fw_build);
 671        if (adapter->ahw->port_type == QLCNIC_XGBE) {
 672                if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
 673                        adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
 674                        adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
 675                } else {
 676                        adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
 677                        adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
 678                }
 679
 680                adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
 681                adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
 682
 683        } else if (adapter->ahw->port_type == QLCNIC_GBE) {
 684                adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
 685                adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
 686                adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
 687                adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
 688        }
 689
 690        adapter->msix_supported = !!use_msi_x;
 691
 692        adapter->num_txd = MAX_CMD_DESCRIPTORS;
 693
 694        adapter->max_rds_rings = MAX_RDS_RINGS;
 695}
 696
 697static int
 698qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
 699{
 700        int err;
 701        struct qlcnic_info nic_info;
 702
 703        err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
 704        if (err)
 705                return err;
 706
 707        adapter->physical_port = (u8)nic_info.phys_port;
 708        adapter->switch_mode = nic_info.switch_mode;
 709        adapter->max_tx_ques = nic_info.max_tx_ques;
 710        adapter->max_rx_ques = nic_info.max_rx_ques;
 711        adapter->capabilities = nic_info.capabilities;
 712        adapter->max_mac_filters = nic_info.max_mac_filters;
 713        adapter->max_mtu = nic_info.max_mtu;
 714
 715        if (adapter->capabilities & BIT_6)
 716                adapter->flags |= QLCNIC_ESWITCH_ENABLED;
 717        else
 718                adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
 719
 720        return err;
 721}
 722
 723static void
 724qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
 725                struct qlcnic_esw_func_cfg *esw_cfg)
 726{
 727        if (esw_cfg->discard_tagged)
 728                adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
 729        else
 730                adapter->flags |= QLCNIC_TAGGING_ENABLED;
 731
 732        if (esw_cfg->vlan_id)
 733                adapter->pvid = esw_cfg->vlan_id;
 734        else
 735                adapter->pvid = 0;
 736}
 737
 738static void
 739qlcnic_vlan_rx_add(struct net_device *netdev, u16 vid)
 740{
 741        struct qlcnic_adapter *adapter = netdev_priv(netdev);
 742        set_bit(vid, adapter->vlans);
 743}
 744
 745static void
 746qlcnic_vlan_rx_del(struct net_device *netdev, u16 vid)
 747{
 748        struct qlcnic_adapter *adapter = netdev_priv(netdev);
 749
 750        qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
 751        clear_bit(vid, adapter->vlans);
 752}
 753
 754static void
 755qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
 756                struct qlcnic_esw_func_cfg *esw_cfg)
 757{
 758        adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
 759                                QLCNIC_PROMISC_DISABLED);
 760
 761        if (esw_cfg->mac_anti_spoof)
 762                adapter->flags |= QLCNIC_MACSPOOF;
 763
 764        if (!esw_cfg->mac_override)
 765                adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
 766
 767        if (!esw_cfg->promisc_mode)
 768                adapter->flags |= QLCNIC_PROMISC_DISABLED;
 769
 770        qlcnic_set_netdev_features(adapter, esw_cfg);
 771}
 772
 773static int
 774qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
 775{
 776        struct qlcnic_esw_func_cfg esw_cfg;
 777
 778        if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
 779                return 0;
 780
 781        esw_cfg.pci_func = adapter->ahw->pci_func;
 782        if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
 783                        return -EIO;
 784        qlcnic_set_vlan_config(adapter, &esw_cfg);
 785        qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
 786
 787        return 0;
 788}
 789
 790static void
 791qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
 792                struct qlcnic_esw_func_cfg *esw_cfg)
 793{
 794        struct net_device *netdev = adapter->netdev;
 795        unsigned long features, vlan_features;
 796
 797        features = (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
 798                        NETIF_F_IPV6_CSUM | NETIF_F_GRO);
 799        vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
 800                        NETIF_F_IPV6_CSUM | NETIF_F_HW_VLAN_FILTER);
 801
 802        if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
 803                features |= (NETIF_F_TSO | NETIF_F_TSO6);
 804                vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
 805        }
 806
 807        if (netdev->features & NETIF_F_LRO)
 808                features |= NETIF_F_LRO;
 809
 810        if (esw_cfg->offload_flags & BIT_0) {
 811                netdev->features |= features;
 812                if (!(esw_cfg->offload_flags & BIT_1))
 813                        netdev->features &= ~NETIF_F_TSO;
 814                if (!(esw_cfg->offload_flags & BIT_2))
 815                        netdev->features &= ~NETIF_F_TSO6;
 816        } else {
 817                netdev->features &= ~features;
 818        }
 819
 820        netdev->vlan_features = (features & vlan_features);
 821}
 822
 823static int
 824qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
 825{
 826        void __iomem *priv_op;
 827        u32 op_mode, priv_level;
 828        int err = 0;
 829
 830        err = qlcnic_initialize_nic(adapter);
 831        if (err)
 832                return err;
 833
 834        if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
 835                return 0;
 836
 837        priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
 838        op_mode = readl(priv_op);
 839        priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
 840
 841        if (op_mode == QLC_DEV_DRV_DEFAULT)
 842                priv_level = QLCNIC_MGMT_FUNC;
 843        else
 844                priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
 845
 846        if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
 847                if (priv_level == QLCNIC_MGMT_FUNC) {
 848                        adapter->op_mode = QLCNIC_MGMT_FUNC;
 849                        err = qlcnic_init_pci_info(adapter);
 850                        if (err)
 851                                return err;
 852                        /* Set privilege level for other functions */
 853                        qlcnic_set_function_modes(adapter);
 854                        dev_info(&adapter->pdev->dev,
 855                                "HAL Version: %d, Management function\n",
 856                                adapter->fw_hal_version);
 857                } else if (priv_level == QLCNIC_PRIV_FUNC) {
 858                        adapter->op_mode = QLCNIC_PRIV_FUNC;
 859                        dev_info(&adapter->pdev->dev,
 860                                "HAL Version: %d, Privileged function\n",
 861                                adapter->fw_hal_version);
 862                }
 863        }
 864
 865        adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
 866
 867        return err;
 868}
 869
 870static int
 871qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
 872{
 873        struct qlcnic_esw_func_cfg esw_cfg;
 874        struct qlcnic_npar_info *npar;
 875        u8 i;
 876
 877        if (adapter->need_fw_reset)
 878                return 0;
 879
 880        for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
 881                if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
 882                        continue;
 883                memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
 884                esw_cfg.pci_func = i;
 885                esw_cfg.offload_flags = BIT_0;
 886                esw_cfg.mac_override = BIT_0;
 887                esw_cfg.promisc_mode = BIT_0;
 888                if (adapter->capabilities  & QLCNIC_FW_CAPABILITY_TSO)
 889                        esw_cfg.offload_flags |= (BIT_1 | BIT_2);
 890                if (qlcnic_config_switch_port(adapter, &esw_cfg))
 891                        return -EIO;
 892                npar = &adapter->npars[i];
 893                npar->pvid = esw_cfg.vlan_id;
 894                npar->mac_override = esw_cfg.mac_override;
 895                npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
 896                npar->discard_tagged = esw_cfg.discard_tagged;
 897                npar->promisc_mode = esw_cfg.promisc_mode;
 898                npar->offload_flags = esw_cfg.offload_flags;
 899        }
 900
 901        return 0;
 902}
 903
 904static int
 905qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
 906                        struct qlcnic_npar_info *npar, int pci_func)
 907{
 908        struct qlcnic_esw_func_cfg esw_cfg;
 909        esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
 910        esw_cfg.pci_func = pci_func;
 911        esw_cfg.vlan_id = npar->pvid;
 912        esw_cfg.mac_override = npar->mac_override;
 913        esw_cfg.discard_tagged = npar->discard_tagged;
 914        esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
 915        esw_cfg.offload_flags = npar->offload_flags;
 916        esw_cfg.promisc_mode = npar->promisc_mode;
 917        if (qlcnic_config_switch_port(adapter, &esw_cfg))
 918                return -EIO;
 919
 920        esw_cfg.op_mode = QLCNIC_ADD_VLAN;
 921        if (qlcnic_config_switch_port(adapter, &esw_cfg))
 922                return -EIO;
 923
 924        return 0;
 925}
 926
 927static int
 928qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
 929{
 930        int i, err;
 931        struct qlcnic_npar_info *npar;
 932        struct qlcnic_info nic_info;
 933
 934        if (!adapter->need_fw_reset)
 935                return 0;
 936
 937        /* Set the NPAR config data after FW reset */
 938        for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
 939                npar = &adapter->npars[i];
 940                if (npar->type != QLCNIC_TYPE_NIC)
 941                        continue;
 942                err = qlcnic_get_nic_info(adapter, &nic_info, i);
 943                if (err)
 944                        return err;
 945                nic_info.min_tx_bw = npar->min_bw;
 946                nic_info.max_tx_bw = npar->max_bw;
 947                err = qlcnic_set_nic_info(adapter, &nic_info);
 948                if (err)
 949                        return err;
 950
 951                if (npar->enable_pm) {
 952                        err = qlcnic_config_port_mirroring(adapter,
 953                                                        npar->dest_npar, 1, i);
 954                        if (err)
 955                                return err;
 956                }
 957                err = qlcnic_reset_eswitch_config(adapter, npar, i);
 958                if (err)
 959                        return err;
 960        }
 961        return 0;
 962}
 963
 964static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
 965{
 966        u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
 967        u32 npar_state;
 968
 969        if (adapter->op_mode == QLCNIC_MGMT_FUNC)
 970                return 0;
 971
 972        npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
 973        while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
 974                msleep(1000);
 975                npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
 976        }
 977        if (!npar_opt_timeo) {
 978                dev_err(&adapter->pdev->dev,
 979                        "Waiting for NPAR state to opertional timeout\n");
 980                return -EIO;
 981        }
 982        return 0;
 983}
 984
 985static int
 986qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
 987{
 988        int err;
 989
 990        if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
 991                    adapter->op_mode != QLCNIC_MGMT_FUNC)
 992                return 0;
 993
 994        err = qlcnic_set_default_offload_settings(adapter);
 995        if (err)
 996                return err;
 997
 998        err = qlcnic_reset_npar_config(adapter);
 999        if (err)
1000                return err;
1001
1002        qlcnic_dev_set_npar_ready(adapter);
1003
1004        return err;
1005}
1006
1007static int
1008qlcnic_start_firmware(struct qlcnic_adapter *adapter)
1009{
1010        int err;
1011
1012        err = qlcnic_can_start_firmware(adapter);
1013        if (err < 0)
1014                return err;
1015        else if (!err)
1016                goto check_fw_status;
1017
1018        if (load_fw_file)
1019                qlcnic_request_firmware(adapter);
1020        else {
1021                err = qlcnic_check_flash_fw_ver(adapter);
1022                if (err)
1023                        goto err_out;
1024
1025                adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
1026        }
1027
1028        err = qlcnic_need_fw_reset(adapter);
1029        if (err == 0)
1030                goto check_fw_status;
1031
1032        err = qlcnic_pinit_from_rom(adapter);
1033        if (err)
1034                goto err_out;
1035
1036        err = qlcnic_load_firmware(adapter);
1037        if (err)
1038                goto err_out;
1039
1040        qlcnic_release_firmware(adapter);
1041        QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
1042
1043check_fw_status:
1044        err = qlcnic_check_fw_status(adapter);
1045        if (err)
1046                goto err_out;
1047
1048        QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
1049        qlcnic_idc_debug_info(adapter, 1);
1050
1051        err = qlcnic_check_eswitch_mode(adapter);
1052        if (err) {
1053                dev_err(&adapter->pdev->dev,
1054                        "Memory allocation failed for eswitch\n");
1055                goto err_out;
1056        }
1057        err = qlcnic_set_mgmt_operations(adapter);
1058        if (err)
1059                goto err_out;
1060
1061        qlcnic_check_options(adapter);
1062        adapter->need_fw_reset = 0;
1063
1064        qlcnic_release_firmware(adapter);
1065        return 0;
1066
1067err_out:
1068        QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1069        dev_err(&adapter->pdev->dev, "Device state set to failed\n");
1070
1071        qlcnic_release_firmware(adapter);
1072        return err;
1073}
1074
1075static int
1076qlcnic_request_irq(struct qlcnic_adapter *adapter)
1077{
1078        irq_handler_t handler;
1079        struct qlcnic_host_sds_ring *sds_ring;
1080        int err, ring;
1081
1082        unsigned long flags = 0;
1083        struct net_device *netdev = adapter->netdev;
1084        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1085
1086        if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1087                handler = qlcnic_tmp_intr;
1088                if (!QLCNIC_IS_MSI_FAMILY(adapter))
1089                        flags |= IRQF_SHARED;
1090
1091        } else {
1092                if (adapter->flags & QLCNIC_MSIX_ENABLED)
1093                        handler = qlcnic_msix_intr;
1094                else if (adapter->flags & QLCNIC_MSI_ENABLED)
1095                        handler = qlcnic_msi_intr;
1096                else {
1097                        flags |= IRQF_SHARED;
1098                        handler = qlcnic_intr;
1099                }
1100        }
1101        adapter->irq = netdev->irq;
1102
1103        for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1104                sds_ring = &recv_ctx->sds_rings[ring];
1105                sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1106                err = request_irq(sds_ring->irq, handler,
1107                                  flags, sds_ring->name, sds_ring);
1108                if (err)
1109                        return err;
1110        }
1111
1112        return 0;
1113}
1114
1115static void
1116qlcnic_free_irq(struct qlcnic_adapter *adapter)
1117{
1118        int ring;
1119        struct qlcnic_host_sds_ring *sds_ring;
1120
1121        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1122
1123        for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1124                sds_ring = &recv_ctx->sds_rings[ring];
1125                free_irq(sds_ring->irq, sds_ring);
1126        }
1127}
1128
1129static int
1130__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1131{
1132        int ring;
1133        struct qlcnic_host_rds_ring *rds_ring;
1134
1135        if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1136                return -EIO;
1137
1138        if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1139                return 0;
1140        if (qlcnic_set_eswitch_port_config(adapter))
1141                return -EIO;
1142
1143        if (qlcnic_fw_create_ctx(adapter))
1144                return -EIO;
1145
1146        for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1147                rds_ring = &adapter->recv_ctx->rds_rings[ring];
1148                qlcnic_post_rx_buffers(adapter, rds_ring);
1149        }
1150
1151        qlcnic_set_multi(netdev);
1152        qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1153
1154        adapter->ahw->linkup = 0;
1155
1156        if (adapter->max_sds_rings > 1)
1157                qlcnic_config_rss(adapter, 1);
1158
1159        qlcnic_config_intr_coalesce(adapter);
1160
1161        if (netdev->features & NETIF_F_LRO)
1162                qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1163
1164        qlcnic_napi_enable(adapter);
1165
1166        qlcnic_linkevent_request(adapter, 1);
1167
1168        adapter->reset_context = 0;
1169        set_bit(__QLCNIC_DEV_UP, &adapter->state);
1170        return 0;
1171}
1172
1173/* Usage: During resume and firmware recovery module.*/
1174
1175static int
1176qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1177{
1178        int err = 0;
1179
1180        rtnl_lock();
1181        if (netif_running(netdev))
1182                err = __qlcnic_up(adapter, netdev);
1183        rtnl_unlock();
1184
1185        return err;
1186}
1187
1188static void
1189__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1190{
1191        if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1192                return;
1193
1194        if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1195                return;
1196
1197        smp_mb();
1198        spin_lock(&adapter->tx_clean_lock);
1199        netif_carrier_off(netdev);
1200        netif_tx_disable(netdev);
1201
1202        qlcnic_free_mac_list(adapter);
1203
1204        if (adapter->fhash.fnum)
1205                qlcnic_delete_lb_filters(adapter);
1206
1207        qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1208
1209        qlcnic_napi_disable(adapter);
1210
1211        qlcnic_fw_destroy_ctx(adapter);
1212
1213        qlcnic_reset_rx_buffers_list(adapter);
1214        qlcnic_release_tx_buffers(adapter);
1215        spin_unlock(&adapter->tx_clean_lock);
1216}
1217
1218/* Usage: During suspend and firmware recovery module */
1219
1220static void
1221qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1222{
1223        rtnl_lock();
1224        if (netif_running(netdev))
1225                __qlcnic_down(adapter, netdev);
1226        rtnl_unlock();
1227
1228}
1229
1230static int
1231qlcnic_attach(struct qlcnic_adapter *adapter)
1232{
1233        struct net_device *netdev = adapter->netdev;
1234        struct pci_dev *pdev = adapter->pdev;
1235        int err;
1236
1237        if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1238                return 0;
1239
1240        err = qlcnic_napi_add(adapter, netdev);
1241        if (err)
1242                return err;
1243
1244        err = qlcnic_alloc_sw_resources(adapter);
1245        if (err) {
1246                dev_err(&pdev->dev, "Error in setting sw resources\n");
1247                goto err_out_napi_del;
1248        }
1249
1250        err = qlcnic_alloc_hw_resources(adapter);
1251        if (err) {
1252                dev_err(&pdev->dev, "Error in setting hw resources\n");
1253                goto err_out_free_sw;
1254        }
1255
1256        err = qlcnic_request_irq(adapter);
1257        if (err) {
1258                dev_err(&pdev->dev, "failed to setup interrupt\n");
1259                goto err_out_free_hw;
1260        }
1261
1262        qlcnic_create_sysfs_entries(adapter);
1263
1264        adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1265        return 0;
1266
1267err_out_free_hw:
1268        qlcnic_free_hw_resources(adapter);
1269err_out_free_sw:
1270        qlcnic_free_sw_resources(adapter);
1271err_out_napi_del:
1272        qlcnic_napi_del(adapter);
1273        return err;
1274}
1275
1276static void
1277qlcnic_detach(struct qlcnic_adapter *adapter)
1278{
1279        if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1280                return;
1281
1282        qlcnic_remove_sysfs_entries(adapter);
1283
1284        qlcnic_free_hw_resources(adapter);
1285        qlcnic_release_rx_buffers(adapter);
1286        qlcnic_free_irq(adapter);
1287        qlcnic_napi_del(adapter);
1288        qlcnic_free_sw_resources(adapter);
1289
1290        adapter->is_up = 0;
1291}
1292
1293void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1294{
1295        struct qlcnic_adapter *adapter = netdev_priv(netdev);
1296        struct qlcnic_host_sds_ring *sds_ring;
1297        int ring;
1298
1299        clear_bit(__QLCNIC_DEV_UP, &adapter->state);
1300        if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1301                for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1302                        sds_ring = &adapter->recv_ctx->sds_rings[ring];
1303                        qlcnic_disable_int(sds_ring);
1304                }
1305        }
1306
1307        qlcnic_fw_destroy_ctx(adapter);
1308
1309        qlcnic_detach(adapter);
1310
1311        adapter->diag_test = 0;
1312        adapter->max_sds_rings = max_sds_rings;
1313
1314        if (qlcnic_attach(adapter))
1315                goto out;
1316
1317        if (netif_running(netdev))
1318                __qlcnic_up(adapter, netdev);
1319out:
1320        netif_device_attach(netdev);
1321}
1322
1323static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1324{
1325        int err = 0;
1326        adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1327                                GFP_KERNEL);
1328        if (!adapter->ahw) {
1329                dev_err(&adapter->pdev->dev,
1330                        "Failed to allocate recv ctx resources for adapter\n");
1331                err = -ENOMEM;
1332                goto err_out;
1333        }
1334        adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1335                                GFP_KERNEL);
1336        if (!adapter->recv_ctx) {
1337                dev_err(&adapter->pdev->dev,
1338                        "Failed to allocate recv ctx resources for adapter\n");
1339                kfree(adapter->ahw);
1340                adapter->ahw = NULL;
1341                err = -ENOMEM;
1342                goto err_out;
1343        }
1344        /* Initialize interrupt coalesce parameters */
1345        adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1346        adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1347        adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
1348err_out:
1349        return err;
1350}
1351
1352static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1353{
1354        kfree(adapter->recv_ctx);
1355        adapter->recv_ctx = NULL;
1356
1357        if (adapter->ahw->fw_dump.tmpl_hdr) {
1358                vfree(adapter->ahw->fw_dump.tmpl_hdr);
1359                adapter->ahw->fw_dump.tmpl_hdr = NULL;
1360        }
1361        kfree(adapter->ahw);
1362        adapter->ahw = NULL;
1363}
1364
1365int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1366{
1367        struct qlcnic_adapter *adapter = netdev_priv(netdev);
1368        struct qlcnic_host_sds_ring *sds_ring;
1369        struct qlcnic_host_rds_ring *rds_ring;
1370        int ring;
1371        int ret;
1372
1373        netif_device_detach(netdev);
1374
1375        if (netif_running(netdev))
1376                __qlcnic_down(adapter, netdev);
1377
1378        qlcnic_detach(adapter);
1379
1380        adapter->max_sds_rings = 1;
1381        adapter->diag_test = test;
1382
1383        ret = qlcnic_attach(adapter);
1384        if (ret) {
1385                netif_device_attach(netdev);
1386                return ret;
1387        }
1388
1389        ret = qlcnic_fw_create_ctx(adapter);
1390        if (ret) {
1391                qlcnic_detach(adapter);
1392                netif_device_attach(netdev);
1393                return ret;
1394        }
1395
1396        for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1397                rds_ring = &adapter->recv_ctx->rds_rings[ring];
1398                qlcnic_post_rx_buffers(adapter, rds_ring);
1399        }
1400
1401        if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1402                for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1403                        sds_ring = &adapter->recv_ctx->sds_rings[ring];
1404                        qlcnic_enable_int(sds_ring);
1405                }
1406        }
1407
1408        if (adapter->diag_test == QLCNIC_LOOPBACK_TEST) {
1409                adapter->ahw->loopback_state = 0;
1410                qlcnic_linkevent_request(adapter, 1);
1411        }
1412
1413        set_bit(__QLCNIC_DEV_UP, &adapter->state);
1414
1415        return 0;
1416}
1417
1418/* Reset context in hardware only */
1419static int
1420qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1421{
1422        struct net_device *netdev = adapter->netdev;
1423
1424        if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1425                return -EBUSY;
1426
1427        netif_device_detach(netdev);
1428
1429        qlcnic_down(adapter, netdev);
1430
1431        qlcnic_up(adapter, netdev);
1432
1433        netif_device_attach(netdev);
1434
1435        clear_bit(__QLCNIC_RESETTING, &adapter->state);
1436        return 0;
1437}
1438
1439int
1440qlcnic_reset_context(struct qlcnic_adapter *adapter)
1441{
1442        int err = 0;
1443        struct net_device *netdev = adapter->netdev;
1444
1445        if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1446                return -EBUSY;
1447
1448        if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1449
1450                netif_device_detach(netdev);
1451
1452                if (netif_running(netdev))
1453                        __qlcnic_down(adapter, netdev);
1454
1455                qlcnic_detach(adapter);
1456
1457                if (netif_running(netdev)) {
1458                        err = qlcnic_attach(adapter);
1459                        if (!err)
1460                                __qlcnic_up(adapter, netdev);
1461                }
1462
1463                netif_device_attach(netdev);
1464        }
1465
1466        clear_bit(__QLCNIC_RESETTING, &adapter->state);
1467        return err;
1468}
1469
1470static int
1471qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
1472                struct net_device *netdev, u8 pci_using_dac)
1473{
1474        int err;
1475        struct pci_dev *pdev = adapter->pdev;
1476
1477        adapter->mc_enabled = 0;
1478        adapter->max_mc_count = 38;
1479
1480        netdev->netdev_ops         = &qlcnic_netdev_ops;
1481        netdev->watchdog_timeo     = 5*HZ;
1482
1483        qlcnic_change_mtu(netdev, netdev->mtu);
1484
1485        SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1486
1487        netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1488                NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
1489
1490        if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
1491                netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1492        if (pci_using_dac)
1493                netdev->hw_features |= NETIF_F_HIGHDMA;
1494
1495        netdev->vlan_features = netdev->hw_features;
1496
1497        if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1498                netdev->hw_features |= NETIF_F_HW_VLAN_TX;
1499        if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1500                netdev->hw_features |= NETIF_F_LRO;
1501
1502        netdev->features |= netdev->hw_features |
1503                NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
1504
1505        netdev->irq = adapter->msix_entries[0].vector;
1506
1507        err = register_netdev(netdev);
1508        if (err) {
1509                dev_err(&pdev->dev, "failed to register net device\n");
1510                return err;
1511        }
1512
1513        return 0;
1514}
1515
1516static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1517{
1518        if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1519                        !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1520                *pci_using_dac = 1;
1521        else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1522                        !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1523                *pci_using_dac = 0;
1524        else {
1525                dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1526                return -EIO;
1527        }
1528
1529        return 0;
1530}
1531
1532static int
1533qlcnic_alloc_msix_entries(struct qlcnic_adapter *adapter, u16 count)
1534{
1535        adapter->msix_entries = kcalloc(count, sizeof(struct msix_entry),
1536                                        GFP_KERNEL);
1537
1538        if (adapter->msix_entries)
1539                return 0;
1540
1541        dev_err(&adapter->pdev->dev, "failed allocating msix_entries\n");
1542        return -ENOMEM;
1543}
1544
1545static int __devinit
1546qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1547{
1548        struct net_device *netdev = NULL;
1549        struct qlcnic_adapter *adapter = NULL;
1550        int err;
1551        uint8_t revision_id;
1552        uint8_t pci_using_dac;
1553        char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
1554
1555        err = pci_enable_device(pdev);
1556        if (err)
1557                return err;
1558
1559        if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1560                err = -ENODEV;
1561                goto err_out_disable_pdev;
1562        }
1563
1564        err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1565        if (err)
1566                goto err_out_disable_pdev;
1567
1568        err = pci_request_regions(pdev, qlcnic_driver_name);
1569        if (err)
1570                goto err_out_disable_pdev;
1571
1572        pci_set_master(pdev);
1573        pci_enable_pcie_error_reporting(pdev);
1574
1575        netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1576        if (!netdev) {
1577                dev_err(&pdev->dev, "failed to allocate net_device\n");
1578                err = -ENOMEM;
1579                goto err_out_free_res;
1580        }
1581
1582        SET_NETDEV_DEV(netdev, &pdev->dev);
1583
1584        adapter = netdev_priv(netdev);
1585        adapter->netdev  = netdev;
1586        adapter->pdev    = pdev;
1587
1588        if (qlcnic_alloc_adapter_resources(adapter))
1589                goto err_out_free_netdev;
1590
1591        adapter->dev_rst_time = jiffies;
1592        revision_id = pdev->revision;
1593        adapter->ahw->revision_id = revision_id;
1594        adapter->mac_learn = qlcnic_mac_learn;
1595
1596        rwlock_init(&adapter->ahw->crb_lock);
1597        mutex_init(&adapter->ahw->mem_lock);
1598
1599        spin_lock_init(&adapter->tx_clean_lock);
1600        INIT_LIST_HEAD(&adapter->mac_list);
1601
1602        err = qlcnic_setup_pci_map(adapter);
1603        if (err)
1604                goto err_out_free_hw;
1605
1606        /* This will be reset for mezz cards  */
1607        adapter->portnum = adapter->ahw->pci_func;
1608
1609        err = qlcnic_get_board_info(adapter);
1610        if (err) {
1611                dev_err(&pdev->dev, "Error getting board config info.\n");
1612                goto err_out_iounmap;
1613        }
1614
1615        err = qlcnic_setup_idc_param(adapter);
1616        if (err)
1617                goto err_out_iounmap;
1618
1619        adapter->flags |= QLCNIC_NEED_FLR;
1620
1621        err = adapter->nic_ops->start_firmware(adapter);
1622        if (err) {
1623                dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
1624                goto err_out_decr_ref;
1625        }
1626
1627        if (qlcnic_read_mac_addr(adapter))
1628                dev_warn(&pdev->dev, "failed to read mac addr\n");
1629
1630        if (adapter->portnum == 0) {
1631                get_brd_name(adapter, brd_name);
1632
1633                pr_info("%s: %s Board Chip rev 0x%x\n",
1634                                module_name(THIS_MODULE),
1635                                brd_name, adapter->ahw->revision_id);
1636        }
1637
1638        qlcnic_clear_stats(adapter);
1639
1640        err = qlcnic_alloc_msix_entries(adapter, adapter->max_rx_ques);
1641        if (err)
1642                goto err_out_decr_ref;
1643
1644        qlcnic_setup_intr(adapter);
1645
1646        err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
1647        if (err)
1648                goto err_out_disable_msi;
1649
1650        pci_set_drvdata(pdev, adapter);
1651
1652        qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1653
1654        switch (adapter->ahw->port_type) {
1655        case QLCNIC_GBE:
1656                dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1657                                adapter->netdev->name);
1658                break;
1659        case QLCNIC_XGBE:
1660                dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1661                                adapter->netdev->name);
1662                break;
1663        }
1664
1665        if (adapter->mac_learn)
1666                qlcnic_alloc_lb_filters_mem(adapter);
1667
1668        qlcnic_create_diag_entries(adapter);
1669
1670        return 0;
1671
1672err_out_disable_msi:
1673        qlcnic_teardown_intr(adapter);
1674        kfree(adapter->msix_entries);
1675
1676err_out_decr_ref:
1677        qlcnic_clr_all_drv_state(adapter, 0);
1678
1679err_out_iounmap:
1680        qlcnic_cleanup_pci_map(adapter);
1681
1682err_out_free_hw:
1683        qlcnic_free_adapter_resources(adapter);
1684
1685err_out_free_netdev:
1686        free_netdev(netdev);
1687
1688err_out_free_res:
1689        pci_release_regions(pdev);
1690
1691err_out_disable_pdev:
1692        pci_set_drvdata(pdev, NULL);
1693        pci_disable_device(pdev);
1694        return err;
1695}
1696
1697static void __devexit qlcnic_remove(struct pci_dev *pdev)
1698{
1699        struct qlcnic_adapter *adapter;
1700        struct net_device *netdev;
1701
1702        adapter = pci_get_drvdata(pdev);
1703        if (adapter == NULL)
1704                return;
1705
1706        netdev = adapter->netdev;
1707
1708        qlcnic_cancel_fw_work(adapter);
1709
1710        unregister_netdev(netdev);
1711
1712        qlcnic_detach(adapter);
1713
1714        if (adapter->npars != NULL)
1715                kfree(adapter->npars);
1716        if (adapter->eswitch != NULL)
1717                kfree(adapter->eswitch);
1718
1719        qlcnic_clr_all_drv_state(adapter, 0);
1720
1721        clear_bit(__QLCNIC_RESETTING, &adapter->state);
1722
1723        qlcnic_free_lb_filters_mem(adapter);
1724
1725        qlcnic_teardown_intr(adapter);
1726        kfree(adapter->msix_entries);
1727
1728        qlcnic_remove_diag_entries(adapter);
1729
1730        qlcnic_cleanup_pci_map(adapter);
1731
1732        qlcnic_release_firmware(adapter);
1733
1734        pci_disable_pcie_error_reporting(pdev);
1735        pci_release_regions(pdev);
1736        pci_disable_device(pdev);
1737        pci_set_drvdata(pdev, NULL);
1738
1739        qlcnic_free_adapter_resources(adapter);
1740        free_netdev(netdev);
1741}
1742static int __qlcnic_shutdown(struct pci_dev *pdev)
1743{
1744        struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1745        struct net_device *netdev = adapter->netdev;
1746        int retval;
1747
1748        netif_device_detach(netdev);
1749
1750        qlcnic_cancel_fw_work(adapter);
1751
1752        if (netif_running(netdev))
1753                qlcnic_down(adapter, netdev);
1754
1755        qlcnic_clr_all_drv_state(adapter, 0);
1756
1757        clear_bit(__QLCNIC_RESETTING, &adapter->state);
1758
1759        retval = pci_save_state(pdev);
1760        if (retval)
1761                return retval;
1762
1763        if (qlcnic_wol_supported(adapter)) {
1764                pci_enable_wake(pdev, PCI_D3cold, 1);
1765                pci_enable_wake(pdev, PCI_D3hot, 1);
1766        }
1767
1768        return 0;
1769}
1770
1771static void qlcnic_shutdown(struct pci_dev *pdev)
1772{
1773        if (__qlcnic_shutdown(pdev))
1774                return;
1775
1776        pci_disable_device(pdev);
1777}
1778
1779#ifdef CONFIG_PM
1780static int
1781qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1782{
1783        int retval;
1784
1785        retval = __qlcnic_shutdown(pdev);
1786        if (retval)
1787                return retval;
1788
1789        pci_set_power_state(pdev, pci_choose_state(pdev, state));
1790        return 0;
1791}
1792
1793static int
1794qlcnic_resume(struct pci_dev *pdev)
1795{
1796        struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1797        struct net_device *netdev = adapter->netdev;
1798        int err;
1799
1800        err = pci_enable_device(pdev);
1801        if (err)
1802                return err;
1803
1804        pci_set_power_state(pdev, PCI_D0);
1805        pci_set_master(pdev);
1806        pci_restore_state(pdev);
1807
1808        err = adapter->nic_ops->start_firmware(adapter);
1809        if (err) {
1810                dev_err(&pdev->dev, "failed to start firmware\n");
1811                return err;
1812        }
1813
1814        if (netif_running(netdev)) {
1815                err = qlcnic_up(adapter, netdev);
1816                if (err)
1817                        goto done;
1818
1819                qlcnic_restore_indev_addr(netdev, NETDEV_UP);
1820        }
1821done:
1822        netif_device_attach(netdev);
1823        qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1824        return 0;
1825}
1826#endif
1827
1828static int qlcnic_open(struct net_device *netdev)
1829{
1830        struct qlcnic_adapter *adapter = netdev_priv(netdev);
1831        int err;
1832
1833        netif_carrier_off(netdev);
1834
1835        err = qlcnic_attach(adapter);
1836        if (err)
1837                return err;
1838
1839        err = __qlcnic_up(adapter, netdev);
1840        if (err)
1841                goto err_out;
1842
1843        netif_start_queue(netdev);
1844
1845        return 0;
1846
1847err_out:
1848        qlcnic_detach(adapter);
1849        return err;
1850}
1851
1852/*
1853 * qlcnic_close - Disables a network interface entry point
1854 */
1855static int qlcnic_close(struct net_device *netdev)
1856{
1857        struct qlcnic_adapter *adapter = netdev_priv(netdev);
1858
1859        __qlcnic_down(adapter, netdev);
1860        return 0;
1861}
1862
1863void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
1864{
1865        void *head;
1866        int i;
1867
1868        if (adapter->fhash.fmax && adapter->fhash.fhead)
1869                return;
1870
1871        spin_lock_init(&adapter->mac_learn_lock);
1872
1873        head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1874                                                                GFP_KERNEL);
1875        if (!head)
1876                return;
1877
1878        adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
1879        adapter->fhash.fhead = head;
1880
1881        for (i = 0; i < adapter->fhash.fmax; i++)
1882                INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1883}
1884
1885static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1886{
1887        if (adapter->fhash.fmax && adapter->fhash.fhead)
1888                kfree(adapter->fhash.fhead);
1889
1890        adapter->fhash.fhead = NULL;
1891        adapter->fhash.fmax = 0;
1892}
1893
1894static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
1895                u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
1896{
1897        struct cmd_desc_type0 *hwdesc;
1898        struct qlcnic_nic_req *req;
1899        struct qlcnic_mac_req *mac_req;
1900        struct qlcnic_vlan_req *vlan_req;
1901        u32 producer;
1902        u64 word;
1903
1904        producer = tx_ring->producer;
1905        hwdesc = &tx_ring->desc_head[tx_ring->producer];
1906
1907        req = (struct qlcnic_nic_req *)hwdesc;
1908        memset(req, 0, sizeof(struct qlcnic_nic_req));
1909        req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1910
1911        word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1912        req->req_hdr = cpu_to_le64(word);
1913
1914        mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
1915        mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
1916        memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1917
1918        vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1919        vlan_req->vlan_id = vlan_id;
1920
1921        tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
1922        smp_mb();
1923}
1924
1925#define QLCNIC_MAC_HASH(MAC)\
1926        ((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1927
1928static void
1929qlcnic_send_filter(struct qlcnic_adapter *adapter,
1930                struct qlcnic_host_tx_ring *tx_ring,
1931                struct cmd_desc_type0 *first_desc,
1932                struct sk_buff *skb)
1933{
1934        struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1935        struct qlcnic_filter *fil, *tmp_fil;
1936        struct hlist_node *tmp_hnode, *n;
1937        struct hlist_head *head;
1938        u64 src_addr = 0;
1939        __le16 vlan_id = 0;
1940        u8 hindex;
1941
1942        if (!compare_ether_addr(phdr->h_source, adapter->mac_addr))
1943                return;
1944
1945        if (adapter->fhash.fnum >= adapter->fhash.fmax)
1946                return;
1947
1948        /* Only NPAR capable devices support vlan based learning*/
1949        if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1950                vlan_id = first_desc->vlan_TCI;
1951        memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1952        hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1953        head = &(adapter->fhash.fhead[hindex]);
1954
1955        hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
1956                if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1957                            tmp_fil->vlan_id == vlan_id) {
1958
1959                        if (jiffies >
1960                            (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1961                                qlcnic_change_filter(adapter, src_addr, vlan_id,
1962                                                                tx_ring);
1963                        tmp_fil->ftime = jiffies;
1964                        return;
1965                }
1966        }
1967
1968        fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
1969        if (!fil)
1970                return;
1971
1972        qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
1973
1974        fil->ftime = jiffies;
1975        fil->vlan_id = vlan_id;
1976        memcpy(fil->faddr, &src_addr, ETH_ALEN);
1977        spin_lock(&adapter->mac_learn_lock);
1978        hlist_add_head(&(fil->fnode), head);
1979        adapter->fhash.fnum++;
1980        spin_unlock(&adapter->mac_learn_lock);
1981}
1982
1983static int
1984qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
1985                struct cmd_desc_type0 *first_desc,
1986                struct sk_buff *skb)
1987{
1988        u8 opcode = 0, hdr_len = 0;
1989        u16 flags = 0, vlan_tci = 0;
1990        int copied, offset, copy_len;
1991        struct cmd_desc_type0 *hwdesc;
1992        struct vlan_ethhdr *vh;
1993        struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1994        u16 protocol = ntohs(skb->protocol);
1995        u32 producer = tx_ring->producer;
1996
1997        if (protocol == ETH_P_8021Q) {
1998                vh = (struct vlan_ethhdr *)skb->data;
1999                flags = FLAGS_VLAN_TAGGED;
2000                vlan_tci = vh->h_vlan_TCI;
2001        } else if (vlan_tx_tag_present(skb)) {
2002                flags = FLAGS_VLAN_OOB;
2003                vlan_tci = vlan_tx_tag_get(skb);
2004        }
2005        if (unlikely(adapter->pvid)) {
2006                if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
2007                        return -EIO;
2008                if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
2009                        goto set_flags;
2010
2011                flags = FLAGS_VLAN_OOB;
2012                vlan_tci = adapter->pvid;
2013        }
2014set_flags:
2015        qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
2016        qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2017
2018        if (*(skb->data) & BIT_0) {
2019                flags |= BIT_0;
2020                memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
2021        }
2022        opcode = TX_ETHER_PKT;
2023        if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
2024                        skb_shinfo(skb)->gso_size > 0) {
2025
2026                hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2027
2028                first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
2029                first_desc->total_hdr_length = hdr_len;
2030
2031                opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
2032
2033                /* For LSO, we need to copy the MAC/IP/TCP headers into
2034                * the descriptor ring */
2035                copied = 0;
2036                offset = 2;
2037
2038                if (flags & FLAGS_VLAN_OOB) {
2039                        first_desc->total_hdr_length += VLAN_HLEN;
2040                        first_desc->tcp_hdr_offset = VLAN_HLEN;
2041                        first_desc->ip_hdr_offset = VLAN_HLEN;
2042                        /* Only in case of TSO on vlan device */
2043                        flags |= FLAGS_VLAN_TAGGED;
2044
2045                        /* Create a TSO vlan header template for firmware */
2046
2047                        hwdesc = &tx_ring->desc_head[producer];
2048                        tx_ring->cmd_buf_arr[producer].skb = NULL;
2049
2050                        copy_len = min((int)sizeof(struct cmd_desc_type0) -
2051                                offset, hdr_len + VLAN_HLEN);
2052
2053                        vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
2054                        skb_copy_from_linear_data(skb, vh, 12);
2055                        vh->h_vlan_proto = htons(ETH_P_8021Q);
2056                        vh->h_vlan_TCI = htons(vlan_tci);
2057
2058                        skb_copy_from_linear_data_offset(skb, 12,
2059                                (char *)vh + 16, copy_len - 16);
2060
2061                        copied = copy_len - VLAN_HLEN;
2062                        offset = 0;
2063
2064                        producer = get_next_index(producer, tx_ring->num_desc);
2065                }
2066
2067                while (copied < hdr_len) {
2068
2069                        copy_len = min((int)sizeof(struct cmd_desc_type0) -
2070                                offset, (hdr_len - copied));
2071
2072                        hwdesc = &tx_ring->desc_head[producer];
2073                        tx_ring->cmd_buf_arr[producer].skb = NULL;
2074
2075                        skb_copy_from_linear_data_offset(skb, copied,
2076                                 (char *) hwdesc + offset, copy_len);
2077
2078                        copied += copy_len;
2079                        offset = 0;
2080
2081                        producer = get_next_index(producer, tx_ring->num_desc);
2082                }
2083
2084                tx_ring->producer = producer;
2085                smp_mb();
2086                adapter->stats.lso_frames++;
2087
2088        } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2089                u8 l4proto;
2090
2091                if (protocol == ETH_P_IP) {
2092                        l4proto = ip_hdr(skb)->protocol;
2093
2094                        if (l4proto == IPPROTO_TCP)
2095                                opcode = TX_TCP_PKT;
2096                        else if (l4proto == IPPROTO_UDP)
2097                                opcode = TX_UDP_PKT;
2098                } else if (protocol == ETH_P_IPV6) {
2099                        l4proto = ipv6_hdr(skb)->nexthdr;
2100
2101                        if (l4proto == IPPROTO_TCP)
2102                                opcode = TX_TCPV6_PKT;
2103                        else if (l4proto == IPPROTO_UDP)
2104                                opcode = TX_UDPV6_PKT;
2105                }
2106        }
2107        first_desc->tcp_hdr_offset += skb_transport_offset(skb);
2108        first_desc->ip_hdr_offset += skb_network_offset(skb);
2109        qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2110
2111        return 0;
2112}
2113
2114static int
2115qlcnic_map_tx_skb(struct pci_dev *pdev,
2116                struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2117{
2118        struct qlcnic_skb_frag *nf;
2119        struct skb_frag_struct *frag;
2120        int i, nr_frags;
2121        dma_addr_t map;
2122
2123        nr_frags = skb_shinfo(skb)->nr_frags;
2124        nf = &pbuf->frag_array[0];
2125
2126        map = pci_map_single(pdev, skb->data,
2127                        skb_headlen(skb), PCI_DMA_TODEVICE);
2128        if (pci_dma_mapping_error(pdev, map))
2129                goto out_err;
2130
2131        nf->dma = map;
2132        nf->length = skb_headlen(skb);
2133
2134        for (i = 0; i < nr_frags; i++) {
2135                frag = &skb_shinfo(skb)->frags[i];
2136                nf = &pbuf->frag_array[i+1];
2137
2138                map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
2139                                       DMA_TO_DEVICE);
2140                if (dma_mapping_error(&pdev->dev, map))
2141                        goto unwind;
2142
2143                nf->dma = map;
2144                nf->length = skb_frag_size(frag);
2145        }
2146
2147        return 0;
2148
2149unwind:
2150        while (--i >= 0) {
2151                nf = &pbuf->frag_array[i+1];
2152                pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2153        }
2154
2155        nf = &pbuf->frag_array[0];
2156        pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2157
2158out_err:
2159        return -ENOMEM;
2160}
2161
2162static void
2163qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
2164                        struct qlcnic_cmd_buffer *pbuf)
2165{
2166        struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
2167        int nr_frags = skb_shinfo(skb)->nr_frags;
2168        int i;
2169
2170        for (i = 0; i < nr_frags; i++) {
2171                nf = &pbuf->frag_array[i+1];
2172                pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2173        }
2174
2175        nf = &pbuf->frag_array[0];
2176        pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2177        pbuf->skb = NULL;
2178}
2179
2180static inline void
2181qlcnic_clear_cmddesc(u64 *desc)
2182{
2183        desc[0] = 0ULL;
2184        desc[2] = 0ULL;
2185        desc[7] = 0ULL;
2186}
2187
2188netdev_tx_t
2189qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2190{
2191        struct qlcnic_adapter *adapter = netdev_priv(netdev);
2192        struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2193        struct qlcnic_cmd_buffer *pbuf;
2194        struct qlcnic_skb_frag *buffrag;
2195        struct cmd_desc_type0 *hwdesc, *first_desc;
2196        struct pci_dev *pdev;
2197        struct ethhdr *phdr;
2198        int delta = 0;
2199        int i, k;
2200
2201        u32 producer;
2202        int frag_count;
2203        u32 num_txd = tx_ring->num_desc;
2204
2205        if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2206                netif_stop_queue(netdev);
2207                return NETDEV_TX_BUSY;
2208        }
2209
2210        if (adapter->flags & QLCNIC_MACSPOOF) {
2211                phdr = (struct ethhdr *)skb->data;
2212                if (compare_ether_addr(phdr->h_source,
2213                                        adapter->mac_addr))
2214                        goto drop_packet;
2215        }
2216
2217        frag_count = skb_shinfo(skb)->nr_frags + 1;
2218        /* 14 frags supported for normal packet and
2219         * 32 frags supported for TSO packet
2220         */
2221        if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
2222
2223                for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
2224                        delta += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2225
2226                if (!__pskb_pull_tail(skb, delta))
2227                        goto drop_packet;
2228
2229                frag_count = 1 + skb_shinfo(skb)->nr_frags;
2230        }
2231
2232        if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
2233                netif_stop_queue(netdev);
2234                if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2235                        netif_start_queue(netdev);
2236                else {
2237                        adapter->stats.xmit_off++;
2238                        return NETDEV_TX_BUSY;
2239                }
2240        }
2241
2242        producer = tx_ring->producer;
2243        pbuf = &tx_ring->cmd_buf_arr[producer];
2244
2245        pdev = adapter->pdev;
2246
2247        first_desc = hwdesc = &tx_ring->desc_head[producer];
2248        qlcnic_clear_cmddesc((u64 *)hwdesc);
2249
2250        if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2251                adapter->stats.tx_dma_map_error++;
2252                goto drop_packet;
2253        }
2254
2255        pbuf->skb = skb;
2256        pbuf->frag_count = frag_count;
2257
2258        qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2259        qlcnic_set_tx_port(first_desc, adapter->portnum);
2260
2261        for (i = 0; i < frag_count; i++) {
2262
2263                k = i % 4;
2264
2265                if ((k == 0) && (i > 0)) {
2266                        /* move to next desc.*/
2267                        producer = get_next_index(producer, num_txd);
2268                        hwdesc = &tx_ring->desc_head[producer];
2269                        qlcnic_clear_cmddesc((u64 *)hwdesc);
2270                        tx_ring->cmd_buf_arr[producer].skb = NULL;
2271                }
2272
2273                buffrag = &pbuf->frag_array[i];
2274
2275                hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2276                switch (k) {
2277                case 0:
2278                        hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2279                        break;
2280                case 1:
2281                        hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2282                        break;
2283                case 2:
2284                        hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2285                        break;
2286                case 3:
2287                        hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2288                        break;
2289                }
2290        }
2291
2292        tx_ring->producer = get_next_index(producer, num_txd);
2293        smp_mb();
2294
2295        if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
2296                goto unwind_buff;
2297
2298        if (adapter->mac_learn)
2299                qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2300
2301        adapter->stats.txbytes += skb->len;
2302        adapter->stats.xmitcalled++;
2303
2304        qlcnic_update_cmd_producer(adapter, tx_ring);
2305
2306        return NETDEV_TX_OK;
2307
2308unwind_buff:
2309        qlcnic_unmap_buffers(pdev, skb, pbuf);
2310drop_packet:
2311        adapter->stats.txdropped++;
2312        dev_kfree_skb_any(skb);
2313        return NETDEV_TX_OK;
2314}
2315
2316static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2317{
2318        struct net_device *netdev = adapter->netdev;
2319        u32 temp, temp_state, temp_val;
2320        int rv = 0;
2321
2322        temp = QLCRD32(adapter, CRB_TEMP_STATE);
2323
2324        temp_state = qlcnic_get_temp_state(temp);
2325        temp_val = qlcnic_get_temp_val(temp);
2326
2327        if (temp_state == QLCNIC_TEMP_PANIC) {
2328                dev_err(&netdev->dev,
2329                       "Device temperature %d degrees C exceeds"
2330                       " maximum allowed. Hardware has been shut down.\n",
2331                       temp_val);
2332                rv = 1;
2333        } else if (temp_state == QLCNIC_TEMP_WARN) {
2334                if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2335                        dev_err(&netdev->dev,
2336                               "Device temperature %d degrees C "
2337                               "exceeds operating range."
2338                               " Immediate action needed.\n",
2339                               temp_val);
2340                }
2341        } else {
2342                if (adapter->temp == QLCNIC_TEMP_WARN) {
2343                        dev_info(&netdev->dev,
2344                               "Device temperature is now %d degrees C"
2345                               " in normal range.\n", temp_val);
2346                }
2347        }
2348        adapter->temp = temp_state;
2349        return rv;
2350}
2351
2352void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2353{
2354        struct net_device *netdev = adapter->netdev;
2355
2356        if (adapter->ahw->linkup && !linkup) {
2357                netdev_info(netdev, "NIC Link is down\n");
2358                adapter->ahw->linkup = 0;
2359                if (netif_running(netdev)) {
2360                        netif_carrier_off(netdev);
2361                        netif_stop_queue(netdev);
2362                }
2363        } else if (!adapter->ahw->linkup && linkup) {
2364                netdev_info(netdev, "NIC Link is up\n");
2365                adapter->ahw->linkup = 1;
2366                if (netif_running(netdev)) {
2367                        netif_carrier_on(netdev);
2368                        netif_wake_queue(netdev);
2369                }
2370        }
2371}
2372
2373static void qlcnic_tx_timeout(struct net_device *netdev)
2374{
2375        struct qlcnic_adapter *adapter = netdev_priv(netdev);
2376
2377        if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2378                return;
2379
2380        dev_err(&netdev->dev, "transmit timeout, resetting.\n");
2381
2382        if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
2383                adapter->need_fw_reset = 1;
2384        else
2385                adapter->reset_context = 1;
2386}
2387
2388static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2389{
2390        struct qlcnic_adapter *adapter = netdev_priv(netdev);
2391        struct net_device_stats *stats = &netdev->stats;
2392
2393        stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2394        stats->tx_packets = adapter->stats.xmitfinished;
2395        stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
2396        stats->tx_bytes = adapter->stats.txbytes;
2397        stats->rx_dropped = adapter->stats.rxdropped;
2398        stats->tx_dropped = adapter->stats.txdropped;
2399
2400        return stats;
2401}
2402
2403static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
2404{
2405        u32 status;
2406
2407        status = readl(adapter->isr_int_vec);
2408
2409        if (!(status & adapter->int_vec_bit))
2410                return IRQ_NONE;
2411
2412        /* check interrupt state machine, to be sure */
2413        status = readl(adapter->crb_int_state_reg);
2414        if (!ISR_LEGACY_INT_TRIGGERED(status))
2415                return IRQ_NONE;
2416
2417        writel(0xffffffff, adapter->tgt_status_reg);
2418        /* read twice to ensure write is flushed */
2419        readl(adapter->isr_int_vec);
2420        readl(adapter->isr_int_vec);
2421
2422        return IRQ_HANDLED;
2423}
2424
2425static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2426{
2427        struct qlcnic_host_sds_ring *sds_ring = data;
2428        struct qlcnic_adapter *adapter = sds_ring->adapter;
2429
2430        if (adapter->flags & QLCNIC_MSIX_ENABLED)
2431                goto done;
2432        else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2433                writel(0xffffffff, adapter->tgt_status_reg);
2434                goto done;
2435        }
2436
2437        if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2438                return IRQ_NONE;
2439
2440done:
2441        adapter->diag_cnt++;
2442        qlcnic_enable_int(sds_ring);
2443        return IRQ_HANDLED;
2444}
2445
2446static irqreturn_t qlcnic_intr(int irq, void *data)
2447{
2448        struct qlcnic_host_sds_ring *sds_ring = data;
2449        struct qlcnic_adapter *adapter = sds_ring->adapter;
2450
2451        if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2452                return IRQ_NONE;
2453
2454        napi_schedule(&sds_ring->napi);
2455
2456        return IRQ_HANDLED;
2457}
2458
2459static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2460{
2461        struct qlcnic_host_sds_ring *sds_ring = data;
2462        struct qlcnic_adapter *adapter = sds_ring->adapter;
2463
2464        /* clear interrupt */
2465        writel(0xffffffff, adapter->tgt_status_reg);
2466
2467        napi_schedule(&sds_ring->napi);
2468        return IRQ_HANDLED;
2469}
2470
2471static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2472{
2473        struct qlcnic_host_sds_ring *sds_ring = data;
2474
2475        napi_schedule(&sds_ring->napi);
2476        return IRQ_HANDLED;
2477}
2478
2479static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2480{
2481        u32 sw_consumer, hw_consumer;
2482        int count = 0, i;
2483        struct qlcnic_cmd_buffer *buffer;
2484        struct pci_dev *pdev = adapter->pdev;
2485        struct net_device *netdev = adapter->netdev;
2486        struct qlcnic_skb_frag *frag;
2487        int done;
2488        struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2489
2490        if (!spin_trylock(&adapter->tx_clean_lock))
2491                return 1;
2492
2493        sw_consumer = tx_ring->sw_consumer;
2494        hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2495
2496        while (sw_consumer != hw_consumer) {
2497                buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2498                if (buffer->skb) {
2499                        frag = &buffer->frag_array[0];
2500                        pci_unmap_single(pdev, frag->dma, frag->length,
2501                                         PCI_DMA_TODEVICE);
2502                        frag->dma = 0ULL;
2503                        for (i = 1; i < buffer->frag_count; i++) {
2504                                frag++;
2505                                pci_unmap_page(pdev, frag->dma, frag->length,
2506                                               PCI_DMA_TODEVICE);
2507                                frag->dma = 0ULL;
2508                        }
2509
2510                        adapter->stats.xmitfinished++;
2511                        dev_kfree_skb_any(buffer->skb);
2512                        buffer->skb = NULL;
2513                }
2514
2515                sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2516                if (++count >= MAX_STATUS_HANDLE)
2517                        break;
2518        }
2519
2520        if (count && netif_running(netdev)) {
2521                tx_ring->sw_consumer = sw_consumer;
2522
2523                smp_mb();
2524
2525                if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
2526                        if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2527                                netif_wake_queue(netdev);
2528                                adapter->stats.xmit_on++;
2529                        }
2530                }
2531                adapter->tx_timeo_cnt = 0;
2532        }
2533        /*
2534         * If everything is freed up to consumer then check if the ring is full
2535         * If the ring is full then check if more needs to be freed and
2536         * schedule the call back again.
2537         *
2538         * This happens when there are 2 CPUs. One could be freeing and the
2539         * other filling it. If the ring is full when we get out of here and
2540         * the card has already interrupted the host then the host can miss the
2541         * interrupt.
2542         *
2543         * There is still a possible race condition and the host could miss an
2544         * interrupt. The card has to take care of this.
2545         */
2546        hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2547        done = (sw_consumer == hw_consumer);
2548        spin_unlock(&adapter->tx_clean_lock);
2549
2550        return done;
2551}
2552
2553static int qlcnic_poll(struct napi_struct *napi, int budget)
2554{
2555        struct qlcnic_host_sds_ring *sds_ring =
2556                container_of(napi, struct qlcnic_host_sds_ring, napi);
2557
2558        struct qlcnic_adapter *adapter = sds_ring->adapter;
2559
2560        int tx_complete;
2561        int work_done;
2562
2563        tx_complete = qlcnic_process_cmd_ring(adapter);
2564
2565        work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2566
2567        if ((work_done < budget) && tx_complete) {
2568                napi_complete(&sds_ring->napi);
2569                if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2570                        qlcnic_enable_int(sds_ring);
2571        }
2572
2573        return work_done;
2574}
2575
2576static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2577{
2578        struct qlcnic_host_sds_ring *sds_ring =
2579                container_of(napi, struct qlcnic_host_sds_ring, napi);
2580
2581        struct qlcnic_adapter *adapter = sds_ring->adapter;
2582        int work_done;
2583
2584        work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2585
2586        if (work_done < budget) {
2587                napi_complete(&sds_ring->napi);
2588                if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2589                        qlcnic_enable_int(sds_ring);
2590        }
2591
2592        return work_done;
2593}
2594
2595#ifdef CONFIG_NET_POLL_CONTROLLER
2596static void qlcnic_poll_controller(struct net_device *netdev)
2597{
2598        int ring;
2599        struct qlcnic_host_sds_ring *sds_ring;
2600        struct qlcnic_adapter *adapter = netdev_priv(netdev);
2601        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2602
2603        disable_irq(adapter->irq);
2604        for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2605                sds_ring = &recv_ctx->sds_rings[ring];
2606                qlcnic_intr(adapter->irq, sds_ring);
2607        }
2608        enable_irq(adapter->irq);
2609}
2610#endif
2611
2612static void
2613qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2614{
2615        u32 val;
2616
2617        val = adapter->portnum & 0xf;
2618        val |= encoding << 7;
2619        val |= (jiffies - adapter->dev_rst_time) << 8;
2620
2621        QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2622        adapter->dev_rst_time = jiffies;
2623}
2624
2625static int
2626qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
2627{
2628        u32  val;
2629
2630        WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2631                        state != QLCNIC_DEV_NEED_QUISCENT);
2632
2633        if (qlcnic_api_lock(adapter))
2634                return -EIO;
2635
2636        val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2637
2638        if (state == QLCNIC_DEV_NEED_RESET)
2639                QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2640        else if (state == QLCNIC_DEV_NEED_QUISCENT)
2641                QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
2642
2643        QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2644
2645        qlcnic_api_unlock(adapter);
2646
2647        return 0;
2648}
2649
2650static int
2651qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2652{
2653        u32  val;
2654
2655        if (qlcnic_api_lock(adapter))
2656                return -EBUSY;
2657
2658        val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2659        QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
2660        QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2661
2662        qlcnic_api_unlock(adapter);
2663
2664        return 0;
2665}
2666
2667static void
2668qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
2669{
2670        u32  val;
2671
2672        if (qlcnic_api_lock(adapter))
2673                goto err;
2674
2675        val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2676        QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
2677        QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
2678
2679        if (failed) {
2680                QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2681                dev_info(&adapter->pdev->dev,
2682                                "Device state set to Failed. Please Reboot\n");
2683        } else if (!(val & 0x11111111))
2684                QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2685
2686        val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2687        QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
2688        QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2689
2690        qlcnic_api_unlock(adapter);
2691err:
2692        adapter->fw_fail_cnt = 0;
2693        adapter->flags &= ~QLCNIC_FW_HANG;
2694        clear_bit(__QLCNIC_START_FW, &adapter->state);
2695        clear_bit(__QLCNIC_RESETTING, &adapter->state);
2696}
2697
2698/* Grab api lock, before checking state */
2699static int
2700qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2701{
2702        int act, state, active_mask;
2703
2704        state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2705        act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2706
2707        if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
2708                active_mask = (~(1 << (adapter->ahw->pci_func * 4)));
2709                act = act & active_mask;
2710        }
2711
2712        if (((state & 0x11111111) == (act & 0x11111111)) ||
2713                        ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2714                return 0;
2715        else
2716                return 1;
2717}
2718
2719static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2720{
2721        u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2722
2723        if (val != QLCNIC_DRV_IDC_VER) {
2724                dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2725                        " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2726        }
2727
2728        return 0;
2729}
2730
2731static int
2732qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2733{
2734        u32 val, prev_state;
2735        u8 dev_init_timeo = adapter->dev_init_timeo;
2736        u8 portnum = adapter->portnum;
2737        u8 ret;
2738
2739        if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2740                return 1;
2741
2742        if (qlcnic_api_lock(adapter))
2743                return -1;
2744
2745        val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2746        if (!(val & (1 << (portnum * 4)))) {
2747                QLC_DEV_SET_REF_CNT(val, portnum);
2748                QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
2749        }
2750
2751        prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2752        QLCDB(adapter, HW, "Device state = %u\n", prev_state);
2753
2754        switch (prev_state) {
2755        case QLCNIC_DEV_COLD:
2756                QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
2757                QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
2758                qlcnic_idc_debug_info(adapter, 0);
2759                qlcnic_api_unlock(adapter);
2760                return 1;
2761
2762        case QLCNIC_DEV_READY:
2763                ret = qlcnic_check_idc_ver(adapter);
2764                qlcnic_api_unlock(adapter);
2765                return ret;
2766
2767        case QLCNIC_DEV_NEED_RESET:
2768                val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2769                QLC_DEV_SET_RST_RDY(val, portnum);
2770                QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2771                break;
2772
2773        case QLCNIC_DEV_NEED_QUISCENT:
2774                val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2775                QLC_DEV_SET_QSCNT_RDY(val, portnum);
2776                QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2777                break;
2778
2779        case QLCNIC_DEV_FAILED:
2780                dev_err(&adapter->pdev->dev, "Device in failed state.\n");
2781                qlcnic_api_unlock(adapter);
2782                return -1;
2783
2784        case QLCNIC_DEV_INITIALIZING:
2785        case QLCNIC_DEV_QUISCENT:
2786                break;
2787        }
2788
2789        qlcnic_api_unlock(adapter);
2790
2791        do {
2792                msleep(1000);
2793                prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2794
2795                if (prev_state == QLCNIC_DEV_QUISCENT)
2796                        continue;
2797        } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
2798
2799        if (!dev_init_timeo) {
2800                dev_err(&adapter->pdev->dev,
2801                        "Waiting for device to initialize timeout\n");
2802                return -1;
2803        }
2804
2805        if (qlcnic_api_lock(adapter))
2806                return -1;
2807
2808        val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2809        QLC_DEV_CLR_RST_QSCNT(val, portnum);
2810        QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2811
2812        ret = qlcnic_check_idc_ver(adapter);
2813        qlcnic_api_unlock(adapter);
2814
2815        return ret;
2816}
2817
2818static void
2819qlcnic_fwinit_work(struct work_struct *work)
2820{
2821        struct qlcnic_adapter *adapter = container_of(work,
2822                        struct qlcnic_adapter, fw_work.work);
2823        u32 dev_state = 0xf;
2824        u32 val;
2825
2826        if (qlcnic_api_lock(adapter))
2827                goto err_ret;
2828
2829        dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2830        if (dev_state == QLCNIC_DEV_QUISCENT ||
2831            dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2832                qlcnic_api_unlock(adapter);
2833                qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2834                                                FW_POLL_DELAY * 2);
2835                return;
2836        }
2837
2838        if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
2839                qlcnic_api_unlock(adapter);
2840                goto wait_npar;
2841        }
2842
2843        if (dev_state == QLCNIC_DEV_INITIALIZING ||
2844            dev_state == QLCNIC_DEV_READY) {
2845                dev_info(&adapter->pdev->dev, "Detected state change from "
2846                                "DEV_NEED_RESET, skipping ack check\n");
2847                goto skip_ack_check;
2848        }
2849
2850        if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2851                dev_info(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2852                                        adapter->reset_ack_timeo);
2853                goto skip_ack_check;
2854        }
2855
2856        if (!qlcnic_check_drv_state(adapter)) {
2857skip_ack_check:
2858                dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2859
2860                if (dev_state == QLCNIC_DEV_NEED_RESET) {
2861                        QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2862                                                QLCNIC_DEV_INITIALIZING);
2863                        set_bit(__QLCNIC_START_FW, &adapter->state);
2864                        QLCDB(adapter, DRV, "Restarting fw\n");
2865                        qlcnic_idc_debug_info(adapter, 0);
2866                        val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2867                        QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2868                        QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2869                }
2870
2871                qlcnic_api_unlock(adapter);
2872
2873                rtnl_lock();
2874                if (adapter->ahw->fw_dump.enable &&
2875                    (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
2876                        QLCDB(adapter, DRV, "Take FW dump\n");
2877                        qlcnic_dump_fw(adapter);
2878                        adapter->flags |= QLCNIC_FW_HANG;
2879                }
2880                rtnl_unlock();
2881
2882                adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
2883                if (!adapter->nic_ops->start_firmware(adapter)) {
2884                        qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2885                        adapter->fw_wait_cnt = 0;
2886                        return;
2887                }
2888                goto err_ret;
2889        }
2890
2891        qlcnic_api_unlock(adapter);
2892
2893wait_npar:
2894        dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2895        QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
2896
2897        switch (dev_state) {
2898        case QLCNIC_DEV_READY:
2899                if (!adapter->nic_ops->start_firmware(adapter)) {
2900                        qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2901                        adapter->fw_wait_cnt = 0;
2902                        return;
2903                }
2904        case QLCNIC_DEV_FAILED:
2905                break;
2906        default:
2907                qlcnic_schedule_work(adapter,
2908                        qlcnic_fwinit_work, FW_POLL_DELAY);
2909                return;
2910        }
2911
2912err_ret:
2913        dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2914                "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
2915        netif_device_attach(adapter->netdev);
2916        qlcnic_clr_all_drv_state(adapter, 0);
2917}
2918
2919static void
2920qlcnic_detach_work(struct work_struct *work)
2921{
2922        struct qlcnic_adapter *adapter = container_of(work,
2923                        struct qlcnic_adapter, fw_work.work);
2924        struct net_device *netdev = adapter->netdev;
2925        u32 status;
2926
2927        netif_device_detach(netdev);
2928
2929        /* Dont grab rtnl lock during Quiscent mode */
2930        if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2931                if (netif_running(netdev))
2932                        __qlcnic_down(adapter, netdev);
2933        } else
2934                qlcnic_down(adapter, netdev);
2935
2936        status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2937
2938        if (status & QLCNIC_RCODE_FATAL_ERROR) {
2939                dev_err(&adapter->pdev->dev,
2940                        "Detaching the device: peg halt status1=0x%x\n",
2941                                        status);
2942
2943                if (QLCNIC_FWERROR_CODE(status) == QLCNIC_FWERROR_FAN_FAILURE) {
2944                        dev_err(&adapter->pdev->dev,
2945                        "On board active cooling fan failed. "
2946                                "Device has been halted.\n");
2947                        dev_err(&adapter->pdev->dev,
2948                                "Replace the adapter.\n");
2949                }
2950
2951                goto err_ret;
2952        }
2953
2954        if (adapter->temp == QLCNIC_TEMP_PANIC) {
2955                dev_err(&adapter->pdev->dev, "Detaching the device: temp=%d\n",
2956                        adapter->temp);
2957                goto err_ret;
2958        }
2959
2960        /* Dont ack if this instance is the reset owner */
2961        if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
2962                if (qlcnic_set_drv_state(adapter, adapter->dev_state)) {
2963                        dev_err(&adapter->pdev->dev,
2964                                "Failed to set driver state,"
2965                                        "detaching the device.\n");
2966                        goto err_ret;
2967                }
2968        }
2969
2970        adapter->fw_wait_cnt = 0;
2971
2972        qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2973
2974        return;
2975
2976err_ret:
2977        netif_device_attach(netdev);
2978        qlcnic_clr_all_drv_state(adapter, 1);
2979}
2980
2981/*Transit NPAR state to NON Operational */
2982static void
2983qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
2984{
2985        u32 state;
2986
2987        state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2988        if (state == QLCNIC_DEV_NPAR_NON_OPER)
2989                return;
2990
2991        if (qlcnic_api_lock(adapter))
2992                return;
2993        QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
2994        qlcnic_api_unlock(adapter);
2995}
2996
2997/*Transit to RESET state from READY state only */
2998void
2999qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
3000{
3001        u32 state;
3002
3003        adapter->need_fw_reset = 1;
3004        if (qlcnic_api_lock(adapter))
3005                return;
3006
3007        state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
3008
3009        if (state == QLCNIC_DEV_READY) {
3010                QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
3011                adapter->flags |= QLCNIC_FW_RESET_OWNER;
3012                QLCDB(adapter, DRV, "NEED_RESET state set\n");
3013                qlcnic_idc_debug_info(adapter, 0);
3014        }
3015
3016        QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
3017        qlcnic_api_unlock(adapter);
3018}
3019
3020/* Transit to NPAR READY state from NPAR NOT READY state */
3021static void
3022qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
3023{
3024        if (qlcnic_api_lock(adapter))
3025                return;
3026
3027        QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
3028        QLCDB(adapter, DRV, "NPAR operational state set\n");
3029
3030        qlcnic_api_unlock(adapter);
3031}
3032
3033static void
3034qlcnic_schedule_work(struct qlcnic_adapter *adapter,
3035                work_func_t func, int delay)
3036{
3037        if (test_bit(__QLCNIC_AER, &adapter->state))
3038                return;
3039
3040        INIT_DELAYED_WORK(&adapter->fw_work, func);
3041        queue_delayed_work(qlcnic_wq, &adapter->fw_work,
3042                                        round_jiffies_relative(delay));
3043}
3044
3045static void
3046qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
3047{
3048        while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3049                msleep(10);
3050
3051        cancel_delayed_work_sync(&adapter->fw_work);
3052}
3053
3054static void
3055qlcnic_attach_work(struct work_struct *work)
3056{
3057        struct qlcnic_adapter *adapter = container_of(work,
3058                                struct qlcnic_adapter, fw_work.work);
3059        struct net_device *netdev = adapter->netdev;
3060        u32 npar_state;
3061
3062        if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
3063                npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3064                if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3065                        qlcnic_clr_all_drv_state(adapter, 0);
3066                else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3067                        qlcnic_schedule_work(adapter, qlcnic_attach_work,
3068                                                        FW_POLL_DELAY);
3069                else
3070                        goto attach;
3071                QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3072                return;
3073        }
3074attach:
3075        if (netif_running(netdev)) {
3076                if (qlcnic_up(adapter, netdev))
3077                        goto done;
3078
3079                qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3080        }
3081
3082done:
3083        netif_device_attach(netdev);
3084        adapter->fw_fail_cnt = 0;
3085        adapter->flags &= ~QLCNIC_FW_HANG;
3086        clear_bit(__QLCNIC_RESETTING, &adapter->state);
3087
3088        if (!qlcnic_clr_drv_state(adapter))
3089                qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3090                                                        FW_POLL_DELAY);
3091}
3092
3093static int
3094qlcnic_check_health(struct qlcnic_adapter *adapter)
3095{
3096        u32 state = 0, heartbeat;
3097        u32 peg_status;
3098
3099        if (qlcnic_check_temp(adapter))
3100                goto detach;
3101
3102        if (adapter->need_fw_reset)
3103                qlcnic_dev_request_reset(adapter);
3104
3105        state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
3106        if (state == QLCNIC_DEV_NEED_RESET) {
3107                qlcnic_set_npar_non_operational(adapter);
3108                adapter->need_fw_reset = 1;
3109        } else if (state == QLCNIC_DEV_NEED_QUISCENT)
3110                goto detach;
3111
3112        heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3113        if (heartbeat != adapter->heartbeat) {
3114                adapter->heartbeat = heartbeat;
3115                adapter->fw_fail_cnt = 0;
3116                if (adapter->need_fw_reset)
3117                        goto detach;
3118
3119                if (adapter->reset_context && auto_fw_reset) {
3120                        qlcnic_reset_hw_context(adapter);
3121                        adapter->netdev->trans_start = jiffies;
3122                }
3123
3124                return 0;
3125        }
3126
3127        if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3128                return 0;
3129
3130        adapter->flags |= QLCNIC_FW_HANG;
3131
3132        qlcnic_dev_request_reset(adapter);
3133
3134        if (auto_fw_reset)
3135                clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
3136
3137        dev_err(&adapter->pdev->dev, "firmware hang detected\n");
3138        dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
3139                        "PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
3140                        "PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
3141                        "PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
3142                        "PEG_NET_4_PC: 0x%x\n",
3143                        QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1),
3144                        QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS2),
3145                        QLCRD32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c),
3146                        QLCRD32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c),
3147                        QLCRD32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c),
3148                        QLCRD32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c),
3149                        QLCRD32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c));
3150        peg_status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
3151        if (LSW(MSB(peg_status)) == 0x67)
3152                dev_err(&adapter->pdev->dev,
3153                        "Firmware aborted with error code 0x00006700. "
3154                                "Device is being reset.\n");
3155detach:
3156        adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3157                QLCNIC_DEV_NEED_RESET;
3158
3159        if (auto_fw_reset &&
3160                !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3161
3162                qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
3163                QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3164        }
3165
3166        return 1;
3167}
3168
3169static void
3170qlcnic_fw_poll_work(struct work_struct *work)
3171{
3172        struct qlcnic_adapter *adapter = container_of(work,
3173                                struct qlcnic_adapter, fw_work.work);
3174
3175        if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3176                goto reschedule;
3177
3178
3179        if (qlcnic_check_health(adapter))
3180                return;
3181
3182        if (adapter->fhash.fnum)
3183                qlcnic_prune_lb_filters(adapter);
3184
3185reschedule:
3186        qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3187}
3188
3189static int qlcnic_is_first_func(struct pci_dev *pdev)
3190{
3191        struct pci_dev *oth_pdev;
3192        int val = pdev->devfn;
3193
3194        while (val-- > 0) {
3195                oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3196                        (pdev->bus), pdev->bus->number,
3197                        PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
3198                if (!oth_pdev)
3199                        continue;
3200
3201                if (oth_pdev->current_state != PCI_D3cold) {
3202                        pci_dev_put(oth_pdev);
3203                        return 0;
3204                }
3205                pci_dev_put(oth_pdev);
3206        }
3207        return 1;
3208}
3209
3210static int qlcnic_attach_func(struct pci_dev *pdev)
3211{
3212        int err, first_func;
3213        struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3214        struct net_device *netdev = adapter->netdev;
3215
3216        pdev->error_state = pci_channel_io_normal;
3217
3218        err = pci_enable_device(pdev);
3219        if (err)
3220                return err;
3221
3222        pci_set_power_state(pdev, PCI_D0);
3223        pci_set_master(pdev);
3224        pci_restore_state(pdev);
3225
3226        first_func = qlcnic_is_first_func(pdev);
3227
3228        if (qlcnic_api_lock(adapter))
3229                return -EINVAL;
3230
3231        if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
3232                adapter->need_fw_reset = 1;
3233                set_bit(__QLCNIC_START_FW, &adapter->state);
3234                QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3235                QLCDB(adapter, DRV, "Restarting fw\n");
3236        }
3237        qlcnic_api_unlock(adapter);
3238
3239        err = adapter->nic_ops->start_firmware(adapter);
3240        if (err)
3241                return err;
3242
3243        qlcnic_clr_drv_state(adapter);
3244        qlcnic_setup_intr(adapter);
3245
3246        if (netif_running(netdev)) {
3247                err = qlcnic_attach(adapter);
3248                if (err) {
3249                        qlcnic_clr_all_drv_state(adapter, 1);
3250                        clear_bit(__QLCNIC_AER, &adapter->state);
3251                        netif_device_attach(netdev);
3252                        return err;
3253                }
3254
3255                err = qlcnic_up(adapter, netdev);
3256                if (err)
3257                        goto done;
3258
3259                qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3260        }
3261 done:
3262        netif_device_attach(netdev);
3263        return err;
3264}
3265
3266static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3267                                                pci_channel_state_t state)
3268{
3269        struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3270        struct net_device *netdev = adapter->netdev;
3271
3272        if (state == pci_channel_io_perm_failure)
3273                return PCI_ERS_RESULT_DISCONNECT;
3274
3275        if (state == pci_channel_io_normal)
3276                return PCI_ERS_RESULT_RECOVERED;
3277
3278        set_bit(__QLCNIC_AER, &adapter->state);
3279        netif_device_detach(netdev);
3280
3281        cancel_delayed_work_sync(&adapter->fw_work);
3282
3283        if (netif_running(netdev))
3284                qlcnic_down(adapter, netdev);
3285
3286        qlcnic_detach(adapter);
3287        qlcnic_teardown_intr(adapter);
3288
3289        clear_bit(__QLCNIC_RESETTING, &adapter->state);
3290
3291        pci_save_state(pdev);
3292        pci_disable_device(pdev);
3293
3294        return PCI_ERS_RESULT_NEED_RESET;
3295}
3296
3297static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3298{
3299        return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3300                                PCI_ERS_RESULT_RECOVERED;
3301}
3302
3303static void qlcnic_io_resume(struct pci_dev *pdev)
3304{
3305        struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3306
3307        pci_cleanup_aer_uncorrect_error_status(pdev);
3308
3309        if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3310            test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3311                qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3312                                                FW_POLL_DELAY);
3313}
3314
3315static int
3316qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3317{
3318        int err;
3319
3320        err = qlcnic_can_start_firmware(adapter);
3321        if (err)
3322                return err;
3323
3324        err = qlcnic_check_npar_opertional(adapter);
3325        if (err)
3326                return err;
3327
3328        err = qlcnic_initialize_nic(adapter);
3329        if (err)
3330                return err;
3331
3332        qlcnic_check_options(adapter);
3333
3334        err = qlcnic_set_eswitch_port_config(adapter);
3335        if (err)
3336                return err;
3337
3338        adapter->need_fw_reset = 0;
3339
3340        return err;
3341}
3342
3343static int
3344qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3345{
3346        return -EOPNOTSUPP;
3347}
3348
3349static int
3350qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3351{
3352        return -EOPNOTSUPP;
3353}
3354
3355static ssize_t
3356qlcnic_store_bridged_mode(struct device *dev,
3357                struct device_attribute *attr, const char *buf, size_t len)
3358{
3359        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3360        unsigned long new;
3361        int ret = -EINVAL;
3362
3363        if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3364                goto err_out;
3365
3366        if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
3367                goto err_out;
3368
3369        if (strict_strtoul(buf, 2, &new))
3370                goto err_out;
3371
3372        if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
3373                ret = len;
3374
3375err_out:
3376        return ret;
3377}
3378
3379static ssize_t
3380qlcnic_show_bridged_mode(struct device *dev,
3381                struct device_attribute *attr, char *buf)
3382{
3383        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3384        int bridged_mode = 0;
3385
3386        if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3387                bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3388
3389        return sprintf(buf, "%d\n", bridged_mode);
3390}
3391
3392static struct device_attribute dev_attr_bridged_mode = {
3393       .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3394       .show = qlcnic_show_bridged_mode,
3395       .store = qlcnic_store_bridged_mode,
3396};
3397
3398static ssize_t
3399qlcnic_store_diag_mode(struct device *dev,
3400                struct device_attribute *attr, const char *buf, size_t len)
3401{
3402        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3403        unsigned long new;
3404
3405        if (strict_strtoul(buf, 2, &new))
3406                return -EINVAL;
3407
3408        if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3409                adapter->flags ^= QLCNIC_DIAG_ENABLED;
3410
3411        return len;
3412}
3413
3414static ssize_t
3415qlcnic_show_diag_mode(struct device *dev,
3416                struct device_attribute *attr, char *buf)
3417{
3418        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3419
3420        return sprintf(buf, "%d\n",
3421                        !!(adapter->flags & QLCNIC_DIAG_ENABLED));
3422}
3423
3424static struct device_attribute dev_attr_diag_mode = {
3425        .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3426        .show = qlcnic_show_diag_mode,
3427        .store = qlcnic_store_diag_mode,
3428};
3429
3430int qlcnic_validate_max_rss(struct net_device *netdev, u8 max_hw, u8 val)
3431{
3432        if (!use_msi_x && !use_msi) {
3433                netdev_info(netdev, "no msix or msi support, hence no rss\n");
3434                return -EINVAL;
3435        }
3436
3437        if ((val > max_hw) || (val <  2) || !is_power_of_2(val)) {
3438                netdev_info(netdev, "rss_ring valid range [2 - %x] in "
3439                        " powers of 2\n", max_hw);
3440                return -EINVAL;
3441        }
3442        return 0;
3443
3444}
3445
3446int qlcnic_set_max_rss(struct qlcnic_adapter *adapter, u8 data)
3447{
3448        struct net_device *netdev = adapter->netdev;
3449        int err = 0;
3450
3451        if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3452                return -EBUSY;
3453
3454        netif_device_detach(netdev);
3455        if (netif_running(netdev))
3456                __qlcnic_down(adapter, netdev);
3457        qlcnic_detach(adapter);
3458        qlcnic_teardown_intr(adapter);
3459
3460        if (qlcnic_enable_msix(adapter, data)) {
3461                netdev_info(netdev, "failed setting max_rss; rss disabled\n");
3462                qlcnic_enable_msi_legacy(adapter);
3463        }
3464
3465        if (netif_running(netdev)) {
3466                err = qlcnic_attach(adapter);
3467                if (err)
3468                        goto done;
3469                err = __qlcnic_up(adapter, netdev);
3470                if (err)
3471                        goto done;
3472                qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3473        }
3474 done:
3475        netif_device_attach(netdev);
3476        clear_bit(__QLCNIC_RESETTING, &adapter->state);
3477        return err;
3478}
3479
3480static int
3481qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon, u8 *state,
3482                        u8 *rate)
3483{
3484        *rate = LSB(beacon);
3485        *state = MSB(beacon);
3486
3487        QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
3488
3489        if (!*state) {
3490                *rate = __QLCNIC_MAX_LED_RATE;
3491                return 0;
3492        } else if (*state > __QLCNIC_MAX_LED_STATE)
3493                return -EINVAL;
3494
3495        if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
3496                return -EINVAL;
3497
3498        return 0;
3499}
3500
3501static ssize_t
3502qlcnic_store_beacon(struct device *dev,
3503                struct device_attribute *attr, const char *buf, size_t len)
3504{
3505        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3506        int max_sds_rings = adapter->max_sds_rings;
3507        u16 beacon;
3508        u8 b_state, b_rate;
3509        int err;
3510
3511        if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
3512                dev_warn(dev, "LED test not supported for non "
3513                                "privilege function\n");
3514                return -EOPNOTSUPP;
3515        }
3516
3517        if (len != sizeof(u16))
3518                return QL_STATUS_INVALID_PARAM;
3519
3520        memcpy(&beacon, buf, sizeof(u16));
3521        err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
3522        if (err)
3523                return err;
3524
3525        if (adapter->ahw->beacon_state == b_state)
3526                return len;
3527
3528        rtnl_lock();
3529
3530        if (!adapter->ahw->beacon_state)
3531                if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
3532                        rtnl_unlock();
3533                        return -EBUSY;
3534                }
3535
3536        if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
3537                err = -EIO;
3538                goto out;
3539        }
3540
3541        if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
3542                err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
3543                if (err)
3544                        goto out;
3545                set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
3546        }
3547
3548        err = qlcnic_config_led(adapter, b_state, b_rate);
3549
3550        if (!err) {
3551                err = len;
3552                adapter->ahw->beacon_state = b_state;
3553        }
3554
3555        if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
3556                qlcnic_diag_free_res(adapter->netdev, max_sds_rings);
3557
3558 out:
3559        if (!adapter->ahw->beacon_state)
3560                clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
3561        rtnl_unlock();
3562
3563        return err;
3564}
3565
3566static ssize_t
3567qlcnic_show_beacon(struct device *dev,
3568                struct device_attribute *attr, char *buf)
3569{
3570        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3571
3572        return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
3573}
3574
3575static struct device_attribute dev_attr_beacon = {
3576        .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
3577        .show = qlcnic_show_beacon,
3578        .store = qlcnic_store_beacon,
3579};
3580
3581static int
3582qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3583                loff_t offset, size_t size)
3584{
3585        size_t crb_size = 4;
3586
3587        if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3588                return -EIO;
3589
3590        if (offset < QLCNIC_PCI_CRBSPACE) {
3591                if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3592                                        QLCNIC_PCI_CAMQM_END))
3593                        crb_size = 8;
3594                else
3595                        return -EINVAL;
3596        }
3597
3598        if ((size != crb_size) || (offset & (crb_size-1)))
3599                return  -EINVAL;
3600
3601        return 0;
3602}
3603
3604static ssize_t
3605qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3606                struct bin_attribute *attr,
3607                char *buf, loff_t offset, size_t size)
3608{
3609        struct device *dev = container_of(kobj, struct device, kobj);
3610        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3611        u32 data;
3612        u64 qmdata;
3613        int ret;
3614
3615        ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3616        if (ret != 0)
3617                return ret;
3618
3619        if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3620                qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3621                memcpy(buf, &qmdata, size);
3622        } else {
3623                data = QLCRD32(adapter, offset);
3624                memcpy(buf, &data, size);
3625        }
3626        return size;
3627}
3628
3629static ssize_t
3630qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3631                struct bin_attribute *attr,
3632                char *buf, loff_t offset, size_t size)
3633{
3634        struct device *dev = container_of(kobj, struct device, kobj);
3635        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3636        u32 data;
3637        u64 qmdata;
3638        int ret;
3639
3640        ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3641        if (ret != 0)
3642                return ret;
3643
3644        if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3645                memcpy(&qmdata, buf, size);
3646                qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3647        } else {
3648                memcpy(&data, buf, size);
3649                QLCWR32(adapter, offset, data);
3650        }
3651        return size;
3652}
3653
3654static int
3655qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3656                loff_t offset, size_t size)
3657{
3658        if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3659                return -EIO;
3660
3661        if ((size != 8) || (offset & 0x7))
3662                return  -EIO;
3663
3664        return 0;
3665}
3666
3667static ssize_t
3668qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3669                struct bin_attribute *attr,
3670                char *buf, loff_t offset, size_t size)
3671{
3672        struct device *dev = container_of(kobj, struct device, kobj);
3673        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3674        u64 data;
3675        int ret;
3676
3677        ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3678        if (ret != 0)
3679                return ret;
3680
3681        if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3682                return -EIO;
3683
3684        memcpy(buf, &data, size);
3685
3686        return size;
3687}
3688
3689static ssize_t
3690qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3691                struct bin_attribute *attr,
3692                char *buf, loff_t offset, size_t size)
3693{
3694        struct device *dev = container_of(kobj, struct device, kobj);
3695        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3696        u64 data;
3697        int ret;
3698
3699        ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3700        if (ret != 0)
3701                return ret;
3702
3703        memcpy(&data, buf, size);
3704
3705        if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3706                return -EIO;
3707
3708        return size;
3709}
3710
3711static struct bin_attribute bin_attr_crb = {
3712        .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3713        .size = 0,
3714        .read = qlcnic_sysfs_read_crb,
3715        .write = qlcnic_sysfs_write_crb,
3716};
3717
3718static struct bin_attribute bin_attr_mem = {
3719        .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3720        .size = 0,
3721        .read = qlcnic_sysfs_read_mem,
3722        .write = qlcnic_sysfs_write_mem,
3723};
3724
3725static int
3726validate_pm_config(struct qlcnic_adapter *adapter,
3727                        struct qlcnic_pm_func_cfg *pm_cfg, int count)
3728{
3729
3730        u8 src_pci_func, s_esw_id, d_esw_id;
3731        u8 dest_pci_func;
3732        int i;
3733
3734        for (i = 0; i < count; i++) {
3735                src_pci_func = pm_cfg[i].pci_func;
3736                dest_pci_func = pm_cfg[i].dest_npar;
3737                if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3738                                || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3739                        return QL_STATUS_INVALID_PARAM;
3740
3741                if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3742                        return QL_STATUS_INVALID_PARAM;
3743
3744                if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3745                        return QL_STATUS_INVALID_PARAM;
3746
3747                s_esw_id = adapter->npars[src_pci_func].phy_port;
3748                d_esw_id = adapter->npars[dest_pci_func].phy_port;
3749
3750                if (s_esw_id != d_esw_id)
3751                        return QL_STATUS_INVALID_PARAM;
3752
3753        }
3754        return 0;
3755
3756}
3757
3758static ssize_t
3759qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3760        struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3761{
3762        struct device *dev = container_of(kobj, struct device, kobj);
3763        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3764        struct qlcnic_pm_func_cfg *pm_cfg;
3765        u32 id, action, pci_func;
3766        int count, rem, i, ret;
3767
3768        count   = size / sizeof(struct qlcnic_pm_func_cfg);
3769        rem     = size % sizeof(struct qlcnic_pm_func_cfg);
3770        if (rem)
3771                return QL_STATUS_INVALID_PARAM;
3772
3773        pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3774
3775        ret = validate_pm_config(adapter, pm_cfg, count);
3776        if (ret)
3777                return ret;
3778        for (i = 0; i < count; i++) {
3779                pci_func = pm_cfg[i].pci_func;
3780                action = !!pm_cfg[i].action;
3781                id = adapter->npars[pci_func].phy_port;
3782                ret = qlcnic_config_port_mirroring(adapter, id,
3783                                                action, pci_func);
3784                if (ret)
3785                        return ret;
3786        }
3787
3788        for (i = 0; i < count; i++) {
3789                pci_func = pm_cfg[i].pci_func;
3790                id = adapter->npars[pci_func].phy_port;
3791                adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
3792                adapter->npars[pci_func].dest_npar = id;
3793        }
3794        return size;
3795}
3796
3797static ssize_t
3798qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3799        struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3800{
3801        struct device *dev = container_of(kobj, struct device, kobj);
3802        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3803        struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3804        int i;
3805
3806        if (size != sizeof(pm_cfg))
3807                return QL_STATUS_INVALID_PARAM;
3808
3809        for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3810                if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3811                        continue;
3812                pm_cfg[i].action = adapter->npars[i].enable_pm;
3813                pm_cfg[i].dest_npar = 0;
3814                pm_cfg[i].pci_func = i;
3815        }
3816        memcpy(buf, &pm_cfg, size);
3817
3818        return size;
3819}
3820
3821static int
3822validate_esw_config(struct qlcnic_adapter *adapter,
3823        struct qlcnic_esw_func_cfg *esw_cfg, int count)
3824{
3825        u32 op_mode;
3826        u8 pci_func;
3827        int i;
3828
3829        op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
3830
3831        for (i = 0; i < count; i++) {
3832                pci_func = esw_cfg[i].pci_func;
3833                if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3834                        return QL_STATUS_INVALID_PARAM;
3835
3836                if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3837                        if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3838                                return QL_STATUS_INVALID_PARAM;
3839
3840                switch (esw_cfg[i].op_mode) {
3841                case QLCNIC_PORT_DEFAULTS:
3842                        if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
3843                                                QLCNIC_NON_PRIV_FUNC) {
3844                                if (esw_cfg[i].mac_anti_spoof != 0)
3845                                        return QL_STATUS_INVALID_PARAM;
3846                                if (esw_cfg[i].mac_override != 1)
3847                                        return QL_STATUS_INVALID_PARAM;
3848                                if (esw_cfg[i].promisc_mode != 1)
3849                                        return QL_STATUS_INVALID_PARAM;
3850                        }
3851                        break;
3852                case QLCNIC_ADD_VLAN:
3853                        if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3854                                return QL_STATUS_INVALID_PARAM;
3855                        if (!esw_cfg[i].op_type)
3856                                return QL_STATUS_INVALID_PARAM;
3857                        break;
3858                case QLCNIC_DEL_VLAN:
3859                        if (!esw_cfg[i].op_type)
3860                                return QL_STATUS_INVALID_PARAM;
3861                        break;
3862                default:
3863                        return QL_STATUS_INVALID_PARAM;
3864                }
3865        }
3866        return 0;
3867}
3868
3869static ssize_t
3870qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3871        struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3872{
3873        struct device *dev = container_of(kobj, struct device, kobj);
3874        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3875        struct qlcnic_esw_func_cfg *esw_cfg;
3876        struct qlcnic_npar_info *npar;
3877        int count, rem, i, ret;
3878        u8 pci_func, op_mode = 0;
3879
3880        count   = size / sizeof(struct qlcnic_esw_func_cfg);
3881        rem     = size % sizeof(struct qlcnic_esw_func_cfg);
3882        if (rem)
3883                return QL_STATUS_INVALID_PARAM;
3884
3885        esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3886        ret = validate_esw_config(adapter, esw_cfg, count);
3887        if (ret)
3888                return ret;
3889
3890        for (i = 0; i < count; i++) {
3891                if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3892                        if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3893                                return QL_STATUS_INVALID_PARAM;
3894
3895                if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
3896                        continue;
3897
3898                op_mode = esw_cfg[i].op_mode;
3899                qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3900                esw_cfg[i].op_mode = op_mode;
3901                esw_cfg[i].pci_func = adapter->ahw->pci_func;
3902
3903                switch (esw_cfg[i].op_mode) {
3904                case QLCNIC_PORT_DEFAULTS:
3905                        qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3906                        break;
3907                case QLCNIC_ADD_VLAN:
3908                        qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3909                        break;
3910                case QLCNIC_DEL_VLAN:
3911                        esw_cfg[i].vlan_id = 0;
3912                        qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3913                        break;
3914                }
3915        }
3916
3917        if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3918                goto out;
3919
3920        for (i = 0; i < count; i++) {
3921                pci_func = esw_cfg[i].pci_func;
3922                npar = &adapter->npars[pci_func];
3923                switch (esw_cfg[i].op_mode) {
3924                case QLCNIC_PORT_DEFAULTS:
3925                        npar->promisc_mode = esw_cfg[i].promisc_mode;
3926                        npar->mac_override = esw_cfg[i].mac_override;
3927                        npar->offload_flags = esw_cfg[i].offload_flags;
3928                        npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3929                        npar->discard_tagged = esw_cfg[i].discard_tagged;
3930                        break;
3931                case QLCNIC_ADD_VLAN:
3932                        npar->pvid = esw_cfg[i].vlan_id;
3933                        break;
3934                case QLCNIC_DEL_VLAN:
3935                        npar->pvid = 0;
3936                        break;
3937                }
3938        }
3939out:
3940        return size;
3941}
3942
3943static ssize_t
3944qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3945        struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3946{
3947        struct device *dev = container_of(kobj, struct device, kobj);
3948        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3949        struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
3950        u8 i;
3951
3952        if (size != sizeof(esw_cfg))
3953                return QL_STATUS_INVALID_PARAM;
3954
3955        for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3956                if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3957                        continue;
3958                esw_cfg[i].pci_func = i;
3959                if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
3960                        return QL_STATUS_INVALID_PARAM;
3961        }
3962        memcpy(buf, &esw_cfg, size);
3963
3964        return size;
3965}
3966
3967static int
3968validate_npar_config(struct qlcnic_adapter *adapter,
3969                                struct qlcnic_npar_func_cfg *np_cfg, int count)
3970{
3971        u8 pci_func, i;
3972
3973        for (i = 0; i < count; i++) {
3974                pci_func = np_cfg[i].pci_func;
3975                if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3976                        return QL_STATUS_INVALID_PARAM;
3977
3978                if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3979                        return QL_STATUS_INVALID_PARAM;
3980
3981                if (!IS_VALID_BW(np_cfg[i].min_bw) ||
3982                    !IS_VALID_BW(np_cfg[i].max_bw))
3983                        return QL_STATUS_INVALID_PARAM;
3984        }
3985        return 0;
3986}
3987
3988static ssize_t
3989qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
3990        struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3991{
3992        struct device *dev = container_of(kobj, struct device, kobj);
3993        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3994        struct qlcnic_info nic_info;
3995        struct qlcnic_npar_func_cfg *np_cfg;
3996        int i, count, rem, ret;
3997        u8 pci_func;
3998
3999        count   = size / sizeof(struct qlcnic_npar_func_cfg);
4000        rem     = size % sizeof(struct qlcnic_npar_func_cfg);
4001        if (rem)
4002                return QL_STATUS_INVALID_PARAM;
4003
4004        np_cfg = (struct qlcnic_npar_func_cfg *) buf;
4005        ret = validate_npar_config(adapter, np_cfg, count);
4006        if (ret)
4007                return ret;
4008
4009        for (i = 0; i < count ; i++) {
4010                pci_func = np_cfg[i].pci_func;
4011                ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
4012                if (ret)
4013                        return ret;
4014                nic_info.pci_func = pci_func;
4015                nic_info.min_tx_bw = np_cfg[i].min_bw;
4016                nic_info.max_tx_bw = np_cfg[i].max_bw;
4017                ret = qlcnic_set_nic_info(adapter, &nic_info);
4018                if (ret)
4019                        return ret;
4020                adapter->npars[i].min_bw = nic_info.min_tx_bw;
4021                adapter->npars[i].max_bw = nic_info.max_tx_bw;
4022        }
4023
4024        return size;
4025
4026}
4027static ssize_t
4028qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
4029        struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4030{
4031        struct device *dev = container_of(kobj, struct device, kobj);
4032        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4033        struct qlcnic_info nic_info;
4034        struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
4035        int i, ret;
4036
4037        if (size != sizeof(np_cfg))
4038                return QL_STATUS_INVALID_PARAM;
4039
4040        for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4041                if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
4042                        continue;
4043                ret = qlcnic_get_nic_info(adapter, &nic_info, i);
4044                if (ret)
4045                        return ret;
4046
4047                np_cfg[i].pci_func = i;
4048                np_cfg[i].op_mode = (u8)nic_info.op_mode;
4049                np_cfg[i].port_num = nic_info.phys_port;
4050                np_cfg[i].fw_capab = nic_info.capabilities;
4051                np_cfg[i].min_bw = nic_info.min_tx_bw ;
4052                np_cfg[i].max_bw = nic_info.max_tx_bw;
4053                np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
4054                np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
4055        }
4056        memcpy(buf, &np_cfg, size);
4057        return size;
4058}
4059
4060static ssize_t
4061qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
4062        struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4063{
4064        struct device *dev = container_of(kobj, struct device, kobj);
4065        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4066        struct qlcnic_esw_statistics port_stats;
4067        int ret;
4068
4069        if (size != sizeof(struct qlcnic_esw_statistics))
4070                return QL_STATUS_INVALID_PARAM;
4071
4072        if (offset >= QLCNIC_MAX_PCI_FUNC)
4073                return QL_STATUS_INVALID_PARAM;
4074
4075        memset(&port_stats, 0, size);
4076        ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
4077                                                                &port_stats.rx);
4078        if (ret)
4079                return ret;
4080
4081        ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
4082                                                                &port_stats.tx);
4083        if (ret)
4084                return ret;
4085
4086        memcpy(buf, &port_stats, size);
4087        return size;
4088}
4089
4090static ssize_t
4091qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
4092        struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4093{
4094        struct device *dev = container_of(kobj, struct device, kobj);
4095        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4096        struct qlcnic_esw_statistics esw_stats;
4097        int ret;
4098
4099        if (size != sizeof(struct qlcnic_esw_statistics))
4100                return QL_STATUS_INVALID_PARAM;
4101
4102        if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
4103                return QL_STATUS_INVALID_PARAM;
4104
4105        memset(&esw_stats, 0, size);
4106        ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
4107                                                                &esw_stats.rx);
4108        if (ret)
4109                return ret;
4110
4111        ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
4112                                                                &esw_stats.tx);
4113        if (ret)
4114                return ret;
4115
4116        memcpy(buf, &esw_stats, size);
4117        return size;
4118}
4119
4120static ssize_t
4121qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
4122        struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4123{
4124        struct device *dev = container_of(kobj, struct device, kobj);
4125        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4126        int ret;
4127
4128        if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
4129                return QL_STATUS_INVALID_PARAM;
4130
4131        ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
4132                                                QLCNIC_QUERY_RX_COUNTER);
4133        if (ret)
4134                return ret;
4135
4136        ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
4137                                                QLCNIC_QUERY_TX_COUNTER);
4138        if (ret)
4139                return ret;
4140
4141        return size;
4142}
4143
4144static ssize_t
4145qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
4146        struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4147{
4148
4149        struct device *dev = container_of(kobj, struct device, kobj);
4150        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4151        int ret;
4152
4153        if (offset >= QLCNIC_MAX_PCI_FUNC)
4154                return QL_STATUS_INVALID_PARAM;
4155
4156        ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4157                                                QLCNIC_QUERY_RX_COUNTER);
4158        if (ret)
4159                return ret;
4160
4161        ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4162                                                QLCNIC_QUERY_TX_COUNTER);
4163        if (ret)
4164                return ret;
4165
4166        return size;
4167}
4168
4169static ssize_t
4170qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
4171        struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4172{
4173        struct device *dev = container_of(kobj, struct device, kobj);
4174        struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4175        struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
4176        struct qlcnic_pci_info *pci_info;
4177        int i, ret;
4178
4179        if (size != sizeof(pci_cfg))
4180                return QL_STATUS_INVALID_PARAM;
4181
4182        pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
4183        if (!pci_info)
4184                return -ENOMEM;
4185
4186        ret = qlcnic_get_pci_info(adapter, pci_info);
4187        if (ret) {
4188                kfree(pci_info);
4189                return ret;
4190        }
4191
4192        for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4193                pci_cfg[i].pci_func = pci_info[i].id;
4194                pci_cfg[i].func_type = pci_info[i].type;
4195                pci_cfg[i].port_num = pci_info[i].default_port;
4196                pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
4197                pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
4198                memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
4199        }
4200        memcpy(buf, &pci_cfg, size);
4201        kfree(pci_info);
4202        return size;
4203}
4204static struct bin_attribute bin_attr_npar_config = {
4205        .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
4206        .size = 0,
4207        .read = qlcnic_sysfs_read_npar_config,
4208        .write = qlcnic_sysfs_write_npar_config,
4209};
4210
4211static struct bin_attribute bin_attr_pci_config = {
4212        .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
4213        .size = 0,
4214        .read = qlcnic_sysfs_read_pci_config,
4215        .write = NULL,
4216};
4217
4218static struct bin_attribute bin_attr_port_stats = {
4219        .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
4220        .size = 0,
4221        .read = qlcnic_sysfs_get_port_stats,
4222        .write = qlcnic_sysfs_clear_port_stats,
4223};
4224
4225static struct bin_attribute bin_attr_esw_stats = {
4226        .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
4227        .size = 0,
4228        .read = qlcnic_sysfs_get_esw_stats,
4229        .write = qlcnic_sysfs_clear_esw_stats,
4230};
4231
4232static struct bin_attribute bin_attr_esw_config = {
4233        .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
4234        .size = 0,
4235        .read = qlcnic_sysfs_read_esw_config,
4236        .write = qlcnic_sysfs_write_esw_config,
4237};
4238
4239static struct bin_attribute bin_attr_pm_config = {
4240        .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
4241        .size = 0,
4242        .read = qlcnic_sysfs_read_pm_config,
4243        .write = qlcnic_sysfs_write_pm_config,
4244};
4245
4246static void
4247qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
4248{
4249        struct device *dev = &adapter->pdev->dev;
4250
4251        if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4252                if (device_create_file(dev, &dev_attr_bridged_mode))
4253                        dev_warn(dev,
4254                                "failed to create bridged_mode sysfs entry\n");
4255}
4256
4257static void
4258qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
4259{
4260        struct device *dev = &adapter->pdev->dev;
4261
4262        if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4263                device_remove_file(dev, &dev_attr_bridged_mode);
4264}
4265
4266static void
4267qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
4268{
4269        struct device *dev = &adapter->pdev->dev;
4270
4271        if (device_create_bin_file(dev, &bin_attr_port_stats))
4272                dev_info(dev, "failed to create port stats sysfs entry");
4273
4274        if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4275                return;
4276        if (device_create_file(dev, &dev_attr_diag_mode))
4277                dev_info(dev, "failed to create diag_mode sysfs entry\n");
4278        if (device_create_file(dev, &dev_attr_beacon))
4279                dev_info(dev, "failed to create beacon sysfs entry");
4280        if (device_create_bin_file(dev, &bin_attr_crb))
4281                dev_info(dev, "failed to create crb sysfs entry\n");
4282        if (device_create_bin_file(dev, &bin_attr_mem))
4283                dev_info(dev, "failed to create mem sysfs entry\n");
4284        if (device_create_bin_file(dev, &bin_attr_pci_config))
4285                dev_info(dev, "failed to create pci config sysfs entry");
4286        if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4287                return;
4288        if (device_create_bin_file(dev, &bin_attr_esw_config))
4289                dev_info(dev, "failed to create esw config sysfs entry");
4290        if (adapter->op_mode != QLCNIC_MGMT_FUNC)
4291                return;
4292        if (device_create_bin_file(dev, &bin_attr_npar_config))
4293                dev_info(dev, "failed to create npar config sysfs entry");
4294        if (device_create_bin_file(dev, &bin_attr_pm_config))
4295                dev_info(dev, "failed to create pm config sysfs entry");
4296        if (device_create_bin_file(dev, &bin_attr_esw_stats))
4297                dev_info(dev, "failed to create eswitch stats sysfs entry");
4298}
4299
4300static void
4301qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4302{
4303        struct device *dev = &adapter->pdev->dev;
4304
4305        device_remove_bin_file(dev, &bin_attr_port_stats);
4306
4307        if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4308                return;
4309        device_remove_file(dev, &dev_attr_diag_mode);
4310        device_remove_file(dev, &dev_attr_beacon);
4311        device_remove_bin_file(dev, &bin_attr_crb);
4312        device_remove_bin_file(dev, &bin_attr_mem);
4313        device_remove_bin_file(dev, &bin_attr_pci_config);
4314        if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4315                return;
4316        device_remove_bin_file(dev, &bin_attr_esw_config);
4317        if (adapter->op_mode != QLCNIC_MGMT_FUNC)
4318                return;
4319        device_remove_bin_file(dev, &bin_attr_npar_config);
4320        device_remove_bin_file(dev, &bin_attr_pm_config);
4321        device_remove_bin_file(dev, &bin_attr_esw_stats);
4322}
4323
4324#ifdef CONFIG_INET
4325
4326#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4327
4328static void
4329qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4330                        struct net_device *dev, unsigned long event)
4331{
4332        struct in_device *indev;
4333
4334        indev = in_dev_get(dev);
4335        if (!indev)
4336                return;
4337
4338        for_ifa(indev) {
4339                switch (event) {
4340                case NETDEV_UP:
4341                        qlcnic_config_ipaddr(adapter,
4342                                        ifa->ifa_address, QLCNIC_IP_UP);
4343                        break;
4344                case NETDEV_DOWN:
4345                        qlcnic_config_ipaddr(adapter,
4346                                        ifa->ifa_address, QLCNIC_IP_DOWN);
4347                        break;
4348                default:
4349                        break;
4350                }
4351        } endfor_ifa(indev);
4352
4353        in_dev_put(indev);
4354}
4355
4356static void
4357qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4358{
4359        struct qlcnic_adapter *adapter = netdev_priv(netdev);
4360        struct net_device *dev;
4361        u16 vid;
4362
4363        qlcnic_config_indev_addr(adapter, netdev, event);
4364
4365        for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4366                dev = __vlan_find_dev_deep(netdev, vid);
4367                if (!dev)
4368                        continue;
4369                qlcnic_config_indev_addr(adapter, dev, event);
4370        }
4371}
4372
4373static int qlcnic_netdev_event(struct notifier_block *this,
4374                                 unsigned long event, void *ptr)
4375{
4376        struct qlcnic_adapter *adapter;
4377        struct net_device *dev = (struct net_device *)ptr;
4378
4379recheck:
4380        if (dev == NULL)
4381                goto done;
4382
4383        if (dev->priv_flags & IFF_802_1Q_VLAN) {
4384                dev = vlan_dev_real_dev(dev);
4385                goto recheck;
4386        }
4387
4388        if (!is_qlcnic_netdev(dev))
4389                goto done;
4390
4391        adapter = netdev_priv(dev);
4392
4393        if (!adapter)
4394                goto done;
4395
4396        if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4397                goto done;
4398
4399        qlcnic_config_indev_addr(adapter, dev, event);
4400done:
4401        return NOTIFY_DONE;
4402}
4403
4404static int
4405qlcnic_inetaddr_event(struct notifier_block *this,
4406                unsigned long event, void *ptr)
4407{
4408        struct qlcnic_adapter *adapter;
4409        struct net_device *dev;
4410
4411        struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4412
4413        dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4414
4415recheck:
4416        if (dev == NULL)
4417                goto done;
4418
4419        if (dev->priv_flags & IFF_802_1Q_VLAN) {
4420                dev = vlan_dev_real_dev(dev);
4421                goto recheck;
4422        }
4423
4424        if (!is_qlcnic_netdev(dev))
4425                goto done;
4426
4427        adapter = netdev_priv(dev);
4428
4429        if (!adapter)
4430                goto done;
4431
4432        if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4433                goto done;
4434
4435        switch (event) {
4436        case NETDEV_UP:
4437                qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4438                break;
4439        case NETDEV_DOWN:
4440                qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4441                break;
4442        default:
4443                break;
4444        }
4445
4446done:
4447        return NOTIFY_DONE;
4448}
4449
4450static struct notifier_block    qlcnic_netdev_cb = {
4451        .notifier_call = qlcnic_netdev_event,
4452};
4453
4454static struct notifier_block qlcnic_inetaddr_cb = {
4455        .notifier_call = qlcnic_inetaddr_event,
4456};
4457#else
4458static void
4459qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
4460{ }
4461#endif
4462static struct pci_error_handlers qlcnic_err_handler = {
4463        .error_detected = qlcnic_io_error_detected,
4464        .slot_reset = qlcnic_io_slot_reset,
4465        .resume = qlcnic_io_resume,
4466};
4467
4468static struct pci_driver qlcnic_driver = {
4469        .name = qlcnic_driver_name,
4470        .id_table = qlcnic_pci_tbl,
4471        .probe = qlcnic_probe,
4472        .remove = __devexit_p(qlcnic_remove),
4473#ifdef CONFIG_PM
4474        .suspend = qlcnic_suspend,
4475        .resume = qlcnic_resume,
4476#endif
4477        .shutdown = qlcnic_shutdown,
4478        .err_handler = &qlcnic_err_handler
4479
4480};
4481
4482static int __init qlcnic_init_module(void)
4483{
4484        int ret;
4485
4486        printk(KERN_INFO "%s\n", qlcnic_driver_string);
4487
4488        qlcnic_wq = create_singlethread_workqueue("qlcnic");
4489        if (qlcnic_wq == NULL) {
4490                printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4491                return -ENOMEM;
4492        }
4493
4494#ifdef CONFIG_INET
4495        register_netdevice_notifier(&qlcnic_netdev_cb);
4496        register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4497#endif
4498
4499        ret = pci_register_driver(&qlcnic_driver);
4500        if (ret) {
4501#ifdef CONFIG_INET
4502                unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4503                unregister_netdevice_notifier(&qlcnic_netdev_cb);
4504#endif
4505                destroy_workqueue(qlcnic_wq);
4506        }
4507
4508        return ret;
4509}
4510
4511module_init(qlcnic_init_module);
4512
4513static void __exit qlcnic_exit_module(void)
4514{
4515
4516        pci_unregister_driver(&qlcnic_driver);
4517
4518#ifdef CONFIG_INET
4519        unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4520        unregister_netdevice_notifier(&qlcnic_netdev_cb);
4521#endif
4522        destroy_workqueue(qlcnic_wq);
4523}
4524
4525module_exit(qlcnic_exit_module);
4526