Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / net / ethernet / qlogic / qlcnic / qlcnic_main.c
blob690c93f76ae43fe4166735a85418ae44f1e9770e
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_rx_mode = qlcnic_set_multi,
329 .ndo_set_mac_address = qlcnic_set_mac,
330 .ndo_change_mtu = qlcnic_change_mtu,
331 .ndo_fix_features = qlcnic_fix_features,
332 .ndo_set_features = qlcnic_set_features,
333 .ndo_tx_timeout = qlcnic_tx_timeout,
334 .ndo_vlan_rx_add_vid = qlcnic_vlan_rx_add,
335 .ndo_vlan_rx_kill_vid = qlcnic_vlan_rx_del,
336 #ifdef CONFIG_NET_POLL_CONTROLLER
337 .ndo_poll_controller = qlcnic_poll_controller,
338 #endif
341 static struct qlcnic_nic_template qlcnic_ops = {
342 .config_bridged_mode = qlcnic_config_bridged_mode,
343 .config_led = qlcnic_config_led,
344 .start_firmware = qlcnic_start_firmware
347 static struct qlcnic_nic_template qlcnic_vf_ops = {
348 .config_bridged_mode = qlcnicvf_config_bridged_mode,
349 .config_led = qlcnicvf_config_led,
350 .start_firmware = qlcnicvf_start_firmware
353 static int qlcnic_enable_msix(struct qlcnic_adapter *adapter, u32 num_msix)
355 struct pci_dev *pdev = adapter->pdev;
356 int err = -1;
358 adapter->max_sds_rings = 1;
359 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
360 qlcnic_set_msix_bit(pdev, 0);
362 if (adapter->msix_supported) {
363 enable_msix:
364 qlcnic_init_msix_entries(adapter, num_msix);
365 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
366 if (err == 0) {
367 adapter->flags |= QLCNIC_MSIX_ENABLED;
368 qlcnic_set_msix_bit(pdev, 1);
370 adapter->max_sds_rings = num_msix;
372 dev_info(&pdev->dev, "using msi-x interrupts\n");
373 return err;
375 if (err > 0) {
376 num_msix = rounddown_pow_of_two(err);
377 if (num_msix)
378 goto enable_msix;
381 return err;
385 static void qlcnic_enable_msi_legacy(struct qlcnic_adapter *adapter)
387 const struct qlcnic_legacy_intr_set *legacy_intrp;
388 struct pci_dev *pdev = adapter->pdev;
390 if (use_msi && !pci_enable_msi(pdev)) {
391 adapter->flags |= QLCNIC_MSI_ENABLED;
392 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
393 msi_tgt_status[adapter->ahw->pci_func]);
394 dev_info(&pdev->dev, "using msi interrupts\n");
395 adapter->msix_entries[0].vector = pdev->irq;
396 return;
399 legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
401 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
402 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
403 legacy_intrp->tgt_status_reg);
404 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
405 legacy_intrp->tgt_mask_reg);
406 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
408 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
409 ISR_INT_STATE_REG);
410 dev_info(&pdev->dev, "using legacy interrupts\n");
411 adapter->msix_entries[0].vector = pdev->irq;
414 static void
415 qlcnic_setup_intr(struct qlcnic_adapter *adapter)
417 int num_msix;
419 if (adapter->msix_supported) {
420 num_msix = rounddown_pow_of_two(min_t(int, num_online_cpus(),
421 QLCNIC_DEF_NUM_STS_DESC_RINGS));
422 } else
423 num_msix = 1;
425 if (!qlcnic_enable_msix(adapter, num_msix))
426 return;
428 qlcnic_enable_msi_legacy(adapter);
431 static void
432 qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
434 if (adapter->flags & QLCNIC_MSIX_ENABLED)
435 pci_disable_msix(adapter->pdev);
436 if (adapter->flags & QLCNIC_MSI_ENABLED)
437 pci_disable_msi(adapter->pdev);
440 static void
441 qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
443 if (adapter->ahw->pci_base0 != NULL)
444 iounmap(adapter->ahw->pci_base0);
447 static int
448 qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
450 struct qlcnic_pci_info *pci_info;
451 int i, ret = 0;
452 u8 pfn;
454 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
455 if (!pci_info)
456 return -ENOMEM;
458 adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
459 QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
460 if (!adapter->npars) {
461 ret = -ENOMEM;
462 goto err_pci_info;
465 adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
466 QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
467 if (!adapter->eswitch) {
468 ret = -ENOMEM;
469 goto err_npars;
472 ret = qlcnic_get_pci_info(adapter, pci_info);
473 if (ret)
474 goto err_eswitch;
476 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
477 pfn = pci_info[i].id;
478 if (pfn > QLCNIC_MAX_PCI_FUNC) {
479 ret = QL_STATUS_INVALID_PARAM;
480 goto err_eswitch;
482 adapter->npars[pfn].active = (u8)pci_info[i].active;
483 adapter->npars[pfn].type = (u8)pci_info[i].type;
484 adapter->npars[pfn].phy_port = (u8)pci_info[i].default_port;
485 adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
486 adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
489 for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
490 adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
492 kfree(pci_info);
493 return 0;
495 err_eswitch:
496 kfree(adapter->eswitch);
497 adapter->eswitch = NULL;
498 err_npars:
499 kfree(adapter->npars);
500 adapter->npars = NULL;
501 err_pci_info:
502 kfree(pci_info);
504 return ret;
507 static int
508 qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
510 u8 id;
511 u32 ref_count;
512 int i, ret = 1;
513 u32 data = QLCNIC_MGMT_FUNC;
514 void __iomem *priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
516 /* If other drivers are not in use set their privilege level */
517 ref_count = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
518 ret = qlcnic_api_lock(adapter);
519 if (ret)
520 goto err_lock;
522 if (qlcnic_config_npars) {
523 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
524 id = i;
525 if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
526 id == adapter->ahw->pci_func)
527 continue;
528 data |= (qlcnic_config_npars &
529 QLC_DEV_SET_DRV(0xf, id));
531 } else {
532 data = readl(priv_op);
533 data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw->pci_func)) |
534 (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
535 adapter->ahw->pci_func));
537 writel(data, priv_op);
538 qlcnic_api_unlock(adapter);
539 err_lock:
540 return ret;
543 static void
544 qlcnic_check_vf(struct qlcnic_adapter *adapter)
546 void __iomem *msix_base_addr;
547 void __iomem *priv_op;
548 u32 func;
549 u32 msix_base;
550 u32 op_mode, priv_level;
552 /* Determine FW API version */
553 adapter->fw_hal_version = readl(adapter->ahw->pci_base0 +
554 QLCNIC_FW_API);
556 /* Find PCI function number */
557 pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
558 msix_base_addr = adapter->ahw->pci_base0 + QLCNIC_MSIX_BASE;
559 msix_base = readl(msix_base_addr);
560 func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
561 adapter->ahw->pci_func = func;
563 /* Determine function privilege level */
564 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
565 op_mode = readl(priv_op);
566 if (op_mode == QLC_DEV_DRV_DEFAULT)
567 priv_level = QLCNIC_MGMT_FUNC;
568 else
569 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
571 if (priv_level == QLCNIC_NON_PRIV_FUNC) {
572 adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
573 dev_info(&adapter->pdev->dev,
574 "HAL Version: %d Non Privileged function\n",
575 adapter->fw_hal_version);
576 adapter->nic_ops = &qlcnic_vf_ops;
577 } else
578 adapter->nic_ops = &qlcnic_ops;
581 static int
582 qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
584 void __iomem *mem_ptr0 = NULL;
585 resource_size_t mem_base;
586 unsigned long mem_len, pci_len0 = 0;
588 struct pci_dev *pdev = adapter->pdev;
590 /* remap phys address */
591 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
592 mem_len = pci_resource_len(pdev, 0);
594 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
596 mem_ptr0 = pci_ioremap_bar(pdev, 0);
597 if (mem_ptr0 == NULL) {
598 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
599 return -EIO;
601 pci_len0 = mem_len;
602 } else {
603 return -EIO;
606 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
608 adapter->ahw->pci_base0 = mem_ptr0;
609 adapter->ahw->pci_len0 = pci_len0;
611 qlcnic_check_vf(adapter);
613 adapter->ahw->ocm_win_crb = qlcnic_get_ioaddr(adapter,
614 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(
615 adapter->ahw->pci_func)));
617 return 0;
620 static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
622 struct pci_dev *pdev = adapter->pdev;
623 int i, found = 0;
625 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
626 if (qlcnic_boards[i].vendor == pdev->vendor &&
627 qlcnic_boards[i].device == pdev->device &&
628 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
629 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
630 sprintf(name, "%pM: %s" ,
631 adapter->mac_addr,
632 qlcnic_boards[i].short_name);
633 found = 1;
634 break;
639 if (!found)
640 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
643 static void
644 qlcnic_check_options(struct qlcnic_adapter *adapter)
646 u32 fw_major, fw_minor, fw_build, prev_fw_version;
647 struct pci_dev *pdev = adapter->pdev;
648 struct qlcnic_fw_dump *fw_dump = &adapter->ahw->fw_dump;
650 prev_fw_version = adapter->fw_version;
652 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
653 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
654 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
656 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
658 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC) {
659 if (fw_dump->tmpl_hdr == NULL ||
660 adapter->fw_version > prev_fw_version) {
661 if (fw_dump->tmpl_hdr)
662 vfree(fw_dump->tmpl_hdr);
663 if (!qlcnic_fw_cmd_get_minidump_temp(adapter))
664 dev_info(&pdev->dev,
665 "Supports FW dump capability\n");
669 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
670 fw_major, fw_minor, fw_build);
671 if (adapter->ahw->port_type == QLCNIC_XGBE) {
672 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
673 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
674 adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
675 } else {
676 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
677 adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
680 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
681 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
683 } else if (adapter->ahw->port_type == QLCNIC_GBE) {
684 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
685 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
686 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
687 adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
690 adapter->msix_supported = !!use_msi_x;
692 adapter->num_txd = MAX_CMD_DESCRIPTORS;
694 adapter->max_rds_rings = MAX_RDS_RINGS;
697 static int
698 qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
700 int err;
701 struct qlcnic_info nic_info;
703 err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
704 if (err)
705 return err;
707 adapter->physical_port = (u8)nic_info.phys_port;
708 adapter->switch_mode = nic_info.switch_mode;
709 adapter->max_tx_ques = nic_info.max_tx_ques;
710 adapter->max_rx_ques = nic_info.max_rx_ques;
711 adapter->capabilities = nic_info.capabilities;
712 adapter->max_mac_filters = nic_info.max_mac_filters;
713 adapter->max_mtu = nic_info.max_mtu;
715 if (adapter->capabilities & BIT_6)
716 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
717 else
718 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
720 return err;
723 static void
724 qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
725 struct qlcnic_esw_func_cfg *esw_cfg)
727 if (esw_cfg->discard_tagged)
728 adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
729 else
730 adapter->flags |= QLCNIC_TAGGING_ENABLED;
732 if (esw_cfg->vlan_id)
733 adapter->pvid = esw_cfg->vlan_id;
734 else
735 adapter->pvid = 0;
738 static void
739 qlcnic_vlan_rx_add(struct net_device *netdev, u16 vid)
741 struct qlcnic_adapter *adapter = netdev_priv(netdev);
742 set_bit(vid, adapter->vlans);
745 static void
746 qlcnic_vlan_rx_del(struct net_device *netdev, u16 vid)
748 struct qlcnic_adapter *adapter = netdev_priv(netdev);
750 qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
751 clear_bit(vid, adapter->vlans);
754 static void
755 qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
756 struct qlcnic_esw_func_cfg *esw_cfg)
758 adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
759 QLCNIC_PROMISC_DISABLED);
761 if (esw_cfg->mac_anti_spoof)
762 adapter->flags |= QLCNIC_MACSPOOF;
764 if (!esw_cfg->mac_override)
765 adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
767 if (!esw_cfg->promisc_mode)
768 adapter->flags |= QLCNIC_PROMISC_DISABLED;
770 qlcnic_set_netdev_features(adapter, esw_cfg);
773 static int
774 qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
776 struct qlcnic_esw_func_cfg esw_cfg;
778 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
779 return 0;
781 esw_cfg.pci_func = adapter->ahw->pci_func;
782 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
783 return -EIO;
784 qlcnic_set_vlan_config(adapter, &esw_cfg);
785 qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
787 return 0;
790 static void
791 qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
792 struct qlcnic_esw_func_cfg *esw_cfg)
794 struct net_device *netdev = adapter->netdev;
795 unsigned long features, vlan_features;
797 features = (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
798 NETIF_F_IPV6_CSUM | NETIF_F_GRO);
799 vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
800 NETIF_F_IPV6_CSUM | NETIF_F_HW_VLAN_FILTER);
802 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
803 features |= (NETIF_F_TSO | NETIF_F_TSO6);
804 vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
807 if (netdev->features & NETIF_F_LRO)
808 features |= NETIF_F_LRO;
810 if (esw_cfg->offload_flags & BIT_0) {
811 netdev->features |= features;
812 if (!(esw_cfg->offload_flags & BIT_1))
813 netdev->features &= ~NETIF_F_TSO;
814 if (!(esw_cfg->offload_flags & BIT_2))
815 netdev->features &= ~NETIF_F_TSO6;
816 } else {
817 netdev->features &= ~features;
820 netdev->vlan_features = (features & vlan_features);
823 static int
824 qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
826 void __iomem *priv_op;
827 u32 op_mode, priv_level;
828 int err = 0;
830 err = qlcnic_initialize_nic(adapter);
831 if (err)
832 return err;
834 if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
835 return 0;
837 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
838 op_mode = readl(priv_op);
839 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
841 if (op_mode == QLC_DEV_DRV_DEFAULT)
842 priv_level = QLCNIC_MGMT_FUNC;
843 else
844 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
846 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
847 if (priv_level == QLCNIC_MGMT_FUNC) {
848 adapter->op_mode = QLCNIC_MGMT_FUNC;
849 err = qlcnic_init_pci_info(adapter);
850 if (err)
851 return err;
852 /* Set privilege level for other functions */
853 qlcnic_set_function_modes(adapter);
854 dev_info(&adapter->pdev->dev,
855 "HAL Version: %d, Management function\n",
856 adapter->fw_hal_version);
857 } else if (priv_level == QLCNIC_PRIV_FUNC) {
858 adapter->op_mode = QLCNIC_PRIV_FUNC;
859 dev_info(&adapter->pdev->dev,
860 "HAL Version: %d, Privileged function\n",
861 adapter->fw_hal_version);
865 adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
867 return err;
870 static int
871 qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
873 struct qlcnic_esw_func_cfg esw_cfg;
874 struct qlcnic_npar_info *npar;
875 u8 i;
877 if (adapter->need_fw_reset)
878 return 0;
880 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
881 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
882 continue;
883 memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
884 esw_cfg.pci_func = i;
885 esw_cfg.offload_flags = BIT_0;
886 esw_cfg.mac_override = BIT_0;
887 esw_cfg.promisc_mode = BIT_0;
888 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
889 esw_cfg.offload_flags |= (BIT_1 | BIT_2);
890 if (qlcnic_config_switch_port(adapter, &esw_cfg))
891 return -EIO;
892 npar = &adapter->npars[i];
893 npar->pvid = esw_cfg.vlan_id;
894 npar->mac_override = esw_cfg.mac_override;
895 npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
896 npar->discard_tagged = esw_cfg.discard_tagged;
897 npar->promisc_mode = esw_cfg.promisc_mode;
898 npar->offload_flags = esw_cfg.offload_flags;
901 return 0;
904 static int
905 qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
906 struct qlcnic_npar_info *npar, int pci_func)
908 struct qlcnic_esw_func_cfg esw_cfg;
909 esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
910 esw_cfg.pci_func = pci_func;
911 esw_cfg.vlan_id = npar->pvid;
912 esw_cfg.mac_override = npar->mac_override;
913 esw_cfg.discard_tagged = npar->discard_tagged;
914 esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
915 esw_cfg.offload_flags = npar->offload_flags;
916 esw_cfg.promisc_mode = npar->promisc_mode;
917 if (qlcnic_config_switch_port(adapter, &esw_cfg))
918 return -EIO;
920 esw_cfg.op_mode = QLCNIC_ADD_VLAN;
921 if (qlcnic_config_switch_port(adapter, &esw_cfg))
922 return -EIO;
924 return 0;
927 static int
928 qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
930 int i, err;
931 struct qlcnic_npar_info *npar;
932 struct qlcnic_info nic_info;
934 if (!adapter->need_fw_reset)
935 return 0;
937 /* Set the NPAR config data after FW reset */
938 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
939 npar = &adapter->npars[i];
940 if (npar->type != QLCNIC_TYPE_NIC)
941 continue;
942 err = qlcnic_get_nic_info(adapter, &nic_info, i);
943 if (err)
944 return err;
945 nic_info.min_tx_bw = npar->min_bw;
946 nic_info.max_tx_bw = npar->max_bw;
947 err = qlcnic_set_nic_info(adapter, &nic_info);
948 if (err)
949 return err;
951 if (npar->enable_pm) {
952 err = qlcnic_config_port_mirroring(adapter,
953 npar->dest_npar, 1, i);
954 if (err)
955 return err;
957 err = qlcnic_reset_eswitch_config(adapter, npar, i);
958 if (err)
959 return err;
961 return 0;
964 static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
966 u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
967 u32 npar_state;
969 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
970 return 0;
972 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
973 while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
974 msleep(1000);
975 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
977 if (!npar_opt_timeo) {
978 dev_err(&adapter->pdev->dev,
979 "Waiting for NPAR state to opertional timeout\n");
980 return -EIO;
982 return 0;
985 static int
986 qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
988 int err;
990 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
991 adapter->op_mode != QLCNIC_MGMT_FUNC)
992 return 0;
994 err = qlcnic_set_default_offload_settings(adapter);
995 if (err)
996 return err;
998 err = qlcnic_reset_npar_config(adapter);
999 if (err)
1000 return err;
1002 qlcnic_dev_set_npar_ready(adapter);
1004 return err;
1007 static int
1008 qlcnic_start_firmware(struct qlcnic_adapter *adapter)
1010 int err;
1012 err = qlcnic_can_start_firmware(adapter);
1013 if (err < 0)
1014 return err;
1015 else if (!err)
1016 goto check_fw_status;
1018 if (load_fw_file)
1019 qlcnic_request_firmware(adapter);
1020 else {
1021 err = qlcnic_check_flash_fw_ver(adapter);
1022 if (err)
1023 goto err_out;
1025 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
1028 err = qlcnic_need_fw_reset(adapter);
1029 if (err == 0)
1030 goto check_fw_status;
1032 err = qlcnic_pinit_from_rom(adapter);
1033 if (err)
1034 goto err_out;
1036 err = qlcnic_load_firmware(adapter);
1037 if (err)
1038 goto err_out;
1040 qlcnic_release_firmware(adapter);
1041 QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
1043 check_fw_status:
1044 err = qlcnic_check_fw_status(adapter);
1045 if (err)
1046 goto err_out;
1048 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
1049 qlcnic_idc_debug_info(adapter, 1);
1051 err = qlcnic_check_eswitch_mode(adapter);
1052 if (err) {
1053 dev_err(&adapter->pdev->dev,
1054 "Memory allocation failed for eswitch\n");
1055 goto err_out;
1057 err = qlcnic_set_mgmt_operations(adapter);
1058 if (err)
1059 goto err_out;
1061 qlcnic_check_options(adapter);
1062 adapter->need_fw_reset = 0;
1064 qlcnic_release_firmware(adapter);
1065 return 0;
1067 err_out:
1068 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1069 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
1071 qlcnic_release_firmware(adapter);
1072 return err;
1075 static int
1076 qlcnic_request_irq(struct qlcnic_adapter *adapter)
1078 irq_handler_t handler;
1079 struct qlcnic_host_sds_ring *sds_ring;
1080 int err, ring;
1082 unsigned long flags = 0;
1083 struct net_device *netdev = adapter->netdev;
1084 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1086 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1087 handler = qlcnic_tmp_intr;
1088 if (!QLCNIC_IS_MSI_FAMILY(adapter))
1089 flags |= IRQF_SHARED;
1091 } else {
1092 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1093 handler = qlcnic_msix_intr;
1094 else if (adapter->flags & QLCNIC_MSI_ENABLED)
1095 handler = qlcnic_msi_intr;
1096 else {
1097 flags |= IRQF_SHARED;
1098 handler = qlcnic_intr;
1101 adapter->irq = netdev->irq;
1103 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1104 sds_ring = &recv_ctx->sds_rings[ring];
1105 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1106 err = request_irq(sds_ring->irq, handler,
1107 flags, sds_ring->name, sds_ring);
1108 if (err)
1109 return err;
1112 return 0;
1115 static void
1116 qlcnic_free_irq(struct qlcnic_adapter *adapter)
1118 int ring;
1119 struct qlcnic_host_sds_ring *sds_ring;
1121 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1123 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1124 sds_ring = &recv_ctx->sds_rings[ring];
1125 free_irq(sds_ring->irq, sds_ring);
1129 static int
1130 __qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1132 int ring;
1133 struct qlcnic_host_rds_ring *rds_ring;
1135 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1136 return -EIO;
1138 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1139 return 0;
1140 if (qlcnic_set_eswitch_port_config(adapter))
1141 return -EIO;
1143 if (qlcnic_fw_create_ctx(adapter))
1144 return -EIO;
1146 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1147 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1148 qlcnic_post_rx_buffers(adapter, rds_ring);
1151 qlcnic_set_multi(netdev);
1152 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1154 adapter->ahw->linkup = 0;
1156 if (adapter->max_sds_rings > 1)
1157 qlcnic_config_rss(adapter, 1);
1159 qlcnic_config_intr_coalesce(adapter);
1161 if (netdev->features & NETIF_F_LRO)
1162 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1164 qlcnic_napi_enable(adapter);
1166 qlcnic_linkevent_request(adapter, 1);
1168 adapter->reset_context = 0;
1169 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1170 return 0;
1173 /* Usage: During resume and firmware recovery module.*/
1175 static int
1176 qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1178 int err = 0;
1180 rtnl_lock();
1181 if (netif_running(netdev))
1182 err = __qlcnic_up(adapter, netdev);
1183 rtnl_unlock();
1185 return err;
1188 static void
1189 __qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1191 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1192 return;
1194 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1195 return;
1197 smp_mb();
1198 spin_lock(&adapter->tx_clean_lock);
1199 netif_carrier_off(netdev);
1200 netif_tx_disable(netdev);
1202 qlcnic_free_mac_list(adapter);
1204 if (adapter->fhash.fnum)
1205 qlcnic_delete_lb_filters(adapter);
1207 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1209 qlcnic_napi_disable(adapter);
1211 qlcnic_fw_destroy_ctx(adapter);
1213 qlcnic_reset_rx_buffers_list(adapter);
1214 qlcnic_release_tx_buffers(adapter);
1215 spin_unlock(&adapter->tx_clean_lock);
1218 /* Usage: During suspend and firmware recovery module */
1220 static void
1221 qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1223 rtnl_lock();
1224 if (netif_running(netdev))
1225 __qlcnic_down(adapter, netdev);
1226 rtnl_unlock();
1230 static int
1231 qlcnic_attach(struct qlcnic_adapter *adapter)
1233 struct net_device *netdev = adapter->netdev;
1234 struct pci_dev *pdev = adapter->pdev;
1235 int err;
1237 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1238 return 0;
1240 err = qlcnic_napi_add(adapter, netdev);
1241 if (err)
1242 return err;
1244 err = qlcnic_alloc_sw_resources(adapter);
1245 if (err) {
1246 dev_err(&pdev->dev, "Error in setting sw resources\n");
1247 goto err_out_napi_del;
1250 err = qlcnic_alloc_hw_resources(adapter);
1251 if (err) {
1252 dev_err(&pdev->dev, "Error in setting hw resources\n");
1253 goto err_out_free_sw;
1256 err = qlcnic_request_irq(adapter);
1257 if (err) {
1258 dev_err(&pdev->dev, "failed to setup interrupt\n");
1259 goto err_out_free_hw;
1262 qlcnic_create_sysfs_entries(adapter);
1264 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1265 return 0;
1267 err_out_free_hw:
1268 qlcnic_free_hw_resources(adapter);
1269 err_out_free_sw:
1270 qlcnic_free_sw_resources(adapter);
1271 err_out_napi_del:
1272 qlcnic_napi_del(adapter);
1273 return err;
1276 static void
1277 qlcnic_detach(struct qlcnic_adapter *adapter)
1279 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1280 return;
1282 qlcnic_remove_sysfs_entries(adapter);
1284 qlcnic_free_hw_resources(adapter);
1285 qlcnic_release_rx_buffers(adapter);
1286 qlcnic_free_irq(adapter);
1287 qlcnic_napi_del(adapter);
1288 qlcnic_free_sw_resources(adapter);
1290 adapter->is_up = 0;
1293 void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1295 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1296 struct qlcnic_host_sds_ring *sds_ring;
1297 int ring;
1299 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
1300 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1301 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1302 sds_ring = &adapter->recv_ctx->sds_rings[ring];
1303 qlcnic_disable_int(sds_ring);
1307 qlcnic_fw_destroy_ctx(adapter);
1309 qlcnic_detach(adapter);
1311 adapter->diag_test = 0;
1312 adapter->max_sds_rings = max_sds_rings;
1314 if (qlcnic_attach(adapter))
1315 goto out;
1317 if (netif_running(netdev))
1318 __qlcnic_up(adapter, netdev);
1319 out:
1320 netif_device_attach(netdev);
1323 static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1325 int err = 0;
1326 adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1327 GFP_KERNEL);
1328 if (!adapter->ahw) {
1329 dev_err(&adapter->pdev->dev,
1330 "Failed to allocate recv ctx resources for adapter\n");
1331 err = -ENOMEM;
1332 goto err_out;
1334 adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1335 GFP_KERNEL);
1336 if (!adapter->recv_ctx) {
1337 dev_err(&adapter->pdev->dev,
1338 "Failed to allocate recv ctx resources for adapter\n");
1339 kfree(adapter->ahw);
1340 adapter->ahw = NULL;
1341 err = -ENOMEM;
1342 goto err_out;
1344 /* Initialize interrupt coalesce parameters */
1345 adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1346 adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1347 adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
1348 err_out:
1349 return err;
1352 static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1354 kfree(adapter->recv_ctx);
1355 adapter->recv_ctx = NULL;
1357 if (adapter->ahw->fw_dump.tmpl_hdr) {
1358 vfree(adapter->ahw->fw_dump.tmpl_hdr);
1359 adapter->ahw->fw_dump.tmpl_hdr = NULL;
1361 kfree(adapter->ahw);
1362 adapter->ahw = NULL;
1365 int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1367 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1368 struct qlcnic_host_sds_ring *sds_ring;
1369 struct qlcnic_host_rds_ring *rds_ring;
1370 int ring;
1371 int ret;
1373 netif_device_detach(netdev);
1375 if (netif_running(netdev))
1376 __qlcnic_down(adapter, netdev);
1378 qlcnic_detach(adapter);
1380 adapter->max_sds_rings = 1;
1381 adapter->diag_test = test;
1383 ret = qlcnic_attach(adapter);
1384 if (ret) {
1385 netif_device_attach(netdev);
1386 return ret;
1389 ret = qlcnic_fw_create_ctx(adapter);
1390 if (ret) {
1391 qlcnic_detach(adapter);
1392 netif_device_attach(netdev);
1393 return ret;
1396 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1397 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1398 qlcnic_post_rx_buffers(adapter, rds_ring);
1401 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1402 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1403 sds_ring = &adapter->recv_ctx->sds_rings[ring];
1404 qlcnic_enable_int(sds_ring);
1408 if (adapter->diag_test == QLCNIC_LOOPBACK_TEST) {
1409 adapter->ahw->loopback_state = 0;
1410 qlcnic_linkevent_request(adapter, 1);
1413 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1415 return 0;
1418 /* Reset context in hardware only */
1419 static int
1420 qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1422 struct net_device *netdev = adapter->netdev;
1424 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1425 return -EBUSY;
1427 netif_device_detach(netdev);
1429 qlcnic_down(adapter, netdev);
1431 qlcnic_up(adapter, netdev);
1433 netif_device_attach(netdev);
1435 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1436 return 0;
1440 qlcnic_reset_context(struct qlcnic_adapter *adapter)
1442 int err = 0;
1443 struct net_device *netdev = adapter->netdev;
1445 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1446 return -EBUSY;
1448 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1450 netif_device_detach(netdev);
1452 if (netif_running(netdev))
1453 __qlcnic_down(adapter, netdev);
1455 qlcnic_detach(adapter);
1457 if (netif_running(netdev)) {
1458 err = qlcnic_attach(adapter);
1459 if (!err)
1460 __qlcnic_up(adapter, netdev);
1463 netif_device_attach(netdev);
1466 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1467 return err;
1470 static int
1471 qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
1472 struct net_device *netdev, u8 pci_using_dac)
1474 int err;
1475 struct pci_dev *pdev = adapter->pdev;
1477 adapter->mc_enabled = 0;
1478 adapter->max_mc_count = 38;
1480 netdev->netdev_ops = &qlcnic_netdev_ops;
1481 netdev->watchdog_timeo = 5*HZ;
1483 qlcnic_change_mtu(netdev, netdev->mtu);
1485 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1487 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1488 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
1490 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
1491 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1492 if (pci_using_dac)
1493 netdev->hw_features |= NETIF_F_HIGHDMA;
1495 netdev->vlan_features = netdev->hw_features;
1497 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1498 netdev->hw_features |= NETIF_F_HW_VLAN_TX;
1499 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1500 netdev->hw_features |= NETIF_F_LRO;
1502 netdev->features |= netdev->hw_features |
1503 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
1505 netdev->irq = adapter->msix_entries[0].vector;
1507 err = register_netdev(netdev);
1508 if (err) {
1509 dev_err(&pdev->dev, "failed to register net device\n");
1510 return err;
1513 return 0;
1516 static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1518 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1519 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1520 *pci_using_dac = 1;
1521 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1522 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1523 *pci_using_dac = 0;
1524 else {
1525 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1526 return -EIO;
1529 return 0;
1532 static int
1533 qlcnic_alloc_msix_entries(struct qlcnic_adapter *adapter, u16 count)
1535 adapter->msix_entries = kcalloc(count, sizeof(struct msix_entry),
1536 GFP_KERNEL);
1538 if (adapter->msix_entries)
1539 return 0;
1541 dev_err(&adapter->pdev->dev, "failed allocating msix_entries\n");
1542 return -ENOMEM;
1545 static int __devinit
1546 qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1548 struct net_device *netdev = NULL;
1549 struct qlcnic_adapter *adapter = NULL;
1550 int err;
1551 uint8_t revision_id;
1552 uint8_t pci_using_dac;
1553 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
1555 err = pci_enable_device(pdev);
1556 if (err)
1557 return err;
1559 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1560 err = -ENODEV;
1561 goto err_out_disable_pdev;
1564 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1565 if (err)
1566 goto err_out_disable_pdev;
1568 err = pci_request_regions(pdev, qlcnic_driver_name);
1569 if (err)
1570 goto err_out_disable_pdev;
1572 pci_set_master(pdev);
1573 pci_enable_pcie_error_reporting(pdev);
1575 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1576 if (!netdev) {
1577 dev_err(&pdev->dev, "failed to allocate net_device\n");
1578 err = -ENOMEM;
1579 goto err_out_free_res;
1582 SET_NETDEV_DEV(netdev, &pdev->dev);
1584 adapter = netdev_priv(netdev);
1585 adapter->netdev = netdev;
1586 adapter->pdev = pdev;
1588 if (qlcnic_alloc_adapter_resources(adapter))
1589 goto err_out_free_netdev;
1591 adapter->dev_rst_time = jiffies;
1592 revision_id = pdev->revision;
1593 adapter->ahw->revision_id = revision_id;
1594 adapter->mac_learn = qlcnic_mac_learn;
1596 rwlock_init(&adapter->ahw->crb_lock);
1597 mutex_init(&adapter->ahw->mem_lock);
1599 spin_lock_init(&adapter->tx_clean_lock);
1600 INIT_LIST_HEAD(&adapter->mac_list);
1602 err = qlcnic_setup_pci_map(adapter);
1603 if (err)
1604 goto err_out_free_hw;
1606 /* This will be reset for mezz cards */
1607 adapter->portnum = adapter->ahw->pci_func;
1609 err = qlcnic_get_board_info(adapter);
1610 if (err) {
1611 dev_err(&pdev->dev, "Error getting board config info.\n");
1612 goto err_out_iounmap;
1615 err = qlcnic_setup_idc_param(adapter);
1616 if (err)
1617 goto err_out_iounmap;
1619 adapter->flags |= QLCNIC_NEED_FLR;
1621 err = adapter->nic_ops->start_firmware(adapter);
1622 if (err) {
1623 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
1624 goto err_out_decr_ref;
1627 if (qlcnic_read_mac_addr(adapter))
1628 dev_warn(&pdev->dev, "failed to read mac addr\n");
1630 if (adapter->portnum == 0) {
1631 get_brd_name(adapter, brd_name);
1633 pr_info("%s: %s Board Chip rev 0x%x\n",
1634 module_name(THIS_MODULE),
1635 brd_name, adapter->ahw->revision_id);
1638 qlcnic_clear_stats(adapter);
1640 err = qlcnic_alloc_msix_entries(adapter, adapter->max_rx_ques);
1641 if (err)
1642 goto err_out_decr_ref;
1644 qlcnic_setup_intr(adapter);
1646 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
1647 if (err)
1648 goto err_out_disable_msi;
1650 pci_set_drvdata(pdev, adapter);
1652 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1654 switch (adapter->ahw->port_type) {
1655 case QLCNIC_GBE:
1656 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1657 adapter->netdev->name);
1658 break;
1659 case QLCNIC_XGBE:
1660 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1661 adapter->netdev->name);
1662 break;
1665 if (adapter->mac_learn)
1666 qlcnic_alloc_lb_filters_mem(adapter);
1668 qlcnic_create_diag_entries(adapter);
1670 return 0;
1672 err_out_disable_msi:
1673 qlcnic_teardown_intr(adapter);
1674 kfree(adapter->msix_entries);
1676 err_out_decr_ref:
1677 qlcnic_clr_all_drv_state(adapter, 0);
1679 err_out_iounmap:
1680 qlcnic_cleanup_pci_map(adapter);
1682 err_out_free_hw:
1683 qlcnic_free_adapter_resources(adapter);
1685 err_out_free_netdev:
1686 free_netdev(netdev);
1688 err_out_free_res:
1689 pci_release_regions(pdev);
1691 err_out_disable_pdev:
1692 pci_set_drvdata(pdev, NULL);
1693 pci_disable_device(pdev);
1694 return err;
1697 static void __devexit qlcnic_remove(struct pci_dev *pdev)
1699 struct qlcnic_adapter *adapter;
1700 struct net_device *netdev;
1702 adapter = pci_get_drvdata(pdev);
1703 if (adapter == NULL)
1704 return;
1706 netdev = adapter->netdev;
1708 qlcnic_cancel_fw_work(adapter);
1710 unregister_netdev(netdev);
1712 qlcnic_detach(adapter);
1714 if (adapter->npars != NULL)
1715 kfree(adapter->npars);
1716 if (adapter->eswitch != NULL)
1717 kfree(adapter->eswitch);
1719 qlcnic_clr_all_drv_state(adapter, 0);
1721 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1723 qlcnic_free_lb_filters_mem(adapter);
1725 qlcnic_teardown_intr(adapter);
1726 kfree(adapter->msix_entries);
1728 qlcnic_remove_diag_entries(adapter);
1730 qlcnic_cleanup_pci_map(adapter);
1732 qlcnic_release_firmware(adapter);
1734 pci_disable_pcie_error_reporting(pdev);
1735 pci_release_regions(pdev);
1736 pci_disable_device(pdev);
1737 pci_set_drvdata(pdev, NULL);
1739 qlcnic_free_adapter_resources(adapter);
1740 free_netdev(netdev);
1742 static int __qlcnic_shutdown(struct pci_dev *pdev)
1744 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1745 struct net_device *netdev = adapter->netdev;
1746 int retval;
1748 netif_device_detach(netdev);
1750 qlcnic_cancel_fw_work(adapter);
1752 if (netif_running(netdev))
1753 qlcnic_down(adapter, netdev);
1755 qlcnic_clr_all_drv_state(adapter, 0);
1757 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1759 retval = pci_save_state(pdev);
1760 if (retval)
1761 return retval;
1763 if (qlcnic_wol_supported(adapter)) {
1764 pci_enable_wake(pdev, PCI_D3cold, 1);
1765 pci_enable_wake(pdev, PCI_D3hot, 1);
1768 return 0;
1771 static void qlcnic_shutdown(struct pci_dev *pdev)
1773 if (__qlcnic_shutdown(pdev))
1774 return;
1776 pci_disable_device(pdev);
1779 #ifdef CONFIG_PM
1780 static int
1781 qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1783 int retval;
1785 retval = __qlcnic_shutdown(pdev);
1786 if (retval)
1787 return retval;
1789 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1790 return 0;
1793 static int
1794 qlcnic_resume(struct pci_dev *pdev)
1796 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1797 struct net_device *netdev = adapter->netdev;
1798 int err;
1800 err = pci_enable_device(pdev);
1801 if (err)
1802 return err;
1804 pci_set_power_state(pdev, PCI_D0);
1805 pci_set_master(pdev);
1806 pci_restore_state(pdev);
1808 err = adapter->nic_ops->start_firmware(adapter);
1809 if (err) {
1810 dev_err(&pdev->dev, "failed to start firmware\n");
1811 return err;
1814 if (netif_running(netdev)) {
1815 err = qlcnic_up(adapter, netdev);
1816 if (err)
1817 goto done;
1819 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
1821 done:
1822 netif_device_attach(netdev);
1823 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1824 return 0;
1826 #endif
1828 static int qlcnic_open(struct net_device *netdev)
1830 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1831 int err;
1833 netif_carrier_off(netdev);
1835 err = qlcnic_attach(adapter);
1836 if (err)
1837 return err;
1839 err = __qlcnic_up(adapter, netdev);
1840 if (err)
1841 goto err_out;
1843 netif_start_queue(netdev);
1845 return 0;
1847 err_out:
1848 qlcnic_detach(adapter);
1849 return err;
1853 * qlcnic_close - Disables a network interface entry point
1855 static int qlcnic_close(struct net_device *netdev)
1857 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1859 __qlcnic_down(adapter, netdev);
1860 return 0;
1863 void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
1865 void *head;
1866 int i;
1868 if (adapter->fhash.fmax && adapter->fhash.fhead)
1869 return;
1871 spin_lock_init(&adapter->mac_learn_lock);
1873 head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1874 GFP_KERNEL);
1875 if (!head)
1876 return;
1878 adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
1879 adapter->fhash.fhead = head;
1881 for (i = 0; i < adapter->fhash.fmax; i++)
1882 INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1885 static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1887 if (adapter->fhash.fmax && adapter->fhash.fhead)
1888 kfree(adapter->fhash.fhead);
1890 adapter->fhash.fhead = NULL;
1891 adapter->fhash.fmax = 0;
1894 static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
1895 u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
1897 struct cmd_desc_type0 *hwdesc;
1898 struct qlcnic_nic_req *req;
1899 struct qlcnic_mac_req *mac_req;
1900 struct qlcnic_vlan_req *vlan_req;
1901 u32 producer;
1902 u64 word;
1904 producer = tx_ring->producer;
1905 hwdesc = &tx_ring->desc_head[tx_ring->producer];
1907 req = (struct qlcnic_nic_req *)hwdesc;
1908 memset(req, 0, sizeof(struct qlcnic_nic_req));
1909 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1911 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1912 req->req_hdr = cpu_to_le64(word);
1914 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
1915 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
1916 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1918 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1919 vlan_req->vlan_id = vlan_id;
1921 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
1922 smp_mb();
1925 #define QLCNIC_MAC_HASH(MAC)\
1926 ((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1928 static void
1929 qlcnic_send_filter(struct qlcnic_adapter *adapter,
1930 struct qlcnic_host_tx_ring *tx_ring,
1931 struct cmd_desc_type0 *first_desc,
1932 struct sk_buff *skb)
1934 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1935 struct qlcnic_filter *fil, *tmp_fil;
1936 struct hlist_node *tmp_hnode, *n;
1937 struct hlist_head *head;
1938 u64 src_addr = 0;
1939 __le16 vlan_id = 0;
1940 u8 hindex;
1942 if (!compare_ether_addr(phdr->h_source, adapter->mac_addr))
1943 return;
1945 if (adapter->fhash.fnum >= adapter->fhash.fmax)
1946 return;
1948 /* Only NPAR capable devices support vlan based learning*/
1949 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1950 vlan_id = first_desc->vlan_TCI;
1951 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1952 hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1953 head = &(adapter->fhash.fhead[hindex]);
1955 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
1956 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1957 tmp_fil->vlan_id == vlan_id) {
1959 if (jiffies >
1960 (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1961 qlcnic_change_filter(adapter, src_addr, vlan_id,
1962 tx_ring);
1963 tmp_fil->ftime = jiffies;
1964 return;
1968 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
1969 if (!fil)
1970 return;
1972 qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
1974 fil->ftime = jiffies;
1975 fil->vlan_id = vlan_id;
1976 memcpy(fil->faddr, &src_addr, ETH_ALEN);
1977 spin_lock(&adapter->mac_learn_lock);
1978 hlist_add_head(&(fil->fnode), head);
1979 adapter->fhash.fnum++;
1980 spin_unlock(&adapter->mac_learn_lock);
1983 static int
1984 qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
1985 struct cmd_desc_type0 *first_desc,
1986 struct sk_buff *skb)
1988 u8 opcode = 0, hdr_len = 0;
1989 u16 flags = 0, vlan_tci = 0;
1990 int copied, offset, copy_len;
1991 struct cmd_desc_type0 *hwdesc;
1992 struct vlan_ethhdr *vh;
1993 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1994 u16 protocol = ntohs(skb->protocol);
1995 u32 producer = tx_ring->producer;
1997 if (protocol == ETH_P_8021Q) {
1998 vh = (struct vlan_ethhdr *)skb->data;
1999 flags = FLAGS_VLAN_TAGGED;
2000 vlan_tci = vh->h_vlan_TCI;
2001 } else if (vlan_tx_tag_present(skb)) {
2002 flags = FLAGS_VLAN_OOB;
2003 vlan_tci = vlan_tx_tag_get(skb);
2005 if (unlikely(adapter->pvid)) {
2006 if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
2007 return -EIO;
2008 if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
2009 goto set_flags;
2011 flags = FLAGS_VLAN_OOB;
2012 vlan_tci = adapter->pvid;
2014 set_flags:
2015 qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
2016 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2018 if (*(skb->data) & BIT_0) {
2019 flags |= BIT_0;
2020 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
2022 opcode = TX_ETHER_PKT;
2023 if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
2024 skb_shinfo(skb)->gso_size > 0) {
2026 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2028 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
2029 first_desc->total_hdr_length = hdr_len;
2031 opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
2033 /* For LSO, we need to copy the MAC/IP/TCP headers into
2034 * the descriptor ring */
2035 copied = 0;
2036 offset = 2;
2038 if (flags & FLAGS_VLAN_OOB) {
2039 first_desc->total_hdr_length += VLAN_HLEN;
2040 first_desc->tcp_hdr_offset = VLAN_HLEN;
2041 first_desc->ip_hdr_offset = VLAN_HLEN;
2042 /* Only in case of TSO on vlan device */
2043 flags |= FLAGS_VLAN_TAGGED;
2045 /* Create a TSO vlan header template for firmware */
2047 hwdesc = &tx_ring->desc_head[producer];
2048 tx_ring->cmd_buf_arr[producer].skb = NULL;
2050 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2051 offset, hdr_len + VLAN_HLEN);
2053 vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
2054 skb_copy_from_linear_data(skb, vh, 12);
2055 vh->h_vlan_proto = htons(ETH_P_8021Q);
2056 vh->h_vlan_TCI = htons(vlan_tci);
2058 skb_copy_from_linear_data_offset(skb, 12,
2059 (char *)vh + 16, copy_len - 16);
2061 copied = copy_len - VLAN_HLEN;
2062 offset = 0;
2064 producer = get_next_index(producer, tx_ring->num_desc);
2067 while (copied < hdr_len) {
2069 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2070 offset, (hdr_len - copied));
2072 hwdesc = &tx_ring->desc_head[producer];
2073 tx_ring->cmd_buf_arr[producer].skb = NULL;
2075 skb_copy_from_linear_data_offset(skb, copied,
2076 (char *) hwdesc + offset, copy_len);
2078 copied += copy_len;
2079 offset = 0;
2081 producer = get_next_index(producer, tx_ring->num_desc);
2084 tx_ring->producer = producer;
2085 smp_mb();
2086 adapter->stats.lso_frames++;
2088 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2089 u8 l4proto;
2091 if (protocol == ETH_P_IP) {
2092 l4proto = ip_hdr(skb)->protocol;
2094 if (l4proto == IPPROTO_TCP)
2095 opcode = TX_TCP_PKT;
2096 else if (l4proto == IPPROTO_UDP)
2097 opcode = TX_UDP_PKT;
2098 } else if (protocol == ETH_P_IPV6) {
2099 l4proto = ipv6_hdr(skb)->nexthdr;
2101 if (l4proto == IPPROTO_TCP)
2102 opcode = TX_TCPV6_PKT;
2103 else if (l4proto == IPPROTO_UDP)
2104 opcode = TX_UDPV6_PKT;
2107 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
2108 first_desc->ip_hdr_offset += skb_network_offset(skb);
2109 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2111 return 0;
2114 static int
2115 qlcnic_map_tx_skb(struct pci_dev *pdev,
2116 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2118 struct qlcnic_skb_frag *nf;
2119 struct skb_frag_struct *frag;
2120 int i, nr_frags;
2121 dma_addr_t map;
2123 nr_frags = skb_shinfo(skb)->nr_frags;
2124 nf = &pbuf->frag_array[0];
2126 map = pci_map_single(pdev, skb->data,
2127 skb_headlen(skb), PCI_DMA_TODEVICE);
2128 if (pci_dma_mapping_error(pdev, map))
2129 goto out_err;
2131 nf->dma = map;
2132 nf->length = skb_headlen(skb);
2134 for (i = 0; i < nr_frags; i++) {
2135 frag = &skb_shinfo(skb)->frags[i];
2136 nf = &pbuf->frag_array[i+1];
2138 map = pci_map_page(pdev, frag->page, frag->page_offset,
2139 frag->size, PCI_DMA_TODEVICE);
2140 if (pci_dma_mapping_error(pdev, map))
2141 goto unwind;
2143 nf->dma = map;
2144 nf->length = frag->size;
2147 return 0;
2149 unwind:
2150 while (--i >= 0) {
2151 nf = &pbuf->frag_array[i+1];
2152 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2155 nf = &pbuf->frag_array[0];
2156 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2158 out_err:
2159 return -ENOMEM;
2162 static void
2163 qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
2164 struct qlcnic_cmd_buffer *pbuf)
2166 struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
2167 int nr_frags = skb_shinfo(skb)->nr_frags;
2168 int i;
2170 for (i = 0; i < nr_frags; i++) {
2171 nf = &pbuf->frag_array[i+1];
2172 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2175 nf = &pbuf->frag_array[0];
2176 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2177 pbuf->skb = NULL;
2180 static inline void
2181 qlcnic_clear_cmddesc(u64 *desc)
2183 desc[0] = 0ULL;
2184 desc[2] = 0ULL;
2185 desc[7] = 0ULL;
2188 netdev_tx_t
2189 qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2191 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2192 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2193 struct qlcnic_cmd_buffer *pbuf;
2194 struct qlcnic_skb_frag *buffrag;
2195 struct cmd_desc_type0 *hwdesc, *first_desc;
2196 struct pci_dev *pdev;
2197 struct ethhdr *phdr;
2198 int delta = 0;
2199 int i, k;
2201 u32 producer;
2202 int frag_count;
2203 u32 num_txd = tx_ring->num_desc;
2205 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2206 netif_stop_queue(netdev);
2207 return NETDEV_TX_BUSY;
2210 if (adapter->flags & QLCNIC_MACSPOOF) {
2211 phdr = (struct ethhdr *)skb->data;
2212 if (compare_ether_addr(phdr->h_source,
2213 adapter->mac_addr))
2214 goto drop_packet;
2217 frag_count = skb_shinfo(skb)->nr_frags + 1;
2218 /* 14 frags supported for normal packet and
2219 * 32 frags supported for TSO packet
2221 if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
2223 for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
2224 delta += skb_shinfo(skb)->frags[i].size;
2226 if (!__pskb_pull_tail(skb, delta))
2227 goto drop_packet;
2229 frag_count = 1 + skb_shinfo(skb)->nr_frags;
2232 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
2233 netif_stop_queue(netdev);
2234 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2235 netif_start_queue(netdev);
2236 else {
2237 adapter->stats.xmit_off++;
2238 return NETDEV_TX_BUSY;
2242 producer = tx_ring->producer;
2243 pbuf = &tx_ring->cmd_buf_arr[producer];
2245 pdev = adapter->pdev;
2247 first_desc = hwdesc = &tx_ring->desc_head[producer];
2248 qlcnic_clear_cmddesc((u64 *)hwdesc);
2250 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2251 adapter->stats.tx_dma_map_error++;
2252 goto drop_packet;
2255 pbuf->skb = skb;
2256 pbuf->frag_count = frag_count;
2258 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2259 qlcnic_set_tx_port(first_desc, adapter->portnum);
2261 for (i = 0; i < frag_count; i++) {
2263 k = i % 4;
2265 if ((k == 0) && (i > 0)) {
2266 /* move to next desc.*/
2267 producer = get_next_index(producer, num_txd);
2268 hwdesc = &tx_ring->desc_head[producer];
2269 qlcnic_clear_cmddesc((u64 *)hwdesc);
2270 tx_ring->cmd_buf_arr[producer].skb = NULL;
2273 buffrag = &pbuf->frag_array[i];
2275 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2276 switch (k) {
2277 case 0:
2278 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2279 break;
2280 case 1:
2281 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2282 break;
2283 case 2:
2284 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2285 break;
2286 case 3:
2287 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2288 break;
2292 tx_ring->producer = get_next_index(producer, num_txd);
2293 smp_mb();
2295 if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
2296 goto unwind_buff;
2298 if (adapter->mac_learn)
2299 qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2301 adapter->stats.txbytes += skb->len;
2302 adapter->stats.xmitcalled++;
2304 qlcnic_update_cmd_producer(adapter, tx_ring);
2306 return NETDEV_TX_OK;
2308 unwind_buff:
2309 qlcnic_unmap_buffers(pdev, skb, pbuf);
2310 drop_packet:
2311 adapter->stats.txdropped++;
2312 dev_kfree_skb_any(skb);
2313 return NETDEV_TX_OK;
2316 static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2318 struct net_device *netdev = adapter->netdev;
2319 u32 temp, temp_state, temp_val;
2320 int rv = 0;
2322 temp = QLCRD32(adapter, CRB_TEMP_STATE);
2324 temp_state = qlcnic_get_temp_state(temp);
2325 temp_val = qlcnic_get_temp_val(temp);
2327 if (temp_state == QLCNIC_TEMP_PANIC) {
2328 dev_err(&netdev->dev,
2329 "Device temperature %d degrees C exceeds"
2330 " maximum allowed. Hardware has been shut down.\n",
2331 temp_val);
2332 rv = 1;
2333 } else if (temp_state == QLCNIC_TEMP_WARN) {
2334 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2335 dev_err(&netdev->dev,
2336 "Device temperature %d degrees C "
2337 "exceeds operating range."
2338 " Immediate action needed.\n",
2339 temp_val);
2341 } else {
2342 if (adapter->temp == QLCNIC_TEMP_WARN) {
2343 dev_info(&netdev->dev,
2344 "Device temperature is now %d degrees C"
2345 " in normal range.\n", temp_val);
2348 adapter->temp = temp_state;
2349 return rv;
2352 void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2354 struct net_device *netdev = adapter->netdev;
2356 if (adapter->ahw->linkup && !linkup) {
2357 netdev_info(netdev, "NIC Link is down\n");
2358 adapter->ahw->linkup = 0;
2359 if (netif_running(netdev)) {
2360 netif_carrier_off(netdev);
2361 netif_stop_queue(netdev);
2363 } else if (!adapter->ahw->linkup && linkup) {
2364 netdev_info(netdev, "NIC Link is up\n");
2365 adapter->ahw->linkup = 1;
2366 if (netif_running(netdev)) {
2367 netif_carrier_on(netdev);
2368 netif_wake_queue(netdev);
2373 static void qlcnic_tx_timeout(struct net_device *netdev)
2375 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2377 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2378 return;
2380 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
2382 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
2383 adapter->need_fw_reset = 1;
2384 else
2385 adapter->reset_context = 1;
2388 static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2390 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2391 struct net_device_stats *stats = &netdev->stats;
2393 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2394 stats->tx_packets = adapter->stats.xmitfinished;
2395 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
2396 stats->tx_bytes = adapter->stats.txbytes;
2397 stats->rx_dropped = adapter->stats.rxdropped;
2398 stats->tx_dropped = adapter->stats.txdropped;
2400 return stats;
2403 static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
2405 u32 status;
2407 status = readl(adapter->isr_int_vec);
2409 if (!(status & adapter->int_vec_bit))
2410 return IRQ_NONE;
2412 /* check interrupt state machine, to be sure */
2413 status = readl(adapter->crb_int_state_reg);
2414 if (!ISR_LEGACY_INT_TRIGGERED(status))
2415 return IRQ_NONE;
2417 writel(0xffffffff, adapter->tgt_status_reg);
2418 /* read twice to ensure write is flushed */
2419 readl(adapter->isr_int_vec);
2420 readl(adapter->isr_int_vec);
2422 return IRQ_HANDLED;
2425 static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2427 struct qlcnic_host_sds_ring *sds_ring = data;
2428 struct qlcnic_adapter *adapter = sds_ring->adapter;
2430 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2431 goto done;
2432 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2433 writel(0xffffffff, adapter->tgt_status_reg);
2434 goto done;
2437 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2438 return IRQ_NONE;
2440 done:
2441 adapter->diag_cnt++;
2442 qlcnic_enable_int(sds_ring);
2443 return IRQ_HANDLED;
2446 static irqreturn_t qlcnic_intr(int irq, void *data)
2448 struct qlcnic_host_sds_ring *sds_ring = data;
2449 struct qlcnic_adapter *adapter = sds_ring->adapter;
2451 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2452 return IRQ_NONE;
2454 napi_schedule(&sds_ring->napi);
2456 return IRQ_HANDLED;
2459 static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2461 struct qlcnic_host_sds_ring *sds_ring = data;
2462 struct qlcnic_adapter *adapter = sds_ring->adapter;
2464 /* clear interrupt */
2465 writel(0xffffffff, adapter->tgt_status_reg);
2467 napi_schedule(&sds_ring->napi);
2468 return IRQ_HANDLED;
2471 static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2473 struct qlcnic_host_sds_ring *sds_ring = data;
2475 napi_schedule(&sds_ring->napi);
2476 return IRQ_HANDLED;
2479 static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2481 u32 sw_consumer, hw_consumer;
2482 int count = 0, i;
2483 struct qlcnic_cmd_buffer *buffer;
2484 struct pci_dev *pdev = adapter->pdev;
2485 struct net_device *netdev = adapter->netdev;
2486 struct qlcnic_skb_frag *frag;
2487 int done;
2488 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2490 if (!spin_trylock(&adapter->tx_clean_lock))
2491 return 1;
2493 sw_consumer = tx_ring->sw_consumer;
2494 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2496 while (sw_consumer != hw_consumer) {
2497 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2498 if (buffer->skb) {
2499 frag = &buffer->frag_array[0];
2500 pci_unmap_single(pdev, frag->dma, frag->length,
2501 PCI_DMA_TODEVICE);
2502 frag->dma = 0ULL;
2503 for (i = 1; i < buffer->frag_count; i++) {
2504 frag++;
2505 pci_unmap_page(pdev, frag->dma, frag->length,
2506 PCI_DMA_TODEVICE);
2507 frag->dma = 0ULL;
2510 adapter->stats.xmitfinished++;
2511 dev_kfree_skb_any(buffer->skb);
2512 buffer->skb = NULL;
2515 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2516 if (++count >= MAX_STATUS_HANDLE)
2517 break;
2520 if (count && netif_running(netdev)) {
2521 tx_ring->sw_consumer = sw_consumer;
2523 smp_mb();
2525 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
2526 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2527 netif_wake_queue(netdev);
2528 adapter->stats.xmit_on++;
2531 adapter->tx_timeo_cnt = 0;
2534 * If everything is freed up to consumer then check if the ring is full
2535 * If the ring is full then check if more needs to be freed and
2536 * schedule the call back again.
2538 * This happens when there are 2 CPUs. One could be freeing and the
2539 * other filling it. If the ring is full when we get out of here and
2540 * the card has already interrupted the host then the host can miss the
2541 * interrupt.
2543 * There is still a possible race condition and the host could miss an
2544 * interrupt. The card has to take care of this.
2546 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2547 done = (sw_consumer == hw_consumer);
2548 spin_unlock(&adapter->tx_clean_lock);
2550 return done;
2553 static int qlcnic_poll(struct napi_struct *napi, int budget)
2555 struct qlcnic_host_sds_ring *sds_ring =
2556 container_of(napi, struct qlcnic_host_sds_ring, napi);
2558 struct qlcnic_adapter *adapter = sds_ring->adapter;
2560 int tx_complete;
2561 int work_done;
2563 tx_complete = qlcnic_process_cmd_ring(adapter);
2565 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2567 if ((work_done < budget) && tx_complete) {
2568 napi_complete(&sds_ring->napi);
2569 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2570 qlcnic_enable_int(sds_ring);
2573 return work_done;
2576 static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2578 struct qlcnic_host_sds_ring *sds_ring =
2579 container_of(napi, struct qlcnic_host_sds_ring, napi);
2581 struct qlcnic_adapter *adapter = sds_ring->adapter;
2582 int work_done;
2584 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2586 if (work_done < budget) {
2587 napi_complete(&sds_ring->napi);
2588 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2589 qlcnic_enable_int(sds_ring);
2592 return work_done;
2595 #ifdef CONFIG_NET_POLL_CONTROLLER
2596 static void qlcnic_poll_controller(struct net_device *netdev)
2598 int ring;
2599 struct qlcnic_host_sds_ring *sds_ring;
2600 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2601 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2603 disable_irq(adapter->irq);
2604 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2605 sds_ring = &recv_ctx->sds_rings[ring];
2606 qlcnic_intr(adapter->irq, sds_ring);
2608 enable_irq(adapter->irq);
2610 #endif
2612 static void
2613 qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2615 u32 val;
2617 val = adapter->portnum & 0xf;
2618 val |= encoding << 7;
2619 val |= (jiffies - adapter->dev_rst_time) << 8;
2621 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2622 adapter->dev_rst_time = jiffies;
2625 static int
2626 qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
2628 u32 val;
2630 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2631 state != QLCNIC_DEV_NEED_QUISCENT);
2633 if (qlcnic_api_lock(adapter))
2634 return -EIO;
2636 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2638 if (state == QLCNIC_DEV_NEED_RESET)
2639 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2640 else if (state == QLCNIC_DEV_NEED_QUISCENT)
2641 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
2643 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2645 qlcnic_api_unlock(adapter);
2647 return 0;
2650 static int
2651 qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2653 u32 val;
2655 if (qlcnic_api_lock(adapter))
2656 return -EBUSY;
2658 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2659 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
2660 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2662 qlcnic_api_unlock(adapter);
2664 return 0;
2667 static void
2668 qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
2670 u32 val;
2672 if (qlcnic_api_lock(adapter))
2673 goto err;
2675 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2676 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
2677 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
2679 if (failed) {
2680 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2681 dev_info(&adapter->pdev->dev,
2682 "Device state set to Failed. Please Reboot\n");
2683 } else if (!(val & 0x11111111))
2684 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2686 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2687 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
2688 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2690 qlcnic_api_unlock(adapter);
2691 err:
2692 adapter->fw_fail_cnt = 0;
2693 adapter->flags &= ~QLCNIC_FW_HANG;
2694 clear_bit(__QLCNIC_START_FW, &adapter->state);
2695 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2698 /* Grab api lock, before checking state */
2699 static int
2700 qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2702 int act, state, active_mask;
2704 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2705 act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2707 if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
2708 active_mask = (~(1 << (adapter->ahw->pci_func * 4)));
2709 act = act & active_mask;
2712 if (((state & 0x11111111) == (act & 0x11111111)) ||
2713 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2714 return 0;
2715 else
2716 return 1;
2719 static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2721 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2723 if (val != QLCNIC_DRV_IDC_VER) {
2724 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2725 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2728 return 0;
2731 static int
2732 qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2734 u32 val, prev_state;
2735 u8 dev_init_timeo = adapter->dev_init_timeo;
2736 u8 portnum = adapter->portnum;
2737 u8 ret;
2739 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2740 return 1;
2742 if (qlcnic_api_lock(adapter))
2743 return -1;
2745 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2746 if (!(val & (1 << (portnum * 4)))) {
2747 QLC_DEV_SET_REF_CNT(val, portnum);
2748 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
2751 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2752 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
2754 switch (prev_state) {
2755 case QLCNIC_DEV_COLD:
2756 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
2757 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
2758 qlcnic_idc_debug_info(adapter, 0);
2759 qlcnic_api_unlock(adapter);
2760 return 1;
2762 case QLCNIC_DEV_READY:
2763 ret = qlcnic_check_idc_ver(adapter);
2764 qlcnic_api_unlock(adapter);
2765 return ret;
2767 case QLCNIC_DEV_NEED_RESET:
2768 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2769 QLC_DEV_SET_RST_RDY(val, portnum);
2770 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2771 break;
2773 case QLCNIC_DEV_NEED_QUISCENT:
2774 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2775 QLC_DEV_SET_QSCNT_RDY(val, portnum);
2776 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2777 break;
2779 case QLCNIC_DEV_FAILED:
2780 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
2781 qlcnic_api_unlock(adapter);
2782 return -1;
2784 case QLCNIC_DEV_INITIALIZING:
2785 case QLCNIC_DEV_QUISCENT:
2786 break;
2789 qlcnic_api_unlock(adapter);
2791 do {
2792 msleep(1000);
2793 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2795 if (prev_state == QLCNIC_DEV_QUISCENT)
2796 continue;
2797 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
2799 if (!dev_init_timeo) {
2800 dev_err(&adapter->pdev->dev,
2801 "Waiting for device to initialize timeout\n");
2802 return -1;
2805 if (qlcnic_api_lock(adapter))
2806 return -1;
2808 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2809 QLC_DEV_CLR_RST_QSCNT(val, portnum);
2810 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2812 ret = qlcnic_check_idc_ver(adapter);
2813 qlcnic_api_unlock(adapter);
2815 return ret;
2818 static void
2819 qlcnic_fwinit_work(struct work_struct *work)
2821 struct qlcnic_adapter *adapter = container_of(work,
2822 struct qlcnic_adapter, fw_work.work);
2823 u32 dev_state = 0xf;
2824 u32 val;
2826 if (qlcnic_api_lock(adapter))
2827 goto err_ret;
2829 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2830 if (dev_state == QLCNIC_DEV_QUISCENT ||
2831 dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2832 qlcnic_api_unlock(adapter);
2833 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2834 FW_POLL_DELAY * 2);
2835 return;
2838 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
2839 qlcnic_api_unlock(adapter);
2840 goto wait_npar;
2843 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2844 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2845 adapter->reset_ack_timeo);
2846 goto skip_ack_check;
2849 if (!qlcnic_check_drv_state(adapter)) {
2850 skip_ack_check:
2851 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2853 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2854 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2855 QLCNIC_DEV_INITIALIZING);
2856 set_bit(__QLCNIC_START_FW, &adapter->state);
2857 QLCDB(adapter, DRV, "Restarting fw\n");
2858 qlcnic_idc_debug_info(adapter, 0);
2859 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2860 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2861 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2864 qlcnic_api_unlock(adapter);
2866 rtnl_lock();
2867 if (adapter->ahw->fw_dump.enable &&
2868 (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
2869 QLCDB(adapter, DRV, "Take FW dump\n");
2870 qlcnic_dump_fw(adapter);
2871 adapter->flags |= QLCNIC_FW_HANG;
2873 rtnl_unlock();
2875 adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
2876 if (!adapter->nic_ops->start_firmware(adapter)) {
2877 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2878 adapter->fw_wait_cnt = 0;
2879 return;
2881 goto err_ret;
2884 qlcnic_api_unlock(adapter);
2886 wait_npar:
2887 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2888 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
2890 switch (dev_state) {
2891 case QLCNIC_DEV_READY:
2892 if (!adapter->nic_ops->start_firmware(adapter)) {
2893 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2894 adapter->fw_wait_cnt = 0;
2895 return;
2897 case QLCNIC_DEV_FAILED:
2898 break;
2899 default:
2900 qlcnic_schedule_work(adapter,
2901 qlcnic_fwinit_work, FW_POLL_DELAY);
2902 return;
2905 err_ret:
2906 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2907 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
2908 netif_device_attach(adapter->netdev);
2909 qlcnic_clr_all_drv_state(adapter, 0);
2912 static void
2913 qlcnic_detach_work(struct work_struct *work)
2915 struct qlcnic_adapter *adapter = container_of(work,
2916 struct qlcnic_adapter, fw_work.work);
2917 struct net_device *netdev = adapter->netdev;
2918 u32 status;
2920 netif_device_detach(netdev);
2922 /* Dont grab rtnl lock during Quiscent mode */
2923 if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2924 if (netif_running(netdev))
2925 __qlcnic_down(adapter, netdev);
2926 } else
2927 qlcnic_down(adapter, netdev);
2929 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2931 if (status & QLCNIC_RCODE_FATAL_ERROR) {
2932 dev_err(&adapter->pdev->dev,
2933 "Detaching the device: peg halt status1=0x%x\n",
2934 status);
2936 if (QLCNIC_FWERROR_CODE(status) == QLCNIC_FWERROR_FAN_FAILURE) {
2937 dev_err(&adapter->pdev->dev,
2938 "On board active cooling fan failed. "
2939 "Device has been halted.\n");
2940 dev_err(&adapter->pdev->dev,
2941 "Replace the adapter.\n");
2944 goto err_ret;
2947 if (adapter->temp == QLCNIC_TEMP_PANIC) {
2948 dev_err(&adapter->pdev->dev, "Detaching the device: temp=%d\n",
2949 adapter->temp);
2950 goto err_ret;
2953 /* Dont ack if this instance is the reset owner */
2954 if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
2955 if (qlcnic_set_drv_state(adapter, adapter->dev_state)) {
2956 dev_err(&adapter->pdev->dev,
2957 "Failed to set driver state,"
2958 "detaching the device.\n");
2959 goto err_ret;
2963 adapter->fw_wait_cnt = 0;
2965 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2967 return;
2969 err_ret:
2970 netif_device_attach(netdev);
2971 qlcnic_clr_all_drv_state(adapter, 1);
2974 /*Transit NPAR state to NON Operational */
2975 static void
2976 qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
2978 u32 state;
2980 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2981 if (state == QLCNIC_DEV_NPAR_NON_OPER)
2982 return;
2984 if (qlcnic_api_lock(adapter))
2985 return;
2986 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
2987 qlcnic_api_unlock(adapter);
2990 /*Transit to RESET state from READY state only */
2991 void
2992 qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2994 u32 state;
2996 adapter->need_fw_reset = 1;
2997 if (qlcnic_api_lock(adapter))
2998 return;
3000 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
3002 if (state == QLCNIC_DEV_READY) {
3003 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
3004 adapter->flags |= QLCNIC_FW_RESET_OWNER;
3005 QLCDB(adapter, DRV, "NEED_RESET state set\n");
3006 qlcnic_idc_debug_info(adapter, 0);
3009 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
3010 qlcnic_api_unlock(adapter);
3013 /* Transit to NPAR READY state from NPAR NOT READY state */
3014 static void
3015 qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
3017 if (qlcnic_api_lock(adapter))
3018 return;
3020 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
3021 QLCDB(adapter, DRV, "NPAR operational state set\n");
3023 qlcnic_api_unlock(adapter);
3026 static void
3027 qlcnic_schedule_work(struct qlcnic_adapter *adapter,
3028 work_func_t func, int delay)
3030 if (test_bit(__QLCNIC_AER, &adapter->state))
3031 return;
3033 INIT_DELAYED_WORK(&adapter->fw_work, func);
3034 queue_delayed_work(qlcnic_wq, &adapter->fw_work,
3035 round_jiffies_relative(delay));
3038 static void
3039 qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
3041 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3042 msleep(10);
3044 cancel_delayed_work_sync(&adapter->fw_work);
3047 static void
3048 qlcnic_attach_work(struct work_struct *work)
3050 struct qlcnic_adapter *adapter = container_of(work,
3051 struct qlcnic_adapter, fw_work.work);
3052 struct net_device *netdev = adapter->netdev;
3053 u32 npar_state;
3055 if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
3056 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3057 if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3058 qlcnic_clr_all_drv_state(adapter, 0);
3059 else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3060 qlcnic_schedule_work(adapter, qlcnic_attach_work,
3061 FW_POLL_DELAY);
3062 else
3063 goto attach;
3064 QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3065 return;
3067 attach:
3068 if (netif_running(netdev)) {
3069 if (qlcnic_up(adapter, netdev))
3070 goto done;
3072 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3075 done:
3076 netif_device_attach(netdev);
3077 adapter->fw_fail_cnt = 0;
3078 adapter->flags &= ~QLCNIC_FW_HANG;
3079 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3081 if (!qlcnic_clr_drv_state(adapter))
3082 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3083 FW_POLL_DELAY);
3086 static int
3087 qlcnic_check_health(struct qlcnic_adapter *adapter)
3089 u32 state = 0, heartbeat;
3090 struct net_device *netdev = adapter->netdev;
3092 if (qlcnic_check_temp(adapter))
3093 goto detach;
3095 if (adapter->need_fw_reset)
3096 qlcnic_dev_request_reset(adapter);
3098 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
3099 if (state == QLCNIC_DEV_NEED_RESET) {
3100 qlcnic_set_npar_non_operational(adapter);
3101 adapter->need_fw_reset = 1;
3102 } else if (state == QLCNIC_DEV_NEED_QUISCENT)
3103 goto detach;
3105 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3106 if (heartbeat != adapter->heartbeat) {
3107 adapter->heartbeat = heartbeat;
3108 adapter->fw_fail_cnt = 0;
3109 if (adapter->need_fw_reset)
3110 goto detach;
3112 if (adapter->reset_context && auto_fw_reset) {
3113 qlcnic_reset_hw_context(adapter);
3114 adapter->netdev->trans_start = jiffies;
3117 return 0;
3120 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3121 return 0;
3123 adapter->flags |= QLCNIC_FW_HANG;
3125 qlcnic_dev_request_reset(adapter);
3127 if (auto_fw_reset)
3128 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
3130 dev_info(&netdev->dev, "firmware hang detected\n");
3131 dev_info(&adapter->pdev->dev, "Dumping hw/fw registers\n"
3132 "PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
3133 "PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
3134 "PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
3135 "PEG_NET_4_PC: 0x%x\n",
3136 QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1),
3137 QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS2),
3138 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c),
3139 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c),
3140 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c),
3141 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c),
3142 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c));
3143 detach:
3144 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3145 QLCNIC_DEV_NEED_RESET;
3147 if (auto_fw_reset &&
3148 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3150 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
3151 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3154 return 1;
3157 static void
3158 qlcnic_fw_poll_work(struct work_struct *work)
3160 struct qlcnic_adapter *adapter = container_of(work,
3161 struct qlcnic_adapter, fw_work.work);
3163 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3164 goto reschedule;
3167 if (qlcnic_check_health(adapter))
3168 return;
3170 if (adapter->fhash.fnum)
3171 qlcnic_prune_lb_filters(adapter);
3173 reschedule:
3174 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3177 static int qlcnic_is_first_func(struct pci_dev *pdev)
3179 struct pci_dev *oth_pdev;
3180 int val = pdev->devfn;
3182 while (val-- > 0) {
3183 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3184 (pdev->bus), pdev->bus->number,
3185 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
3186 if (!oth_pdev)
3187 continue;
3189 if (oth_pdev->current_state != PCI_D3cold) {
3190 pci_dev_put(oth_pdev);
3191 return 0;
3193 pci_dev_put(oth_pdev);
3195 return 1;
3198 static int qlcnic_attach_func(struct pci_dev *pdev)
3200 int err, first_func;
3201 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3202 struct net_device *netdev = adapter->netdev;
3204 pdev->error_state = pci_channel_io_normal;
3206 err = pci_enable_device(pdev);
3207 if (err)
3208 return err;
3210 pci_set_power_state(pdev, PCI_D0);
3211 pci_set_master(pdev);
3212 pci_restore_state(pdev);
3214 first_func = qlcnic_is_first_func(pdev);
3216 if (qlcnic_api_lock(adapter))
3217 return -EINVAL;
3219 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
3220 adapter->need_fw_reset = 1;
3221 set_bit(__QLCNIC_START_FW, &adapter->state);
3222 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3223 QLCDB(adapter, DRV, "Restarting fw\n");
3225 qlcnic_api_unlock(adapter);
3227 err = adapter->nic_ops->start_firmware(adapter);
3228 if (err)
3229 return err;
3231 qlcnic_clr_drv_state(adapter);
3232 qlcnic_setup_intr(adapter);
3234 if (netif_running(netdev)) {
3235 err = qlcnic_attach(adapter);
3236 if (err) {
3237 qlcnic_clr_all_drv_state(adapter, 1);
3238 clear_bit(__QLCNIC_AER, &adapter->state);
3239 netif_device_attach(netdev);
3240 return err;
3243 err = qlcnic_up(adapter, netdev);
3244 if (err)
3245 goto done;
3247 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3249 done:
3250 netif_device_attach(netdev);
3251 return err;
3254 static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3255 pci_channel_state_t state)
3257 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3258 struct net_device *netdev = adapter->netdev;
3260 if (state == pci_channel_io_perm_failure)
3261 return PCI_ERS_RESULT_DISCONNECT;
3263 if (state == pci_channel_io_normal)
3264 return PCI_ERS_RESULT_RECOVERED;
3266 set_bit(__QLCNIC_AER, &adapter->state);
3267 netif_device_detach(netdev);
3269 cancel_delayed_work_sync(&adapter->fw_work);
3271 if (netif_running(netdev))
3272 qlcnic_down(adapter, netdev);
3274 qlcnic_detach(adapter);
3275 qlcnic_teardown_intr(adapter);
3277 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3279 pci_save_state(pdev);
3280 pci_disable_device(pdev);
3282 return PCI_ERS_RESULT_NEED_RESET;
3285 static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3287 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3288 PCI_ERS_RESULT_RECOVERED;
3291 static void qlcnic_io_resume(struct pci_dev *pdev)
3293 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3295 pci_cleanup_aer_uncorrect_error_status(pdev);
3297 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3298 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3299 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3300 FW_POLL_DELAY);
3303 static int
3304 qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3306 int err;
3308 err = qlcnic_can_start_firmware(adapter);
3309 if (err)
3310 return err;
3312 err = qlcnic_check_npar_opertional(adapter);
3313 if (err)
3314 return err;
3316 err = qlcnic_initialize_nic(adapter);
3317 if (err)
3318 return err;
3320 qlcnic_check_options(adapter);
3322 err = qlcnic_set_eswitch_port_config(adapter);
3323 if (err)
3324 return err;
3326 adapter->need_fw_reset = 0;
3328 return err;
3331 static int
3332 qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3334 return -EOPNOTSUPP;
3337 static int
3338 qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3340 return -EOPNOTSUPP;
3343 static ssize_t
3344 qlcnic_store_bridged_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;
3349 int ret = -EINVAL;
3351 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3352 goto err_out;
3354 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
3355 goto err_out;
3357 if (strict_strtoul(buf, 2, &new))
3358 goto err_out;
3360 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
3361 ret = len;
3363 err_out:
3364 return ret;
3367 static ssize_t
3368 qlcnic_show_bridged_mode(struct device *dev,
3369 struct device_attribute *attr, char *buf)
3371 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3372 int bridged_mode = 0;
3374 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3375 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3377 return sprintf(buf, "%d\n", bridged_mode);
3380 static struct device_attribute dev_attr_bridged_mode = {
3381 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3382 .show = qlcnic_show_bridged_mode,
3383 .store = qlcnic_store_bridged_mode,
3386 static ssize_t
3387 qlcnic_store_diag_mode(struct device *dev,
3388 struct device_attribute *attr, const char *buf, size_t len)
3390 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3391 unsigned long new;
3393 if (strict_strtoul(buf, 2, &new))
3394 return -EINVAL;
3396 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3397 adapter->flags ^= QLCNIC_DIAG_ENABLED;
3399 return len;
3402 static ssize_t
3403 qlcnic_show_diag_mode(struct device *dev,
3404 struct device_attribute *attr, char *buf)
3406 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3408 return sprintf(buf, "%d\n",
3409 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
3412 static struct device_attribute dev_attr_diag_mode = {
3413 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3414 .show = qlcnic_show_diag_mode,
3415 .store = qlcnic_store_diag_mode,
3418 int qlcnic_validate_max_rss(struct net_device *netdev, u8 max_hw, u8 val)
3420 if (!use_msi_x && !use_msi) {
3421 netdev_info(netdev, "no msix or msi support, hence no rss\n");
3422 return -EINVAL;
3425 if ((val > max_hw) || (val < 2) || !is_power_of_2(val)) {
3426 netdev_info(netdev, "rss_ring valid range [2 - %x] in "
3427 " powers of 2\n", max_hw);
3428 return -EINVAL;
3430 return 0;
3434 int qlcnic_set_max_rss(struct qlcnic_adapter *adapter, u8 data)
3436 struct net_device *netdev = adapter->netdev;
3437 int err = 0;
3439 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3440 return -EBUSY;
3442 netif_device_detach(netdev);
3443 if (netif_running(netdev))
3444 __qlcnic_down(adapter, netdev);
3445 qlcnic_detach(adapter);
3446 qlcnic_teardown_intr(adapter);
3448 if (qlcnic_enable_msix(adapter, data)) {
3449 netdev_info(netdev, "failed setting max_rss; rss disabled\n");
3450 qlcnic_enable_msi_legacy(adapter);
3453 if (netif_running(netdev)) {
3454 err = qlcnic_attach(adapter);
3455 if (err)
3456 goto done;
3457 err = __qlcnic_up(adapter, netdev);
3458 if (err)
3459 goto done;
3460 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3462 done:
3463 netif_device_attach(netdev);
3464 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3465 return err;
3468 static int
3469 qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon, u8 *state,
3470 u8 *rate)
3472 *rate = LSB(beacon);
3473 *state = MSB(beacon);
3475 QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
3477 if (!*state) {
3478 *rate = __QLCNIC_MAX_LED_RATE;
3479 return 0;
3480 } else if (*state > __QLCNIC_MAX_LED_STATE)
3481 return -EINVAL;
3483 if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
3484 return -EINVAL;
3486 return 0;
3489 static ssize_t
3490 qlcnic_store_beacon(struct device *dev,
3491 struct device_attribute *attr, const char *buf, size_t len)
3493 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3494 int max_sds_rings = adapter->max_sds_rings;
3495 int dev_down = 0;
3496 u16 beacon;
3497 u8 b_state, b_rate;
3498 int err;
3500 if (len != sizeof(u16))
3501 return QL_STATUS_INVALID_PARAM;
3503 memcpy(&beacon, buf, sizeof(u16));
3504 err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
3505 if (err)
3506 return err;
3508 if (adapter->ahw->beacon_state == b_state)
3509 return len;
3511 if (!adapter->ahw->beacon_state)
3512 if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state))
3513 return -EBUSY;
3515 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
3516 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3517 return -EIO;
3518 err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
3519 if (err) {
3520 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3521 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
3522 return err;
3524 dev_down = 1;
3527 err = qlcnic_config_led(adapter, b_state, b_rate);
3529 if (!err) {
3530 adapter->ahw->beacon_state = b_state;
3531 err = len;
3534 if (dev_down) {
3535 qlcnic_diag_free_res(adapter->netdev, max_sds_rings);
3536 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3539 if (!b_state)
3540 clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
3542 return err;
3545 static ssize_t
3546 qlcnic_show_beacon(struct device *dev,
3547 struct device_attribute *attr, char *buf)
3549 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3551 return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
3554 static struct device_attribute dev_attr_beacon = {
3555 .attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
3556 .show = qlcnic_show_beacon,
3557 .store = qlcnic_store_beacon,
3560 static int
3561 qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3562 loff_t offset, size_t size)
3564 size_t crb_size = 4;
3566 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3567 return -EIO;
3569 if (offset < QLCNIC_PCI_CRBSPACE) {
3570 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3571 QLCNIC_PCI_CAMQM_END))
3572 crb_size = 8;
3573 else
3574 return -EINVAL;
3577 if ((size != crb_size) || (offset & (crb_size-1)))
3578 return -EINVAL;
3580 return 0;
3583 static ssize_t
3584 qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3585 struct bin_attribute *attr,
3586 char *buf, loff_t offset, size_t size)
3588 struct device *dev = container_of(kobj, struct device, kobj);
3589 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3590 u32 data;
3591 u64 qmdata;
3592 int ret;
3594 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3595 if (ret != 0)
3596 return ret;
3598 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3599 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3600 memcpy(buf, &qmdata, size);
3601 } else {
3602 data = QLCRD32(adapter, offset);
3603 memcpy(buf, &data, size);
3605 return size;
3608 static ssize_t
3609 qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3610 struct bin_attribute *attr,
3611 char *buf, loff_t offset, size_t size)
3613 struct device *dev = container_of(kobj, struct device, kobj);
3614 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3615 u32 data;
3616 u64 qmdata;
3617 int ret;
3619 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3620 if (ret != 0)
3621 return ret;
3623 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3624 memcpy(&qmdata, buf, size);
3625 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3626 } else {
3627 memcpy(&data, buf, size);
3628 QLCWR32(adapter, offset, data);
3630 return size;
3633 static int
3634 qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3635 loff_t offset, size_t size)
3637 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3638 return -EIO;
3640 if ((size != 8) || (offset & 0x7))
3641 return -EIO;
3643 return 0;
3646 static ssize_t
3647 qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3648 struct bin_attribute *attr,
3649 char *buf, loff_t offset, size_t size)
3651 struct device *dev = container_of(kobj, struct device, kobj);
3652 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3653 u64 data;
3654 int ret;
3656 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3657 if (ret != 0)
3658 return ret;
3660 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3661 return -EIO;
3663 memcpy(buf, &data, size);
3665 return size;
3668 static ssize_t
3669 qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3670 struct bin_attribute *attr,
3671 char *buf, loff_t offset, size_t size)
3673 struct device *dev = container_of(kobj, struct device, kobj);
3674 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3675 u64 data;
3676 int ret;
3678 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3679 if (ret != 0)
3680 return ret;
3682 memcpy(&data, buf, size);
3684 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3685 return -EIO;
3687 return size;
3690 static struct bin_attribute bin_attr_crb = {
3691 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3692 .size = 0,
3693 .read = qlcnic_sysfs_read_crb,
3694 .write = qlcnic_sysfs_write_crb,
3697 static struct bin_attribute bin_attr_mem = {
3698 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3699 .size = 0,
3700 .read = qlcnic_sysfs_read_mem,
3701 .write = qlcnic_sysfs_write_mem,
3704 static int
3705 validate_pm_config(struct qlcnic_adapter *adapter,
3706 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3709 u8 src_pci_func, s_esw_id, d_esw_id;
3710 u8 dest_pci_func;
3711 int i;
3713 for (i = 0; i < count; i++) {
3714 src_pci_func = pm_cfg[i].pci_func;
3715 dest_pci_func = pm_cfg[i].dest_npar;
3716 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3717 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3718 return QL_STATUS_INVALID_PARAM;
3720 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3721 return QL_STATUS_INVALID_PARAM;
3723 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3724 return QL_STATUS_INVALID_PARAM;
3726 s_esw_id = adapter->npars[src_pci_func].phy_port;
3727 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3729 if (s_esw_id != d_esw_id)
3730 return QL_STATUS_INVALID_PARAM;
3733 return 0;
3737 static ssize_t
3738 qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3739 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3741 struct device *dev = container_of(kobj, struct device, kobj);
3742 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3743 struct qlcnic_pm_func_cfg *pm_cfg;
3744 u32 id, action, pci_func;
3745 int count, rem, i, ret;
3747 count = size / sizeof(struct qlcnic_pm_func_cfg);
3748 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3749 if (rem)
3750 return QL_STATUS_INVALID_PARAM;
3752 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3754 ret = validate_pm_config(adapter, pm_cfg, count);
3755 if (ret)
3756 return ret;
3757 for (i = 0; i < count; i++) {
3758 pci_func = pm_cfg[i].pci_func;
3759 action = !!pm_cfg[i].action;
3760 id = adapter->npars[pci_func].phy_port;
3761 ret = qlcnic_config_port_mirroring(adapter, id,
3762 action, pci_func);
3763 if (ret)
3764 return ret;
3767 for (i = 0; i < count; i++) {
3768 pci_func = pm_cfg[i].pci_func;
3769 id = adapter->npars[pci_func].phy_port;
3770 adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
3771 adapter->npars[pci_func].dest_npar = id;
3773 return size;
3776 static ssize_t
3777 qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3778 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3780 struct device *dev = container_of(kobj, struct device, kobj);
3781 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3782 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3783 int i;
3785 if (size != sizeof(pm_cfg))
3786 return QL_STATUS_INVALID_PARAM;
3788 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3789 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3790 continue;
3791 pm_cfg[i].action = adapter->npars[i].enable_pm;
3792 pm_cfg[i].dest_npar = 0;
3793 pm_cfg[i].pci_func = i;
3795 memcpy(buf, &pm_cfg, size);
3797 return size;
3800 static int
3801 validate_esw_config(struct qlcnic_adapter *adapter,
3802 struct qlcnic_esw_func_cfg *esw_cfg, int count)
3804 u32 op_mode;
3805 u8 pci_func;
3806 int i;
3808 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
3810 for (i = 0; i < count; i++) {
3811 pci_func = esw_cfg[i].pci_func;
3812 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3813 return QL_STATUS_INVALID_PARAM;
3815 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3816 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3817 return QL_STATUS_INVALID_PARAM;
3819 switch (esw_cfg[i].op_mode) {
3820 case QLCNIC_PORT_DEFAULTS:
3821 if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
3822 QLCNIC_NON_PRIV_FUNC) {
3823 if (esw_cfg[i].mac_anti_spoof != 0)
3824 return QL_STATUS_INVALID_PARAM;
3825 if (esw_cfg[i].mac_override != 1)
3826 return QL_STATUS_INVALID_PARAM;
3827 if (esw_cfg[i].promisc_mode != 1)
3828 return QL_STATUS_INVALID_PARAM;
3830 break;
3831 case QLCNIC_ADD_VLAN:
3832 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3833 return QL_STATUS_INVALID_PARAM;
3834 if (!esw_cfg[i].op_type)
3835 return QL_STATUS_INVALID_PARAM;
3836 break;
3837 case QLCNIC_DEL_VLAN:
3838 if (!esw_cfg[i].op_type)
3839 return QL_STATUS_INVALID_PARAM;
3840 break;
3841 default:
3842 return QL_STATUS_INVALID_PARAM;
3845 return 0;
3848 static ssize_t
3849 qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3850 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3852 struct device *dev = container_of(kobj, struct device, kobj);
3853 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3854 struct qlcnic_esw_func_cfg *esw_cfg;
3855 struct qlcnic_npar_info *npar;
3856 int count, rem, i, ret;
3857 u8 pci_func, op_mode = 0;
3859 count = size / sizeof(struct qlcnic_esw_func_cfg);
3860 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3861 if (rem)
3862 return QL_STATUS_INVALID_PARAM;
3864 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3865 ret = validate_esw_config(adapter, esw_cfg, count);
3866 if (ret)
3867 return ret;
3869 for (i = 0; i < count; i++) {
3870 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3871 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3872 return QL_STATUS_INVALID_PARAM;
3874 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
3875 continue;
3877 op_mode = esw_cfg[i].op_mode;
3878 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3879 esw_cfg[i].op_mode = op_mode;
3880 esw_cfg[i].pci_func = adapter->ahw->pci_func;
3882 switch (esw_cfg[i].op_mode) {
3883 case QLCNIC_PORT_DEFAULTS:
3884 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3885 break;
3886 case QLCNIC_ADD_VLAN:
3887 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3888 break;
3889 case QLCNIC_DEL_VLAN:
3890 esw_cfg[i].vlan_id = 0;
3891 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3892 break;
3896 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3897 goto out;
3899 for (i = 0; i < count; i++) {
3900 pci_func = esw_cfg[i].pci_func;
3901 npar = &adapter->npars[pci_func];
3902 switch (esw_cfg[i].op_mode) {
3903 case QLCNIC_PORT_DEFAULTS:
3904 npar->promisc_mode = esw_cfg[i].promisc_mode;
3905 npar->mac_override = esw_cfg[i].mac_override;
3906 npar->offload_flags = esw_cfg[i].offload_flags;
3907 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3908 npar->discard_tagged = esw_cfg[i].discard_tagged;
3909 break;
3910 case QLCNIC_ADD_VLAN:
3911 npar->pvid = esw_cfg[i].vlan_id;
3912 break;
3913 case QLCNIC_DEL_VLAN:
3914 npar->pvid = 0;
3915 break;
3918 out:
3919 return size;
3922 static ssize_t
3923 qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3924 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3926 struct device *dev = container_of(kobj, struct device, kobj);
3927 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3928 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
3929 u8 i;
3931 if (size != sizeof(esw_cfg))
3932 return QL_STATUS_INVALID_PARAM;
3934 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3935 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3936 continue;
3937 esw_cfg[i].pci_func = i;
3938 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
3939 return QL_STATUS_INVALID_PARAM;
3941 memcpy(buf, &esw_cfg, size);
3943 return size;
3946 static int
3947 validate_npar_config(struct qlcnic_adapter *adapter,
3948 struct qlcnic_npar_func_cfg *np_cfg, int count)
3950 u8 pci_func, i;
3952 for (i = 0; i < count; i++) {
3953 pci_func = np_cfg[i].pci_func;
3954 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3955 return QL_STATUS_INVALID_PARAM;
3957 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3958 return QL_STATUS_INVALID_PARAM;
3960 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
3961 !IS_VALID_BW(np_cfg[i].max_bw))
3962 return QL_STATUS_INVALID_PARAM;
3964 return 0;
3967 static ssize_t
3968 qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
3969 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3971 struct device *dev = container_of(kobj, struct device, kobj);
3972 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3973 struct qlcnic_info nic_info;
3974 struct qlcnic_npar_func_cfg *np_cfg;
3975 int i, count, rem, ret;
3976 u8 pci_func;
3978 count = size / sizeof(struct qlcnic_npar_func_cfg);
3979 rem = size % sizeof(struct qlcnic_npar_func_cfg);
3980 if (rem)
3981 return QL_STATUS_INVALID_PARAM;
3983 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
3984 ret = validate_npar_config(adapter, np_cfg, count);
3985 if (ret)
3986 return ret;
3988 for (i = 0; i < count ; i++) {
3989 pci_func = np_cfg[i].pci_func;
3990 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
3991 if (ret)
3992 return ret;
3993 nic_info.pci_func = pci_func;
3994 nic_info.min_tx_bw = np_cfg[i].min_bw;
3995 nic_info.max_tx_bw = np_cfg[i].max_bw;
3996 ret = qlcnic_set_nic_info(adapter, &nic_info);
3997 if (ret)
3998 return ret;
3999 adapter->npars[i].min_bw = nic_info.min_tx_bw;
4000 adapter->npars[i].max_bw = nic_info.max_tx_bw;
4003 return size;
4006 static ssize_t
4007 qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
4008 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4010 struct device *dev = container_of(kobj, struct device, kobj);
4011 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4012 struct qlcnic_info nic_info;
4013 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
4014 int i, ret;
4016 if (size != sizeof(np_cfg))
4017 return QL_STATUS_INVALID_PARAM;
4019 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4020 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
4021 continue;
4022 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
4023 if (ret)
4024 return ret;
4026 np_cfg[i].pci_func = i;
4027 np_cfg[i].op_mode = (u8)nic_info.op_mode;
4028 np_cfg[i].port_num = nic_info.phys_port;
4029 np_cfg[i].fw_capab = nic_info.capabilities;
4030 np_cfg[i].min_bw = nic_info.min_tx_bw ;
4031 np_cfg[i].max_bw = nic_info.max_tx_bw;
4032 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
4033 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
4035 memcpy(buf, &np_cfg, size);
4036 return size;
4039 static ssize_t
4040 qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
4041 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4043 struct device *dev = container_of(kobj, struct device, kobj);
4044 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4045 struct qlcnic_esw_statistics port_stats;
4046 int ret;
4048 if (size != sizeof(struct qlcnic_esw_statistics))
4049 return QL_STATUS_INVALID_PARAM;
4051 if (offset >= QLCNIC_MAX_PCI_FUNC)
4052 return QL_STATUS_INVALID_PARAM;
4054 memset(&port_stats, 0, size);
4055 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
4056 &port_stats.rx);
4057 if (ret)
4058 return ret;
4060 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
4061 &port_stats.tx);
4062 if (ret)
4063 return ret;
4065 memcpy(buf, &port_stats, size);
4066 return size;
4069 static ssize_t
4070 qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
4071 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4073 struct device *dev = container_of(kobj, struct device, kobj);
4074 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4075 struct qlcnic_esw_statistics esw_stats;
4076 int ret;
4078 if (size != sizeof(struct qlcnic_esw_statistics))
4079 return QL_STATUS_INVALID_PARAM;
4081 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
4082 return QL_STATUS_INVALID_PARAM;
4084 memset(&esw_stats, 0, size);
4085 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
4086 &esw_stats.rx);
4087 if (ret)
4088 return ret;
4090 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
4091 &esw_stats.tx);
4092 if (ret)
4093 return ret;
4095 memcpy(buf, &esw_stats, size);
4096 return size;
4099 static ssize_t
4100 qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
4101 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4103 struct device *dev = container_of(kobj, struct device, kobj);
4104 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4105 int ret;
4107 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
4108 return QL_STATUS_INVALID_PARAM;
4110 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
4111 QLCNIC_QUERY_RX_COUNTER);
4112 if (ret)
4113 return ret;
4115 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
4116 QLCNIC_QUERY_TX_COUNTER);
4117 if (ret)
4118 return ret;
4120 return size;
4123 static ssize_t
4124 qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
4125 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4128 struct device *dev = container_of(kobj, struct device, kobj);
4129 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4130 int ret;
4132 if (offset >= QLCNIC_MAX_PCI_FUNC)
4133 return QL_STATUS_INVALID_PARAM;
4135 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4136 QLCNIC_QUERY_RX_COUNTER);
4137 if (ret)
4138 return ret;
4140 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4141 QLCNIC_QUERY_TX_COUNTER);
4142 if (ret)
4143 return ret;
4145 return size;
4148 static ssize_t
4149 qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
4150 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4152 struct device *dev = container_of(kobj, struct device, kobj);
4153 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4154 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
4155 struct qlcnic_pci_info *pci_info;
4156 int i, ret;
4158 if (size != sizeof(pci_cfg))
4159 return QL_STATUS_INVALID_PARAM;
4161 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
4162 if (!pci_info)
4163 return -ENOMEM;
4165 ret = qlcnic_get_pci_info(adapter, pci_info);
4166 if (ret) {
4167 kfree(pci_info);
4168 return ret;
4171 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4172 pci_cfg[i].pci_func = pci_info[i].id;
4173 pci_cfg[i].func_type = pci_info[i].type;
4174 pci_cfg[i].port_num = pci_info[i].default_port;
4175 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
4176 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
4177 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
4179 memcpy(buf, &pci_cfg, size);
4180 kfree(pci_info);
4181 return size;
4183 static struct bin_attribute bin_attr_npar_config = {
4184 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
4185 .size = 0,
4186 .read = qlcnic_sysfs_read_npar_config,
4187 .write = qlcnic_sysfs_write_npar_config,
4190 static struct bin_attribute bin_attr_pci_config = {
4191 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
4192 .size = 0,
4193 .read = qlcnic_sysfs_read_pci_config,
4194 .write = NULL,
4197 static struct bin_attribute bin_attr_port_stats = {
4198 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
4199 .size = 0,
4200 .read = qlcnic_sysfs_get_port_stats,
4201 .write = qlcnic_sysfs_clear_port_stats,
4204 static struct bin_attribute bin_attr_esw_stats = {
4205 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
4206 .size = 0,
4207 .read = qlcnic_sysfs_get_esw_stats,
4208 .write = qlcnic_sysfs_clear_esw_stats,
4211 static struct bin_attribute bin_attr_esw_config = {
4212 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
4213 .size = 0,
4214 .read = qlcnic_sysfs_read_esw_config,
4215 .write = qlcnic_sysfs_write_esw_config,
4218 static struct bin_attribute bin_attr_pm_config = {
4219 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
4220 .size = 0,
4221 .read = qlcnic_sysfs_read_pm_config,
4222 .write = qlcnic_sysfs_write_pm_config,
4225 static void
4226 qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
4228 struct device *dev = &adapter->pdev->dev;
4230 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4231 if (device_create_file(dev, &dev_attr_bridged_mode))
4232 dev_warn(dev,
4233 "failed to create bridged_mode sysfs entry\n");
4236 static void
4237 qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
4239 struct device *dev = &adapter->pdev->dev;
4241 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4242 device_remove_file(dev, &dev_attr_bridged_mode);
4245 static void
4246 qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
4248 struct device *dev = &adapter->pdev->dev;
4250 if (device_create_bin_file(dev, &bin_attr_port_stats))
4251 dev_info(dev, "failed to create port stats sysfs entry");
4253 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4254 return;
4255 if (device_create_file(dev, &dev_attr_diag_mode))
4256 dev_info(dev, "failed to create diag_mode sysfs entry\n");
4257 if (device_create_file(dev, &dev_attr_beacon))
4258 dev_info(dev, "failed to create beacon sysfs entry");
4259 if (device_create_bin_file(dev, &bin_attr_crb))
4260 dev_info(dev, "failed to create crb sysfs entry\n");
4261 if (device_create_bin_file(dev, &bin_attr_mem))
4262 dev_info(dev, "failed to create mem sysfs entry\n");
4263 if (device_create_bin_file(dev, &bin_attr_pci_config))
4264 dev_info(dev, "failed to create pci config sysfs entry");
4265 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4266 return;
4267 if (device_create_bin_file(dev, &bin_attr_esw_config))
4268 dev_info(dev, "failed to create esw config sysfs entry");
4269 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
4270 return;
4271 if (device_create_bin_file(dev, &bin_attr_npar_config))
4272 dev_info(dev, "failed to create npar config sysfs entry");
4273 if (device_create_bin_file(dev, &bin_attr_pm_config))
4274 dev_info(dev, "failed to create pm config sysfs entry");
4275 if (device_create_bin_file(dev, &bin_attr_esw_stats))
4276 dev_info(dev, "failed to create eswitch stats sysfs entry");
4279 static void
4280 qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4282 struct device *dev = &adapter->pdev->dev;
4284 device_remove_bin_file(dev, &bin_attr_port_stats);
4286 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4287 return;
4288 device_remove_file(dev, &dev_attr_diag_mode);
4289 device_remove_file(dev, &dev_attr_beacon);
4290 device_remove_bin_file(dev, &bin_attr_crb);
4291 device_remove_bin_file(dev, &bin_attr_mem);
4292 device_remove_bin_file(dev, &bin_attr_pci_config);
4293 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4294 return;
4295 device_remove_bin_file(dev, &bin_attr_esw_config);
4296 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
4297 return;
4298 device_remove_bin_file(dev, &bin_attr_npar_config);
4299 device_remove_bin_file(dev, &bin_attr_pm_config);
4300 device_remove_bin_file(dev, &bin_attr_esw_stats);
4303 #ifdef CONFIG_INET
4305 #define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4307 static void
4308 qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4309 struct net_device *dev, unsigned long event)
4311 struct in_device *indev;
4313 indev = in_dev_get(dev);
4314 if (!indev)
4315 return;
4317 for_ifa(indev) {
4318 switch (event) {
4319 case NETDEV_UP:
4320 qlcnic_config_ipaddr(adapter,
4321 ifa->ifa_address, QLCNIC_IP_UP);
4322 break;
4323 case NETDEV_DOWN:
4324 qlcnic_config_ipaddr(adapter,
4325 ifa->ifa_address, QLCNIC_IP_DOWN);
4326 break;
4327 default:
4328 break;
4330 } endfor_ifa(indev);
4332 in_dev_put(indev);
4335 static void
4336 qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4338 struct qlcnic_adapter *adapter = netdev_priv(netdev);
4339 struct net_device *dev;
4340 u16 vid;
4342 qlcnic_config_indev_addr(adapter, netdev, event);
4344 for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4345 dev = __vlan_find_dev_deep(netdev, vid);
4346 if (!dev)
4347 continue;
4348 qlcnic_config_indev_addr(adapter, dev, event);
4352 static int qlcnic_netdev_event(struct notifier_block *this,
4353 unsigned long event, void *ptr)
4355 struct qlcnic_adapter *adapter;
4356 struct net_device *dev = (struct net_device *)ptr;
4358 recheck:
4359 if (dev == NULL)
4360 goto done;
4362 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4363 dev = vlan_dev_real_dev(dev);
4364 goto recheck;
4367 if (!is_qlcnic_netdev(dev))
4368 goto done;
4370 adapter = netdev_priv(dev);
4372 if (!adapter)
4373 goto done;
4375 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4376 goto done;
4378 qlcnic_config_indev_addr(adapter, dev, event);
4379 done:
4380 return NOTIFY_DONE;
4383 static int
4384 qlcnic_inetaddr_event(struct notifier_block *this,
4385 unsigned long event, void *ptr)
4387 struct qlcnic_adapter *adapter;
4388 struct net_device *dev;
4390 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4392 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4394 recheck:
4395 if (dev == NULL)
4396 goto done;
4398 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4399 dev = vlan_dev_real_dev(dev);
4400 goto recheck;
4403 if (!is_qlcnic_netdev(dev))
4404 goto done;
4406 adapter = netdev_priv(dev);
4408 if (!adapter)
4409 goto done;
4411 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4412 goto done;
4414 switch (event) {
4415 case NETDEV_UP:
4416 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4417 break;
4418 case NETDEV_DOWN:
4419 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4420 break;
4421 default:
4422 break;
4425 done:
4426 return NOTIFY_DONE;
4429 static struct notifier_block qlcnic_netdev_cb = {
4430 .notifier_call = qlcnic_netdev_event,
4433 static struct notifier_block qlcnic_inetaddr_cb = {
4434 .notifier_call = qlcnic_inetaddr_event,
4436 #else
4437 static void
4438 qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
4440 #endif
4441 static struct pci_error_handlers qlcnic_err_handler = {
4442 .error_detected = qlcnic_io_error_detected,
4443 .slot_reset = qlcnic_io_slot_reset,
4444 .resume = qlcnic_io_resume,
4447 static struct pci_driver qlcnic_driver = {
4448 .name = qlcnic_driver_name,
4449 .id_table = qlcnic_pci_tbl,
4450 .probe = qlcnic_probe,
4451 .remove = __devexit_p(qlcnic_remove),
4452 #ifdef CONFIG_PM
4453 .suspend = qlcnic_suspend,
4454 .resume = qlcnic_resume,
4455 #endif
4456 .shutdown = qlcnic_shutdown,
4457 .err_handler = &qlcnic_err_handler
4461 static int __init qlcnic_init_module(void)
4463 int ret;
4465 printk(KERN_INFO "%s\n", qlcnic_driver_string);
4467 qlcnic_wq = create_singlethread_workqueue("qlcnic");
4468 if (qlcnic_wq == NULL) {
4469 printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4470 return -ENOMEM;
4473 #ifdef CONFIG_INET
4474 register_netdevice_notifier(&qlcnic_netdev_cb);
4475 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4476 #endif
4478 ret = pci_register_driver(&qlcnic_driver);
4479 if (ret) {
4480 #ifdef CONFIG_INET
4481 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4482 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4483 #endif
4484 destroy_workqueue(qlcnic_wq);
4487 return ret;
4490 module_init(qlcnic_init_module);
4492 static void __exit qlcnic_exit_module(void)
4495 pci_unregister_driver(&qlcnic_driver);
4497 #ifdef CONFIG_INET
4498 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4499 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4500 #endif
4501 destroy_workqueue(qlcnic_wq);
4504 module_exit(qlcnic_exit_module);