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