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