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