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