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