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