Linux 3.4.102
[linux/fpc-iii.git] / drivers / net / ethernet / qlogic / qlcnic / qlcnic_main.c
blob75c32e875fef17d3705b87692f9341337578460c
1 /*
2 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2010 QLogic Corporation
5 * See LICENSE.qlcnic for copyright and licensing details.
6 */
8 #include <linux/slab.h>
9 #include <linux/vmalloc.h>
10 #include <linux/interrupt.h>
12 #include "qlcnic.h"
14 #include <linux/swab.h>
15 #include <linux/dma-mapping.h>
16 #include <net/ip.h>
17 #include <linux/ipv6.h>
18 #include <linux/inetdevice.h>
19 #include <linux/sysfs.h>
20 #include <linux/aer.h>
21 #include <linux/log2.h>
23 MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
24 MODULE_LICENSE("GPL");
25 MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
26 MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
28 char qlcnic_driver_name[] = "qlcnic";
29 static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
30 "Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
32 static struct workqueue_struct *qlcnic_wq;
33 static int qlcnic_mac_learn;
34 module_param(qlcnic_mac_learn, int, 0444);
35 MODULE_PARM_DESC(qlcnic_mac_learn, "Mac Filter (0=disabled, 1=enabled)");
37 static int use_msi = 1;
38 module_param(use_msi, int, 0444);
39 MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
41 static int use_msi_x = 1;
42 module_param(use_msi_x, int, 0444);
43 MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
45 static int auto_fw_reset = 1;
46 module_param(auto_fw_reset, int, 0644);
47 MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
49 static int load_fw_file;
50 module_param(load_fw_file, int, 0444);
51 MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
53 static int qlcnic_config_npars;
54 module_param(qlcnic_config_npars, int, 0444);
55 MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
57 static int __devinit qlcnic_probe(struct pci_dev *pdev,
58 const struct pci_device_id *ent);
59 static void __devexit qlcnic_remove(struct pci_dev *pdev);
60 static int qlcnic_open(struct net_device *netdev);
61 static int qlcnic_close(struct net_device *netdev);
62 static void qlcnic_tx_timeout(struct net_device *netdev);
63 static void qlcnic_attach_work(struct work_struct *work);
64 static void qlcnic_fwinit_work(struct work_struct *work);
65 static void qlcnic_fw_poll_work(struct work_struct *work);
66 static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
67 work_func_t func, int delay);
68 static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
69 static int qlcnic_poll(struct napi_struct *napi, int budget);
70 static int qlcnic_rx_poll(struct napi_struct *napi, int budget);
71 #ifdef CONFIG_NET_POLL_CONTROLLER
72 static void qlcnic_poll_controller(struct net_device *netdev);
73 #endif
75 static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
76 static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
77 static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
78 static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
80 static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
81 static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8);
82 static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
84 static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
85 static irqreturn_t qlcnic_intr(int irq, void *data);
86 static irqreturn_t qlcnic_msi_intr(int irq, void *data);
87 static irqreturn_t qlcnic_msix_intr(int irq, void *data);
89 static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
90 static void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long);
91 static int qlcnic_start_firmware(struct qlcnic_adapter *);
93 static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
94 static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
95 static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
96 static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
97 static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
98 static void qlcnic_set_netdev_features(struct qlcnic_adapter *,
99 struct qlcnic_esw_func_cfg *);
100 static int qlcnic_vlan_rx_add(struct net_device *, u16);
101 static int qlcnic_vlan_rx_del(struct net_device *, u16);
103 /* PCI Device ID Table */
104 #define ENTRY(device) \
105 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
106 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
108 #define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
110 static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
111 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
112 {0,}
115 MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
118 inline void
119 qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
120 struct qlcnic_host_tx_ring *tx_ring)
122 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
125 static const u32 msi_tgt_status[8] = {
126 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
127 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
128 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
129 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
132 static const
133 struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
135 static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
137 writel(0, sds_ring->crb_intr_mask);
140 static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
142 struct qlcnic_adapter *adapter = sds_ring->adapter;
144 writel(0x1, sds_ring->crb_intr_mask);
146 if (!QLCNIC_IS_MSI_FAMILY(adapter))
147 writel(0xfbff, adapter->tgt_mask_reg);
150 static int
151 qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
153 int size = sizeof(struct qlcnic_host_sds_ring) * count;
155 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
157 return recv_ctx->sds_rings == NULL;
160 static void
161 qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
163 if (recv_ctx->sds_rings != NULL)
164 kfree(recv_ctx->sds_rings);
166 recv_ctx->sds_rings = NULL;
169 static int
170 qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
172 int ring;
173 struct qlcnic_host_sds_ring *sds_ring;
174 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
176 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
177 return -ENOMEM;
179 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
180 sds_ring = &recv_ctx->sds_rings[ring];
182 if (ring == adapter->max_sds_rings - 1)
183 netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
184 QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
185 else
186 netif_napi_add(netdev, &sds_ring->napi,
187 qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
190 return 0;
193 static void
194 qlcnic_napi_del(struct qlcnic_adapter *adapter)
196 int ring;
197 struct qlcnic_host_sds_ring *sds_ring;
198 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
200 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
201 sds_ring = &recv_ctx->sds_rings[ring];
202 netif_napi_del(&sds_ring->napi);
205 qlcnic_free_sds_rings(adapter->recv_ctx);
208 static void
209 qlcnic_napi_enable(struct qlcnic_adapter *adapter)
211 int ring;
212 struct qlcnic_host_sds_ring *sds_ring;
213 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
215 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
216 return;
218 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
219 sds_ring = &recv_ctx->sds_rings[ring];
220 napi_enable(&sds_ring->napi);
221 qlcnic_enable_int(sds_ring);
225 static void
226 qlcnic_napi_disable(struct qlcnic_adapter *adapter)
228 int ring;
229 struct qlcnic_host_sds_ring *sds_ring;
230 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
232 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
233 return;
235 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
236 sds_ring = &recv_ctx->sds_rings[ring];
237 qlcnic_disable_int(sds_ring);
238 napi_synchronize(&sds_ring->napi);
239 napi_disable(&sds_ring->napi);
243 static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
245 memset(&adapter->stats, 0, sizeof(adapter->stats));
248 static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
250 u32 control;
251 int pos;
253 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
254 if (pos) {
255 pci_read_config_dword(pdev, pos, &control);
256 if (enable)
257 control |= PCI_MSIX_FLAGS_ENABLE;
258 else
259 control = 0;
260 pci_write_config_dword(pdev, pos, control);
264 static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
266 int i;
268 for (i = 0; i < count; i++)
269 adapter->msix_entries[i].entry = i;
272 static int
273 qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
275 u8 mac_addr[ETH_ALEN];
276 struct net_device *netdev = adapter->netdev;
277 struct pci_dev *pdev = adapter->pdev;
279 if (qlcnic_get_mac_address(adapter, mac_addr) != 0)
280 return -EIO;
282 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
283 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
284 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
286 /* set station address */
288 if (!is_valid_ether_addr(netdev->perm_addr))
289 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
290 netdev->dev_addr);
292 return 0;
295 static int qlcnic_set_mac(struct net_device *netdev, void *p)
297 struct qlcnic_adapter *adapter = netdev_priv(netdev);
298 struct sockaddr *addr = p;
300 if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
301 return -EOPNOTSUPP;
303 if (!is_valid_ether_addr(addr->sa_data))
304 return -EADDRNOTAVAIL;
306 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
307 netif_device_detach(netdev);
308 qlcnic_napi_disable(adapter);
311 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
312 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
313 qlcnic_set_multi(adapter->netdev);
315 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
316 netif_device_attach(netdev);
317 qlcnic_napi_enable(adapter);
319 return 0;
322 static const struct net_device_ops qlcnic_netdev_ops = {
323 .ndo_open = qlcnic_open,
324 .ndo_stop = qlcnic_close,
325 .ndo_start_xmit = qlcnic_xmit_frame,
326 .ndo_get_stats = qlcnic_get_stats,
327 .ndo_validate_addr = eth_validate_addr,
328 .ndo_set_rx_mode = qlcnic_set_multi,
329 .ndo_set_mac_address = qlcnic_set_mac,
330 .ndo_change_mtu = qlcnic_change_mtu,
331 .ndo_fix_features = qlcnic_fix_features,
332 .ndo_set_features = qlcnic_set_features,
333 .ndo_tx_timeout = qlcnic_tx_timeout,
334 .ndo_vlan_rx_add_vid = qlcnic_vlan_rx_add,
335 .ndo_vlan_rx_kill_vid = qlcnic_vlan_rx_del,
336 #ifdef CONFIG_NET_POLL_CONTROLLER
337 .ndo_poll_controller = qlcnic_poll_controller,
338 #endif
341 static struct qlcnic_nic_template qlcnic_ops = {
342 .config_bridged_mode = qlcnic_config_bridged_mode,
343 .config_led = qlcnic_config_led,
344 .start_firmware = qlcnic_start_firmware
347 static struct qlcnic_nic_template qlcnic_vf_ops = {
348 .config_bridged_mode = qlcnicvf_config_bridged_mode,
349 .config_led = qlcnicvf_config_led,
350 .start_firmware = qlcnicvf_start_firmware
353 static int qlcnic_enable_msix(struct qlcnic_adapter *adapter, u32 num_msix)
355 struct pci_dev *pdev = adapter->pdev;
356 int err = -1;
358 adapter->max_sds_rings = 1;
359 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
360 qlcnic_set_msix_bit(pdev, 0);
362 if (adapter->msix_supported) {
363 enable_msix:
364 qlcnic_init_msix_entries(adapter, num_msix);
365 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
366 if (err == 0) {
367 adapter->flags |= QLCNIC_MSIX_ENABLED;
368 qlcnic_set_msix_bit(pdev, 1);
370 adapter->max_sds_rings = num_msix;
372 dev_info(&pdev->dev, "using msi-x interrupts\n");
373 return err;
375 if (err > 0) {
376 num_msix = rounddown_pow_of_two(err);
377 if (num_msix)
378 goto enable_msix;
381 return err;
385 static void qlcnic_enable_msi_legacy(struct qlcnic_adapter *adapter)
387 const struct qlcnic_legacy_intr_set *legacy_intrp;
388 struct pci_dev *pdev = adapter->pdev;
390 if (use_msi && !pci_enable_msi(pdev)) {
391 adapter->flags |= QLCNIC_MSI_ENABLED;
392 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
393 msi_tgt_status[adapter->ahw->pci_func]);
394 dev_info(&pdev->dev, "using msi interrupts\n");
395 adapter->msix_entries[0].vector = pdev->irq;
396 return;
399 legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
401 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
402 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
403 legacy_intrp->tgt_status_reg);
404 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
405 legacy_intrp->tgt_mask_reg);
406 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
408 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
409 ISR_INT_STATE_REG);
410 dev_info(&pdev->dev, "using legacy interrupts\n");
411 adapter->msix_entries[0].vector = pdev->irq;
414 static void
415 qlcnic_setup_intr(struct qlcnic_adapter *adapter)
417 int num_msix;
419 if (adapter->msix_supported) {
420 num_msix = rounddown_pow_of_two(min_t(int, num_online_cpus(),
421 QLCNIC_DEF_NUM_STS_DESC_RINGS));
422 } else
423 num_msix = 1;
425 if (!qlcnic_enable_msix(adapter, num_msix))
426 return;
428 qlcnic_enable_msi_legacy(adapter);
431 static void
432 qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
434 if (adapter->flags & QLCNIC_MSIX_ENABLED)
435 pci_disable_msix(adapter->pdev);
436 if (adapter->flags & QLCNIC_MSI_ENABLED)
437 pci_disable_msi(adapter->pdev);
440 static void
441 qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
443 if (adapter->ahw->pci_base0 != NULL)
444 iounmap(adapter->ahw->pci_base0);
447 static int
448 qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
450 struct qlcnic_pci_info *pci_info;
451 int i, ret = 0;
452 u8 pfn;
454 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
455 if (!pci_info)
456 return -ENOMEM;
458 adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
459 QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
460 if (!adapter->npars) {
461 ret = -ENOMEM;
462 goto err_pci_info;
465 adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
466 QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
467 if (!adapter->eswitch) {
468 ret = -ENOMEM;
469 goto err_npars;
472 ret = qlcnic_get_pci_info(adapter, pci_info);
473 if (ret)
474 goto err_eswitch;
476 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
477 pfn = pci_info[i].id;
478 if (pfn > QLCNIC_MAX_PCI_FUNC) {
479 ret = QL_STATUS_INVALID_PARAM;
480 goto err_eswitch;
482 adapter->npars[pfn].active = (u8)pci_info[i].active;
483 adapter->npars[pfn].type = (u8)pci_info[i].type;
484 adapter->npars[pfn].phy_port = (u8)pci_info[i].default_port;
485 adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
486 adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
489 for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
490 adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
492 kfree(pci_info);
493 return 0;
495 err_eswitch:
496 kfree(adapter->eswitch);
497 adapter->eswitch = NULL;
498 err_npars:
499 kfree(adapter->npars);
500 adapter->npars = NULL;
501 err_pci_info:
502 kfree(pci_info);
504 return ret;
507 static int
508 qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
510 u8 id;
511 u32 ref_count;
512 int i, ret = 1;
513 u32 data = QLCNIC_MGMT_FUNC;
514 void __iomem *priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
516 /* If other drivers are not in use set their privilege level */
517 ref_count = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
518 ret = qlcnic_api_lock(adapter);
519 if (ret)
520 goto err_lock;
522 if (qlcnic_config_npars) {
523 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
524 id = i;
525 if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
526 id == adapter->ahw->pci_func)
527 continue;
528 data |= (qlcnic_config_npars &
529 QLC_DEV_SET_DRV(0xf, id));
531 } else {
532 data = readl(priv_op);
533 data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw->pci_func)) |
534 (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
535 adapter->ahw->pci_func));
537 writel(data, priv_op);
538 qlcnic_api_unlock(adapter);
539 err_lock:
540 return ret;
543 static void
544 qlcnic_check_vf(struct qlcnic_adapter *adapter)
546 void __iomem *msix_base_addr;
547 void __iomem *priv_op;
548 u32 func;
549 u32 msix_base;
550 u32 op_mode, priv_level;
552 /* Determine FW API version */
553 adapter->fw_hal_version = readl(adapter->ahw->pci_base0 +
554 QLCNIC_FW_API);
556 /* Find PCI function number */
557 pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
558 msix_base_addr = adapter->ahw->pci_base0 + QLCNIC_MSIX_BASE;
559 msix_base = readl(msix_base_addr);
560 func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
561 adapter->ahw->pci_func = func;
563 /* Determine function privilege level */
564 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
565 op_mode = readl(priv_op);
566 if (op_mode == QLC_DEV_DRV_DEFAULT)
567 priv_level = QLCNIC_MGMT_FUNC;
568 else
569 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
571 if (priv_level == QLCNIC_NON_PRIV_FUNC) {
572 adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
573 dev_info(&adapter->pdev->dev,
574 "HAL Version: %d Non Privileged function\n",
575 adapter->fw_hal_version);
576 adapter->nic_ops = &qlcnic_vf_ops;
577 } else
578 adapter->nic_ops = &qlcnic_ops;
581 static int
582 qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
584 void __iomem *mem_ptr0 = NULL;
585 resource_size_t mem_base;
586 unsigned long mem_len, pci_len0 = 0;
588 struct pci_dev *pdev = adapter->pdev;
590 /* remap phys address */
591 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
592 mem_len = pci_resource_len(pdev, 0);
594 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
596 mem_ptr0 = pci_ioremap_bar(pdev, 0);
597 if (mem_ptr0 == NULL) {
598 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
599 return -EIO;
601 pci_len0 = mem_len;
602 } else {
603 return -EIO;
606 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
608 adapter->ahw->pci_base0 = mem_ptr0;
609 adapter->ahw->pci_len0 = pci_len0;
611 qlcnic_check_vf(adapter);
613 adapter->ahw->ocm_win_crb = qlcnic_get_ioaddr(adapter,
614 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(
615 adapter->ahw->pci_func)));
617 return 0;
620 static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
622 struct pci_dev *pdev = adapter->pdev;
623 int i, found = 0;
625 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
626 if (qlcnic_boards[i].vendor == pdev->vendor &&
627 qlcnic_boards[i].device == pdev->device &&
628 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
629 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
630 sprintf(name, "%pM: %s" ,
631 adapter->mac_addr,
632 qlcnic_boards[i].short_name);
633 found = 1;
634 break;
639 if (!found)
640 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
643 static void
644 qlcnic_check_options(struct qlcnic_adapter *adapter)
646 u32 fw_major, fw_minor, fw_build, prev_fw_version;
647 struct pci_dev *pdev = adapter->pdev;
648 struct qlcnic_fw_dump *fw_dump = &adapter->ahw->fw_dump;
650 prev_fw_version = adapter->fw_version;
652 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
653 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
654 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
656 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
658 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC) {
659 if (fw_dump->tmpl_hdr == NULL ||
660 adapter->fw_version > prev_fw_version) {
661 if (fw_dump->tmpl_hdr)
662 vfree(fw_dump->tmpl_hdr);
663 if (!qlcnic_fw_cmd_get_minidump_temp(adapter))
664 dev_info(&pdev->dev,
665 "Supports FW dump capability\n");
669 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
670 fw_major, fw_minor, fw_build);
671 if (adapter->ahw->port_type == QLCNIC_XGBE) {
672 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
673 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
674 adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
675 } else {
676 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
677 adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
680 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
681 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
683 } else if (adapter->ahw->port_type == QLCNIC_GBE) {
684 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
685 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
686 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
687 adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
690 adapter->msix_supported = !!use_msi_x;
692 adapter->num_txd = MAX_CMD_DESCRIPTORS;
694 adapter->max_rds_rings = MAX_RDS_RINGS;
697 static int
698 qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
700 int err;
701 struct qlcnic_info nic_info;
703 err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
704 if (err)
705 return err;
707 adapter->physical_port = (u8)nic_info.phys_port;
708 adapter->switch_mode = nic_info.switch_mode;
709 adapter->max_tx_ques = nic_info.max_tx_ques;
710 adapter->max_rx_ques = nic_info.max_rx_ques;
711 adapter->capabilities = nic_info.capabilities;
712 adapter->max_mac_filters = nic_info.max_mac_filters;
713 adapter->max_mtu = nic_info.max_mtu;
715 if (adapter->capabilities & BIT_6)
716 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
717 else
718 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
720 return err;
723 static void
724 qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
725 struct qlcnic_esw_func_cfg *esw_cfg)
727 if (esw_cfg->discard_tagged)
728 adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
729 else
730 adapter->flags |= QLCNIC_TAGGING_ENABLED;
732 if (esw_cfg->vlan_id)
733 adapter->pvid = esw_cfg->vlan_id;
734 else
735 adapter->pvid = 0;
738 static int
739 qlcnic_vlan_rx_add(struct net_device *netdev, u16 vid)
741 struct qlcnic_adapter *adapter = netdev_priv(netdev);
742 set_bit(vid, adapter->vlans);
743 return 0;
746 static int
747 qlcnic_vlan_rx_del(struct net_device *netdev, u16 vid)
749 struct qlcnic_adapter *adapter = netdev_priv(netdev);
751 qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
752 clear_bit(vid, adapter->vlans);
753 return 0;
756 static void
757 qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
758 struct qlcnic_esw_func_cfg *esw_cfg)
760 adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
761 QLCNIC_PROMISC_DISABLED);
763 if (esw_cfg->mac_anti_spoof)
764 adapter->flags |= QLCNIC_MACSPOOF;
766 if (!esw_cfg->mac_override)
767 adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
769 if (!esw_cfg->promisc_mode)
770 adapter->flags |= QLCNIC_PROMISC_DISABLED;
772 qlcnic_set_netdev_features(adapter, esw_cfg);
775 static int
776 qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
778 struct qlcnic_esw_func_cfg esw_cfg;
780 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
781 return 0;
783 esw_cfg.pci_func = adapter->ahw->pci_func;
784 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
785 return -EIO;
786 qlcnic_set_vlan_config(adapter, &esw_cfg);
787 qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
789 return 0;
792 static void
793 qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
794 struct qlcnic_esw_func_cfg *esw_cfg)
796 struct net_device *netdev = adapter->netdev;
797 netdev_features_t features, vlan_features;
799 features = (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
800 NETIF_F_IPV6_CSUM | NETIF_F_GRO);
801 vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
802 NETIF_F_IPV6_CSUM | NETIF_F_HW_VLAN_FILTER);
804 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
805 features |= (NETIF_F_TSO | NETIF_F_TSO6);
806 vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
809 if (netdev->features & NETIF_F_LRO)
810 features |= NETIF_F_LRO;
812 if (esw_cfg->offload_flags & BIT_0) {
813 netdev->features |= features;
814 if (!(esw_cfg->offload_flags & BIT_1))
815 netdev->features &= ~NETIF_F_TSO;
816 if (!(esw_cfg->offload_flags & BIT_2))
817 netdev->features &= ~NETIF_F_TSO6;
818 } else {
819 netdev->features &= ~features;
822 netdev->vlan_features = (features & vlan_features);
825 static int
826 qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
828 void __iomem *priv_op;
829 u32 op_mode, priv_level;
830 int err = 0;
832 err = qlcnic_initialize_nic(adapter);
833 if (err)
834 return err;
836 if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
837 return 0;
839 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
840 op_mode = readl(priv_op);
841 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
843 if (op_mode == QLC_DEV_DRV_DEFAULT)
844 priv_level = QLCNIC_MGMT_FUNC;
845 else
846 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
848 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
849 if (priv_level == QLCNIC_MGMT_FUNC) {
850 adapter->op_mode = QLCNIC_MGMT_FUNC;
851 err = qlcnic_init_pci_info(adapter);
852 if (err)
853 return err;
854 /* Set privilege level for other functions */
855 qlcnic_set_function_modes(adapter);
856 dev_info(&adapter->pdev->dev,
857 "HAL Version: %d, Management function\n",
858 adapter->fw_hal_version);
859 } else if (priv_level == QLCNIC_PRIV_FUNC) {
860 adapter->op_mode = QLCNIC_PRIV_FUNC;
861 dev_info(&adapter->pdev->dev,
862 "HAL Version: %d, Privileged function\n",
863 adapter->fw_hal_version);
867 adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
869 return err;
872 static int
873 qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
875 struct qlcnic_esw_func_cfg esw_cfg;
876 struct qlcnic_npar_info *npar;
877 u8 i;
879 if (adapter->need_fw_reset)
880 return 0;
882 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
883 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
884 continue;
885 memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
886 esw_cfg.pci_func = i;
887 esw_cfg.offload_flags = BIT_0;
888 esw_cfg.mac_override = BIT_0;
889 esw_cfg.promisc_mode = BIT_0;
890 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
891 esw_cfg.offload_flags |= (BIT_1 | BIT_2);
892 if (qlcnic_config_switch_port(adapter, &esw_cfg))
893 return -EIO;
894 npar = &adapter->npars[i];
895 npar->pvid = esw_cfg.vlan_id;
896 npar->mac_override = esw_cfg.mac_override;
897 npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
898 npar->discard_tagged = esw_cfg.discard_tagged;
899 npar->promisc_mode = esw_cfg.promisc_mode;
900 npar->offload_flags = esw_cfg.offload_flags;
903 return 0;
906 static int
907 qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
908 struct qlcnic_npar_info *npar, int pci_func)
910 struct qlcnic_esw_func_cfg esw_cfg;
911 esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
912 esw_cfg.pci_func = pci_func;
913 esw_cfg.vlan_id = npar->pvid;
914 esw_cfg.mac_override = npar->mac_override;
915 esw_cfg.discard_tagged = npar->discard_tagged;
916 esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
917 esw_cfg.offload_flags = npar->offload_flags;
918 esw_cfg.promisc_mode = npar->promisc_mode;
919 if (qlcnic_config_switch_port(adapter, &esw_cfg))
920 return -EIO;
922 esw_cfg.op_mode = QLCNIC_ADD_VLAN;
923 if (qlcnic_config_switch_port(adapter, &esw_cfg))
924 return -EIO;
926 return 0;
929 static int
930 qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
932 int i, err;
933 struct qlcnic_npar_info *npar;
934 struct qlcnic_info nic_info;
936 if (!adapter->need_fw_reset)
937 return 0;
939 /* Set the NPAR config data after FW reset */
940 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
941 npar = &adapter->npars[i];
942 if (npar->type != QLCNIC_TYPE_NIC)
943 continue;
944 err = qlcnic_get_nic_info(adapter, &nic_info, i);
945 if (err)
946 return err;
947 nic_info.min_tx_bw = npar->min_bw;
948 nic_info.max_tx_bw = npar->max_bw;
949 err = qlcnic_set_nic_info(adapter, &nic_info);
950 if (err)
951 return err;
953 if (npar->enable_pm) {
954 err = qlcnic_config_port_mirroring(adapter,
955 npar->dest_npar, 1, i);
956 if (err)
957 return err;
959 err = qlcnic_reset_eswitch_config(adapter, npar, i);
960 if (err)
961 return err;
963 return 0;
966 static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
968 u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
969 u32 npar_state;
971 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
972 return 0;
974 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
975 while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
976 msleep(1000);
977 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
979 if (!npar_opt_timeo) {
980 dev_err(&adapter->pdev->dev,
981 "Waiting for NPAR state to opertional timeout\n");
982 return -EIO;
984 return 0;
987 static int
988 qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
990 int err;
992 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
993 adapter->op_mode != QLCNIC_MGMT_FUNC)
994 return 0;
996 err = qlcnic_set_default_offload_settings(adapter);
997 if (err)
998 return err;
1000 err = qlcnic_reset_npar_config(adapter);
1001 if (err)
1002 return err;
1004 qlcnic_dev_set_npar_ready(adapter);
1006 return err;
1009 static int
1010 qlcnic_start_firmware(struct qlcnic_adapter *adapter)
1012 int err;
1014 err = qlcnic_can_start_firmware(adapter);
1015 if (err < 0)
1016 return err;
1017 else if (!err)
1018 goto check_fw_status;
1020 if (load_fw_file)
1021 qlcnic_request_firmware(adapter);
1022 else {
1023 err = qlcnic_check_flash_fw_ver(adapter);
1024 if (err)
1025 goto err_out;
1027 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
1030 err = qlcnic_need_fw_reset(adapter);
1031 if (err == 0)
1032 goto check_fw_status;
1034 err = qlcnic_pinit_from_rom(adapter);
1035 if (err)
1036 goto err_out;
1038 err = qlcnic_load_firmware(adapter);
1039 if (err)
1040 goto err_out;
1042 qlcnic_release_firmware(adapter);
1043 QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
1045 check_fw_status:
1046 err = qlcnic_check_fw_status(adapter);
1047 if (err)
1048 goto err_out;
1050 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
1051 qlcnic_idc_debug_info(adapter, 1);
1053 err = qlcnic_check_eswitch_mode(adapter);
1054 if (err) {
1055 dev_err(&adapter->pdev->dev,
1056 "Memory allocation failed for eswitch\n");
1057 goto err_out;
1059 err = qlcnic_set_mgmt_operations(adapter);
1060 if (err)
1061 goto err_out;
1063 qlcnic_check_options(adapter);
1064 adapter->need_fw_reset = 0;
1066 qlcnic_release_firmware(adapter);
1067 return 0;
1069 err_out:
1070 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1071 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
1073 qlcnic_release_firmware(adapter);
1074 return err;
1077 static int
1078 qlcnic_request_irq(struct qlcnic_adapter *adapter)
1080 irq_handler_t handler;
1081 struct qlcnic_host_sds_ring *sds_ring;
1082 int err, ring;
1084 unsigned long flags = 0;
1085 struct net_device *netdev = adapter->netdev;
1086 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1088 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1089 handler = qlcnic_tmp_intr;
1090 if (!QLCNIC_IS_MSI_FAMILY(adapter))
1091 flags |= IRQF_SHARED;
1093 } else {
1094 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1095 handler = qlcnic_msix_intr;
1096 else if (adapter->flags & QLCNIC_MSI_ENABLED)
1097 handler = qlcnic_msi_intr;
1098 else {
1099 flags |= IRQF_SHARED;
1100 handler = qlcnic_intr;
1103 adapter->irq = netdev->irq;
1105 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1106 sds_ring = &recv_ctx->sds_rings[ring];
1107 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1108 err = request_irq(sds_ring->irq, handler,
1109 flags, sds_ring->name, sds_ring);
1110 if (err)
1111 return err;
1114 return 0;
1117 static void
1118 qlcnic_free_irq(struct qlcnic_adapter *adapter)
1120 int ring;
1121 struct qlcnic_host_sds_ring *sds_ring;
1123 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1125 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1126 sds_ring = &recv_ctx->sds_rings[ring];
1127 free_irq(sds_ring->irq, sds_ring);
1131 static int
1132 __qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1134 int ring;
1135 struct qlcnic_host_rds_ring *rds_ring;
1137 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1138 return -EIO;
1140 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1141 return 0;
1142 if (qlcnic_set_eswitch_port_config(adapter))
1143 return -EIO;
1145 if (qlcnic_fw_create_ctx(adapter))
1146 return -EIO;
1148 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1149 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1150 qlcnic_post_rx_buffers(adapter, rds_ring);
1153 qlcnic_set_multi(netdev);
1154 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1156 adapter->ahw->linkup = 0;
1158 if (adapter->max_sds_rings > 1)
1159 qlcnic_config_rss(adapter, 1);
1161 qlcnic_config_intr_coalesce(adapter);
1163 if (netdev->features & NETIF_F_LRO)
1164 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1166 qlcnic_napi_enable(adapter);
1168 qlcnic_linkevent_request(adapter, 1);
1170 adapter->reset_context = 0;
1171 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1172 return 0;
1175 /* Usage: During resume and firmware recovery module.*/
1177 static int
1178 qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1180 int err = 0;
1182 rtnl_lock();
1183 if (netif_running(netdev))
1184 err = __qlcnic_up(adapter, netdev);
1185 rtnl_unlock();
1187 return err;
1190 static void
1191 __qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1193 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1194 return;
1196 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1197 return;
1199 smp_mb();
1200 spin_lock(&adapter->tx_clean_lock);
1201 netif_carrier_off(netdev);
1202 netif_tx_disable(netdev);
1204 qlcnic_free_mac_list(adapter);
1206 if (adapter->fhash.fnum)
1207 qlcnic_delete_lb_filters(adapter);
1209 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1211 qlcnic_napi_disable(adapter);
1213 qlcnic_fw_destroy_ctx(adapter);
1215 qlcnic_reset_rx_buffers_list(adapter);
1216 qlcnic_release_tx_buffers(adapter);
1217 spin_unlock(&adapter->tx_clean_lock);
1220 /* Usage: During suspend and firmware recovery module */
1222 static void
1223 qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1225 rtnl_lock();
1226 if (netif_running(netdev))
1227 __qlcnic_down(adapter, netdev);
1228 rtnl_unlock();
1232 static int
1233 qlcnic_attach(struct qlcnic_adapter *adapter)
1235 struct net_device *netdev = adapter->netdev;
1236 struct pci_dev *pdev = adapter->pdev;
1237 int err;
1239 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1240 return 0;
1242 err = qlcnic_napi_add(adapter, netdev);
1243 if (err)
1244 return err;
1246 err = qlcnic_alloc_sw_resources(adapter);
1247 if (err) {
1248 dev_err(&pdev->dev, "Error in setting sw resources\n");
1249 goto err_out_napi_del;
1252 err = qlcnic_alloc_hw_resources(adapter);
1253 if (err) {
1254 dev_err(&pdev->dev, "Error in setting hw resources\n");
1255 goto err_out_free_sw;
1258 err = qlcnic_request_irq(adapter);
1259 if (err) {
1260 dev_err(&pdev->dev, "failed to setup interrupt\n");
1261 goto err_out_free_hw;
1264 qlcnic_create_sysfs_entries(adapter);
1266 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1267 return 0;
1269 err_out_free_hw:
1270 qlcnic_free_hw_resources(adapter);
1271 err_out_free_sw:
1272 qlcnic_free_sw_resources(adapter);
1273 err_out_napi_del:
1274 qlcnic_napi_del(adapter);
1275 return err;
1278 static void
1279 qlcnic_detach(struct qlcnic_adapter *adapter)
1281 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1282 return;
1284 qlcnic_remove_sysfs_entries(adapter);
1286 qlcnic_free_hw_resources(adapter);
1287 qlcnic_release_rx_buffers(adapter);
1288 qlcnic_free_irq(adapter);
1289 qlcnic_napi_del(adapter);
1290 qlcnic_free_sw_resources(adapter);
1292 adapter->is_up = 0;
1295 void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1297 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1298 struct qlcnic_host_sds_ring *sds_ring;
1299 int ring;
1301 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
1302 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1303 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1304 sds_ring = &adapter->recv_ctx->sds_rings[ring];
1305 qlcnic_disable_int(sds_ring);
1309 qlcnic_fw_destroy_ctx(adapter);
1311 qlcnic_detach(adapter);
1313 adapter->diag_test = 0;
1314 adapter->max_sds_rings = max_sds_rings;
1316 if (qlcnic_attach(adapter))
1317 goto out;
1319 if (netif_running(netdev))
1320 __qlcnic_up(adapter, netdev);
1321 out:
1322 netif_device_attach(netdev);
1325 static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1327 int err = 0;
1328 adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1329 GFP_KERNEL);
1330 if (!adapter->ahw) {
1331 dev_err(&adapter->pdev->dev,
1332 "Failed to allocate recv ctx resources for adapter\n");
1333 err = -ENOMEM;
1334 goto err_out;
1336 adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1337 GFP_KERNEL);
1338 if (!adapter->recv_ctx) {
1339 dev_err(&adapter->pdev->dev,
1340 "Failed to allocate recv ctx resources for adapter\n");
1341 kfree(adapter->ahw);
1342 adapter->ahw = NULL;
1343 err = -ENOMEM;
1344 goto err_out;
1346 /* Initialize interrupt coalesce parameters */
1347 adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1348 adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1349 adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
1350 err_out:
1351 return err;
1354 static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1356 kfree(adapter->recv_ctx);
1357 adapter->recv_ctx = NULL;
1359 if (adapter->ahw->fw_dump.tmpl_hdr) {
1360 vfree(adapter->ahw->fw_dump.tmpl_hdr);
1361 adapter->ahw->fw_dump.tmpl_hdr = NULL;
1363 kfree(adapter->ahw);
1364 adapter->ahw = NULL;
1367 int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1369 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1370 struct qlcnic_host_sds_ring *sds_ring;
1371 struct qlcnic_host_rds_ring *rds_ring;
1372 int ring;
1373 int ret;
1375 netif_device_detach(netdev);
1377 if (netif_running(netdev))
1378 __qlcnic_down(adapter, netdev);
1380 qlcnic_detach(adapter);
1382 adapter->max_sds_rings = 1;
1383 adapter->diag_test = test;
1385 ret = qlcnic_attach(adapter);
1386 if (ret) {
1387 netif_device_attach(netdev);
1388 return ret;
1391 ret = qlcnic_fw_create_ctx(adapter);
1392 if (ret) {
1393 qlcnic_detach(adapter);
1394 netif_device_attach(netdev);
1395 return ret;
1398 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1399 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1400 qlcnic_post_rx_buffers(adapter, rds_ring);
1403 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1404 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1405 sds_ring = &adapter->recv_ctx->sds_rings[ring];
1406 qlcnic_enable_int(sds_ring);
1410 if (adapter->diag_test == QLCNIC_LOOPBACK_TEST) {
1411 adapter->ahw->loopback_state = 0;
1412 qlcnic_linkevent_request(adapter, 1);
1415 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1417 return 0;
1420 /* Reset context in hardware only */
1421 static int
1422 qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1424 struct net_device *netdev = adapter->netdev;
1426 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1427 return -EBUSY;
1429 netif_device_detach(netdev);
1431 qlcnic_down(adapter, netdev);
1433 qlcnic_up(adapter, netdev);
1435 netif_device_attach(netdev);
1437 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1438 return 0;
1442 qlcnic_reset_context(struct qlcnic_adapter *adapter)
1444 int err = 0;
1445 struct net_device *netdev = adapter->netdev;
1447 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1448 return -EBUSY;
1450 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1452 netif_device_detach(netdev);
1454 if (netif_running(netdev))
1455 __qlcnic_down(adapter, netdev);
1457 qlcnic_detach(adapter);
1459 if (netif_running(netdev)) {
1460 err = qlcnic_attach(adapter);
1461 if (!err) {
1462 __qlcnic_up(adapter, netdev);
1463 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
1467 netif_device_attach(netdev);
1470 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1471 return err;
1474 static int
1475 qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
1476 struct net_device *netdev, u8 pci_using_dac)
1478 int err;
1479 struct pci_dev *pdev = adapter->pdev;
1481 adapter->mc_enabled = 0;
1482 adapter->max_mc_count = 38;
1484 netdev->netdev_ops = &qlcnic_netdev_ops;
1485 netdev->watchdog_timeo = 5*HZ;
1487 qlcnic_change_mtu(netdev, netdev->mtu);
1489 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1491 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1492 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
1494 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
1495 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1496 if (pci_using_dac)
1497 netdev->hw_features |= NETIF_F_HIGHDMA;
1499 netdev->vlan_features = netdev->hw_features;
1501 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1502 netdev->hw_features |= NETIF_F_HW_VLAN_TX;
1503 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1504 netdev->hw_features |= NETIF_F_LRO;
1506 netdev->features |= netdev->hw_features |
1507 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
1509 netdev->irq = adapter->msix_entries[0].vector;
1511 err = register_netdev(netdev);
1512 if (err) {
1513 dev_err(&pdev->dev, "failed to register net device\n");
1514 return err;
1517 return 0;
1520 static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1522 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1523 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1524 *pci_using_dac = 1;
1525 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1526 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1527 *pci_using_dac = 0;
1528 else {
1529 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1530 return -EIO;
1533 return 0;
1536 static int
1537 qlcnic_alloc_msix_entries(struct qlcnic_adapter *adapter, u16 count)
1539 adapter->msix_entries = kcalloc(count, sizeof(struct msix_entry),
1540 GFP_KERNEL);
1542 if (adapter->msix_entries)
1543 return 0;
1545 dev_err(&adapter->pdev->dev, "failed allocating msix_entries\n");
1546 return -ENOMEM;
1549 static int __devinit
1550 qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1552 struct net_device *netdev = NULL;
1553 struct qlcnic_adapter *adapter = NULL;
1554 int err;
1555 uint8_t revision_id;
1556 uint8_t pci_using_dac;
1557 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
1559 err = pci_enable_device(pdev);
1560 if (err)
1561 return err;
1563 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1564 err = -ENODEV;
1565 goto err_out_disable_pdev;
1568 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1569 if (err)
1570 goto err_out_disable_pdev;
1572 err = pci_request_regions(pdev, qlcnic_driver_name);
1573 if (err)
1574 goto err_out_disable_pdev;
1576 pci_set_master(pdev);
1577 pci_enable_pcie_error_reporting(pdev);
1579 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1580 if (!netdev) {
1581 err = -ENOMEM;
1582 goto err_out_free_res;
1585 SET_NETDEV_DEV(netdev, &pdev->dev);
1587 adapter = netdev_priv(netdev);
1588 adapter->netdev = netdev;
1589 adapter->pdev = pdev;
1591 if (qlcnic_alloc_adapter_resources(adapter))
1592 goto err_out_free_netdev;
1594 adapter->dev_rst_time = jiffies;
1595 revision_id = pdev->revision;
1596 adapter->ahw->revision_id = revision_id;
1597 adapter->mac_learn = qlcnic_mac_learn;
1599 rwlock_init(&adapter->ahw->crb_lock);
1600 mutex_init(&adapter->ahw->mem_lock);
1602 spin_lock_init(&adapter->tx_clean_lock);
1603 INIT_LIST_HEAD(&adapter->mac_list);
1605 err = qlcnic_setup_pci_map(adapter);
1606 if (err)
1607 goto err_out_free_hw;
1609 /* This will be reset for mezz cards */
1610 adapter->portnum = adapter->ahw->pci_func;
1612 err = qlcnic_get_board_info(adapter);
1613 if (err) {
1614 dev_err(&pdev->dev, "Error getting board config info.\n");
1615 goto err_out_iounmap;
1618 err = qlcnic_setup_idc_param(adapter);
1619 if (err)
1620 goto err_out_iounmap;
1622 adapter->flags |= QLCNIC_NEED_FLR;
1624 err = adapter->nic_ops->start_firmware(adapter);
1625 if (err) {
1626 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
1627 goto err_out_decr_ref;
1630 if (qlcnic_read_mac_addr(adapter))
1631 dev_warn(&pdev->dev, "failed to read mac addr\n");
1633 if (adapter->portnum == 0) {
1634 get_brd_name(adapter, brd_name);
1636 pr_info("%s: %s Board Chip rev 0x%x\n",
1637 module_name(THIS_MODULE),
1638 brd_name, adapter->ahw->revision_id);
1641 qlcnic_clear_stats(adapter);
1643 err = qlcnic_alloc_msix_entries(adapter, adapter->max_rx_ques);
1644 if (err)
1645 goto err_out_decr_ref;
1647 qlcnic_setup_intr(adapter);
1649 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
1650 if (err)
1651 goto err_out_disable_msi;
1653 pci_set_drvdata(pdev, adapter);
1655 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1657 switch (adapter->ahw->port_type) {
1658 case QLCNIC_GBE:
1659 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1660 adapter->netdev->name);
1661 break;
1662 case QLCNIC_XGBE:
1663 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1664 adapter->netdev->name);
1665 break;
1668 if (adapter->mac_learn)
1669 qlcnic_alloc_lb_filters_mem(adapter);
1671 qlcnic_create_diag_entries(adapter);
1673 return 0;
1675 err_out_disable_msi:
1676 qlcnic_teardown_intr(adapter);
1677 kfree(adapter->msix_entries);
1679 err_out_decr_ref:
1680 qlcnic_clr_all_drv_state(adapter, 0);
1682 err_out_iounmap:
1683 qlcnic_cleanup_pci_map(adapter);
1685 err_out_free_hw:
1686 qlcnic_free_adapter_resources(adapter);
1688 err_out_free_netdev:
1689 free_netdev(netdev);
1691 err_out_free_res:
1692 pci_release_regions(pdev);
1694 err_out_disable_pdev:
1695 pci_set_drvdata(pdev, NULL);
1696 pci_disable_device(pdev);
1697 return err;
1700 static void __devexit qlcnic_remove(struct pci_dev *pdev)
1702 struct qlcnic_adapter *adapter;
1703 struct net_device *netdev;
1705 adapter = pci_get_drvdata(pdev);
1706 if (adapter == NULL)
1707 return;
1709 netdev = adapter->netdev;
1711 qlcnic_cancel_fw_work(adapter);
1713 unregister_netdev(netdev);
1715 qlcnic_detach(adapter);
1717 if (adapter->npars != NULL)
1718 kfree(adapter->npars);
1719 if (adapter->eswitch != NULL)
1720 kfree(adapter->eswitch);
1722 qlcnic_clr_all_drv_state(adapter, 0);
1724 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1726 qlcnic_free_lb_filters_mem(adapter);
1728 qlcnic_teardown_intr(adapter);
1729 kfree(adapter->msix_entries);
1731 qlcnic_remove_diag_entries(adapter);
1733 qlcnic_cleanup_pci_map(adapter);
1735 qlcnic_release_firmware(adapter);
1737 pci_disable_pcie_error_reporting(pdev);
1738 pci_release_regions(pdev);
1739 pci_disable_device(pdev);
1740 pci_set_drvdata(pdev, NULL);
1742 qlcnic_free_adapter_resources(adapter);
1743 free_netdev(netdev);
1745 static int __qlcnic_shutdown(struct pci_dev *pdev)
1747 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1748 struct net_device *netdev = adapter->netdev;
1749 int retval;
1751 netif_device_detach(netdev);
1753 qlcnic_cancel_fw_work(adapter);
1755 if (netif_running(netdev))
1756 qlcnic_down(adapter, netdev);
1758 qlcnic_clr_all_drv_state(adapter, 0);
1760 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1762 retval = pci_save_state(pdev);
1763 if (retval)
1764 return retval;
1766 if (qlcnic_wol_supported(adapter)) {
1767 pci_enable_wake(pdev, PCI_D3cold, 1);
1768 pci_enable_wake(pdev, PCI_D3hot, 1);
1771 return 0;
1774 static void qlcnic_shutdown(struct pci_dev *pdev)
1776 if (__qlcnic_shutdown(pdev))
1777 return;
1779 pci_disable_device(pdev);
1782 #ifdef CONFIG_PM
1783 static int
1784 qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1786 int retval;
1788 retval = __qlcnic_shutdown(pdev);
1789 if (retval)
1790 return retval;
1792 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1793 return 0;
1796 static int
1797 qlcnic_resume(struct pci_dev *pdev)
1799 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1800 struct net_device *netdev = adapter->netdev;
1801 int err;
1803 err = pci_enable_device(pdev);
1804 if (err)
1805 return err;
1807 pci_set_power_state(pdev, PCI_D0);
1808 pci_set_master(pdev);
1809 pci_restore_state(pdev);
1811 err = adapter->nic_ops->start_firmware(adapter);
1812 if (err) {
1813 dev_err(&pdev->dev, "failed to start firmware\n");
1814 return err;
1817 if (netif_running(netdev)) {
1818 err = qlcnic_up(adapter, netdev);
1819 if (err)
1820 goto done;
1822 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
1824 done:
1825 netif_device_attach(netdev);
1826 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1827 return 0;
1829 #endif
1831 static int qlcnic_open(struct net_device *netdev)
1833 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1834 int err;
1836 netif_carrier_off(netdev);
1838 err = qlcnic_attach(adapter);
1839 if (err)
1840 return err;
1842 err = __qlcnic_up(adapter, netdev);
1843 if (err)
1844 goto err_out;
1846 netif_start_queue(netdev);
1848 return 0;
1850 err_out:
1851 qlcnic_detach(adapter);
1852 return err;
1856 * qlcnic_close - Disables a network interface entry point
1858 static int qlcnic_close(struct net_device *netdev)
1860 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1862 __qlcnic_down(adapter, netdev);
1863 return 0;
1866 void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
1868 void *head;
1869 int i;
1871 if (adapter->fhash.fmax && adapter->fhash.fhead)
1872 return;
1874 spin_lock_init(&adapter->mac_learn_lock);
1876 head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1877 GFP_KERNEL);
1878 if (!head)
1879 return;
1881 adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
1882 adapter->fhash.fhead = head;
1884 for (i = 0; i < adapter->fhash.fmax; i++)
1885 INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1888 static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1890 if (adapter->fhash.fmax && adapter->fhash.fhead)
1891 kfree(adapter->fhash.fhead);
1893 adapter->fhash.fhead = NULL;
1894 adapter->fhash.fmax = 0;
1897 static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
1898 u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
1900 struct cmd_desc_type0 *hwdesc;
1901 struct qlcnic_nic_req *req;
1902 struct qlcnic_mac_req *mac_req;
1903 struct qlcnic_vlan_req *vlan_req;
1904 u32 producer;
1905 u64 word;
1907 producer = tx_ring->producer;
1908 hwdesc = &tx_ring->desc_head[tx_ring->producer];
1910 req = (struct qlcnic_nic_req *)hwdesc;
1911 memset(req, 0, sizeof(struct qlcnic_nic_req));
1912 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1914 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1915 req->req_hdr = cpu_to_le64(word);
1917 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
1918 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
1919 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1921 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1922 vlan_req->vlan_id = vlan_id;
1924 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
1925 smp_mb();
1928 #define QLCNIC_MAC_HASH(MAC)\
1929 ((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1931 static void
1932 qlcnic_send_filter(struct qlcnic_adapter *adapter,
1933 struct qlcnic_host_tx_ring *tx_ring,
1934 struct cmd_desc_type0 *first_desc,
1935 struct sk_buff *skb)
1937 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1938 struct qlcnic_filter *fil, *tmp_fil;
1939 struct hlist_node *tmp_hnode, *n;
1940 struct hlist_head *head;
1941 u64 src_addr = 0;
1942 __le16 vlan_id = 0;
1943 u8 hindex;
1945 if (!compare_ether_addr(phdr->h_source, adapter->mac_addr))
1946 return;
1948 if (adapter->fhash.fnum >= adapter->fhash.fmax)
1949 return;
1951 /* Only NPAR capable devices support vlan based learning*/
1952 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1953 vlan_id = first_desc->vlan_TCI;
1954 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1955 hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1956 head = &(adapter->fhash.fhead[hindex]);
1958 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
1959 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1960 tmp_fil->vlan_id == vlan_id) {
1962 if (jiffies >
1963 (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1964 qlcnic_change_filter(adapter, src_addr, vlan_id,
1965 tx_ring);
1966 tmp_fil->ftime = jiffies;
1967 return;
1971 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
1972 if (!fil)
1973 return;
1975 qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
1977 fil->ftime = jiffies;
1978 fil->vlan_id = vlan_id;
1979 memcpy(fil->faddr, &src_addr, ETH_ALEN);
1980 spin_lock(&adapter->mac_learn_lock);
1981 hlist_add_head(&(fil->fnode), head);
1982 adapter->fhash.fnum++;
1983 spin_unlock(&adapter->mac_learn_lock);
1986 static int
1987 qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
1988 struct cmd_desc_type0 *first_desc,
1989 struct sk_buff *skb)
1991 u8 opcode = 0, hdr_len = 0;
1992 u16 flags = 0, vlan_tci = 0;
1993 int copied, offset, copy_len;
1994 struct cmd_desc_type0 *hwdesc;
1995 struct vlan_ethhdr *vh;
1996 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1997 u16 protocol = ntohs(skb->protocol);
1998 u32 producer = tx_ring->producer;
2000 if (protocol == ETH_P_8021Q) {
2001 vh = (struct vlan_ethhdr *)skb->data;
2002 flags = FLAGS_VLAN_TAGGED;
2003 vlan_tci = vh->h_vlan_TCI;
2004 } else if (vlan_tx_tag_present(skb)) {
2005 flags = FLAGS_VLAN_OOB;
2006 vlan_tci = vlan_tx_tag_get(skb);
2008 if (unlikely(adapter->pvid)) {
2009 if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
2010 return -EIO;
2011 if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
2012 goto set_flags;
2014 flags = FLAGS_VLAN_OOB;
2015 vlan_tci = adapter->pvid;
2017 set_flags:
2018 qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
2019 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2021 if (*(skb->data) & BIT_0) {
2022 flags |= BIT_0;
2023 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
2025 opcode = TX_ETHER_PKT;
2026 if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
2027 skb_shinfo(skb)->gso_size > 0) {
2029 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2031 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
2032 first_desc->total_hdr_length = hdr_len;
2034 opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
2036 /* For LSO, we need to copy the MAC/IP/TCP headers into
2037 * the descriptor ring */
2038 copied = 0;
2039 offset = 2;
2041 if (flags & FLAGS_VLAN_OOB) {
2042 first_desc->total_hdr_length += VLAN_HLEN;
2043 first_desc->tcp_hdr_offset = VLAN_HLEN;
2044 first_desc->ip_hdr_offset = VLAN_HLEN;
2045 /* Only in case of TSO on vlan device */
2046 flags |= FLAGS_VLAN_TAGGED;
2048 /* Create a TSO vlan header template for firmware */
2050 hwdesc = &tx_ring->desc_head[producer];
2051 tx_ring->cmd_buf_arr[producer].skb = NULL;
2053 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2054 offset, hdr_len + VLAN_HLEN);
2056 vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
2057 skb_copy_from_linear_data(skb, vh, 12);
2058 vh->h_vlan_proto = htons(ETH_P_8021Q);
2059 vh->h_vlan_TCI = htons(vlan_tci);
2061 skb_copy_from_linear_data_offset(skb, 12,
2062 (char *)vh + 16, copy_len - 16);
2064 copied = copy_len - VLAN_HLEN;
2065 offset = 0;
2067 producer = get_next_index(producer, tx_ring->num_desc);
2070 while (copied < hdr_len) {
2072 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2073 offset, (hdr_len - copied));
2075 hwdesc = &tx_ring->desc_head[producer];
2076 tx_ring->cmd_buf_arr[producer].skb = NULL;
2078 skb_copy_from_linear_data_offset(skb, copied,
2079 (char *) hwdesc + offset, copy_len);
2081 copied += copy_len;
2082 offset = 0;
2084 producer = get_next_index(producer, tx_ring->num_desc);
2087 tx_ring->producer = producer;
2088 smp_mb();
2089 adapter->stats.lso_frames++;
2091 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2092 u8 l4proto;
2094 if (protocol == ETH_P_IP) {
2095 l4proto = ip_hdr(skb)->protocol;
2097 if (l4proto == IPPROTO_TCP)
2098 opcode = TX_TCP_PKT;
2099 else if (l4proto == IPPROTO_UDP)
2100 opcode = TX_UDP_PKT;
2101 } else if (protocol == ETH_P_IPV6) {
2102 l4proto = ipv6_hdr(skb)->nexthdr;
2104 if (l4proto == IPPROTO_TCP)
2105 opcode = TX_TCPV6_PKT;
2106 else if (l4proto == IPPROTO_UDP)
2107 opcode = TX_UDPV6_PKT;
2110 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
2111 first_desc->ip_hdr_offset += skb_network_offset(skb);
2112 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2114 return 0;
2117 static int
2118 qlcnic_map_tx_skb(struct pci_dev *pdev,
2119 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2121 struct qlcnic_skb_frag *nf;
2122 struct skb_frag_struct *frag;
2123 int i, nr_frags;
2124 dma_addr_t map;
2126 nr_frags = skb_shinfo(skb)->nr_frags;
2127 nf = &pbuf->frag_array[0];
2129 map = pci_map_single(pdev, skb->data,
2130 skb_headlen(skb), PCI_DMA_TODEVICE);
2131 if (pci_dma_mapping_error(pdev, map))
2132 goto out_err;
2134 nf->dma = map;
2135 nf->length = skb_headlen(skb);
2137 for (i = 0; i < nr_frags; i++) {
2138 frag = &skb_shinfo(skb)->frags[i];
2139 nf = &pbuf->frag_array[i+1];
2141 map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
2142 DMA_TO_DEVICE);
2143 if (dma_mapping_error(&pdev->dev, map))
2144 goto unwind;
2146 nf->dma = map;
2147 nf->length = skb_frag_size(frag);
2150 return 0;
2152 unwind:
2153 while (--i >= 0) {
2154 nf = &pbuf->frag_array[i+1];
2155 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2158 nf = &pbuf->frag_array[0];
2159 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2161 out_err:
2162 return -ENOMEM;
2165 static void
2166 qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
2167 struct qlcnic_cmd_buffer *pbuf)
2169 struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
2170 int nr_frags = skb_shinfo(skb)->nr_frags;
2171 int i;
2173 for (i = 0; i < nr_frags; i++) {
2174 nf = &pbuf->frag_array[i+1];
2175 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2178 nf = &pbuf->frag_array[0];
2179 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2180 pbuf->skb = NULL;
2183 static inline void
2184 qlcnic_clear_cmddesc(u64 *desc)
2186 desc[0] = 0ULL;
2187 desc[2] = 0ULL;
2188 desc[7] = 0ULL;
2191 netdev_tx_t
2192 qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2194 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2195 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2196 struct qlcnic_cmd_buffer *pbuf;
2197 struct qlcnic_skb_frag *buffrag;
2198 struct cmd_desc_type0 *hwdesc, *first_desc;
2199 struct pci_dev *pdev;
2200 struct ethhdr *phdr;
2201 int delta = 0;
2202 int i, k;
2204 u32 producer;
2205 int frag_count;
2206 u32 num_txd = tx_ring->num_desc;
2208 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2209 netif_stop_queue(netdev);
2210 return NETDEV_TX_BUSY;
2213 if (adapter->flags & QLCNIC_MACSPOOF) {
2214 phdr = (struct ethhdr *)skb->data;
2215 if (compare_ether_addr(phdr->h_source,
2216 adapter->mac_addr))
2217 goto drop_packet;
2220 frag_count = skb_shinfo(skb)->nr_frags + 1;
2221 /* 14 frags supported for normal packet and
2222 * 32 frags supported for TSO packet
2224 if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
2226 for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
2227 delta += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2229 if (!__pskb_pull_tail(skb, delta))
2230 goto drop_packet;
2232 frag_count = 1 + skb_shinfo(skb)->nr_frags;
2235 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
2236 netif_stop_queue(netdev);
2237 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2238 netif_start_queue(netdev);
2239 else {
2240 adapter->stats.xmit_off++;
2241 return NETDEV_TX_BUSY;
2245 producer = tx_ring->producer;
2246 pbuf = &tx_ring->cmd_buf_arr[producer];
2248 pdev = adapter->pdev;
2250 first_desc = hwdesc = &tx_ring->desc_head[producer];
2251 qlcnic_clear_cmddesc((u64 *)hwdesc);
2253 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2254 adapter->stats.tx_dma_map_error++;
2255 goto drop_packet;
2258 pbuf->skb = skb;
2259 pbuf->frag_count = frag_count;
2261 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2262 qlcnic_set_tx_port(first_desc, adapter->portnum);
2264 for (i = 0; i < frag_count; i++) {
2266 k = i % 4;
2268 if ((k == 0) && (i > 0)) {
2269 /* move to next desc.*/
2270 producer = get_next_index(producer, num_txd);
2271 hwdesc = &tx_ring->desc_head[producer];
2272 qlcnic_clear_cmddesc((u64 *)hwdesc);
2273 tx_ring->cmd_buf_arr[producer].skb = NULL;
2276 buffrag = &pbuf->frag_array[i];
2278 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2279 switch (k) {
2280 case 0:
2281 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2282 break;
2283 case 1:
2284 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2285 break;
2286 case 2:
2287 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2288 break;
2289 case 3:
2290 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2291 break;
2295 tx_ring->producer = get_next_index(producer, num_txd);
2296 smp_mb();
2298 if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
2299 goto unwind_buff;
2301 if (adapter->mac_learn)
2302 qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2304 adapter->stats.txbytes += skb->len;
2305 adapter->stats.xmitcalled++;
2307 qlcnic_update_cmd_producer(adapter, tx_ring);
2309 return NETDEV_TX_OK;
2311 unwind_buff:
2312 qlcnic_unmap_buffers(pdev, skb, pbuf);
2313 drop_packet:
2314 adapter->stats.txdropped++;
2315 dev_kfree_skb_any(skb);
2316 return NETDEV_TX_OK;
2319 static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2321 struct net_device *netdev = adapter->netdev;
2322 u32 temp, temp_state, temp_val;
2323 int rv = 0;
2325 temp = QLCRD32(adapter, CRB_TEMP_STATE);
2327 temp_state = qlcnic_get_temp_state(temp);
2328 temp_val = qlcnic_get_temp_val(temp);
2330 if (temp_state == QLCNIC_TEMP_PANIC) {
2331 dev_err(&netdev->dev,
2332 "Device temperature %d degrees C exceeds"
2333 " maximum allowed. Hardware has been shut down.\n",
2334 temp_val);
2335 rv = 1;
2336 } else if (temp_state == QLCNIC_TEMP_WARN) {
2337 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2338 dev_err(&netdev->dev,
2339 "Device temperature %d degrees C "
2340 "exceeds operating range."
2341 " Immediate action needed.\n",
2342 temp_val);
2344 } else {
2345 if (adapter->temp == QLCNIC_TEMP_WARN) {
2346 dev_info(&netdev->dev,
2347 "Device temperature is now %d degrees C"
2348 " in normal range.\n", temp_val);
2351 adapter->temp = temp_state;
2352 return rv;
2355 void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2357 struct net_device *netdev = adapter->netdev;
2359 if (adapter->ahw->linkup && !linkup) {
2360 netdev_info(netdev, "NIC Link is down\n");
2361 adapter->ahw->linkup = 0;
2362 if (netif_running(netdev)) {
2363 netif_carrier_off(netdev);
2364 netif_stop_queue(netdev);
2366 } else if (!adapter->ahw->linkup && linkup) {
2367 netdev_info(netdev, "NIC Link is up\n");
2368 adapter->ahw->linkup = 1;
2369 if (netif_running(netdev)) {
2370 netif_carrier_on(netdev);
2371 netif_wake_queue(netdev);
2376 static void qlcnic_tx_timeout(struct net_device *netdev)
2378 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2380 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2381 return;
2383 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
2385 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
2386 adapter->need_fw_reset = 1;
2387 else
2388 adapter->reset_context = 1;
2391 static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2393 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2394 struct net_device_stats *stats = &netdev->stats;
2396 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2397 stats->tx_packets = adapter->stats.xmitfinished;
2398 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
2399 stats->tx_bytes = adapter->stats.txbytes;
2400 stats->rx_dropped = adapter->stats.rxdropped;
2401 stats->tx_dropped = adapter->stats.txdropped;
2403 return stats;
2406 static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
2408 u32 status;
2410 status = readl(adapter->isr_int_vec);
2412 if (!(status & adapter->int_vec_bit))
2413 return IRQ_NONE;
2415 /* check interrupt state machine, to be sure */
2416 status = readl(adapter->crb_int_state_reg);
2417 if (!ISR_LEGACY_INT_TRIGGERED(status))
2418 return IRQ_NONE;
2420 writel(0xffffffff, adapter->tgt_status_reg);
2421 /* read twice to ensure write is flushed */
2422 readl(adapter->isr_int_vec);
2423 readl(adapter->isr_int_vec);
2425 return IRQ_HANDLED;
2428 static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2430 struct qlcnic_host_sds_ring *sds_ring = data;
2431 struct qlcnic_adapter *adapter = sds_ring->adapter;
2433 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2434 goto done;
2435 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2436 writel(0xffffffff, adapter->tgt_status_reg);
2437 goto done;
2440 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2441 return IRQ_NONE;
2443 done:
2444 adapter->diag_cnt++;
2445 qlcnic_enable_int(sds_ring);
2446 return IRQ_HANDLED;
2449 static irqreturn_t qlcnic_intr(int irq, void *data)
2451 struct qlcnic_host_sds_ring *sds_ring = data;
2452 struct qlcnic_adapter *adapter = sds_ring->adapter;
2454 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2455 return IRQ_NONE;
2457 napi_schedule(&sds_ring->napi);
2459 return IRQ_HANDLED;
2462 static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2464 struct qlcnic_host_sds_ring *sds_ring = data;
2465 struct qlcnic_adapter *adapter = sds_ring->adapter;
2467 /* clear interrupt */
2468 writel(0xffffffff, adapter->tgt_status_reg);
2470 napi_schedule(&sds_ring->napi);
2471 return IRQ_HANDLED;
2474 static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2476 struct qlcnic_host_sds_ring *sds_ring = data;
2478 napi_schedule(&sds_ring->napi);
2479 return IRQ_HANDLED;
2482 static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2484 u32 sw_consumer, hw_consumer;
2485 int count = 0, i;
2486 struct qlcnic_cmd_buffer *buffer;
2487 struct pci_dev *pdev = adapter->pdev;
2488 struct net_device *netdev = adapter->netdev;
2489 struct qlcnic_skb_frag *frag;
2490 int done;
2491 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2493 if (!spin_trylock(&adapter->tx_clean_lock))
2494 return 1;
2496 sw_consumer = tx_ring->sw_consumer;
2497 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2499 while (sw_consumer != hw_consumer) {
2500 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2501 if (buffer->skb) {
2502 frag = &buffer->frag_array[0];
2503 pci_unmap_single(pdev, frag->dma, frag->length,
2504 PCI_DMA_TODEVICE);
2505 frag->dma = 0ULL;
2506 for (i = 1; i < buffer->frag_count; i++) {
2507 frag++;
2508 pci_unmap_page(pdev, frag->dma, frag->length,
2509 PCI_DMA_TODEVICE);
2510 frag->dma = 0ULL;
2513 adapter->stats.xmitfinished++;
2514 dev_kfree_skb_any(buffer->skb);
2515 buffer->skb = NULL;
2518 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2519 if (++count >= MAX_STATUS_HANDLE)
2520 break;
2523 if (count && netif_running(netdev)) {
2524 tx_ring->sw_consumer = sw_consumer;
2526 smp_mb();
2528 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
2529 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2530 netif_wake_queue(netdev);
2531 adapter->stats.xmit_on++;
2534 adapter->tx_timeo_cnt = 0;
2537 * If everything is freed up to consumer then check if the ring is full
2538 * If the ring is full then check if more needs to be freed and
2539 * schedule the call back again.
2541 * This happens when there are 2 CPUs. One could be freeing and the
2542 * other filling it. If the ring is full when we get out of here and
2543 * the card has already interrupted the host then the host can miss the
2544 * interrupt.
2546 * There is still a possible race condition and the host could miss an
2547 * interrupt. The card has to take care of this.
2549 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2550 done = (sw_consumer == hw_consumer);
2551 spin_unlock(&adapter->tx_clean_lock);
2553 return done;
2556 static int qlcnic_poll(struct napi_struct *napi, int budget)
2558 struct qlcnic_host_sds_ring *sds_ring =
2559 container_of(napi, struct qlcnic_host_sds_ring, napi);
2561 struct qlcnic_adapter *adapter = sds_ring->adapter;
2563 int tx_complete;
2564 int work_done;
2566 tx_complete = qlcnic_process_cmd_ring(adapter);
2568 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2570 if ((work_done < budget) && tx_complete) {
2571 napi_complete(&sds_ring->napi);
2572 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2573 qlcnic_enable_int(sds_ring);
2576 return work_done;
2579 static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2581 struct qlcnic_host_sds_ring *sds_ring =
2582 container_of(napi, struct qlcnic_host_sds_ring, napi);
2584 struct qlcnic_adapter *adapter = sds_ring->adapter;
2585 int work_done;
2587 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2589 if (work_done < budget) {
2590 napi_complete(&sds_ring->napi);
2591 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2592 qlcnic_enable_int(sds_ring);
2595 return work_done;
2598 #ifdef CONFIG_NET_POLL_CONTROLLER
2599 static void qlcnic_poll_controller(struct net_device *netdev)
2601 int ring;
2602 struct qlcnic_host_sds_ring *sds_ring;
2603 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2604 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2606 disable_irq(adapter->irq);
2607 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2608 sds_ring = &recv_ctx->sds_rings[ring];
2609 qlcnic_intr(adapter->irq, sds_ring);
2611 enable_irq(adapter->irq);
2613 #endif
2615 static void
2616 qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2618 u32 val;
2620 val = adapter->portnum & 0xf;
2621 val |= encoding << 7;
2622 val |= (jiffies - adapter->dev_rst_time) << 8;
2624 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2625 adapter->dev_rst_time = jiffies;
2628 static int
2629 qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
2631 u32 val;
2633 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2634 state != QLCNIC_DEV_NEED_QUISCENT);
2636 if (qlcnic_api_lock(adapter))
2637 return -EIO;
2639 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2641 if (state == QLCNIC_DEV_NEED_RESET)
2642 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2643 else if (state == QLCNIC_DEV_NEED_QUISCENT)
2644 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
2646 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2648 qlcnic_api_unlock(adapter);
2650 return 0;
2653 static int
2654 qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2656 u32 val;
2658 if (qlcnic_api_lock(adapter))
2659 return -EBUSY;
2661 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2662 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
2663 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2665 qlcnic_api_unlock(adapter);
2667 return 0;
2670 static void
2671 qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
2673 u32 val;
2675 if (qlcnic_api_lock(adapter))
2676 goto err;
2678 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2679 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
2680 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
2682 if (failed) {
2683 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2684 dev_info(&adapter->pdev->dev,
2685 "Device state set to Failed. Please Reboot\n");
2686 } else if (!(val & 0x11111111))
2687 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2689 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2690 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
2691 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2693 qlcnic_api_unlock(adapter);
2694 err:
2695 adapter->fw_fail_cnt = 0;
2696 adapter->flags &= ~QLCNIC_FW_HANG;
2697 clear_bit(__QLCNIC_START_FW, &adapter->state);
2698 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2701 /* Grab api lock, before checking state */
2702 static int
2703 qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2705 int act, state, active_mask;
2707 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2708 act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2710 if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
2711 active_mask = (~(1 << (adapter->ahw->pci_func * 4)));
2712 act = act & active_mask;
2715 if (((state & 0x11111111) == (act & 0x11111111)) ||
2716 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2717 return 0;
2718 else
2719 return 1;
2722 static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2724 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2726 if (val != QLCNIC_DRV_IDC_VER) {
2727 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2728 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2731 return 0;
2734 static int
2735 qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2737 u32 val, prev_state;
2738 u8 dev_init_timeo = adapter->dev_init_timeo;
2739 u8 portnum = adapter->portnum;
2740 u8 ret;
2742 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2743 return 1;
2745 if (qlcnic_api_lock(adapter))
2746 return -1;
2748 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2749 if (!(val & (1 << (portnum * 4)))) {
2750 QLC_DEV_SET_REF_CNT(val, portnum);
2751 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
2754 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2755 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
2757 switch (prev_state) {
2758 case QLCNIC_DEV_COLD:
2759 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
2760 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
2761 qlcnic_idc_debug_info(adapter, 0);
2762 qlcnic_api_unlock(adapter);
2763 return 1;
2765 case QLCNIC_DEV_READY:
2766 ret = qlcnic_check_idc_ver(adapter);
2767 qlcnic_api_unlock(adapter);
2768 return ret;
2770 case QLCNIC_DEV_NEED_RESET:
2771 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2772 QLC_DEV_SET_RST_RDY(val, portnum);
2773 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2774 break;
2776 case QLCNIC_DEV_NEED_QUISCENT:
2777 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2778 QLC_DEV_SET_QSCNT_RDY(val, portnum);
2779 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2780 break;
2782 case QLCNIC_DEV_FAILED:
2783 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
2784 qlcnic_api_unlock(adapter);
2785 return -1;
2787 case QLCNIC_DEV_INITIALIZING:
2788 case QLCNIC_DEV_QUISCENT:
2789 break;
2792 qlcnic_api_unlock(adapter);
2794 do {
2795 msleep(1000);
2796 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2798 if (prev_state == QLCNIC_DEV_QUISCENT)
2799 continue;
2800 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
2802 if (!dev_init_timeo) {
2803 dev_err(&adapter->pdev->dev,
2804 "Waiting for device to initialize timeout\n");
2805 return -1;
2808 if (qlcnic_api_lock(adapter))
2809 return -1;
2811 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2812 QLC_DEV_CLR_RST_QSCNT(val, portnum);
2813 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2815 ret = qlcnic_check_idc_ver(adapter);
2816 qlcnic_api_unlock(adapter);
2818 return ret;
2821 static void
2822 qlcnic_fwinit_work(struct work_struct *work)
2824 struct qlcnic_adapter *adapter = container_of(work,
2825 struct qlcnic_adapter, fw_work.work);
2826 u32 dev_state = 0xf;
2827 u32 val;
2829 if (qlcnic_api_lock(adapter))
2830 goto err_ret;
2832 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2833 if (dev_state == QLCNIC_DEV_QUISCENT ||
2834 dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2835 qlcnic_api_unlock(adapter);
2836 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2837 FW_POLL_DELAY * 2);
2838 return;
2841 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
2842 qlcnic_api_unlock(adapter);
2843 goto wait_npar;
2846 if (dev_state == QLCNIC_DEV_INITIALIZING ||
2847 dev_state == QLCNIC_DEV_READY) {
2848 dev_info(&adapter->pdev->dev, "Detected state change from "
2849 "DEV_NEED_RESET, skipping ack check\n");
2850 goto skip_ack_check;
2853 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2854 dev_info(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2855 adapter->reset_ack_timeo);
2856 goto skip_ack_check;
2859 if (!qlcnic_check_drv_state(adapter)) {
2860 skip_ack_check:
2861 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2863 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2864 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2865 QLCNIC_DEV_INITIALIZING);
2866 set_bit(__QLCNIC_START_FW, &adapter->state);
2867 QLCDB(adapter, DRV, "Restarting fw\n");
2868 qlcnic_idc_debug_info(adapter, 0);
2869 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2870 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2871 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2874 qlcnic_api_unlock(adapter);
2876 rtnl_lock();
2877 if (adapter->ahw->fw_dump.enable &&
2878 (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
2879 QLCDB(adapter, DRV, "Take FW dump\n");
2880 qlcnic_dump_fw(adapter);
2881 adapter->flags |= QLCNIC_FW_HANG;
2883 rtnl_unlock();
2885 adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
2886 if (!adapter->nic_ops->start_firmware(adapter)) {
2887 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2888 adapter->fw_wait_cnt = 0;
2889 return;
2891 goto err_ret;
2894 qlcnic_api_unlock(adapter);
2896 wait_npar:
2897 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2898 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
2900 switch (dev_state) {
2901 case QLCNIC_DEV_READY:
2902 if (!adapter->nic_ops->start_firmware(adapter)) {
2903 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2904 adapter->fw_wait_cnt = 0;
2905 return;
2907 case QLCNIC_DEV_FAILED:
2908 break;
2909 default:
2910 qlcnic_schedule_work(adapter,
2911 qlcnic_fwinit_work, FW_POLL_DELAY);
2912 return;
2915 err_ret:
2916 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2917 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
2918 netif_device_attach(adapter->netdev);
2919 qlcnic_clr_all_drv_state(adapter, 0);
2922 static void
2923 qlcnic_detach_work(struct work_struct *work)
2925 struct qlcnic_adapter *adapter = container_of(work,
2926 struct qlcnic_adapter, fw_work.work);
2927 struct net_device *netdev = adapter->netdev;
2928 u32 status;
2930 netif_device_detach(netdev);
2932 /* Dont grab rtnl lock during Quiscent mode */
2933 if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2934 if (netif_running(netdev))
2935 __qlcnic_down(adapter, netdev);
2936 } else
2937 qlcnic_down(adapter, netdev);
2939 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2941 if (status & QLCNIC_RCODE_FATAL_ERROR) {
2942 dev_err(&adapter->pdev->dev,
2943 "Detaching the device: peg halt status1=0x%x\n",
2944 status);
2946 if (QLCNIC_FWERROR_CODE(status) == QLCNIC_FWERROR_FAN_FAILURE) {
2947 dev_err(&adapter->pdev->dev,
2948 "On board active cooling fan failed. "
2949 "Device has been halted.\n");
2950 dev_err(&adapter->pdev->dev,
2951 "Replace the adapter.\n");
2954 goto err_ret;
2957 if (adapter->temp == QLCNIC_TEMP_PANIC) {
2958 dev_err(&adapter->pdev->dev, "Detaching the device: temp=%d\n",
2959 adapter->temp);
2960 goto err_ret;
2963 /* Dont ack if this instance is the reset owner */
2964 if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
2965 if (qlcnic_set_drv_state(adapter, adapter->dev_state)) {
2966 dev_err(&adapter->pdev->dev,
2967 "Failed to set driver state,"
2968 "detaching the device.\n");
2969 goto err_ret;
2973 adapter->fw_wait_cnt = 0;
2975 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2977 return;
2979 err_ret:
2980 netif_device_attach(netdev);
2981 qlcnic_clr_all_drv_state(adapter, 1);
2984 /*Transit NPAR state to NON Operational */
2985 static void
2986 qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
2988 u32 state;
2990 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2991 if (state == QLCNIC_DEV_NPAR_NON_OPER)
2992 return;
2994 if (qlcnic_api_lock(adapter))
2995 return;
2996 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
2997 qlcnic_api_unlock(adapter);
3000 /*Transit to RESET state from READY state only */
3001 void
3002 qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
3004 u32 state, xg_val = 0, gb_val = 0;
3006 qlcnic_xg_set_xg0_mask(xg_val);
3007 qlcnic_xg_set_xg1_mask(xg_val);
3008 QLCWR32(adapter, QLCNIC_NIU_XG_PAUSE_CTL, xg_val);
3009 qlcnic_gb_set_gb0_mask(gb_val);
3010 qlcnic_gb_set_gb1_mask(gb_val);
3011 qlcnic_gb_set_gb2_mask(gb_val);
3012 qlcnic_gb_set_gb3_mask(gb_val);
3013 QLCWR32(adapter, QLCNIC_NIU_GB_PAUSE_CTL, gb_val);
3014 dev_info(&adapter->pdev->dev, "Pause control frames disabled"
3015 " on all ports\n");
3016 adapter->need_fw_reset = 1;
3017 if (qlcnic_api_lock(adapter))
3018 return;
3020 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
3022 if (state == QLCNIC_DEV_READY) {
3023 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
3024 adapter->flags |= QLCNIC_FW_RESET_OWNER;
3025 QLCDB(adapter, DRV, "NEED_RESET state set\n");
3026 qlcnic_idc_debug_info(adapter, 0);
3029 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
3030 qlcnic_api_unlock(adapter);
3033 /* Transit to NPAR READY state from NPAR NOT READY state */
3034 static void
3035 qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
3037 if (qlcnic_api_lock(adapter))
3038 return;
3040 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
3041 QLCDB(adapter, DRV, "NPAR operational state set\n");
3043 qlcnic_api_unlock(adapter);
3046 static void
3047 qlcnic_schedule_work(struct qlcnic_adapter *adapter,
3048 work_func_t func, int delay)
3050 if (test_bit(__QLCNIC_AER, &adapter->state))
3051 return;
3053 INIT_DELAYED_WORK(&adapter->fw_work, func);
3054 queue_delayed_work(qlcnic_wq, &adapter->fw_work,
3055 round_jiffies_relative(delay));
3058 static void
3059 qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
3061 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3062 msleep(10);
3064 cancel_delayed_work_sync(&adapter->fw_work);
3067 static void
3068 qlcnic_attach_work(struct work_struct *work)
3070 struct qlcnic_adapter *adapter = container_of(work,
3071 struct qlcnic_adapter, fw_work.work);
3072 struct net_device *netdev = adapter->netdev;
3073 u32 npar_state;
3075 if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
3076 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3077 if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3078 qlcnic_clr_all_drv_state(adapter, 0);
3079 else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3080 qlcnic_schedule_work(adapter, qlcnic_attach_work,
3081 FW_POLL_DELAY);
3082 else
3083 goto attach;
3084 QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3085 return;
3087 attach:
3088 if (netif_running(netdev)) {
3089 if (qlcnic_up(adapter, netdev))
3090 goto done;
3092 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3095 done:
3096 netif_device_attach(netdev);
3097 adapter->fw_fail_cnt = 0;
3098 adapter->flags &= ~QLCNIC_FW_HANG;
3099 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3101 if (!qlcnic_clr_drv_state(adapter))
3102 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3103 FW_POLL_DELAY);
3106 static int
3107 qlcnic_check_health(struct qlcnic_adapter *adapter)
3109 u32 state = 0, heartbeat;
3110 u32 peg_status;
3112 if (qlcnic_check_temp(adapter))
3113 goto detach;
3115 if (adapter->need_fw_reset)
3116 qlcnic_dev_request_reset(adapter);
3118 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
3119 if (state == QLCNIC_DEV_NEED_RESET) {
3120 qlcnic_set_npar_non_operational(adapter);
3121 adapter->need_fw_reset = 1;
3122 } else if (state == QLCNIC_DEV_NEED_QUISCENT)
3123 goto detach;
3125 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3126 if (heartbeat != adapter->heartbeat) {
3127 adapter->heartbeat = heartbeat;
3128 adapter->fw_fail_cnt = 0;
3129 if (adapter->need_fw_reset)
3130 goto detach;
3132 if (adapter->reset_context && auto_fw_reset) {
3133 qlcnic_reset_hw_context(adapter);
3134 adapter->netdev->trans_start = jiffies;
3137 return 0;
3140 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3141 return 0;
3143 adapter->flags |= QLCNIC_FW_HANG;
3145 qlcnic_dev_request_reset(adapter);
3147 if (auto_fw_reset)
3148 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
3150 dev_err(&adapter->pdev->dev, "firmware hang detected\n");
3151 dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
3152 "PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
3153 "PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
3154 "PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
3155 "PEG_NET_4_PC: 0x%x\n",
3156 QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1),
3157 QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS2),
3158 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c),
3159 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c),
3160 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c),
3161 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c),
3162 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c));
3163 peg_status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
3164 if (QLCNIC_FWERROR_CODE(peg_status) == 0x67)
3165 dev_err(&adapter->pdev->dev,
3166 "Firmware aborted with error code 0x00006700. "
3167 "Device is being reset.\n");
3168 detach:
3169 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3170 QLCNIC_DEV_NEED_RESET;
3172 if (auto_fw_reset &&
3173 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3175 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
3176 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3179 return 1;
3182 static void
3183 qlcnic_fw_poll_work(struct work_struct *work)
3185 struct qlcnic_adapter *adapter = container_of(work,
3186 struct qlcnic_adapter, fw_work.work);
3188 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3189 goto reschedule;
3192 if (qlcnic_check_health(adapter))
3193 return;
3195 if (adapter->fhash.fnum)
3196 qlcnic_prune_lb_filters(adapter);
3198 reschedule:
3199 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3202 static int qlcnic_is_first_func(struct pci_dev *pdev)
3204 struct pci_dev *oth_pdev;
3205 int val = pdev->devfn;
3207 while (val-- > 0) {
3208 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3209 (pdev->bus), pdev->bus->number,
3210 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
3211 if (!oth_pdev)
3212 continue;
3214 if (oth_pdev->current_state != PCI_D3cold) {
3215 pci_dev_put(oth_pdev);
3216 return 0;
3218 pci_dev_put(oth_pdev);
3220 return 1;
3223 static int qlcnic_attach_func(struct pci_dev *pdev)
3225 int err, first_func;
3226 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3227 struct net_device *netdev = adapter->netdev;
3229 pdev->error_state = pci_channel_io_normal;
3231 err = pci_enable_device(pdev);
3232 if (err)
3233 return err;
3235 pci_set_power_state(pdev, PCI_D0);
3236 pci_set_master(pdev);
3237 pci_restore_state(pdev);
3239 first_func = qlcnic_is_first_func(pdev);
3241 if (qlcnic_api_lock(adapter))
3242 return -EINVAL;
3244 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
3245 adapter->need_fw_reset = 1;
3246 set_bit(__QLCNIC_START_FW, &adapter->state);
3247 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3248 QLCDB(adapter, DRV, "Restarting fw\n");
3250 qlcnic_api_unlock(adapter);
3252 err = adapter->nic_ops->start_firmware(adapter);
3253 if (err)
3254 return err;
3256 qlcnic_clr_drv_state(adapter);
3257 qlcnic_setup_intr(adapter);
3259 if (netif_running(netdev)) {
3260 err = qlcnic_attach(adapter);
3261 if (err) {
3262 qlcnic_clr_all_drv_state(adapter, 1);
3263 clear_bit(__QLCNIC_AER, &adapter->state);
3264 netif_device_attach(netdev);
3265 return err;
3268 err = qlcnic_up(adapter, netdev);
3269 if (err)
3270 goto done;
3272 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3274 done:
3275 netif_device_attach(netdev);
3276 return err;
3279 static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3280 pci_channel_state_t state)
3282 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3283 struct net_device *netdev = adapter->netdev;
3285 if (state == pci_channel_io_perm_failure)
3286 return PCI_ERS_RESULT_DISCONNECT;
3288 if (state == pci_channel_io_normal)
3289 return PCI_ERS_RESULT_RECOVERED;
3291 set_bit(__QLCNIC_AER, &adapter->state);
3292 netif_device_detach(netdev);
3294 cancel_delayed_work_sync(&adapter->fw_work);
3296 if (netif_running(netdev))
3297 qlcnic_down(adapter, netdev);
3299 qlcnic_detach(adapter);
3300 qlcnic_teardown_intr(adapter);
3302 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3304 pci_save_state(pdev);
3305 pci_disable_device(pdev);
3307 return PCI_ERS_RESULT_NEED_RESET;
3310 static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3312 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3313 PCI_ERS_RESULT_RECOVERED;
3316 static void qlcnic_io_resume(struct pci_dev *pdev)
3318 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3320 pci_cleanup_aer_uncorrect_error_status(pdev);
3322 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3323 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3324 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3325 FW_POLL_DELAY);
3328 static int
3329 qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3331 int err;
3333 err = qlcnic_can_start_firmware(adapter);
3334 if (err)
3335 return err;
3337 err = qlcnic_check_npar_opertional(adapter);
3338 if (err)
3339 return err;
3341 err = qlcnic_initialize_nic(adapter);
3342 if (err)
3343 return err;
3345 qlcnic_check_options(adapter);
3347 err = qlcnic_set_eswitch_port_config(adapter);
3348 if (err)
3349 return err;
3351 adapter->need_fw_reset = 0;
3353 return err;
3356 static int
3357 qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3359 return -EOPNOTSUPP;
3362 static int
3363 qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3365 return -EOPNOTSUPP;
3368 static ssize_t
3369 qlcnic_store_bridged_mode(struct device *dev,
3370 struct device_attribute *attr, const char *buf, size_t len)
3372 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3373 unsigned long new;
3374 int ret = -EINVAL;
3376 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3377 goto err_out;
3379 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
3380 goto err_out;
3382 if (strict_strtoul(buf, 2, &new))
3383 goto err_out;
3385 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
3386 ret = len;
3388 err_out:
3389 return ret;
3392 static ssize_t
3393 qlcnic_show_bridged_mode(struct device *dev,
3394 struct device_attribute *attr, char *buf)
3396 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3397 int bridged_mode = 0;
3399 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3400 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3402 return sprintf(buf, "%d\n", bridged_mode);
3405 static struct device_attribute dev_attr_bridged_mode = {
3406 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3407 .show = qlcnic_show_bridged_mode,
3408 .store = qlcnic_store_bridged_mode,
3411 static ssize_t
3412 qlcnic_store_diag_mode(struct device *dev,
3413 struct device_attribute *attr, const char *buf, size_t len)
3415 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3416 unsigned long new;
3418 if (strict_strtoul(buf, 2, &new))
3419 return -EINVAL;
3421 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3422 adapter->flags ^= QLCNIC_DIAG_ENABLED;
3424 return len;
3427 static ssize_t
3428 qlcnic_show_diag_mode(struct device *dev,
3429 struct device_attribute *attr, char *buf)
3431 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3433 return sprintf(buf, "%d\n",
3434 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
3437 static struct device_attribute dev_attr_diag_mode = {
3438 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3439 .show = qlcnic_show_diag_mode,
3440 .store = qlcnic_store_diag_mode,
3443 int qlcnic_validate_max_rss(struct net_device *netdev, u8 max_hw, u8 val)
3445 if (!use_msi_x && !use_msi) {
3446 netdev_info(netdev, "no msix or msi support, hence no rss\n");
3447 return -EINVAL;
3450 if ((val > max_hw) || (val < 2) || !is_power_of_2(val)) {
3451 netdev_info(netdev, "rss_ring valid range [2 - %x] in "
3452 " powers of 2\n", max_hw);
3453 return -EINVAL;
3455 return 0;
3459 int qlcnic_set_max_rss(struct qlcnic_adapter *adapter, u8 data)
3461 struct net_device *netdev = adapter->netdev;
3462 int err = 0;
3464 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3465 return -EBUSY;
3467 netif_device_detach(netdev);
3468 if (netif_running(netdev))
3469 __qlcnic_down(adapter, netdev);
3470 qlcnic_detach(adapter);
3471 qlcnic_teardown_intr(adapter);
3473 if (qlcnic_enable_msix(adapter, data)) {
3474 netdev_info(netdev, "failed setting max_rss; rss disabled\n");
3475 qlcnic_enable_msi_legacy(adapter);
3478 if (netif_running(netdev)) {
3479 err = qlcnic_attach(adapter);
3480 if (err)
3481 goto done;
3482 err = __qlcnic_up(adapter, netdev);
3483 if (err)
3484 goto done;
3485 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3487 done:
3488 netif_device_attach(netdev);
3489 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3490 return err;
3493 static int
3494 qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon, u8 *state,
3495 u8 *rate)
3497 *rate = LSB(beacon);
3498 *state = MSB(beacon);
3500 QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
3502 if (!*state) {
3503 *rate = __QLCNIC_MAX_LED_RATE;
3504 return 0;
3505 } else if (*state > __QLCNIC_MAX_LED_STATE)
3506 return -EINVAL;
3508 if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
3509 return -EINVAL;
3511 return 0;
3514 static ssize_t
3515 qlcnic_store_beacon(struct device *dev,
3516 struct device_attribute *attr, const char *buf, size_t len)
3518 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3519 int max_sds_rings = adapter->max_sds_rings;
3520 u16 beacon;
3521 u8 b_state, b_rate;
3522 int err;
3524 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
3525 dev_warn(dev, "LED test not supported for non "
3526 "privilege function\n");
3527 return -EOPNOTSUPP;
3530 if (len != sizeof(u16))
3531 return QL_STATUS_INVALID_PARAM;
3533 memcpy(&beacon, buf, sizeof(u16));
3534 err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
3535 if (err)
3536 return err;
3538 if (adapter->ahw->beacon_state == b_state)
3539 return len;
3541 rtnl_lock();
3543 if (!adapter->ahw->beacon_state)
3544 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
3545 rtnl_unlock();
3546 return -EBUSY;
3549 if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
3550 err = -EIO;
3551 goto out;
3554 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
3555 err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
3556 if (err)
3557 goto out;
3558 set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
3561 err = qlcnic_config_led(adapter, b_state, b_rate);
3563 if (!err) {
3564 err = len;
3565 adapter->ahw->beacon_state = b_state;
3568 if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
3569 qlcnic_diag_free_res(adapter->netdev, max_sds_rings);
3571 out:
3572 if (!adapter->ahw->beacon_state)
3573 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
3574 rtnl_unlock();
3576 return err;
3579 static ssize_t
3580 qlcnic_show_beacon(struct device *dev,
3581 struct device_attribute *attr, char *buf)
3583 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3585 return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
3588 static struct device_attribute dev_attr_beacon = {
3589 .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
3590 .show = qlcnic_show_beacon,
3591 .store = qlcnic_store_beacon,
3594 static int
3595 qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3596 loff_t offset, size_t size)
3598 size_t crb_size = 4;
3600 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3601 return -EIO;
3603 if (offset < QLCNIC_PCI_CRBSPACE) {
3604 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3605 QLCNIC_PCI_CAMQM_END))
3606 crb_size = 8;
3607 else
3608 return -EINVAL;
3611 if ((size != crb_size) || (offset & (crb_size-1)))
3612 return -EINVAL;
3614 return 0;
3617 static ssize_t
3618 qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3619 struct bin_attribute *attr,
3620 char *buf, loff_t offset, size_t size)
3622 struct device *dev = container_of(kobj, struct device, kobj);
3623 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3624 u32 data;
3625 u64 qmdata;
3626 int ret;
3628 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3629 if (ret != 0)
3630 return ret;
3632 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3633 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3634 memcpy(buf, &qmdata, size);
3635 } else {
3636 data = QLCRD32(adapter, offset);
3637 memcpy(buf, &data, size);
3639 return size;
3642 static ssize_t
3643 qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3644 struct bin_attribute *attr,
3645 char *buf, loff_t offset, size_t size)
3647 struct device *dev = container_of(kobj, struct device, kobj);
3648 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3649 u32 data;
3650 u64 qmdata;
3651 int ret;
3653 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3654 if (ret != 0)
3655 return ret;
3657 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3658 memcpy(&qmdata, buf, size);
3659 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3660 } else {
3661 memcpy(&data, buf, size);
3662 QLCWR32(adapter, offset, data);
3664 return size;
3667 static int
3668 qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3669 loff_t offset, size_t size)
3671 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3672 return -EIO;
3674 if ((size != 8) || (offset & 0x7))
3675 return -EIO;
3677 return 0;
3680 static ssize_t
3681 qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3682 struct bin_attribute *attr,
3683 char *buf, loff_t offset, size_t size)
3685 struct device *dev = container_of(kobj, struct device, kobj);
3686 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3687 u64 data;
3688 int ret;
3690 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3691 if (ret != 0)
3692 return ret;
3694 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3695 return -EIO;
3697 memcpy(buf, &data, size);
3699 return size;
3702 static ssize_t
3703 qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3704 struct bin_attribute *attr,
3705 char *buf, loff_t offset, size_t size)
3707 struct device *dev = container_of(kobj, struct device, kobj);
3708 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3709 u64 data;
3710 int ret;
3712 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3713 if (ret != 0)
3714 return ret;
3716 memcpy(&data, buf, size);
3718 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3719 return -EIO;
3721 return size;
3724 static struct bin_attribute bin_attr_crb = {
3725 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3726 .size = 0,
3727 .read = qlcnic_sysfs_read_crb,
3728 .write = qlcnic_sysfs_write_crb,
3731 static struct bin_attribute bin_attr_mem = {
3732 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3733 .size = 0,
3734 .read = qlcnic_sysfs_read_mem,
3735 .write = qlcnic_sysfs_write_mem,
3738 static int
3739 validate_pm_config(struct qlcnic_adapter *adapter,
3740 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3743 u8 src_pci_func, s_esw_id, d_esw_id;
3744 u8 dest_pci_func;
3745 int i;
3747 for (i = 0; i < count; i++) {
3748 src_pci_func = pm_cfg[i].pci_func;
3749 dest_pci_func = pm_cfg[i].dest_npar;
3750 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3751 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3752 return QL_STATUS_INVALID_PARAM;
3754 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3755 return QL_STATUS_INVALID_PARAM;
3757 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3758 return QL_STATUS_INVALID_PARAM;
3760 s_esw_id = adapter->npars[src_pci_func].phy_port;
3761 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3763 if (s_esw_id != d_esw_id)
3764 return QL_STATUS_INVALID_PARAM;
3767 return 0;
3771 static ssize_t
3772 qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3773 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3775 struct device *dev = container_of(kobj, struct device, kobj);
3776 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3777 struct qlcnic_pm_func_cfg *pm_cfg;
3778 u32 id, action, pci_func;
3779 int count, rem, i, ret;
3781 count = size / sizeof(struct qlcnic_pm_func_cfg);
3782 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3783 if (rem)
3784 return QL_STATUS_INVALID_PARAM;
3786 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3788 ret = validate_pm_config(adapter, pm_cfg, count);
3789 if (ret)
3790 return ret;
3791 for (i = 0; i < count; i++) {
3792 pci_func = pm_cfg[i].pci_func;
3793 action = !!pm_cfg[i].action;
3794 id = adapter->npars[pci_func].phy_port;
3795 ret = qlcnic_config_port_mirroring(adapter, id,
3796 action, pci_func);
3797 if (ret)
3798 return ret;
3801 for (i = 0; i < count; i++) {
3802 pci_func = pm_cfg[i].pci_func;
3803 id = adapter->npars[pci_func].phy_port;
3804 adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
3805 adapter->npars[pci_func].dest_npar = id;
3807 return size;
3810 static ssize_t
3811 qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3812 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3814 struct device *dev = container_of(kobj, struct device, kobj);
3815 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3816 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3817 int i;
3819 if (size != sizeof(pm_cfg))
3820 return QL_STATUS_INVALID_PARAM;
3822 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3823 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3824 continue;
3825 pm_cfg[i].action = adapter->npars[i].enable_pm;
3826 pm_cfg[i].dest_npar = 0;
3827 pm_cfg[i].pci_func = i;
3829 memcpy(buf, &pm_cfg, size);
3831 return size;
3834 static int
3835 validate_esw_config(struct qlcnic_adapter *adapter,
3836 struct qlcnic_esw_func_cfg *esw_cfg, int count)
3838 u32 op_mode;
3839 u8 pci_func;
3840 int i;
3842 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
3844 for (i = 0; i < count; i++) {
3845 pci_func = esw_cfg[i].pci_func;
3846 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3847 return QL_STATUS_INVALID_PARAM;
3849 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3850 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3851 return QL_STATUS_INVALID_PARAM;
3853 switch (esw_cfg[i].op_mode) {
3854 case QLCNIC_PORT_DEFAULTS:
3855 if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
3856 QLCNIC_NON_PRIV_FUNC) {
3857 if (esw_cfg[i].mac_anti_spoof != 0)
3858 return QL_STATUS_INVALID_PARAM;
3859 if (esw_cfg[i].mac_override != 1)
3860 return QL_STATUS_INVALID_PARAM;
3861 if (esw_cfg[i].promisc_mode != 1)
3862 return QL_STATUS_INVALID_PARAM;
3864 break;
3865 case QLCNIC_ADD_VLAN:
3866 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3867 return QL_STATUS_INVALID_PARAM;
3868 if (!esw_cfg[i].op_type)
3869 return QL_STATUS_INVALID_PARAM;
3870 break;
3871 case QLCNIC_DEL_VLAN:
3872 if (!esw_cfg[i].op_type)
3873 return QL_STATUS_INVALID_PARAM;
3874 break;
3875 default:
3876 return QL_STATUS_INVALID_PARAM;
3879 return 0;
3882 static ssize_t
3883 qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3884 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3886 struct device *dev = container_of(kobj, struct device, kobj);
3887 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3888 struct qlcnic_esw_func_cfg *esw_cfg;
3889 struct qlcnic_npar_info *npar;
3890 int count, rem, i, ret;
3891 u8 pci_func, op_mode = 0;
3893 count = size / sizeof(struct qlcnic_esw_func_cfg);
3894 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3895 if (rem)
3896 return QL_STATUS_INVALID_PARAM;
3898 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3899 ret = validate_esw_config(adapter, esw_cfg, count);
3900 if (ret)
3901 return ret;
3903 for (i = 0; i < count; i++) {
3904 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3905 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3906 return QL_STATUS_INVALID_PARAM;
3908 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
3909 continue;
3911 op_mode = esw_cfg[i].op_mode;
3912 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3913 esw_cfg[i].op_mode = op_mode;
3914 esw_cfg[i].pci_func = adapter->ahw->pci_func;
3916 switch (esw_cfg[i].op_mode) {
3917 case QLCNIC_PORT_DEFAULTS:
3918 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3919 break;
3920 case QLCNIC_ADD_VLAN:
3921 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3922 break;
3923 case QLCNIC_DEL_VLAN:
3924 esw_cfg[i].vlan_id = 0;
3925 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3926 break;
3930 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3931 goto out;
3933 for (i = 0; i < count; i++) {
3934 pci_func = esw_cfg[i].pci_func;
3935 npar = &adapter->npars[pci_func];
3936 switch (esw_cfg[i].op_mode) {
3937 case QLCNIC_PORT_DEFAULTS:
3938 npar->promisc_mode = esw_cfg[i].promisc_mode;
3939 npar->mac_override = esw_cfg[i].mac_override;
3940 npar->offload_flags = esw_cfg[i].offload_flags;
3941 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3942 npar->discard_tagged = esw_cfg[i].discard_tagged;
3943 break;
3944 case QLCNIC_ADD_VLAN:
3945 npar->pvid = esw_cfg[i].vlan_id;
3946 break;
3947 case QLCNIC_DEL_VLAN:
3948 npar->pvid = 0;
3949 break;
3952 out:
3953 return size;
3956 static ssize_t
3957 qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3958 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3960 struct device *dev = container_of(kobj, struct device, kobj);
3961 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3962 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
3963 u8 i;
3965 if (size != sizeof(esw_cfg))
3966 return QL_STATUS_INVALID_PARAM;
3968 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3969 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3970 continue;
3971 esw_cfg[i].pci_func = i;
3972 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
3973 return QL_STATUS_INVALID_PARAM;
3975 memcpy(buf, &esw_cfg, size);
3977 return size;
3980 static int
3981 validate_npar_config(struct qlcnic_adapter *adapter,
3982 struct qlcnic_npar_func_cfg *np_cfg, int count)
3984 u8 pci_func, i;
3986 for (i = 0; i < count; i++) {
3987 pci_func = np_cfg[i].pci_func;
3988 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3989 return QL_STATUS_INVALID_PARAM;
3991 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3992 return QL_STATUS_INVALID_PARAM;
3994 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
3995 !IS_VALID_BW(np_cfg[i].max_bw))
3996 return QL_STATUS_INVALID_PARAM;
3998 return 0;
4001 static ssize_t
4002 qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
4003 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4005 struct device *dev = container_of(kobj, struct device, kobj);
4006 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4007 struct qlcnic_info nic_info;
4008 struct qlcnic_npar_func_cfg *np_cfg;
4009 int i, count, rem, ret;
4010 u8 pci_func;
4012 count = size / sizeof(struct qlcnic_npar_func_cfg);
4013 rem = size % sizeof(struct qlcnic_npar_func_cfg);
4014 if (rem)
4015 return QL_STATUS_INVALID_PARAM;
4017 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
4018 ret = validate_npar_config(adapter, np_cfg, count);
4019 if (ret)
4020 return ret;
4022 for (i = 0; i < count ; i++) {
4023 pci_func = np_cfg[i].pci_func;
4024 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
4025 if (ret)
4026 return ret;
4027 nic_info.pci_func = pci_func;
4028 nic_info.min_tx_bw = np_cfg[i].min_bw;
4029 nic_info.max_tx_bw = np_cfg[i].max_bw;
4030 ret = qlcnic_set_nic_info(adapter, &nic_info);
4031 if (ret)
4032 return ret;
4033 adapter->npars[i].min_bw = nic_info.min_tx_bw;
4034 adapter->npars[i].max_bw = nic_info.max_tx_bw;
4037 return size;
4040 static ssize_t
4041 qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
4042 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4044 struct device *dev = container_of(kobj, struct device, kobj);
4045 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4046 struct qlcnic_info nic_info;
4047 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
4048 int i, ret;
4050 if (size != sizeof(np_cfg))
4051 return QL_STATUS_INVALID_PARAM;
4053 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4054 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
4055 continue;
4056 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
4057 if (ret)
4058 return ret;
4060 np_cfg[i].pci_func = i;
4061 np_cfg[i].op_mode = (u8)nic_info.op_mode;
4062 np_cfg[i].port_num = nic_info.phys_port;
4063 np_cfg[i].fw_capab = nic_info.capabilities;
4064 np_cfg[i].min_bw = nic_info.min_tx_bw ;
4065 np_cfg[i].max_bw = nic_info.max_tx_bw;
4066 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
4067 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
4069 memcpy(buf, &np_cfg, size);
4070 return size;
4073 static ssize_t
4074 qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
4075 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4077 struct device *dev = container_of(kobj, struct device, kobj);
4078 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4079 struct qlcnic_esw_statistics port_stats;
4080 int ret;
4082 if (size != sizeof(struct qlcnic_esw_statistics))
4083 return QL_STATUS_INVALID_PARAM;
4085 if (offset >= QLCNIC_MAX_PCI_FUNC)
4086 return QL_STATUS_INVALID_PARAM;
4088 memset(&port_stats, 0, size);
4089 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
4090 &port_stats.rx);
4091 if (ret)
4092 return ret;
4094 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
4095 &port_stats.tx);
4096 if (ret)
4097 return ret;
4099 memcpy(buf, &port_stats, size);
4100 return size;
4103 static ssize_t
4104 qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
4105 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4107 struct device *dev = container_of(kobj, struct device, kobj);
4108 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4109 struct qlcnic_esw_statistics esw_stats;
4110 int ret;
4112 if (size != sizeof(struct qlcnic_esw_statistics))
4113 return QL_STATUS_INVALID_PARAM;
4115 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
4116 return QL_STATUS_INVALID_PARAM;
4118 memset(&esw_stats, 0, size);
4119 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
4120 &esw_stats.rx);
4121 if (ret)
4122 return ret;
4124 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
4125 &esw_stats.tx);
4126 if (ret)
4127 return ret;
4129 memcpy(buf, &esw_stats, size);
4130 return size;
4133 static ssize_t
4134 qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
4135 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4137 struct device *dev = container_of(kobj, struct device, kobj);
4138 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4139 int ret;
4141 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
4142 return QL_STATUS_INVALID_PARAM;
4144 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
4145 QLCNIC_QUERY_RX_COUNTER);
4146 if (ret)
4147 return ret;
4149 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
4150 QLCNIC_QUERY_TX_COUNTER);
4151 if (ret)
4152 return ret;
4154 return size;
4157 static ssize_t
4158 qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
4159 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4162 struct device *dev = container_of(kobj, struct device, kobj);
4163 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4164 int ret;
4166 if (offset >= QLCNIC_MAX_PCI_FUNC)
4167 return QL_STATUS_INVALID_PARAM;
4169 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4170 QLCNIC_QUERY_RX_COUNTER);
4171 if (ret)
4172 return ret;
4174 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4175 QLCNIC_QUERY_TX_COUNTER);
4176 if (ret)
4177 return ret;
4179 return size;
4182 static ssize_t
4183 qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
4184 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4186 struct device *dev = container_of(kobj, struct device, kobj);
4187 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4188 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
4189 struct qlcnic_pci_info *pci_info;
4190 int i, ret;
4192 if (size != sizeof(pci_cfg))
4193 return QL_STATUS_INVALID_PARAM;
4195 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
4196 if (!pci_info)
4197 return -ENOMEM;
4199 ret = qlcnic_get_pci_info(adapter, pci_info);
4200 if (ret) {
4201 kfree(pci_info);
4202 return ret;
4205 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4206 pci_cfg[i].pci_func = pci_info[i].id;
4207 pci_cfg[i].func_type = pci_info[i].type;
4208 pci_cfg[i].port_num = pci_info[i].default_port;
4209 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
4210 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
4211 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
4213 memcpy(buf, &pci_cfg, size);
4214 kfree(pci_info);
4215 return size;
4217 static struct bin_attribute bin_attr_npar_config = {
4218 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
4219 .size = 0,
4220 .read = qlcnic_sysfs_read_npar_config,
4221 .write = qlcnic_sysfs_write_npar_config,
4224 static struct bin_attribute bin_attr_pci_config = {
4225 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
4226 .size = 0,
4227 .read = qlcnic_sysfs_read_pci_config,
4228 .write = NULL,
4231 static struct bin_attribute bin_attr_port_stats = {
4232 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
4233 .size = 0,
4234 .read = qlcnic_sysfs_get_port_stats,
4235 .write = qlcnic_sysfs_clear_port_stats,
4238 static struct bin_attribute bin_attr_esw_stats = {
4239 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
4240 .size = 0,
4241 .read = qlcnic_sysfs_get_esw_stats,
4242 .write = qlcnic_sysfs_clear_esw_stats,
4245 static struct bin_attribute bin_attr_esw_config = {
4246 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
4247 .size = 0,
4248 .read = qlcnic_sysfs_read_esw_config,
4249 .write = qlcnic_sysfs_write_esw_config,
4252 static struct bin_attribute bin_attr_pm_config = {
4253 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
4254 .size = 0,
4255 .read = qlcnic_sysfs_read_pm_config,
4256 .write = qlcnic_sysfs_write_pm_config,
4259 static void
4260 qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
4262 struct device *dev = &adapter->pdev->dev;
4264 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4265 if (device_create_file(dev, &dev_attr_bridged_mode))
4266 dev_warn(dev,
4267 "failed to create bridged_mode sysfs entry\n");
4270 static void
4271 qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
4273 struct device *dev = &adapter->pdev->dev;
4275 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4276 device_remove_file(dev, &dev_attr_bridged_mode);
4279 static void
4280 qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
4282 struct device *dev = &adapter->pdev->dev;
4284 if (device_create_bin_file(dev, &bin_attr_port_stats))
4285 dev_info(dev, "failed to create port stats sysfs entry");
4287 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4288 return;
4289 if (device_create_file(dev, &dev_attr_diag_mode))
4290 dev_info(dev, "failed to create diag_mode sysfs entry\n");
4291 if (device_create_file(dev, &dev_attr_beacon))
4292 dev_info(dev, "failed to create beacon sysfs entry");
4293 if (device_create_bin_file(dev, &bin_attr_crb))
4294 dev_info(dev, "failed to create crb sysfs entry\n");
4295 if (device_create_bin_file(dev, &bin_attr_mem))
4296 dev_info(dev, "failed to create mem sysfs entry\n");
4297 if (device_create_bin_file(dev, &bin_attr_pci_config))
4298 dev_info(dev, "failed to create pci config sysfs entry");
4299 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4300 return;
4301 if (device_create_bin_file(dev, &bin_attr_esw_config))
4302 dev_info(dev, "failed to create esw config sysfs entry");
4303 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
4304 return;
4305 if (device_create_bin_file(dev, &bin_attr_npar_config))
4306 dev_info(dev, "failed to create npar config sysfs entry");
4307 if (device_create_bin_file(dev, &bin_attr_pm_config))
4308 dev_info(dev, "failed to create pm config sysfs entry");
4309 if (device_create_bin_file(dev, &bin_attr_esw_stats))
4310 dev_info(dev, "failed to create eswitch stats sysfs entry");
4313 static void
4314 qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4316 struct device *dev = &adapter->pdev->dev;
4318 device_remove_bin_file(dev, &bin_attr_port_stats);
4320 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4321 return;
4322 device_remove_file(dev, &dev_attr_diag_mode);
4323 device_remove_file(dev, &dev_attr_beacon);
4324 device_remove_bin_file(dev, &bin_attr_crb);
4325 device_remove_bin_file(dev, &bin_attr_mem);
4326 device_remove_bin_file(dev, &bin_attr_pci_config);
4327 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4328 return;
4329 device_remove_bin_file(dev, &bin_attr_esw_config);
4330 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
4331 return;
4332 device_remove_bin_file(dev, &bin_attr_npar_config);
4333 device_remove_bin_file(dev, &bin_attr_pm_config);
4334 device_remove_bin_file(dev, &bin_attr_esw_stats);
4337 #ifdef CONFIG_INET
4339 #define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4341 static void
4342 qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4343 struct net_device *dev, unsigned long event)
4345 struct in_device *indev;
4347 indev = in_dev_get(dev);
4348 if (!indev)
4349 return;
4351 for_ifa(indev) {
4352 switch (event) {
4353 case NETDEV_UP:
4354 qlcnic_config_ipaddr(adapter,
4355 ifa->ifa_address, QLCNIC_IP_UP);
4356 break;
4357 case NETDEV_DOWN:
4358 qlcnic_config_ipaddr(adapter,
4359 ifa->ifa_address, QLCNIC_IP_DOWN);
4360 break;
4361 default:
4362 break;
4364 } endfor_ifa(indev);
4366 in_dev_put(indev);
4369 static void
4370 qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4372 struct qlcnic_adapter *adapter = netdev_priv(netdev);
4373 struct net_device *dev;
4374 u16 vid;
4376 qlcnic_config_indev_addr(adapter, netdev, event);
4378 for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4379 dev = __vlan_find_dev_deep(netdev, vid);
4380 if (!dev)
4381 continue;
4382 qlcnic_config_indev_addr(adapter, dev, event);
4386 static int qlcnic_netdev_event(struct notifier_block *this,
4387 unsigned long event, void *ptr)
4389 struct qlcnic_adapter *adapter;
4390 struct net_device *dev = (struct net_device *)ptr;
4392 recheck:
4393 if (dev == NULL)
4394 goto done;
4396 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4397 dev = vlan_dev_real_dev(dev);
4398 goto recheck;
4401 if (!is_qlcnic_netdev(dev))
4402 goto done;
4404 adapter = netdev_priv(dev);
4406 if (!adapter)
4407 goto done;
4409 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4410 goto done;
4412 qlcnic_config_indev_addr(adapter, dev, event);
4413 done:
4414 return NOTIFY_DONE;
4417 static int
4418 qlcnic_inetaddr_event(struct notifier_block *this,
4419 unsigned long event, void *ptr)
4421 struct qlcnic_adapter *adapter;
4422 struct net_device *dev;
4424 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4426 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4428 recheck:
4429 if (dev == NULL)
4430 goto done;
4432 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4433 dev = vlan_dev_real_dev(dev);
4434 goto recheck;
4437 if (!is_qlcnic_netdev(dev))
4438 goto done;
4440 adapter = netdev_priv(dev);
4442 if (!adapter)
4443 goto done;
4445 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4446 goto done;
4448 switch (event) {
4449 case NETDEV_UP:
4450 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4451 break;
4452 case NETDEV_DOWN:
4453 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4454 break;
4455 default:
4456 break;
4459 done:
4460 return NOTIFY_DONE;
4463 static struct notifier_block qlcnic_netdev_cb = {
4464 .notifier_call = qlcnic_netdev_event,
4467 static struct notifier_block qlcnic_inetaddr_cb = {
4468 .notifier_call = qlcnic_inetaddr_event,
4470 #else
4471 static void
4472 qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
4474 #endif
4475 static struct pci_error_handlers qlcnic_err_handler = {
4476 .error_detected = qlcnic_io_error_detected,
4477 .slot_reset = qlcnic_io_slot_reset,
4478 .resume = qlcnic_io_resume,
4481 static struct pci_driver qlcnic_driver = {
4482 .name = qlcnic_driver_name,
4483 .id_table = qlcnic_pci_tbl,
4484 .probe = qlcnic_probe,
4485 .remove = __devexit_p(qlcnic_remove),
4486 #ifdef CONFIG_PM
4487 .suspend = qlcnic_suspend,
4488 .resume = qlcnic_resume,
4489 #endif
4490 .shutdown = qlcnic_shutdown,
4491 .err_handler = &qlcnic_err_handler
4495 static int __init qlcnic_init_module(void)
4497 int ret;
4499 printk(KERN_INFO "%s\n", qlcnic_driver_string);
4501 qlcnic_wq = create_singlethread_workqueue("qlcnic");
4502 if (qlcnic_wq == NULL) {
4503 printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4504 return -ENOMEM;
4507 #ifdef CONFIG_INET
4508 register_netdevice_notifier(&qlcnic_netdev_cb);
4509 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4510 #endif
4512 ret = pci_register_driver(&qlcnic_driver);
4513 if (ret) {
4514 #ifdef CONFIG_INET
4515 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4516 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4517 #endif
4518 destroy_workqueue(qlcnic_wq);
4521 return ret;
4524 module_init(qlcnic_init_module);
4526 static void __exit qlcnic_exit_module(void)
4529 pci_unregister_driver(&qlcnic_driver);
4531 #ifdef CONFIG_INET
4532 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4533 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4534 #endif
4535 destroy_workqueue(qlcnic_wq);
4538 module_exit(qlcnic_exit_module);