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