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