include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / net / qlcnic / qlcnic_main.c
blob5ca1b562443c402b7f0e82e405d61e9f319a86a1
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 void qlcnic_vlan_rx_add(struct net_device *, u16);
101 static void 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 -EINVAL;
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_multicast_list = 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;
647 struct pci_dev *pdev = adapter->pdev;
649 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
650 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
651 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
653 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
655 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
656 fw_major, fw_minor, fw_build);
657 if (adapter->ahw->port_type == QLCNIC_XGBE) {
658 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
659 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
660 adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
661 } else {
662 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
663 adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
666 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
667 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
669 } else if (adapter->ahw->port_type == QLCNIC_GBE) {
670 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
671 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
672 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
673 adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
676 adapter->msix_supported = !!use_msi_x;
678 adapter->num_txd = MAX_CMD_DESCRIPTORS;
680 adapter->max_rds_rings = MAX_RDS_RINGS;
683 static int
684 qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
686 int err;
687 struct qlcnic_info nic_info;
689 err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
690 if (err)
691 return err;
693 adapter->physical_port = (u8)nic_info.phys_port;
694 adapter->switch_mode = nic_info.switch_mode;
695 adapter->max_tx_ques = nic_info.max_tx_ques;
696 adapter->max_rx_ques = nic_info.max_rx_ques;
697 adapter->capabilities = nic_info.capabilities;
698 adapter->max_mac_filters = nic_info.max_mac_filters;
699 adapter->max_mtu = nic_info.max_mtu;
701 if (adapter->capabilities & BIT_6)
702 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
703 else
704 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
706 return err;
709 static void
710 qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
711 struct qlcnic_esw_func_cfg *esw_cfg)
713 if (esw_cfg->discard_tagged)
714 adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
715 else
716 adapter->flags |= QLCNIC_TAGGING_ENABLED;
718 if (esw_cfg->vlan_id)
719 adapter->pvid = esw_cfg->vlan_id;
720 else
721 adapter->pvid = 0;
724 static void
725 qlcnic_vlan_rx_add(struct net_device *netdev, u16 vid)
727 struct qlcnic_adapter *adapter = netdev_priv(netdev);
728 set_bit(vid, adapter->vlans);
731 static void
732 qlcnic_vlan_rx_del(struct net_device *netdev, u16 vid)
734 struct qlcnic_adapter *adapter = netdev_priv(netdev);
736 qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
737 clear_bit(vid, adapter->vlans);
740 static void
741 qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
742 struct qlcnic_esw_func_cfg *esw_cfg)
744 adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
745 QLCNIC_PROMISC_DISABLED);
747 if (esw_cfg->mac_anti_spoof)
748 adapter->flags |= QLCNIC_MACSPOOF;
750 if (!esw_cfg->mac_override)
751 adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
753 if (!esw_cfg->promisc_mode)
754 adapter->flags |= QLCNIC_PROMISC_DISABLED;
756 qlcnic_set_netdev_features(adapter, esw_cfg);
759 static int
760 qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
762 struct qlcnic_esw_func_cfg esw_cfg;
764 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
765 return 0;
767 esw_cfg.pci_func = adapter->ahw->pci_func;
768 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
769 return -EIO;
770 qlcnic_set_vlan_config(adapter, &esw_cfg);
771 qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
773 return 0;
776 static void
777 qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
778 struct qlcnic_esw_func_cfg *esw_cfg)
780 struct net_device *netdev = adapter->netdev;
781 unsigned long features, vlan_features;
783 features = (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
784 NETIF_F_IPV6_CSUM | NETIF_F_GRO);
785 vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
786 NETIF_F_IPV6_CSUM | NETIF_F_HW_VLAN_FILTER);
788 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
789 features |= (NETIF_F_TSO | NETIF_F_TSO6);
790 vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
793 if (netdev->features & NETIF_F_LRO)
794 features |= NETIF_F_LRO;
796 if (esw_cfg->offload_flags & BIT_0) {
797 netdev->features |= features;
798 if (!(esw_cfg->offload_flags & BIT_1))
799 netdev->features &= ~NETIF_F_TSO;
800 if (!(esw_cfg->offload_flags & BIT_2))
801 netdev->features &= ~NETIF_F_TSO6;
802 } else {
803 netdev->features &= ~features;
806 netdev->vlan_features = (features & vlan_features);
809 static int
810 qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
812 void __iomem *priv_op;
813 u32 op_mode, priv_level;
814 int err = 0;
816 err = qlcnic_initialize_nic(adapter);
817 if (err)
818 return err;
820 if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
821 return 0;
823 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
824 op_mode = readl(priv_op);
825 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
827 if (op_mode == QLC_DEV_DRV_DEFAULT)
828 priv_level = QLCNIC_MGMT_FUNC;
829 else
830 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
832 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
833 if (priv_level == QLCNIC_MGMT_FUNC) {
834 adapter->op_mode = QLCNIC_MGMT_FUNC;
835 err = qlcnic_init_pci_info(adapter);
836 if (err)
837 return err;
838 /* Set privilege level for other functions */
839 qlcnic_set_function_modes(adapter);
840 dev_info(&adapter->pdev->dev,
841 "HAL Version: %d, Management function\n",
842 adapter->fw_hal_version);
843 } else if (priv_level == QLCNIC_PRIV_FUNC) {
844 adapter->op_mode = QLCNIC_PRIV_FUNC;
845 dev_info(&adapter->pdev->dev,
846 "HAL Version: %d, Privileged function\n",
847 adapter->fw_hal_version);
851 adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
853 return err;
856 static int
857 qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
859 struct qlcnic_esw_func_cfg esw_cfg;
860 struct qlcnic_npar_info *npar;
861 u8 i;
863 if (adapter->need_fw_reset)
864 return 0;
866 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
867 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
868 continue;
869 memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
870 esw_cfg.pci_func = i;
871 esw_cfg.offload_flags = BIT_0;
872 esw_cfg.mac_override = BIT_0;
873 esw_cfg.promisc_mode = BIT_0;
874 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
875 esw_cfg.offload_flags |= (BIT_1 | BIT_2);
876 if (qlcnic_config_switch_port(adapter, &esw_cfg))
877 return -EIO;
878 npar = &adapter->npars[i];
879 npar->pvid = esw_cfg.vlan_id;
880 npar->mac_override = esw_cfg.mac_override;
881 npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
882 npar->discard_tagged = esw_cfg.discard_tagged;
883 npar->promisc_mode = esw_cfg.promisc_mode;
884 npar->offload_flags = esw_cfg.offload_flags;
887 return 0;
890 static int
891 qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
892 struct qlcnic_npar_info *npar, int pci_func)
894 struct qlcnic_esw_func_cfg esw_cfg;
895 esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
896 esw_cfg.pci_func = pci_func;
897 esw_cfg.vlan_id = npar->pvid;
898 esw_cfg.mac_override = npar->mac_override;
899 esw_cfg.discard_tagged = npar->discard_tagged;
900 esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
901 esw_cfg.offload_flags = npar->offload_flags;
902 esw_cfg.promisc_mode = npar->promisc_mode;
903 if (qlcnic_config_switch_port(adapter, &esw_cfg))
904 return -EIO;
906 esw_cfg.op_mode = QLCNIC_ADD_VLAN;
907 if (qlcnic_config_switch_port(adapter, &esw_cfg))
908 return -EIO;
910 return 0;
913 static int
914 qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
916 int i, err;
917 struct qlcnic_npar_info *npar;
918 struct qlcnic_info nic_info;
920 if (!adapter->need_fw_reset)
921 return 0;
923 /* Set the NPAR config data after FW reset */
924 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
925 npar = &adapter->npars[i];
926 if (npar->type != QLCNIC_TYPE_NIC)
927 continue;
928 err = qlcnic_get_nic_info(adapter, &nic_info, i);
929 if (err)
930 return err;
931 nic_info.min_tx_bw = npar->min_bw;
932 nic_info.max_tx_bw = npar->max_bw;
933 err = qlcnic_set_nic_info(adapter, &nic_info);
934 if (err)
935 return err;
937 if (npar->enable_pm) {
938 err = qlcnic_config_port_mirroring(adapter,
939 npar->dest_npar, 1, i);
940 if (err)
941 return err;
943 err = qlcnic_reset_eswitch_config(adapter, npar, i);
944 if (err)
945 return err;
947 return 0;
950 static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
952 u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
953 u32 npar_state;
955 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
956 return 0;
958 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
959 while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
960 msleep(1000);
961 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
963 if (!npar_opt_timeo) {
964 dev_err(&adapter->pdev->dev,
965 "Waiting for NPAR state to opertional timeout\n");
966 return -EIO;
968 return 0;
971 static int
972 qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
974 int err;
976 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
977 adapter->op_mode != QLCNIC_MGMT_FUNC)
978 return 0;
980 err = qlcnic_set_default_offload_settings(adapter);
981 if (err)
982 return err;
984 err = qlcnic_reset_npar_config(adapter);
985 if (err)
986 return err;
988 qlcnic_dev_set_npar_ready(adapter);
990 return err;
993 static int
994 qlcnic_start_firmware(struct qlcnic_adapter *adapter)
996 int err;
998 err = qlcnic_can_start_firmware(adapter);
999 if (err < 0)
1000 return err;
1001 else if (!err)
1002 goto check_fw_status;
1004 if (load_fw_file)
1005 qlcnic_request_firmware(adapter);
1006 else {
1007 err = qlcnic_check_flash_fw_ver(adapter);
1008 if (err)
1009 goto err_out;
1011 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
1014 err = qlcnic_need_fw_reset(adapter);
1015 if (err == 0)
1016 goto check_fw_status;
1018 err = qlcnic_pinit_from_rom(adapter);
1019 if (err)
1020 goto err_out;
1022 err = qlcnic_load_firmware(adapter);
1023 if (err)
1024 goto err_out;
1026 qlcnic_release_firmware(adapter);
1027 QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
1029 check_fw_status:
1030 err = qlcnic_check_fw_status(adapter);
1031 if (err)
1032 goto err_out;
1034 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
1035 qlcnic_idc_debug_info(adapter, 1);
1037 err = qlcnic_check_eswitch_mode(adapter);
1038 if (err) {
1039 dev_err(&adapter->pdev->dev,
1040 "Memory allocation failed for eswitch\n");
1041 goto err_out;
1043 err = qlcnic_set_mgmt_operations(adapter);
1044 if (err)
1045 goto err_out;
1047 qlcnic_check_options(adapter);
1048 adapter->need_fw_reset = 0;
1050 qlcnic_release_firmware(adapter);
1051 return 0;
1053 err_out:
1054 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1055 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
1057 qlcnic_release_firmware(adapter);
1058 return err;
1061 static int
1062 qlcnic_request_irq(struct qlcnic_adapter *adapter)
1064 irq_handler_t handler;
1065 struct qlcnic_host_sds_ring *sds_ring;
1066 int err, ring;
1068 unsigned long flags = 0;
1069 struct net_device *netdev = adapter->netdev;
1070 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1072 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1073 handler = qlcnic_tmp_intr;
1074 if (!QLCNIC_IS_MSI_FAMILY(adapter))
1075 flags |= IRQF_SHARED;
1077 } else {
1078 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1079 handler = qlcnic_msix_intr;
1080 else if (adapter->flags & QLCNIC_MSI_ENABLED)
1081 handler = qlcnic_msi_intr;
1082 else {
1083 flags |= IRQF_SHARED;
1084 handler = qlcnic_intr;
1087 adapter->irq = netdev->irq;
1089 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1090 sds_ring = &recv_ctx->sds_rings[ring];
1091 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1092 err = request_irq(sds_ring->irq, handler,
1093 flags, sds_ring->name, sds_ring);
1094 if (err)
1095 return err;
1098 return 0;
1101 static void
1102 qlcnic_free_irq(struct qlcnic_adapter *adapter)
1104 int ring;
1105 struct qlcnic_host_sds_ring *sds_ring;
1107 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1109 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1110 sds_ring = &recv_ctx->sds_rings[ring];
1111 free_irq(sds_ring->irq, sds_ring);
1115 static int
1116 __qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1118 int ring;
1119 struct qlcnic_host_rds_ring *rds_ring;
1121 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1122 return -EIO;
1124 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1125 return 0;
1126 if (qlcnic_set_eswitch_port_config(adapter))
1127 return -EIO;
1129 if (qlcnic_fw_create_ctx(adapter))
1130 return -EIO;
1132 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1133 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1134 qlcnic_post_rx_buffers(adapter, rds_ring);
1137 qlcnic_set_multi(netdev);
1138 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1140 adapter->ahw->linkup = 0;
1142 if (adapter->max_sds_rings > 1)
1143 qlcnic_config_rss(adapter, 1);
1145 qlcnic_config_intr_coalesce(adapter);
1147 if (netdev->features & NETIF_F_LRO)
1148 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1150 qlcnic_napi_enable(adapter);
1152 qlcnic_linkevent_request(adapter, 1);
1154 adapter->reset_context = 0;
1155 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1156 return 0;
1159 /* Usage: During resume and firmware recovery module.*/
1161 static int
1162 qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1164 int err = 0;
1166 rtnl_lock();
1167 if (netif_running(netdev))
1168 err = __qlcnic_up(adapter, netdev);
1169 rtnl_unlock();
1171 return err;
1174 static void
1175 __qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1177 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1178 return;
1180 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1181 return;
1183 smp_mb();
1184 spin_lock(&adapter->tx_clean_lock);
1185 netif_carrier_off(netdev);
1186 netif_tx_disable(netdev);
1188 qlcnic_free_mac_list(adapter);
1190 if (adapter->fhash.fnum)
1191 qlcnic_delete_lb_filters(adapter);
1193 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1195 qlcnic_napi_disable(adapter);
1197 qlcnic_fw_destroy_ctx(adapter);
1199 qlcnic_reset_rx_buffers_list(adapter);
1200 qlcnic_release_tx_buffers(adapter);
1201 spin_unlock(&adapter->tx_clean_lock);
1204 /* Usage: During suspend and firmware recovery module */
1206 static void
1207 qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1209 rtnl_lock();
1210 if (netif_running(netdev))
1211 __qlcnic_down(adapter, netdev);
1212 rtnl_unlock();
1216 static int
1217 qlcnic_attach(struct qlcnic_adapter *adapter)
1219 struct net_device *netdev = adapter->netdev;
1220 struct pci_dev *pdev = adapter->pdev;
1221 int err;
1223 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1224 return 0;
1226 err = qlcnic_napi_add(adapter, netdev);
1227 if (err)
1228 return err;
1230 err = qlcnic_alloc_sw_resources(adapter);
1231 if (err) {
1232 dev_err(&pdev->dev, "Error in setting sw resources\n");
1233 goto err_out_napi_del;
1236 err = qlcnic_alloc_hw_resources(adapter);
1237 if (err) {
1238 dev_err(&pdev->dev, "Error in setting hw resources\n");
1239 goto err_out_free_sw;
1242 err = qlcnic_request_irq(adapter);
1243 if (err) {
1244 dev_err(&pdev->dev, "failed to setup interrupt\n");
1245 goto err_out_free_hw;
1248 qlcnic_create_sysfs_entries(adapter);
1250 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1251 return 0;
1253 err_out_free_hw:
1254 qlcnic_free_hw_resources(adapter);
1255 err_out_free_sw:
1256 qlcnic_free_sw_resources(adapter);
1257 err_out_napi_del:
1258 qlcnic_napi_del(adapter);
1259 return err;
1262 static void
1263 qlcnic_detach(struct qlcnic_adapter *adapter)
1265 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1266 return;
1268 qlcnic_remove_sysfs_entries(adapter);
1270 qlcnic_free_hw_resources(adapter);
1271 qlcnic_release_rx_buffers(adapter);
1272 qlcnic_free_irq(adapter);
1273 qlcnic_napi_del(adapter);
1274 qlcnic_free_sw_resources(adapter);
1276 adapter->is_up = 0;
1279 void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1281 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1282 struct qlcnic_host_sds_ring *sds_ring;
1283 int ring;
1285 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
1286 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1287 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1288 sds_ring = &adapter->recv_ctx->sds_rings[ring];
1289 qlcnic_disable_int(sds_ring);
1293 qlcnic_fw_destroy_ctx(adapter);
1295 qlcnic_detach(adapter);
1297 adapter->diag_test = 0;
1298 adapter->max_sds_rings = max_sds_rings;
1300 if (qlcnic_attach(adapter))
1301 goto out;
1303 if (netif_running(netdev))
1304 __qlcnic_up(adapter, netdev);
1305 out:
1306 netif_device_attach(netdev);
1309 static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1311 int err = 0;
1312 adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1313 GFP_KERNEL);
1314 if (!adapter->ahw) {
1315 dev_err(&adapter->pdev->dev,
1316 "Failed to allocate recv ctx resources for adapter\n");
1317 err = -ENOMEM;
1318 goto err_out;
1320 adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1321 GFP_KERNEL);
1322 if (!adapter->recv_ctx) {
1323 dev_err(&adapter->pdev->dev,
1324 "Failed to allocate recv ctx resources for adapter\n");
1325 kfree(adapter->ahw);
1326 adapter->ahw = NULL;
1327 err = -ENOMEM;
1328 goto err_out;
1330 /* Initialize interrupt coalesce parameters */
1331 adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1332 adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1333 adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
1334 err_out:
1335 return err;
1338 static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1340 kfree(adapter->recv_ctx);
1341 adapter->recv_ctx = NULL;
1343 if (adapter->ahw->fw_dump.tmpl_hdr) {
1344 vfree(adapter->ahw->fw_dump.tmpl_hdr);
1345 adapter->ahw->fw_dump.tmpl_hdr = NULL;
1347 kfree(adapter->ahw);
1348 adapter->ahw = NULL;
1351 int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1353 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1354 struct qlcnic_host_sds_ring *sds_ring;
1355 struct qlcnic_host_rds_ring *rds_ring;
1356 int ring;
1357 int ret;
1359 netif_device_detach(netdev);
1361 if (netif_running(netdev))
1362 __qlcnic_down(adapter, netdev);
1364 qlcnic_detach(adapter);
1366 adapter->max_sds_rings = 1;
1367 adapter->diag_test = test;
1369 ret = qlcnic_attach(adapter);
1370 if (ret) {
1371 netif_device_attach(netdev);
1372 return ret;
1375 ret = qlcnic_fw_create_ctx(adapter);
1376 if (ret) {
1377 qlcnic_detach(adapter);
1378 netif_device_attach(netdev);
1379 return ret;
1382 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1383 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1384 qlcnic_post_rx_buffers(adapter, rds_ring);
1387 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1388 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1389 sds_ring = &adapter->recv_ctx->sds_rings[ring];
1390 qlcnic_enable_int(sds_ring);
1394 if (adapter->diag_test == QLCNIC_LOOPBACK_TEST) {
1395 adapter->ahw->loopback_state = 0;
1396 qlcnic_linkevent_request(adapter, 1);
1399 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1401 return 0;
1404 /* Reset context in hardware only */
1405 static int
1406 qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1408 struct net_device *netdev = adapter->netdev;
1410 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1411 return -EBUSY;
1413 netif_device_detach(netdev);
1415 qlcnic_down(adapter, netdev);
1417 qlcnic_up(adapter, netdev);
1419 netif_device_attach(netdev);
1421 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1422 return 0;
1426 qlcnic_reset_context(struct qlcnic_adapter *adapter)
1428 int err = 0;
1429 struct net_device *netdev = adapter->netdev;
1431 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1432 return -EBUSY;
1434 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1436 netif_device_detach(netdev);
1438 if (netif_running(netdev))
1439 __qlcnic_down(adapter, netdev);
1441 qlcnic_detach(adapter);
1443 if (netif_running(netdev)) {
1444 err = qlcnic_attach(adapter);
1445 if (!err)
1446 __qlcnic_up(adapter, netdev);
1449 netif_device_attach(netdev);
1452 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1453 return err;
1456 static int
1457 qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
1458 struct net_device *netdev, u8 pci_using_dac)
1460 int err;
1461 struct pci_dev *pdev = adapter->pdev;
1463 adapter->mc_enabled = 0;
1464 adapter->max_mc_count = 38;
1466 netdev->netdev_ops = &qlcnic_netdev_ops;
1467 netdev->watchdog_timeo = 5*HZ;
1469 qlcnic_change_mtu(netdev, netdev->mtu);
1471 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1473 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1474 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
1476 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
1477 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1478 if (pci_using_dac)
1479 netdev->hw_features |= NETIF_F_HIGHDMA;
1481 netdev->vlan_features = netdev->hw_features;
1483 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1484 netdev->hw_features |= NETIF_F_HW_VLAN_TX;
1485 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1486 netdev->hw_features |= NETIF_F_LRO;
1488 netdev->features |= netdev->hw_features |
1489 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
1491 netdev->irq = adapter->msix_entries[0].vector;
1493 err = register_netdev(netdev);
1494 if (err) {
1495 dev_err(&pdev->dev, "failed to register net device\n");
1496 return err;
1499 return 0;
1502 static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1504 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1505 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1506 *pci_using_dac = 1;
1507 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1508 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1509 *pci_using_dac = 0;
1510 else {
1511 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1512 return -EIO;
1515 return 0;
1518 static int
1519 qlcnic_alloc_msix_entries(struct qlcnic_adapter *adapter, u16 count)
1521 adapter->msix_entries = kcalloc(count, sizeof(struct msix_entry),
1522 GFP_KERNEL);
1524 if (adapter->msix_entries)
1525 return 0;
1527 dev_err(&adapter->pdev->dev, "failed allocating msix_entries\n");
1528 return -ENOMEM;
1531 static int __devinit
1532 qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1534 struct net_device *netdev = NULL;
1535 struct qlcnic_adapter *adapter = NULL;
1536 int err;
1537 uint8_t revision_id;
1538 uint8_t pci_using_dac;
1539 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
1541 err = pci_enable_device(pdev);
1542 if (err)
1543 return err;
1545 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1546 err = -ENODEV;
1547 goto err_out_disable_pdev;
1550 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1551 if (err)
1552 goto err_out_disable_pdev;
1554 err = pci_request_regions(pdev, qlcnic_driver_name);
1555 if (err)
1556 goto err_out_disable_pdev;
1558 pci_set_master(pdev);
1559 pci_enable_pcie_error_reporting(pdev);
1561 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1562 if (!netdev) {
1563 dev_err(&pdev->dev, "failed to allocate net_device\n");
1564 err = -ENOMEM;
1565 goto err_out_free_res;
1568 SET_NETDEV_DEV(netdev, &pdev->dev);
1570 adapter = netdev_priv(netdev);
1571 adapter->netdev = netdev;
1572 adapter->pdev = pdev;
1574 if (qlcnic_alloc_adapter_resources(adapter))
1575 goto err_out_free_netdev;
1577 adapter->dev_rst_time = jiffies;
1578 revision_id = pdev->revision;
1579 adapter->ahw->revision_id = revision_id;
1580 adapter->mac_learn = qlcnic_mac_learn;
1582 rwlock_init(&adapter->ahw->crb_lock);
1583 mutex_init(&adapter->ahw->mem_lock);
1585 spin_lock_init(&adapter->tx_clean_lock);
1586 INIT_LIST_HEAD(&adapter->mac_list);
1588 err = qlcnic_setup_pci_map(adapter);
1589 if (err)
1590 goto err_out_free_hw;
1592 /* This will be reset for mezz cards */
1593 adapter->portnum = adapter->ahw->pci_func;
1595 err = qlcnic_get_board_info(adapter);
1596 if (err) {
1597 dev_err(&pdev->dev, "Error getting board config info.\n");
1598 goto err_out_iounmap;
1601 err = qlcnic_setup_idc_param(adapter);
1602 if (err)
1603 goto err_out_iounmap;
1605 adapter->flags |= QLCNIC_NEED_FLR;
1607 err = adapter->nic_ops->start_firmware(adapter);
1608 if (err) {
1609 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
1610 goto err_out_decr_ref;
1613 /* Get FW dump template and store it */
1614 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC)
1615 if (!qlcnic_fw_cmd_get_minidump_temp(adapter))
1616 dev_info(&pdev->dev,
1617 "Supports FW dump capability\n");
1619 if (qlcnic_read_mac_addr(adapter))
1620 dev_warn(&pdev->dev, "failed to read mac addr\n");
1622 if (adapter->portnum == 0) {
1623 get_brd_name(adapter, brd_name);
1625 pr_info("%s: %s Board Chip rev 0x%x\n",
1626 module_name(THIS_MODULE),
1627 brd_name, adapter->ahw->revision_id);
1630 qlcnic_clear_stats(adapter);
1632 err = qlcnic_alloc_msix_entries(adapter, adapter->max_rx_ques);
1633 if (err)
1634 goto err_out_decr_ref;
1636 qlcnic_setup_intr(adapter);
1638 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
1639 if (err)
1640 goto err_out_disable_msi;
1642 pci_set_drvdata(pdev, adapter);
1644 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1646 switch (adapter->ahw->port_type) {
1647 case QLCNIC_GBE:
1648 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1649 adapter->netdev->name);
1650 break;
1651 case QLCNIC_XGBE:
1652 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1653 adapter->netdev->name);
1654 break;
1657 if (adapter->mac_learn)
1658 qlcnic_alloc_lb_filters_mem(adapter);
1660 qlcnic_create_diag_entries(adapter);
1662 return 0;
1664 err_out_disable_msi:
1665 qlcnic_teardown_intr(adapter);
1666 kfree(adapter->msix_entries);
1668 err_out_decr_ref:
1669 qlcnic_clr_all_drv_state(adapter, 0);
1671 err_out_iounmap:
1672 qlcnic_cleanup_pci_map(adapter);
1674 err_out_free_hw:
1675 qlcnic_free_adapter_resources(adapter);
1677 err_out_free_netdev:
1678 free_netdev(netdev);
1680 err_out_free_res:
1681 pci_release_regions(pdev);
1683 err_out_disable_pdev:
1684 pci_set_drvdata(pdev, NULL);
1685 pci_disable_device(pdev);
1686 return err;
1689 static void __devexit qlcnic_remove(struct pci_dev *pdev)
1691 struct qlcnic_adapter *adapter;
1692 struct net_device *netdev;
1694 adapter = pci_get_drvdata(pdev);
1695 if (adapter == NULL)
1696 return;
1698 netdev = adapter->netdev;
1700 qlcnic_cancel_fw_work(adapter);
1702 unregister_netdev(netdev);
1704 qlcnic_detach(adapter);
1706 if (adapter->npars != NULL)
1707 kfree(adapter->npars);
1708 if (adapter->eswitch != NULL)
1709 kfree(adapter->eswitch);
1711 qlcnic_clr_all_drv_state(adapter, 0);
1713 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1715 qlcnic_free_lb_filters_mem(adapter);
1717 qlcnic_teardown_intr(adapter);
1718 kfree(adapter->msix_entries);
1720 qlcnic_remove_diag_entries(adapter);
1722 qlcnic_cleanup_pci_map(adapter);
1724 qlcnic_release_firmware(adapter);
1726 pci_disable_pcie_error_reporting(pdev);
1727 pci_release_regions(pdev);
1728 pci_disable_device(pdev);
1729 pci_set_drvdata(pdev, NULL);
1731 qlcnic_free_adapter_resources(adapter);
1732 free_netdev(netdev);
1734 static int __qlcnic_shutdown(struct pci_dev *pdev)
1736 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1737 struct net_device *netdev = adapter->netdev;
1738 int retval;
1740 netif_device_detach(netdev);
1742 qlcnic_cancel_fw_work(adapter);
1744 if (netif_running(netdev))
1745 qlcnic_down(adapter, netdev);
1747 qlcnic_clr_all_drv_state(adapter, 0);
1749 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1751 retval = pci_save_state(pdev);
1752 if (retval)
1753 return retval;
1755 if (qlcnic_wol_supported(adapter)) {
1756 pci_enable_wake(pdev, PCI_D3cold, 1);
1757 pci_enable_wake(pdev, PCI_D3hot, 1);
1760 return 0;
1763 static void qlcnic_shutdown(struct pci_dev *pdev)
1765 if (__qlcnic_shutdown(pdev))
1766 return;
1768 pci_disable_device(pdev);
1771 #ifdef CONFIG_PM
1772 static int
1773 qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1775 int retval;
1777 retval = __qlcnic_shutdown(pdev);
1778 if (retval)
1779 return retval;
1781 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1782 return 0;
1785 static int
1786 qlcnic_resume(struct pci_dev *pdev)
1788 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1789 struct net_device *netdev = adapter->netdev;
1790 int err;
1792 err = pci_enable_device(pdev);
1793 if (err)
1794 return err;
1796 pci_set_power_state(pdev, PCI_D0);
1797 pci_set_master(pdev);
1798 pci_restore_state(pdev);
1800 err = adapter->nic_ops->start_firmware(adapter);
1801 if (err) {
1802 dev_err(&pdev->dev, "failed to start firmware\n");
1803 return err;
1806 if (netif_running(netdev)) {
1807 err = qlcnic_up(adapter, netdev);
1808 if (err)
1809 goto done;
1811 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
1813 done:
1814 netif_device_attach(netdev);
1815 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1816 return 0;
1818 #endif
1820 static int qlcnic_open(struct net_device *netdev)
1822 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1823 int err;
1825 netif_carrier_off(netdev);
1827 err = qlcnic_attach(adapter);
1828 if (err)
1829 return err;
1831 err = __qlcnic_up(adapter, netdev);
1832 if (err)
1833 goto err_out;
1835 netif_start_queue(netdev);
1837 return 0;
1839 err_out:
1840 qlcnic_detach(adapter);
1841 return err;
1845 * qlcnic_close - Disables a network interface entry point
1847 static int qlcnic_close(struct net_device *netdev)
1849 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1851 __qlcnic_down(adapter, netdev);
1852 return 0;
1855 void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
1857 void *head;
1858 int i;
1860 if (adapter->fhash.fmax && adapter->fhash.fhead)
1861 return;
1863 spin_lock_init(&adapter->mac_learn_lock);
1865 head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1866 GFP_KERNEL);
1867 if (!head)
1868 return;
1870 adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
1871 adapter->fhash.fhead = head;
1873 for (i = 0; i < adapter->fhash.fmax; i++)
1874 INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1877 static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1879 if (adapter->fhash.fmax && adapter->fhash.fhead)
1880 kfree(adapter->fhash.fhead);
1882 adapter->fhash.fhead = NULL;
1883 adapter->fhash.fmax = 0;
1886 static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
1887 u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
1889 struct cmd_desc_type0 *hwdesc;
1890 struct qlcnic_nic_req *req;
1891 struct qlcnic_mac_req *mac_req;
1892 struct qlcnic_vlan_req *vlan_req;
1893 u32 producer;
1894 u64 word;
1896 producer = tx_ring->producer;
1897 hwdesc = &tx_ring->desc_head[tx_ring->producer];
1899 req = (struct qlcnic_nic_req *)hwdesc;
1900 memset(req, 0, sizeof(struct qlcnic_nic_req));
1901 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1903 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1904 req->req_hdr = cpu_to_le64(word);
1906 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
1907 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
1908 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1910 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1911 vlan_req->vlan_id = vlan_id;
1913 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
1914 smp_mb();
1917 #define QLCNIC_MAC_HASH(MAC)\
1918 ((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1920 static void
1921 qlcnic_send_filter(struct qlcnic_adapter *adapter,
1922 struct qlcnic_host_tx_ring *tx_ring,
1923 struct cmd_desc_type0 *first_desc,
1924 struct sk_buff *skb)
1926 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1927 struct qlcnic_filter *fil, *tmp_fil;
1928 struct hlist_node *tmp_hnode, *n;
1929 struct hlist_head *head;
1930 u64 src_addr = 0;
1931 __le16 vlan_id = 0;
1932 u8 hindex;
1934 if (!compare_ether_addr(phdr->h_source, adapter->mac_addr))
1935 return;
1937 if (adapter->fhash.fnum >= adapter->fhash.fmax)
1938 return;
1940 /* Only NPAR capable devices support vlan based learning*/
1941 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1942 vlan_id = first_desc->vlan_TCI;
1943 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1944 hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1945 head = &(adapter->fhash.fhead[hindex]);
1947 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
1948 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1949 tmp_fil->vlan_id == vlan_id) {
1951 if (jiffies >
1952 (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1953 qlcnic_change_filter(adapter, src_addr, vlan_id,
1954 tx_ring);
1955 tmp_fil->ftime = jiffies;
1956 return;
1960 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
1961 if (!fil)
1962 return;
1964 qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
1966 fil->ftime = jiffies;
1967 fil->vlan_id = vlan_id;
1968 memcpy(fil->faddr, &src_addr, ETH_ALEN);
1969 spin_lock(&adapter->mac_learn_lock);
1970 hlist_add_head(&(fil->fnode), head);
1971 adapter->fhash.fnum++;
1972 spin_unlock(&adapter->mac_learn_lock);
1975 static int
1976 qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
1977 struct cmd_desc_type0 *first_desc,
1978 struct sk_buff *skb)
1980 u8 opcode = 0, hdr_len = 0;
1981 u16 flags = 0, vlan_tci = 0;
1982 int copied, offset, copy_len;
1983 struct cmd_desc_type0 *hwdesc;
1984 struct vlan_ethhdr *vh;
1985 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1986 u16 protocol = ntohs(skb->protocol);
1987 u32 producer = tx_ring->producer;
1989 if (protocol == ETH_P_8021Q) {
1990 vh = (struct vlan_ethhdr *)skb->data;
1991 flags = FLAGS_VLAN_TAGGED;
1992 vlan_tci = vh->h_vlan_TCI;
1993 } else if (vlan_tx_tag_present(skb)) {
1994 flags = FLAGS_VLAN_OOB;
1995 vlan_tci = vlan_tx_tag_get(skb);
1997 if (unlikely(adapter->pvid)) {
1998 if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
1999 return -EIO;
2000 if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
2001 goto set_flags;
2003 flags = FLAGS_VLAN_OOB;
2004 vlan_tci = adapter->pvid;
2006 set_flags:
2007 qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
2008 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2010 if (*(skb->data) & BIT_0) {
2011 flags |= BIT_0;
2012 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
2014 opcode = TX_ETHER_PKT;
2015 if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
2016 skb_shinfo(skb)->gso_size > 0) {
2018 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2020 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
2021 first_desc->total_hdr_length = hdr_len;
2023 opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
2025 /* For LSO, we need to copy the MAC/IP/TCP headers into
2026 * the descriptor ring */
2027 copied = 0;
2028 offset = 2;
2030 if (flags & FLAGS_VLAN_OOB) {
2031 first_desc->total_hdr_length += VLAN_HLEN;
2032 first_desc->tcp_hdr_offset = VLAN_HLEN;
2033 first_desc->ip_hdr_offset = VLAN_HLEN;
2034 /* Only in case of TSO on vlan device */
2035 flags |= FLAGS_VLAN_TAGGED;
2037 /* Create a TSO vlan header template for firmware */
2039 hwdesc = &tx_ring->desc_head[producer];
2040 tx_ring->cmd_buf_arr[producer].skb = NULL;
2042 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2043 offset, hdr_len + VLAN_HLEN);
2045 vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
2046 skb_copy_from_linear_data(skb, vh, 12);
2047 vh->h_vlan_proto = htons(ETH_P_8021Q);
2048 vh->h_vlan_TCI = htons(vlan_tci);
2050 skb_copy_from_linear_data_offset(skb, 12,
2051 (char *)vh + 16, copy_len - 16);
2053 copied = copy_len - VLAN_HLEN;
2054 offset = 0;
2056 producer = get_next_index(producer, tx_ring->num_desc);
2059 while (copied < hdr_len) {
2061 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2062 offset, (hdr_len - copied));
2064 hwdesc = &tx_ring->desc_head[producer];
2065 tx_ring->cmd_buf_arr[producer].skb = NULL;
2067 skb_copy_from_linear_data_offset(skb, copied,
2068 (char *) hwdesc + offset, copy_len);
2070 copied += copy_len;
2071 offset = 0;
2073 producer = get_next_index(producer, tx_ring->num_desc);
2076 tx_ring->producer = producer;
2077 smp_mb();
2078 adapter->stats.lso_frames++;
2080 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2081 u8 l4proto;
2083 if (protocol == ETH_P_IP) {
2084 l4proto = ip_hdr(skb)->protocol;
2086 if (l4proto == IPPROTO_TCP)
2087 opcode = TX_TCP_PKT;
2088 else if (l4proto == IPPROTO_UDP)
2089 opcode = TX_UDP_PKT;
2090 } else if (protocol == ETH_P_IPV6) {
2091 l4proto = ipv6_hdr(skb)->nexthdr;
2093 if (l4proto == IPPROTO_TCP)
2094 opcode = TX_TCPV6_PKT;
2095 else if (l4proto == IPPROTO_UDP)
2096 opcode = TX_UDPV6_PKT;
2099 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
2100 first_desc->ip_hdr_offset += skb_network_offset(skb);
2101 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2103 return 0;
2106 static int
2107 qlcnic_map_tx_skb(struct pci_dev *pdev,
2108 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2110 struct qlcnic_skb_frag *nf;
2111 struct skb_frag_struct *frag;
2112 int i, nr_frags;
2113 dma_addr_t map;
2115 nr_frags = skb_shinfo(skb)->nr_frags;
2116 nf = &pbuf->frag_array[0];
2118 map = pci_map_single(pdev, skb->data,
2119 skb_headlen(skb), PCI_DMA_TODEVICE);
2120 if (pci_dma_mapping_error(pdev, map))
2121 goto out_err;
2123 nf->dma = map;
2124 nf->length = skb_headlen(skb);
2126 for (i = 0; i < nr_frags; i++) {
2127 frag = &skb_shinfo(skb)->frags[i];
2128 nf = &pbuf->frag_array[i+1];
2130 map = pci_map_page(pdev, frag->page, frag->page_offset,
2131 frag->size, PCI_DMA_TODEVICE);
2132 if (pci_dma_mapping_error(pdev, map))
2133 goto unwind;
2135 nf->dma = map;
2136 nf->length = frag->size;
2139 return 0;
2141 unwind:
2142 while (--i >= 0) {
2143 nf = &pbuf->frag_array[i+1];
2144 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2147 nf = &pbuf->frag_array[0];
2148 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2150 out_err:
2151 return -ENOMEM;
2154 static void
2155 qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
2156 struct qlcnic_cmd_buffer *pbuf)
2158 struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
2159 int nr_frags = skb_shinfo(skb)->nr_frags;
2160 int i;
2162 for (i = 0; i < nr_frags; i++) {
2163 nf = &pbuf->frag_array[i+1];
2164 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2167 nf = &pbuf->frag_array[0];
2168 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2169 pbuf->skb = NULL;
2172 static inline void
2173 qlcnic_clear_cmddesc(u64 *desc)
2175 desc[0] = 0ULL;
2176 desc[2] = 0ULL;
2177 desc[7] = 0ULL;
2180 netdev_tx_t
2181 qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2183 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2184 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2185 struct qlcnic_cmd_buffer *pbuf;
2186 struct qlcnic_skb_frag *buffrag;
2187 struct cmd_desc_type0 *hwdesc, *first_desc;
2188 struct pci_dev *pdev;
2189 struct ethhdr *phdr;
2190 int delta = 0;
2191 int i, k;
2193 u32 producer;
2194 int frag_count;
2195 u32 num_txd = tx_ring->num_desc;
2197 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2198 netif_stop_queue(netdev);
2199 return NETDEV_TX_BUSY;
2202 if (adapter->flags & QLCNIC_MACSPOOF) {
2203 phdr = (struct ethhdr *)skb->data;
2204 if (compare_ether_addr(phdr->h_source,
2205 adapter->mac_addr))
2206 goto drop_packet;
2209 frag_count = skb_shinfo(skb)->nr_frags + 1;
2210 /* 14 frags supported for normal packet and
2211 * 32 frags supported for TSO packet
2213 if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
2215 for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
2216 delta += skb_shinfo(skb)->frags[i].size;
2218 if (!__pskb_pull_tail(skb, delta))
2219 goto drop_packet;
2221 frag_count = 1 + skb_shinfo(skb)->nr_frags;
2224 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
2225 netif_stop_queue(netdev);
2226 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2227 netif_start_queue(netdev);
2228 else {
2229 adapter->stats.xmit_off++;
2230 return NETDEV_TX_BUSY;
2234 producer = tx_ring->producer;
2235 pbuf = &tx_ring->cmd_buf_arr[producer];
2237 pdev = adapter->pdev;
2239 first_desc = hwdesc = &tx_ring->desc_head[producer];
2240 qlcnic_clear_cmddesc((u64 *)hwdesc);
2242 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2243 adapter->stats.tx_dma_map_error++;
2244 goto drop_packet;
2247 pbuf->skb = skb;
2248 pbuf->frag_count = frag_count;
2250 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2251 qlcnic_set_tx_port(first_desc, adapter->portnum);
2253 for (i = 0; i < frag_count; i++) {
2255 k = i % 4;
2257 if ((k == 0) && (i > 0)) {
2258 /* move to next desc.*/
2259 producer = get_next_index(producer, num_txd);
2260 hwdesc = &tx_ring->desc_head[producer];
2261 qlcnic_clear_cmddesc((u64 *)hwdesc);
2262 tx_ring->cmd_buf_arr[producer].skb = NULL;
2265 buffrag = &pbuf->frag_array[i];
2267 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2268 switch (k) {
2269 case 0:
2270 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2271 break;
2272 case 1:
2273 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2274 break;
2275 case 2:
2276 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2277 break;
2278 case 3:
2279 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2280 break;
2284 tx_ring->producer = get_next_index(producer, num_txd);
2285 smp_mb();
2287 if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
2288 goto unwind_buff;
2290 if (adapter->mac_learn)
2291 qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2293 adapter->stats.txbytes += skb->len;
2294 adapter->stats.xmitcalled++;
2296 qlcnic_update_cmd_producer(adapter, tx_ring);
2298 return NETDEV_TX_OK;
2300 unwind_buff:
2301 qlcnic_unmap_buffers(pdev, skb, pbuf);
2302 drop_packet:
2303 adapter->stats.txdropped++;
2304 dev_kfree_skb_any(skb);
2305 return NETDEV_TX_OK;
2308 static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2310 struct net_device *netdev = adapter->netdev;
2311 u32 temp, temp_state, temp_val;
2312 int rv = 0;
2314 temp = QLCRD32(adapter, CRB_TEMP_STATE);
2316 temp_state = qlcnic_get_temp_state(temp);
2317 temp_val = qlcnic_get_temp_val(temp);
2319 if (temp_state == QLCNIC_TEMP_PANIC) {
2320 dev_err(&netdev->dev,
2321 "Device temperature %d degrees C exceeds"
2322 " maximum allowed. Hardware has been shut down.\n",
2323 temp_val);
2324 rv = 1;
2325 } else if (temp_state == QLCNIC_TEMP_WARN) {
2326 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2327 dev_err(&netdev->dev,
2328 "Device temperature %d degrees C "
2329 "exceeds operating range."
2330 " Immediate action needed.\n",
2331 temp_val);
2333 } else {
2334 if (adapter->temp == QLCNIC_TEMP_WARN) {
2335 dev_info(&netdev->dev,
2336 "Device temperature is now %d degrees C"
2337 " in normal range.\n", temp_val);
2340 adapter->temp = temp_state;
2341 return rv;
2344 void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2346 struct net_device *netdev = adapter->netdev;
2348 if (adapter->ahw->linkup && !linkup) {
2349 netdev_info(netdev, "NIC Link is down\n");
2350 adapter->ahw->linkup = 0;
2351 if (netif_running(netdev)) {
2352 netif_carrier_off(netdev);
2353 netif_stop_queue(netdev);
2355 } else if (!adapter->ahw->linkup && linkup) {
2356 netdev_info(netdev, "NIC Link is up\n");
2357 adapter->ahw->linkup = 1;
2358 if (netif_running(netdev)) {
2359 netif_carrier_on(netdev);
2360 netif_wake_queue(netdev);
2365 static void qlcnic_tx_timeout(struct net_device *netdev)
2367 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2369 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2370 return;
2372 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
2374 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
2375 adapter->need_fw_reset = 1;
2376 else
2377 adapter->reset_context = 1;
2380 static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2382 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2383 struct net_device_stats *stats = &netdev->stats;
2385 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2386 stats->tx_packets = adapter->stats.xmitfinished;
2387 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
2388 stats->tx_bytes = adapter->stats.txbytes;
2389 stats->rx_dropped = adapter->stats.rxdropped;
2390 stats->tx_dropped = adapter->stats.txdropped;
2392 return stats;
2395 static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
2397 u32 status;
2399 status = readl(adapter->isr_int_vec);
2401 if (!(status & adapter->int_vec_bit))
2402 return IRQ_NONE;
2404 /* check interrupt state machine, to be sure */
2405 status = readl(adapter->crb_int_state_reg);
2406 if (!ISR_LEGACY_INT_TRIGGERED(status))
2407 return IRQ_NONE;
2409 writel(0xffffffff, adapter->tgt_status_reg);
2410 /* read twice to ensure write is flushed */
2411 readl(adapter->isr_int_vec);
2412 readl(adapter->isr_int_vec);
2414 return IRQ_HANDLED;
2417 static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2419 struct qlcnic_host_sds_ring *sds_ring = data;
2420 struct qlcnic_adapter *adapter = sds_ring->adapter;
2422 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2423 goto done;
2424 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2425 writel(0xffffffff, adapter->tgt_status_reg);
2426 goto done;
2429 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2430 return IRQ_NONE;
2432 done:
2433 adapter->diag_cnt++;
2434 qlcnic_enable_int(sds_ring);
2435 return IRQ_HANDLED;
2438 static irqreturn_t qlcnic_intr(int irq, void *data)
2440 struct qlcnic_host_sds_ring *sds_ring = data;
2441 struct qlcnic_adapter *adapter = sds_ring->adapter;
2443 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2444 return IRQ_NONE;
2446 napi_schedule(&sds_ring->napi);
2448 return IRQ_HANDLED;
2451 static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2453 struct qlcnic_host_sds_ring *sds_ring = data;
2454 struct qlcnic_adapter *adapter = sds_ring->adapter;
2456 /* clear interrupt */
2457 writel(0xffffffff, adapter->tgt_status_reg);
2459 napi_schedule(&sds_ring->napi);
2460 return IRQ_HANDLED;
2463 static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2465 struct qlcnic_host_sds_ring *sds_ring = data;
2467 napi_schedule(&sds_ring->napi);
2468 return IRQ_HANDLED;
2471 static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2473 u32 sw_consumer, hw_consumer;
2474 int count = 0, i;
2475 struct qlcnic_cmd_buffer *buffer;
2476 struct pci_dev *pdev = adapter->pdev;
2477 struct net_device *netdev = adapter->netdev;
2478 struct qlcnic_skb_frag *frag;
2479 int done;
2480 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2482 if (!spin_trylock(&adapter->tx_clean_lock))
2483 return 1;
2485 sw_consumer = tx_ring->sw_consumer;
2486 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2488 while (sw_consumer != hw_consumer) {
2489 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2490 if (buffer->skb) {
2491 frag = &buffer->frag_array[0];
2492 pci_unmap_single(pdev, frag->dma, frag->length,
2493 PCI_DMA_TODEVICE);
2494 frag->dma = 0ULL;
2495 for (i = 1; i < buffer->frag_count; i++) {
2496 frag++;
2497 pci_unmap_page(pdev, frag->dma, frag->length,
2498 PCI_DMA_TODEVICE);
2499 frag->dma = 0ULL;
2502 adapter->stats.xmitfinished++;
2503 dev_kfree_skb_any(buffer->skb);
2504 buffer->skb = NULL;
2507 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2508 if (++count >= MAX_STATUS_HANDLE)
2509 break;
2512 if (count && netif_running(netdev)) {
2513 tx_ring->sw_consumer = sw_consumer;
2515 smp_mb();
2517 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
2518 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2519 netif_wake_queue(netdev);
2520 adapter->stats.xmit_on++;
2523 adapter->tx_timeo_cnt = 0;
2526 * If everything is freed up to consumer then check if the ring is full
2527 * If the ring is full then check if more needs to be freed and
2528 * schedule the call back again.
2530 * This happens when there are 2 CPUs. One could be freeing and the
2531 * other filling it. If the ring is full when we get out of here and
2532 * the card has already interrupted the host then the host can miss the
2533 * interrupt.
2535 * There is still a possible race condition and the host could miss an
2536 * interrupt. The card has to take care of this.
2538 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2539 done = (sw_consumer == hw_consumer);
2540 spin_unlock(&adapter->tx_clean_lock);
2542 return done;
2545 static int qlcnic_poll(struct napi_struct *napi, int budget)
2547 struct qlcnic_host_sds_ring *sds_ring =
2548 container_of(napi, struct qlcnic_host_sds_ring, napi);
2550 struct qlcnic_adapter *adapter = sds_ring->adapter;
2552 int tx_complete;
2553 int work_done;
2555 tx_complete = qlcnic_process_cmd_ring(adapter);
2557 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2559 if ((work_done < budget) && tx_complete) {
2560 napi_complete(&sds_ring->napi);
2561 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2562 qlcnic_enable_int(sds_ring);
2565 return work_done;
2568 static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2570 struct qlcnic_host_sds_ring *sds_ring =
2571 container_of(napi, struct qlcnic_host_sds_ring, napi);
2573 struct qlcnic_adapter *adapter = sds_ring->adapter;
2574 int work_done;
2576 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2578 if (work_done < budget) {
2579 napi_complete(&sds_ring->napi);
2580 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2581 qlcnic_enable_int(sds_ring);
2584 return work_done;
2587 #ifdef CONFIG_NET_POLL_CONTROLLER
2588 static void qlcnic_poll_controller(struct net_device *netdev)
2590 int ring;
2591 struct qlcnic_host_sds_ring *sds_ring;
2592 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2593 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2595 disable_irq(adapter->irq);
2596 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2597 sds_ring = &recv_ctx->sds_rings[ring];
2598 qlcnic_intr(adapter->irq, sds_ring);
2600 enable_irq(adapter->irq);
2602 #endif
2604 static void
2605 qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2607 u32 val;
2609 val = adapter->portnum & 0xf;
2610 val |= encoding << 7;
2611 val |= (jiffies - adapter->dev_rst_time) << 8;
2613 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2614 adapter->dev_rst_time = jiffies;
2617 static int
2618 qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
2620 u32 val;
2622 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2623 state != QLCNIC_DEV_NEED_QUISCENT);
2625 if (qlcnic_api_lock(adapter))
2626 return -EIO;
2628 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2630 if (state == QLCNIC_DEV_NEED_RESET)
2631 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2632 else if (state == QLCNIC_DEV_NEED_QUISCENT)
2633 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
2635 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2637 qlcnic_api_unlock(adapter);
2639 return 0;
2642 static int
2643 qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2645 u32 val;
2647 if (qlcnic_api_lock(adapter))
2648 return -EBUSY;
2650 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2651 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
2652 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2654 qlcnic_api_unlock(adapter);
2656 return 0;
2659 static void
2660 qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
2662 u32 val;
2664 if (qlcnic_api_lock(adapter))
2665 goto err;
2667 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2668 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
2669 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
2671 if (failed) {
2672 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2673 dev_info(&adapter->pdev->dev,
2674 "Device state set to Failed. Please Reboot\n");
2675 } else if (!(val & 0x11111111))
2676 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2678 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2679 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
2680 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2682 qlcnic_api_unlock(adapter);
2683 err:
2684 adapter->fw_fail_cnt = 0;
2685 clear_bit(__QLCNIC_START_FW, &adapter->state);
2686 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2689 /* Grab api lock, before checking state */
2690 static int
2691 qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2693 int act, state, active_mask;
2695 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2696 act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2698 if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
2699 active_mask = (~(1 << (adapter->ahw->pci_func * 4)));
2700 act = act & active_mask;
2703 if (((state & 0x11111111) == (act & 0x11111111)) ||
2704 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2705 return 0;
2706 else
2707 return 1;
2710 static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2712 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2714 if (val != QLCNIC_DRV_IDC_VER) {
2715 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2716 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2719 return 0;
2722 static int
2723 qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2725 u32 val, prev_state;
2726 u8 dev_init_timeo = adapter->dev_init_timeo;
2727 u8 portnum = adapter->portnum;
2728 u8 ret;
2730 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2731 return 1;
2733 if (qlcnic_api_lock(adapter))
2734 return -1;
2736 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2737 if (!(val & (1 << (portnum * 4)))) {
2738 QLC_DEV_SET_REF_CNT(val, portnum);
2739 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
2742 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2743 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
2745 switch (prev_state) {
2746 case QLCNIC_DEV_COLD:
2747 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
2748 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
2749 qlcnic_idc_debug_info(adapter, 0);
2750 qlcnic_api_unlock(adapter);
2751 return 1;
2753 case QLCNIC_DEV_READY:
2754 ret = qlcnic_check_idc_ver(adapter);
2755 qlcnic_api_unlock(adapter);
2756 return ret;
2758 case QLCNIC_DEV_NEED_RESET:
2759 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2760 QLC_DEV_SET_RST_RDY(val, portnum);
2761 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2762 break;
2764 case QLCNIC_DEV_NEED_QUISCENT:
2765 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2766 QLC_DEV_SET_QSCNT_RDY(val, portnum);
2767 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2768 break;
2770 case QLCNIC_DEV_FAILED:
2771 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
2772 qlcnic_api_unlock(adapter);
2773 return -1;
2775 case QLCNIC_DEV_INITIALIZING:
2776 case QLCNIC_DEV_QUISCENT:
2777 break;
2780 qlcnic_api_unlock(adapter);
2782 do {
2783 msleep(1000);
2784 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2786 if (prev_state == QLCNIC_DEV_QUISCENT)
2787 continue;
2788 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
2790 if (!dev_init_timeo) {
2791 dev_err(&adapter->pdev->dev,
2792 "Waiting for device to initialize timeout\n");
2793 return -1;
2796 if (qlcnic_api_lock(adapter))
2797 return -1;
2799 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2800 QLC_DEV_CLR_RST_QSCNT(val, portnum);
2801 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2803 ret = qlcnic_check_idc_ver(adapter);
2804 qlcnic_api_unlock(adapter);
2806 return ret;
2809 static void
2810 qlcnic_fwinit_work(struct work_struct *work)
2812 struct qlcnic_adapter *adapter = container_of(work,
2813 struct qlcnic_adapter, fw_work.work);
2814 u32 dev_state = 0xf;
2815 u32 val;
2817 if (qlcnic_api_lock(adapter))
2818 goto err_ret;
2820 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2821 if (dev_state == QLCNIC_DEV_QUISCENT ||
2822 dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2823 qlcnic_api_unlock(adapter);
2824 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2825 FW_POLL_DELAY * 2);
2826 return;
2829 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
2830 qlcnic_api_unlock(adapter);
2831 goto wait_npar;
2834 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2835 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2836 adapter->reset_ack_timeo);
2837 goto skip_ack_check;
2840 if (!qlcnic_check_drv_state(adapter)) {
2841 skip_ack_check:
2842 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2844 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2845 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2846 QLCNIC_DEV_INITIALIZING);
2847 set_bit(__QLCNIC_START_FW, &adapter->state);
2848 QLCDB(adapter, DRV, "Restarting fw\n");
2849 qlcnic_idc_debug_info(adapter, 0);
2850 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2851 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2852 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2855 qlcnic_api_unlock(adapter);
2857 rtnl_lock();
2858 if (adapter->ahw->fw_dump.enable &&
2859 (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
2860 QLCDB(adapter, DRV, "Take FW dump\n");
2861 qlcnic_dump_fw(adapter);
2863 rtnl_unlock();
2865 adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
2866 if (!adapter->nic_ops->start_firmware(adapter)) {
2867 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2868 adapter->fw_wait_cnt = 0;
2869 return;
2871 goto err_ret;
2874 qlcnic_api_unlock(adapter);
2876 wait_npar:
2877 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2878 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
2880 switch (dev_state) {
2881 case QLCNIC_DEV_READY:
2882 if (!adapter->nic_ops->start_firmware(adapter)) {
2883 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2884 adapter->fw_wait_cnt = 0;
2885 return;
2887 case QLCNIC_DEV_FAILED:
2888 break;
2889 default:
2890 qlcnic_schedule_work(adapter,
2891 qlcnic_fwinit_work, FW_POLL_DELAY);
2892 return;
2895 err_ret:
2896 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2897 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
2898 netif_device_attach(adapter->netdev);
2899 qlcnic_clr_all_drv_state(adapter, 0);
2902 static void
2903 qlcnic_detach_work(struct work_struct *work)
2905 struct qlcnic_adapter *adapter = container_of(work,
2906 struct qlcnic_adapter, fw_work.work);
2907 struct net_device *netdev = adapter->netdev;
2908 u32 status;
2910 netif_device_detach(netdev);
2912 /* Dont grab rtnl lock during Quiscent mode */
2913 if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2914 if (netif_running(netdev))
2915 __qlcnic_down(adapter, netdev);
2916 } else
2917 qlcnic_down(adapter, netdev);
2919 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2921 if (status & QLCNIC_RCODE_FATAL_ERROR)
2922 goto err_ret;
2924 if (adapter->temp == QLCNIC_TEMP_PANIC)
2925 goto err_ret;
2926 /* Dont ack if this instance is the reset owner */
2927 if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
2928 if (qlcnic_set_drv_state(adapter, adapter->dev_state))
2929 goto err_ret;
2932 adapter->fw_wait_cnt = 0;
2934 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2936 return;
2938 err_ret:
2939 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2940 status, adapter->temp);
2941 netif_device_attach(netdev);
2942 qlcnic_clr_all_drv_state(adapter, 1);
2945 /*Transit NPAR state to NON Operational */
2946 static void
2947 qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
2949 u32 state;
2951 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2952 if (state == QLCNIC_DEV_NPAR_NON_OPER)
2953 return;
2955 if (qlcnic_api_lock(adapter))
2956 return;
2957 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
2958 qlcnic_api_unlock(adapter);
2961 /*Transit to RESET state from READY state only */
2962 void
2963 qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2965 u32 state;
2967 adapter->need_fw_reset = 1;
2968 if (qlcnic_api_lock(adapter))
2969 return;
2971 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2973 if (state == QLCNIC_DEV_READY) {
2974 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
2975 adapter->flags |= QLCNIC_FW_RESET_OWNER;
2976 QLCDB(adapter, DRV, "NEED_RESET state set\n");
2977 qlcnic_idc_debug_info(adapter, 0);
2980 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
2981 qlcnic_api_unlock(adapter);
2984 /* Transit to NPAR READY state from NPAR NOT READY state */
2985 static void
2986 qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
2988 if (qlcnic_api_lock(adapter))
2989 return;
2991 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
2992 QLCDB(adapter, DRV, "NPAR operational state set\n");
2994 qlcnic_api_unlock(adapter);
2997 static void
2998 qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2999 work_func_t func, int delay)
3001 if (test_bit(__QLCNIC_AER, &adapter->state))
3002 return;
3004 INIT_DELAYED_WORK(&adapter->fw_work, func);
3005 queue_delayed_work(qlcnic_wq, &adapter->fw_work,
3006 round_jiffies_relative(delay));
3009 static void
3010 qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
3012 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3013 msleep(10);
3015 cancel_delayed_work_sync(&adapter->fw_work);
3018 static void
3019 qlcnic_attach_work(struct work_struct *work)
3021 struct qlcnic_adapter *adapter = container_of(work,
3022 struct qlcnic_adapter, fw_work.work);
3023 struct net_device *netdev = adapter->netdev;
3024 u32 npar_state;
3026 if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
3027 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3028 if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3029 qlcnic_clr_all_drv_state(adapter, 0);
3030 else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3031 qlcnic_schedule_work(adapter, qlcnic_attach_work,
3032 FW_POLL_DELAY);
3033 else
3034 goto attach;
3035 QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3036 return;
3038 attach:
3039 if (netif_running(netdev)) {
3040 if (qlcnic_up(adapter, netdev))
3041 goto done;
3043 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3046 done:
3047 netif_device_attach(netdev);
3048 adapter->fw_fail_cnt = 0;
3049 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3051 if (!qlcnic_clr_drv_state(adapter))
3052 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3053 FW_POLL_DELAY);
3056 static int
3057 qlcnic_check_health(struct qlcnic_adapter *adapter)
3059 u32 state = 0, heartbeat;
3060 struct net_device *netdev = adapter->netdev;
3062 if (qlcnic_check_temp(adapter))
3063 goto detach;
3065 if (adapter->need_fw_reset)
3066 qlcnic_dev_request_reset(adapter);
3068 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
3069 if (state == QLCNIC_DEV_NEED_RESET) {
3070 qlcnic_set_npar_non_operational(adapter);
3071 adapter->need_fw_reset = 1;
3072 } else if (state == QLCNIC_DEV_NEED_QUISCENT)
3073 goto detach;
3075 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3076 if (heartbeat != adapter->heartbeat) {
3077 adapter->heartbeat = heartbeat;
3078 adapter->fw_fail_cnt = 0;
3079 if (adapter->need_fw_reset)
3080 goto detach;
3082 if (adapter->reset_context && auto_fw_reset) {
3083 qlcnic_reset_hw_context(adapter);
3084 adapter->netdev->trans_start = jiffies;
3087 return 0;
3090 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3091 return 0;
3093 qlcnic_dev_request_reset(adapter);
3095 if (auto_fw_reset)
3096 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
3098 dev_info(&netdev->dev, "firmware hang detected\n");
3100 detach:
3101 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3102 QLCNIC_DEV_NEED_RESET;
3104 if (auto_fw_reset &&
3105 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3107 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
3108 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3111 return 1;
3114 static void
3115 qlcnic_fw_poll_work(struct work_struct *work)
3117 struct qlcnic_adapter *adapter = container_of(work,
3118 struct qlcnic_adapter, fw_work.work);
3120 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3121 goto reschedule;
3124 if (qlcnic_check_health(adapter))
3125 return;
3127 if (adapter->fhash.fnum)
3128 qlcnic_prune_lb_filters(adapter);
3130 reschedule:
3131 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3134 static int qlcnic_is_first_func(struct pci_dev *pdev)
3136 struct pci_dev *oth_pdev;
3137 int val = pdev->devfn;
3139 while (val-- > 0) {
3140 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3141 (pdev->bus), pdev->bus->number,
3142 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
3143 if (!oth_pdev)
3144 continue;
3146 if (oth_pdev->current_state != PCI_D3cold) {
3147 pci_dev_put(oth_pdev);
3148 return 0;
3150 pci_dev_put(oth_pdev);
3152 return 1;
3155 static int qlcnic_attach_func(struct pci_dev *pdev)
3157 int err, first_func;
3158 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3159 struct net_device *netdev = adapter->netdev;
3161 pdev->error_state = pci_channel_io_normal;
3163 err = pci_enable_device(pdev);
3164 if (err)
3165 return err;
3167 pci_set_power_state(pdev, PCI_D0);
3168 pci_set_master(pdev);
3169 pci_restore_state(pdev);
3171 first_func = qlcnic_is_first_func(pdev);
3173 if (qlcnic_api_lock(adapter))
3174 return -EINVAL;
3176 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
3177 adapter->need_fw_reset = 1;
3178 set_bit(__QLCNIC_START_FW, &adapter->state);
3179 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3180 QLCDB(adapter, DRV, "Restarting fw\n");
3182 qlcnic_api_unlock(adapter);
3184 err = adapter->nic_ops->start_firmware(adapter);
3185 if (err)
3186 return err;
3188 qlcnic_clr_drv_state(adapter);
3189 qlcnic_setup_intr(adapter);
3191 if (netif_running(netdev)) {
3192 err = qlcnic_attach(adapter);
3193 if (err) {
3194 qlcnic_clr_all_drv_state(adapter, 1);
3195 clear_bit(__QLCNIC_AER, &adapter->state);
3196 netif_device_attach(netdev);
3197 return err;
3200 err = qlcnic_up(adapter, netdev);
3201 if (err)
3202 goto done;
3204 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3206 done:
3207 netif_device_attach(netdev);
3208 return err;
3211 static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3212 pci_channel_state_t state)
3214 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3215 struct net_device *netdev = adapter->netdev;
3217 if (state == pci_channel_io_perm_failure)
3218 return PCI_ERS_RESULT_DISCONNECT;
3220 if (state == pci_channel_io_normal)
3221 return PCI_ERS_RESULT_RECOVERED;
3223 set_bit(__QLCNIC_AER, &adapter->state);
3224 netif_device_detach(netdev);
3226 cancel_delayed_work_sync(&adapter->fw_work);
3228 if (netif_running(netdev))
3229 qlcnic_down(adapter, netdev);
3231 qlcnic_detach(adapter);
3232 qlcnic_teardown_intr(adapter);
3234 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3236 pci_save_state(pdev);
3237 pci_disable_device(pdev);
3239 return PCI_ERS_RESULT_NEED_RESET;
3242 static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3244 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3245 PCI_ERS_RESULT_RECOVERED;
3248 static void qlcnic_io_resume(struct pci_dev *pdev)
3250 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3252 pci_cleanup_aer_uncorrect_error_status(pdev);
3254 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3255 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3256 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3257 FW_POLL_DELAY);
3260 static int
3261 qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3263 int err;
3265 err = qlcnic_can_start_firmware(adapter);
3266 if (err)
3267 return err;
3269 err = qlcnic_check_npar_opertional(adapter);
3270 if (err)
3271 return err;
3273 err = qlcnic_initialize_nic(adapter);
3274 if (err)
3275 return err;
3277 qlcnic_check_options(adapter);
3279 err = qlcnic_set_eswitch_port_config(adapter);
3280 if (err)
3281 return err;
3283 adapter->need_fw_reset = 0;
3285 return err;
3288 static int
3289 qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3291 return -EOPNOTSUPP;
3294 static int
3295 qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3297 return -EOPNOTSUPP;
3300 static ssize_t
3301 qlcnic_store_bridged_mode(struct device *dev,
3302 struct device_attribute *attr, const char *buf, size_t len)
3304 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3305 unsigned long new;
3306 int ret = -EINVAL;
3308 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3309 goto err_out;
3311 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
3312 goto err_out;
3314 if (strict_strtoul(buf, 2, &new))
3315 goto err_out;
3317 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
3318 ret = len;
3320 err_out:
3321 return ret;
3324 static ssize_t
3325 qlcnic_show_bridged_mode(struct device *dev,
3326 struct device_attribute *attr, char *buf)
3328 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3329 int bridged_mode = 0;
3331 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3332 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3334 return sprintf(buf, "%d\n", bridged_mode);
3337 static struct device_attribute dev_attr_bridged_mode = {
3338 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3339 .show = qlcnic_show_bridged_mode,
3340 .store = qlcnic_store_bridged_mode,
3343 static ssize_t
3344 qlcnic_store_diag_mode(struct device *dev,
3345 struct device_attribute *attr, const char *buf, size_t len)
3347 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3348 unsigned long new;
3350 if (strict_strtoul(buf, 2, &new))
3351 return -EINVAL;
3353 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3354 adapter->flags ^= QLCNIC_DIAG_ENABLED;
3356 return len;
3359 static ssize_t
3360 qlcnic_show_diag_mode(struct device *dev,
3361 struct device_attribute *attr, char *buf)
3363 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3365 return sprintf(buf, "%d\n",
3366 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
3369 static struct device_attribute dev_attr_diag_mode = {
3370 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3371 .show = qlcnic_show_diag_mode,
3372 .store = qlcnic_store_diag_mode,
3375 int qlcnic_validate_max_rss(struct net_device *netdev, u8 max_hw, u8 val)
3377 if (!use_msi_x && !use_msi) {
3378 netdev_info(netdev, "no msix or msi support, hence no rss\n");
3379 return -EINVAL;
3382 if ((val > max_hw) || (val < 2) || !is_power_of_2(val)) {
3383 netdev_info(netdev, "rss_ring valid range [2 - %x] in "
3384 " powers of 2\n", max_hw);
3385 return -EINVAL;
3387 return 0;
3391 int qlcnic_set_max_rss(struct qlcnic_adapter *adapter, u8 data)
3393 struct net_device *netdev = adapter->netdev;
3394 int err = 0;
3396 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3397 return -EBUSY;
3399 netif_device_detach(netdev);
3400 if (netif_running(netdev))
3401 __qlcnic_down(adapter, netdev);
3402 qlcnic_detach(adapter);
3403 qlcnic_teardown_intr(adapter);
3405 if (qlcnic_enable_msix(adapter, data)) {
3406 netdev_info(netdev, "failed setting max_rss; rss disabled\n");
3407 qlcnic_enable_msi_legacy(adapter);
3410 if (netif_running(netdev)) {
3411 err = qlcnic_attach(adapter);
3412 if (err)
3413 goto done;
3414 err = __qlcnic_up(adapter, netdev);
3415 if (err)
3416 goto done;
3417 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3419 done:
3420 netif_device_attach(netdev);
3421 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3422 return err;
3425 static int
3426 qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3427 loff_t offset, size_t size)
3429 size_t crb_size = 4;
3431 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3432 return -EIO;
3434 if (offset < QLCNIC_PCI_CRBSPACE) {
3435 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3436 QLCNIC_PCI_CAMQM_END))
3437 crb_size = 8;
3438 else
3439 return -EINVAL;
3442 if ((size != crb_size) || (offset & (crb_size-1)))
3443 return -EINVAL;
3445 return 0;
3448 static ssize_t
3449 qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3450 struct bin_attribute *attr,
3451 char *buf, loff_t offset, size_t size)
3453 struct device *dev = container_of(kobj, struct device, kobj);
3454 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3455 u32 data;
3456 u64 qmdata;
3457 int ret;
3459 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3460 if (ret != 0)
3461 return ret;
3463 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3464 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3465 memcpy(buf, &qmdata, size);
3466 } else {
3467 data = QLCRD32(adapter, offset);
3468 memcpy(buf, &data, size);
3470 return size;
3473 static ssize_t
3474 qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3475 struct bin_attribute *attr,
3476 char *buf, loff_t offset, size_t size)
3478 struct device *dev = container_of(kobj, struct device, kobj);
3479 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3480 u32 data;
3481 u64 qmdata;
3482 int ret;
3484 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3485 if (ret != 0)
3486 return ret;
3488 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3489 memcpy(&qmdata, buf, size);
3490 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3491 } else {
3492 memcpy(&data, buf, size);
3493 QLCWR32(adapter, offset, data);
3495 return size;
3498 static int
3499 qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3500 loff_t offset, size_t size)
3502 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3503 return -EIO;
3505 if ((size != 8) || (offset & 0x7))
3506 return -EIO;
3508 return 0;
3511 static ssize_t
3512 qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3513 struct bin_attribute *attr,
3514 char *buf, loff_t offset, size_t size)
3516 struct device *dev = container_of(kobj, struct device, kobj);
3517 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3518 u64 data;
3519 int ret;
3521 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3522 if (ret != 0)
3523 return ret;
3525 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3526 return -EIO;
3528 memcpy(buf, &data, size);
3530 return size;
3533 static ssize_t
3534 qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3535 struct bin_attribute *attr,
3536 char *buf, loff_t offset, size_t size)
3538 struct device *dev = container_of(kobj, struct device, kobj);
3539 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3540 u64 data;
3541 int ret;
3543 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3544 if (ret != 0)
3545 return ret;
3547 memcpy(&data, buf, size);
3549 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3550 return -EIO;
3552 return size;
3555 static struct bin_attribute bin_attr_crb = {
3556 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3557 .size = 0,
3558 .read = qlcnic_sysfs_read_crb,
3559 .write = qlcnic_sysfs_write_crb,
3562 static struct bin_attribute bin_attr_mem = {
3563 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3564 .size = 0,
3565 .read = qlcnic_sysfs_read_mem,
3566 .write = qlcnic_sysfs_write_mem,
3569 static int
3570 validate_pm_config(struct qlcnic_adapter *adapter,
3571 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3574 u8 src_pci_func, s_esw_id, d_esw_id;
3575 u8 dest_pci_func;
3576 int i;
3578 for (i = 0; i < count; i++) {
3579 src_pci_func = pm_cfg[i].pci_func;
3580 dest_pci_func = pm_cfg[i].dest_npar;
3581 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3582 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3583 return QL_STATUS_INVALID_PARAM;
3585 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3586 return QL_STATUS_INVALID_PARAM;
3588 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3589 return QL_STATUS_INVALID_PARAM;
3591 s_esw_id = adapter->npars[src_pci_func].phy_port;
3592 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3594 if (s_esw_id != d_esw_id)
3595 return QL_STATUS_INVALID_PARAM;
3598 return 0;
3602 static ssize_t
3603 qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3604 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3606 struct device *dev = container_of(kobj, struct device, kobj);
3607 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3608 struct qlcnic_pm_func_cfg *pm_cfg;
3609 u32 id, action, pci_func;
3610 int count, rem, i, ret;
3612 count = size / sizeof(struct qlcnic_pm_func_cfg);
3613 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3614 if (rem)
3615 return QL_STATUS_INVALID_PARAM;
3617 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3619 ret = validate_pm_config(adapter, pm_cfg, count);
3620 if (ret)
3621 return ret;
3622 for (i = 0; i < count; i++) {
3623 pci_func = pm_cfg[i].pci_func;
3624 action = !!pm_cfg[i].action;
3625 id = adapter->npars[pci_func].phy_port;
3626 ret = qlcnic_config_port_mirroring(adapter, id,
3627 action, pci_func);
3628 if (ret)
3629 return ret;
3632 for (i = 0; i < count; i++) {
3633 pci_func = pm_cfg[i].pci_func;
3634 id = adapter->npars[pci_func].phy_port;
3635 adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
3636 adapter->npars[pci_func].dest_npar = id;
3638 return size;
3641 static ssize_t
3642 qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3643 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3645 struct device *dev = container_of(kobj, struct device, kobj);
3646 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3647 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3648 int i;
3650 if (size != sizeof(pm_cfg))
3651 return QL_STATUS_INVALID_PARAM;
3653 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3654 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3655 continue;
3656 pm_cfg[i].action = adapter->npars[i].enable_pm;
3657 pm_cfg[i].dest_npar = 0;
3658 pm_cfg[i].pci_func = i;
3660 memcpy(buf, &pm_cfg, size);
3662 return size;
3665 static int
3666 validate_esw_config(struct qlcnic_adapter *adapter,
3667 struct qlcnic_esw_func_cfg *esw_cfg, int count)
3669 u32 op_mode;
3670 u8 pci_func;
3671 int i;
3673 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
3675 for (i = 0; i < count; i++) {
3676 pci_func = esw_cfg[i].pci_func;
3677 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3678 return QL_STATUS_INVALID_PARAM;
3680 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3681 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3682 return QL_STATUS_INVALID_PARAM;
3684 switch (esw_cfg[i].op_mode) {
3685 case QLCNIC_PORT_DEFAULTS:
3686 if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
3687 QLCNIC_NON_PRIV_FUNC) {
3688 if (esw_cfg[i].mac_anti_spoof != 0)
3689 return QL_STATUS_INVALID_PARAM;
3690 if (esw_cfg[i].mac_override != 1)
3691 return QL_STATUS_INVALID_PARAM;
3692 if (esw_cfg[i].promisc_mode != 1)
3693 return QL_STATUS_INVALID_PARAM;
3695 break;
3696 case QLCNIC_ADD_VLAN:
3697 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3698 return QL_STATUS_INVALID_PARAM;
3699 if (!esw_cfg[i].op_type)
3700 return QL_STATUS_INVALID_PARAM;
3701 break;
3702 case QLCNIC_DEL_VLAN:
3703 if (!esw_cfg[i].op_type)
3704 return QL_STATUS_INVALID_PARAM;
3705 break;
3706 default:
3707 return QL_STATUS_INVALID_PARAM;
3710 return 0;
3713 static ssize_t
3714 qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3715 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3717 struct device *dev = container_of(kobj, struct device, kobj);
3718 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3719 struct qlcnic_esw_func_cfg *esw_cfg;
3720 struct qlcnic_npar_info *npar;
3721 int count, rem, i, ret;
3722 u8 pci_func, op_mode = 0;
3724 count = size / sizeof(struct qlcnic_esw_func_cfg);
3725 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3726 if (rem)
3727 return QL_STATUS_INVALID_PARAM;
3729 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3730 ret = validate_esw_config(adapter, esw_cfg, count);
3731 if (ret)
3732 return ret;
3734 for (i = 0; i < count; i++) {
3735 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3736 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3737 return QL_STATUS_INVALID_PARAM;
3739 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
3740 continue;
3742 op_mode = esw_cfg[i].op_mode;
3743 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3744 esw_cfg[i].op_mode = op_mode;
3745 esw_cfg[i].pci_func = adapter->ahw->pci_func;
3747 switch (esw_cfg[i].op_mode) {
3748 case QLCNIC_PORT_DEFAULTS:
3749 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3750 break;
3751 case QLCNIC_ADD_VLAN:
3752 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3753 break;
3754 case QLCNIC_DEL_VLAN:
3755 esw_cfg[i].vlan_id = 0;
3756 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3757 break;
3761 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3762 goto out;
3764 for (i = 0; i < count; i++) {
3765 pci_func = esw_cfg[i].pci_func;
3766 npar = &adapter->npars[pci_func];
3767 switch (esw_cfg[i].op_mode) {
3768 case QLCNIC_PORT_DEFAULTS:
3769 npar->promisc_mode = esw_cfg[i].promisc_mode;
3770 npar->mac_override = esw_cfg[i].mac_override;
3771 npar->offload_flags = esw_cfg[i].offload_flags;
3772 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3773 npar->discard_tagged = esw_cfg[i].discard_tagged;
3774 break;
3775 case QLCNIC_ADD_VLAN:
3776 npar->pvid = esw_cfg[i].vlan_id;
3777 break;
3778 case QLCNIC_DEL_VLAN:
3779 npar->pvid = 0;
3780 break;
3783 out:
3784 return size;
3787 static ssize_t
3788 qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3789 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3791 struct device *dev = container_of(kobj, struct device, kobj);
3792 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3793 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
3794 u8 i;
3796 if (size != sizeof(esw_cfg))
3797 return QL_STATUS_INVALID_PARAM;
3799 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3800 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3801 continue;
3802 esw_cfg[i].pci_func = i;
3803 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
3804 return QL_STATUS_INVALID_PARAM;
3806 memcpy(buf, &esw_cfg, size);
3808 return size;
3811 static int
3812 validate_npar_config(struct qlcnic_adapter *adapter,
3813 struct qlcnic_npar_func_cfg *np_cfg, int count)
3815 u8 pci_func, i;
3817 for (i = 0; i < count; i++) {
3818 pci_func = np_cfg[i].pci_func;
3819 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3820 return QL_STATUS_INVALID_PARAM;
3822 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3823 return QL_STATUS_INVALID_PARAM;
3825 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
3826 !IS_VALID_BW(np_cfg[i].max_bw))
3827 return QL_STATUS_INVALID_PARAM;
3829 return 0;
3832 static ssize_t
3833 qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
3834 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3836 struct device *dev = container_of(kobj, struct device, kobj);
3837 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3838 struct qlcnic_info nic_info;
3839 struct qlcnic_npar_func_cfg *np_cfg;
3840 int i, count, rem, ret;
3841 u8 pci_func;
3843 count = size / sizeof(struct qlcnic_npar_func_cfg);
3844 rem = size % sizeof(struct qlcnic_npar_func_cfg);
3845 if (rem)
3846 return QL_STATUS_INVALID_PARAM;
3848 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
3849 ret = validate_npar_config(adapter, np_cfg, count);
3850 if (ret)
3851 return ret;
3853 for (i = 0; i < count ; i++) {
3854 pci_func = np_cfg[i].pci_func;
3855 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
3856 if (ret)
3857 return ret;
3858 nic_info.pci_func = pci_func;
3859 nic_info.min_tx_bw = np_cfg[i].min_bw;
3860 nic_info.max_tx_bw = np_cfg[i].max_bw;
3861 ret = qlcnic_set_nic_info(adapter, &nic_info);
3862 if (ret)
3863 return ret;
3864 adapter->npars[i].min_bw = nic_info.min_tx_bw;
3865 adapter->npars[i].max_bw = nic_info.max_tx_bw;
3868 return size;
3871 static ssize_t
3872 qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
3873 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3875 struct device *dev = container_of(kobj, struct device, kobj);
3876 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3877 struct qlcnic_info nic_info;
3878 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
3879 int i, ret;
3881 if (size != sizeof(np_cfg))
3882 return QL_STATUS_INVALID_PARAM;
3884 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3885 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3886 continue;
3887 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
3888 if (ret)
3889 return ret;
3891 np_cfg[i].pci_func = i;
3892 np_cfg[i].op_mode = (u8)nic_info.op_mode;
3893 np_cfg[i].port_num = nic_info.phys_port;
3894 np_cfg[i].fw_capab = nic_info.capabilities;
3895 np_cfg[i].min_bw = nic_info.min_tx_bw ;
3896 np_cfg[i].max_bw = nic_info.max_tx_bw;
3897 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
3898 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
3900 memcpy(buf, &np_cfg, size);
3901 return size;
3904 static ssize_t
3905 qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
3906 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3908 struct device *dev = container_of(kobj, struct device, kobj);
3909 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3910 struct qlcnic_esw_statistics port_stats;
3911 int ret;
3913 if (size != sizeof(struct qlcnic_esw_statistics))
3914 return QL_STATUS_INVALID_PARAM;
3916 if (offset >= QLCNIC_MAX_PCI_FUNC)
3917 return QL_STATUS_INVALID_PARAM;
3919 memset(&port_stats, 0, size);
3920 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3921 &port_stats.rx);
3922 if (ret)
3923 return ret;
3925 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3926 &port_stats.tx);
3927 if (ret)
3928 return ret;
3930 memcpy(buf, &port_stats, size);
3931 return size;
3934 static ssize_t
3935 qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
3936 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3938 struct device *dev = container_of(kobj, struct device, kobj);
3939 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3940 struct qlcnic_esw_statistics esw_stats;
3941 int ret;
3943 if (size != sizeof(struct qlcnic_esw_statistics))
3944 return QL_STATUS_INVALID_PARAM;
3946 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3947 return QL_STATUS_INVALID_PARAM;
3949 memset(&esw_stats, 0, size);
3950 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3951 &esw_stats.rx);
3952 if (ret)
3953 return ret;
3955 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3956 &esw_stats.tx);
3957 if (ret)
3958 return ret;
3960 memcpy(buf, &esw_stats, size);
3961 return size;
3964 static ssize_t
3965 qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
3966 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3968 struct device *dev = container_of(kobj, struct device, kobj);
3969 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3970 int ret;
3972 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3973 return QL_STATUS_INVALID_PARAM;
3975 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3976 QLCNIC_QUERY_RX_COUNTER);
3977 if (ret)
3978 return ret;
3980 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3981 QLCNIC_QUERY_TX_COUNTER);
3982 if (ret)
3983 return ret;
3985 return size;
3988 static ssize_t
3989 qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
3990 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3993 struct device *dev = container_of(kobj, struct device, kobj);
3994 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3995 int ret;
3997 if (offset >= QLCNIC_MAX_PCI_FUNC)
3998 return QL_STATUS_INVALID_PARAM;
4000 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4001 QLCNIC_QUERY_RX_COUNTER);
4002 if (ret)
4003 return ret;
4005 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4006 QLCNIC_QUERY_TX_COUNTER);
4007 if (ret)
4008 return ret;
4010 return size;
4013 static ssize_t
4014 qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
4015 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4017 struct device *dev = container_of(kobj, struct device, kobj);
4018 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4019 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
4020 struct qlcnic_pci_info *pci_info;
4021 int i, ret;
4023 if (size != sizeof(pci_cfg))
4024 return QL_STATUS_INVALID_PARAM;
4026 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
4027 if (!pci_info)
4028 return -ENOMEM;
4030 ret = qlcnic_get_pci_info(adapter, pci_info);
4031 if (ret) {
4032 kfree(pci_info);
4033 return ret;
4036 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4037 pci_cfg[i].pci_func = pci_info[i].id;
4038 pci_cfg[i].func_type = pci_info[i].type;
4039 pci_cfg[i].port_num = pci_info[i].default_port;
4040 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
4041 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
4042 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
4044 memcpy(buf, &pci_cfg, size);
4045 kfree(pci_info);
4046 return size;
4048 static struct bin_attribute bin_attr_npar_config = {
4049 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
4050 .size = 0,
4051 .read = qlcnic_sysfs_read_npar_config,
4052 .write = qlcnic_sysfs_write_npar_config,
4055 static struct bin_attribute bin_attr_pci_config = {
4056 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
4057 .size = 0,
4058 .read = qlcnic_sysfs_read_pci_config,
4059 .write = NULL,
4062 static struct bin_attribute bin_attr_port_stats = {
4063 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
4064 .size = 0,
4065 .read = qlcnic_sysfs_get_port_stats,
4066 .write = qlcnic_sysfs_clear_port_stats,
4069 static struct bin_attribute bin_attr_esw_stats = {
4070 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
4071 .size = 0,
4072 .read = qlcnic_sysfs_get_esw_stats,
4073 .write = qlcnic_sysfs_clear_esw_stats,
4076 static struct bin_attribute bin_attr_esw_config = {
4077 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
4078 .size = 0,
4079 .read = qlcnic_sysfs_read_esw_config,
4080 .write = qlcnic_sysfs_write_esw_config,
4083 static struct bin_attribute bin_attr_pm_config = {
4084 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
4085 .size = 0,
4086 .read = qlcnic_sysfs_read_pm_config,
4087 .write = qlcnic_sysfs_write_pm_config,
4090 static void
4091 qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
4093 struct device *dev = &adapter->pdev->dev;
4095 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4096 if (device_create_file(dev, &dev_attr_bridged_mode))
4097 dev_warn(dev,
4098 "failed to create bridged_mode sysfs entry\n");
4101 static void
4102 qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
4104 struct device *dev = &adapter->pdev->dev;
4106 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4107 device_remove_file(dev, &dev_attr_bridged_mode);
4110 static void
4111 qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
4113 struct device *dev = &adapter->pdev->dev;
4115 if (device_create_bin_file(dev, &bin_attr_port_stats))
4116 dev_info(dev, "failed to create port stats sysfs entry");
4118 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4119 return;
4120 if (device_create_file(dev, &dev_attr_diag_mode))
4121 dev_info(dev, "failed to create diag_mode sysfs entry\n");
4122 if (device_create_bin_file(dev, &bin_attr_crb))
4123 dev_info(dev, "failed to create crb sysfs entry\n");
4124 if (device_create_bin_file(dev, &bin_attr_mem))
4125 dev_info(dev, "failed to create mem sysfs entry\n");
4126 if (device_create_bin_file(dev, &bin_attr_pci_config))
4127 dev_info(dev, "failed to create pci config sysfs entry");
4128 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4129 return;
4130 if (device_create_bin_file(dev, &bin_attr_esw_config))
4131 dev_info(dev, "failed to create esw config sysfs entry");
4132 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
4133 return;
4134 if (device_create_bin_file(dev, &bin_attr_npar_config))
4135 dev_info(dev, "failed to create npar config sysfs entry");
4136 if (device_create_bin_file(dev, &bin_attr_pm_config))
4137 dev_info(dev, "failed to create pm config sysfs entry");
4138 if (device_create_bin_file(dev, &bin_attr_esw_stats))
4139 dev_info(dev, "failed to create eswitch stats sysfs entry");
4142 static void
4143 qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4145 struct device *dev = &adapter->pdev->dev;
4147 device_remove_bin_file(dev, &bin_attr_port_stats);
4149 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4150 return;
4151 device_remove_file(dev, &dev_attr_diag_mode);
4152 device_remove_bin_file(dev, &bin_attr_crb);
4153 device_remove_bin_file(dev, &bin_attr_mem);
4154 device_remove_bin_file(dev, &bin_attr_pci_config);
4155 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4156 return;
4157 device_remove_bin_file(dev, &bin_attr_esw_config);
4158 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
4159 return;
4160 device_remove_bin_file(dev, &bin_attr_npar_config);
4161 device_remove_bin_file(dev, &bin_attr_pm_config);
4162 device_remove_bin_file(dev, &bin_attr_esw_stats);
4165 #ifdef CONFIG_INET
4167 #define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4169 static void
4170 qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4171 struct net_device *dev, unsigned long event)
4173 struct in_device *indev;
4175 indev = in_dev_get(dev);
4176 if (!indev)
4177 return;
4179 for_ifa(indev) {
4180 switch (event) {
4181 case NETDEV_UP:
4182 qlcnic_config_ipaddr(adapter,
4183 ifa->ifa_address, QLCNIC_IP_UP);
4184 break;
4185 case NETDEV_DOWN:
4186 qlcnic_config_ipaddr(adapter,
4187 ifa->ifa_address, QLCNIC_IP_DOWN);
4188 break;
4189 default:
4190 break;
4192 } endfor_ifa(indev);
4194 in_dev_put(indev);
4197 static void
4198 qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4200 struct qlcnic_adapter *adapter = netdev_priv(netdev);
4201 struct net_device *dev;
4202 u16 vid;
4204 qlcnic_config_indev_addr(adapter, netdev, event);
4206 for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4207 dev = __vlan_find_dev_deep(netdev, vid);
4208 if (!dev)
4209 continue;
4210 qlcnic_config_indev_addr(adapter, dev, event);
4214 static int qlcnic_netdev_event(struct notifier_block *this,
4215 unsigned long event, void *ptr)
4217 struct qlcnic_adapter *adapter;
4218 struct net_device *dev = (struct net_device *)ptr;
4220 recheck:
4221 if (dev == NULL)
4222 goto done;
4224 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4225 dev = vlan_dev_real_dev(dev);
4226 goto recheck;
4229 if (!is_qlcnic_netdev(dev))
4230 goto done;
4232 adapter = netdev_priv(dev);
4234 if (!adapter)
4235 goto done;
4237 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4238 goto done;
4240 qlcnic_config_indev_addr(adapter, dev, event);
4241 done:
4242 return NOTIFY_DONE;
4245 static int
4246 qlcnic_inetaddr_event(struct notifier_block *this,
4247 unsigned long event, void *ptr)
4249 struct qlcnic_adapter *adapter;
4250 struct net_device *dev;
4252 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4254 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4256 recheck:
4257 if (dev == NULL)
4258 goto done;
4260 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4261 dev = vlan_dev_real_dev(dev);
4262 goto recheck;
4265 if (!is_qlcnic_netdev(dev))
4266 goto done;
4268 adapter = netdev_priv(dev);
4270 if (!adapter)
4271 goto done;
4273 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4274 goto done;
4276 switch (event) {
4277 case NETDEV_UP:
4278 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4279 break;
4280 case NETDEV_DOWN:
4281 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4282 break;
4283 default:
4284 break;
4287 done:
4288 return NOTIFY_DONE;
4291 static struct notifier_block qlcnic_netdev_cb = {
4292 .notifier_call = qlcnic_netdev_event,
4295 static struct notifier_block qlcnic_inetaddr_cb = {
4296 .notifier_call = qlcnic_inetaddr_event,
4298 #else
4299 static void
4300 qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
4302 #endif
4303 static struct pci_error_handlers qlcnic_err_handler = {
4304 .error_detected = qlcnic_io_error_detected,
4305 .slot_reset = qlcnic_io_slot_reset,
4306 .resume = qlcnic_io_resume,
4309 static struct pci_driver qlcnic_driver = {
4310 .name = qlcnic_driver_name,
4311 .id_table = qlcnic_pci_tbl,
4312 .probe = qlcnic_probe,
4313 .remove = __devexit_p(qlcnic_remove),
4314 #ifdef CONFIG_PM
4315 .suspend = qlcnic_suspend,
4316 .resume = qlcnic_resume,
4317 #endif
4318 .shutdown = qlcnic_shutdown,
4319 .err_handler = &qlcnic_err_handler
4323 static int __init qlcnic_init_module(void)
4325 int ret;
4327 printk(KERN_INFO "%s\n", qlcnic_driver_string);
4329 qlcnic_wq = create_singlethread_workqueue("qlcnic");
4330 if (qlcnic_wq == NULL) {
4331 printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4332 return -ENOMEM;
4335 #ifdef CONFIG_INET
4336 register_netdevice_notifier(&qlcnic_netdev_cb);
4337 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4338 #endif
4340 ret = pci_register_driver(&qlcnic_driver);
4341 if (ret) {
4342 #ifdef CONFIG_INET
4343 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4344 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4345 #endif
4346 destroy_workqueue(qlcnic_wq);
4349 return ret;
4352 module_init(qlcnic_init_module);
4354 static void __exit qlcnic_exit_module(void)
4357 pci_unregister_driver(&qlcnic_driver);
4359 #ifdef CONFIG_INET
4360 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4361 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4362 #endif
4363 destroy_workqueue(qlcnic_wq);
4366 module_exit(qlcnic_exit_module);