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