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