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