2 * Copyright (C) 2009 - QLogic Corporation.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * The full GNU General Public License is included in this distribution
21 * in the file called "COPYING".
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/interrupt.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/if_vlan.h>
34 #include <linux/ipv6.h>
35 #include <linux/inetdevice.h>
36 #include <linux/sysfs.h>
38 MODULE_DESCRIPTION("QLogic 10 GbE Converged Ethernet Driver");
39 MODULE_LICENSE("GPL");
40 MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
41 MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
43 char qlcnic_driver_name[] = "qlcnic";
44 static const char qlcnic_driver_string[] = "QLogic Converged Ethernet Driver v"
45 QLCNIC_LINUX_VERSIONID;
47 static int port_mode = QLCNIC_PORT_MODE_AUTO_NEG;
49 /* Default to restricted 1G auto-neg mode */
50 static int wol_port_mode = 5;
52 static int use_msi = 1;
53 module_param(use_msi, int, 0644);
54 MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
56 static int use_msi_x = 1;
57 module_param(use_msi_x, int, 0644);
58 MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
60 static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
61 module_param(auto_fw_reset, int, 0644);
62 MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
64 static int __devinit qlcnic_probe(struct pci_dev *pdev,
65 const struct pci_device_id *ent);
66 static void __devexit qlcnic_remove(struct pci_dev *pdev);
67 static int qlcnic_open(struct net_device *netdev);
68 static int qlcnic_close(struct net_device *netdev);
69 static void qlcnic_tx_timeout(struct net_device *netdev);
70 static void qlcnic_tx_timeout_task(struct work_struct *work);
71 static void qlcnic_attach_work(struct work_struct *work);
72 static void qlcnic_fwinit_work(struct work_struct *work);
73 static void qlcnic_fw_poll_work(struct work_struct *work);
74 static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
75 work_func_t func, int delay);
76 static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
77 static int qlcnic_poll(struct napi_struct *napi, int budget);
78 #ifdef CONFIG_NET_POLL_CONTROLLER
79 static void qlcnic_poll_controller(struct net_device *netdev);
82 static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
83 static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
84 static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
85 static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
87 static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter);
88 static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
90 static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
91 static irqreturn_t qlcnic_intr(int irq, void *data);
92 static irqreturn_t qlcnic_msi_intr(int irq, void *data);
93 static irqreturn_t qlcnic_msix_intr(int irq, void *data);
95 static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
96 static void qlcnic_config_indev_addr(struct net_device *dev, unsigned long);
98 /* PCI Device ID Table */
99 #define ENTRY(device) \
100 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
101 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
103 #define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
105 static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
106 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
110 MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
114 qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
115 struct qlcnic_host_tx_ring *tx_ring)
117 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
119 if (qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH) {
120 netif_stop_queue(adapter->netdev);
122 adapter->stats.xmit_off++;
126 static const u32 msi_tgt_status[8] = {
127 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
128 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
129 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
130 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
134 struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
136 static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
138 writel(0, sds_ring->crb_intr_mask);
141 static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
143 struct qlcnic_adapter *adapter = sds_ring->adapter;
145 writel(0x1, sds_ring->crb_intr_mask);
147 if (!QLCNIC_IS_MSI_FAMILY(adapter))
148 writel(0xfbff, adapter->tgt_mask_reg);
152 qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
154 int size = sizeof(struct qlcnic_host_sds_ring) * count;
156 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
158 return (recv_ctx->sds_rings == NULL);
162 qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
164 if (recv_ctx->sds_rings != NULL)
165 kfree(recv_ctx->sds_rings);
167 recv_ctx->sds_rings = NULL;
171 qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
174 struct qlcnic_host_sds_ring *sds_ring;
175 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
177 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
180 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
181 sds_ring = &recv_ctx->sds_rings[ring];
182 netif_napi_add(netdev, &sds_ring->napi,
183 qlcnic_poll, QLCNIC_NETDEV_WEIGHT);
190 qlcnic_napi_del(struct qlcnic_adapter *adapter)
193 struct qlcnic_host_sds_ring *sds_ring;
194 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
196 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
197 sds_ring = &recv_ctx->sds_rings[ring];
198 netif_napi_del(&sds_ring->napi);
201 qlcnic_free_sds_rings(&adapter->recv_ctx);
205 qlcnic_napi_enable(struct qlcnic_adapter *adapter)
208 struct qlcnic_host_sds_ring *sds_ring;
209 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
211 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
212 sds_ring = &recv_ctx->sds_rings[ring];
213 napi_enable(&sds_ring->napi);
214 qlcnic_enable_int(sds_ring);
219 qlcnic_napi_disable(struct qlcnic_adapter *adapter)
222 struct qlcnic_host_sds_ring *sds_ring;
223 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
225 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
226 sds_ring = &recv_ctx->sds_rings[ring];
227 qlcnic_disable_int(sds_ring);
228 napi_synchronize(&sds_ring->napi);
229 napi_disable(&sds_ring->napi);
233 static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
235 memset(&adapter->stats, 0, sizeof(adapter->stats));
239 static int qlcnic_set_dma_mask(struct qlcnic_adapter *adapter)
241 struct pci_dev *pdev = adapter->pdev;
244 adapter->pci_using_dac = 0;
246 mask = DMA_BIT_MASK(39);
249 if (pci_set_dma_mask(pdev, mask) == 0 &&
250 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
251 adapter->pci_using_dac = 1;
258 /* Update addressable range if firmware supports it */
260 qlcnic_update_dma_mask(struct qlcnic_adapter *adapter)
262 int change, shift, err;
263 u64 mask, old_mask, old_cmask;
264 struct pci_dev *pdev = adapter->pdev;
268 shift = QLCRD32(adapter, CRB_DMA_SHIFT);
276 old_mask = pdev->dma_mask;
277 old_cmask = pdev->dev.coherent_dma_mask;
279 mask = DMA_BIT_MASK(32+shift);
281 err = pci_set_dma_mask(pdev, mask);
285 err = pci_set_consistent_dma_mask(pdev, mask);
288 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
294 pci_set_dma_mask(pdev, old_mask);
295 pci_set_consistent_dma_mask(pdev, old_cmask);
299 static void qlcnic_set_port_mode(struct qlcnic_adapter *adapter)
303 val = adapter->ahw.board_type;
304 if ((val == QLCNIC_BRDTYPE_P3_HMEZ) ||
305 (val == QLCNIC_BRDTYPE_P3_XG_LOM)) {
306 if (port_mode == QLCNIC_PORT_MODE_802_3_AP) {
307 data = QLCNIC_PORT_MODE_802_3_AP;
308 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
309 } else if (port_mode == QLCNIC_PORT_MODE_XG) {
310 data = QLCNIC_PORT_MODE_XG;
311 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
312 } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_1G) {
313 data = QLCNIC_PORT_MODE_AUTO_NEG_1G;
314 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
315 } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_XG) {
316 data = QLCNIC_PORT_MODE_AUTO_NEG_XG;
317 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
319 data = QLCNIC_PORT_MODE_AUTO_NEG;
320 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
323 if ((wol_port_mode != QLCNIC_PORT_MODE_802_3_AP) &&
324 (wol_port_mode != QLCNIC_PORT_MODE_XG) &&
325 (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_1G) &&
326 (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_XG)) {
327 wol_port_mode = QLCNIC_PORT_MODE_AUTO_NEG;
329 QLCWR32(adapter, QLCNIC_WOL_PORT_MODE, wol_port_mode);
333 static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
338 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
340 pci_read_config_dword(pdev, pos, &control);
342 control |= PCI_MSIX_FLAGS_ENABLE;
345 pci_write_config_dword(pdev, pos, control);
349 static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
353 for (i = 0; i < count; i++)
354 adapter->msix_entries[i].entry = i;
358 qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
363 struct net_device *netdev = adapter->netdev;
364 struct pci_dev *pdev = adapter->pdev;
366 if (qlcnic_get_mac_addr(adapter, &mac_addr) != 0)
369 p = (unsigned char *)&mac_addr;
370 for (i = 0; i < 6; i++)
371 netdev->dev_addr[i] = *(p + 5 - i);
373 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
374 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
376 /* set station address */
378 if (!is_valid_ether_addr(netdev->perm_addr))
379 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
385 static int qlcnic_set_mac(struct net_device *netdev, void *p)
387 struct qlcnic_adapter *adapter = netdev_priv(netdev);
388 struct sockaddr *addr = p;
390 if (!is_valid_ether_addr(addr->sa_data))
393 if (netif_running(netdev)) {
394 netif_device_detach(netdev);
395 qlcnic_napi_disable(adapter);
398 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
399 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
400 qlcnic_set_multi(adapter->netdev);
402 if (netif_running(netdev)) {
403 netif_device_attach(netdev);
404 qlcnic_napi_enable(adapter);
409 static const struct net_device_ops qlcnic_netdev_ops = {
410 .ndo_open = qlcnic_open,
411 .ndo_stop = qlcnic_close,
412 .ndo_start_xmit = qlcnic_xmit_frame,
413 .ndo_get_stats = qlcnic_get_stats,
414 .ndo_validate_addr = eth_validate_addr,
415 .ndo_set_multicast_list = qlcnic_set_multi,
416 .ndo_set_mac_address = qlcnic_set_mac,
417 .ndo_change_mtu = qlcnic_change_mtu,
418 .ndo_tx_timeout = qlcnic_tx_timeout,
419 #ifdef CONFIG_NET_POLL_CONTROLLER
420 .ndo_poll_controller = qlcnic_poll_controller,
425 qlcnic_setup_intr(struct qlcnic_adapter *adapter)
427 const struct qlcnic_legacy_intr_set *legacy_intrp;
428 struct pci_dev *pdev = adapter->pdev;
431 if (adapter->rss_supported) {
432 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
433 MSIX_ENTRIES_PER_ADAPTER : 2;
437 adapter->max_sds_rings = 1;
439 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
441 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
443 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
444 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
445 legacy_intrp->tgt_status_reg);
446 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
447 legacy_intrp->tgt_mask_reg);
448 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
450 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
453 qlcnic_set_msix_bit(pdev, 0);
455 if (adapter->msix_supported) {
457 qlcnic_init_msix_entries(adapter, num_msix);
458 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
460 adapter->flags |= QLCNIC_MSIX_ENABLED;
461 qlcnic_set_msix_bit(pdev, 1);
463 if (adapter->rss_supported)
464 adapter->max_sds_rings = num_msix;
466 dev_info(&pdev->dev, "using msi-x interrupts\n");
471 pci_disable_msix(pdev);
473 /* fall through for msi */
476 if (use_msi && !pci_enable_msi(pdev)) {
477 adapter->flags |= QLCNIC_MSI_ENABLED;
478 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
479 msi_tgt_status[adapter->ahw.pci_func]);
480 dev_info(&pdev->dev, "using msi interrupts\n");
481 adapter->msix_entries[0].vector = pdev->irq;
485 dev_info(&pdev->dev, "using legacy interrupts\n");
486 adapter->msix_entries[0].vector = pdev->irq;
490 qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
492 if (adapter->flags & QLCNIC_MSIX_ENABLED)
493 pci_disable_msix(adapter->pdev);
494 if (adapter->flags & QLCNIC_MSI_ENABLED)
495 pci_disable_msi(adapter->pdev);
499 qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
501 if (adapter->ahw.pci_base0 != NULL)
502 iounmap(adapter->ahw.pci_base0);
506 qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
508 void __iomem *mem_ptr0 = NULL;
509 resource_size_t mem_base;
510 unsigned long mem_len, pci_len0 = 0;
512 struct pci_dev *pdev = adapter->pdev;
513 int pci_func = adapter->ahw.pci_func;
516 * Set the CRB window to invalid. If any register in window 0 is
517 * accessed it should set the window to 0 and then reset it to 1.
519 adapter->ahw.crb_win = -1;
520 adapter->ahw.ocm_win = -1;
522 /* remap phys address */
523 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
524 mem_len = pci_resource_len(pdev, 0);
526 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
528 mem_ptr0 = pci_ioremap_bar(pdev, 0);
529 if (mem_ptr0 == NULL) {
530 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
538 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
540 adapter->ahw.pci_base0 = mem_ptr0;
541 adapter->ahw.pci_len0 = pci_len0;
543 adapter->ahw.ocm_win_crb = qlcnic_get_ioaddr(adapter,
544 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
549 static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
551 struct pci_dev *pdev = adapter->pdev;
554 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
555 if (qlcnic_boards[i].vendor == pdev->vendor &&
556 qlcnic_boards[i].device == pdev->device &&
557 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
558 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
559 strcpy(name, qlcnic_boards[i].short_name);
571 qlcnic_check_options(struct qlcnic_adapter *adapter)
573 u32 fw_major, fw_minor, fw_build;
574 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
578 struct pci_dev *pdev = adapter->pdev;
580 adapter->driver_mismatch = 0;
582 ptr32 = (int *)&serial_num;
583 offset = QLCNIC_FW_SERIAL_NUM_OFFSET;
584 for (i = 0; i < 8; i++) {
585 if (qlcnic_rom_fast_read(adapter, offset, &val) == -1) {
586 dev_err(&pdev->dev, "error reading board info\n");
587 adapter->driver_mismatch = 1;
590 ptr32[i] = cpu_to_le32(val);
591 offset += sizeof(u32);
594 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
595 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
596 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
598 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
600 if (adapter->portnum == 0) {
601 get_brd_name(adapter, brd_name);
603 pr_info("%s: %s Board Chip rev 0x%x\n",
604 module_name(THIS_MODULE),
605 brd_name, adapter->ahw.revision_id);
608 if (adapter->fw_version < QLCNIC_VERSION_CODE(3, 4, 216)) {
609 adapter->driver_mismatch = 1;
610 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
611 fw_major, fw_minor, fw_build);
615 i = QLCRD32(adapter, QLCNIC_SRE_MISC);
616 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
618 dev_info(&pdev->dev, "firmware v%d.%d.%d [%s]\n",
619 fw_major, fw_minor, fw_build,
620 adapter->ahw.cut_through ? "cut-through" : "legacy");
622 if (adapter->fw_version >= QLCNIC_VERSION_CODE(4, 0, 222))
623 adapter->capabilities = QLCRD32(adapter, CRB_FW_CAPABILITIES_1);
625 adapter->flags &= ~QLCNIC_LRO_ENABLED;
627 if (adapter->ahw.port_type == QLCNIC_XGBE) {
628 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
629 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
630 } else if (adapter->ahw.port_type == QLCNIC_GBE) {
631 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
632 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
635 adapter->msix_supported = !!use_msi_x;
636 adapter->rss_supported = !!use_msi_x;
638 adapter->num_txd = MAX_CMD_DESCRIPTORS;
640 adapter->num_lro_rxd = 0;
641 adapter->max_rds_rings = 2;
645 qlcnic_start_firmware(struct qlcnic_adapter *adapter)
647 int val, err, first_boot;
649 err = qlcnic_set_dma_mask(adapter);
653 err = qlcnic_can_start_firmware(adapter);
659 first_boot = QLCRD32(adapter, QLCNIC_CAM_RAM(0x1fc));
660 if (first_boot == 0x55555555)
661 /* This is the first boot after power up */
662 QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
664 qlcnic_request_firmware(adapter);
666 err = qlcnic_need_fw_reset(adapter);
672 if (first_boot != 0x55555555) {
673 QLCWR32(adapter, CRB_CMDPEG_STATE, 0);
674 qlcnic_pinit_from_rom(adapter);
678 QLCWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
679 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS1, 0);
680 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS2, 0);
682 qlcnic_set_port_mode(adapter);
684 err = qlcnic_load_firmware(adapter);
688 qlcnic_release_firmware(adapter);
690 val = (_QLCNIC_LINUX_MAJOR << 16)
691 | ((_QLCNIC_LINUX_MINOR << 8))
692 | (_QLCNIC_LINUX_SUBVERSION);
693 QLCWR32(adapter, CRB_DRIVER_VERSION, val);
696 /* Handshake with the card before we register the devices. */
697 err = qlcnic_phantom_init(adapter);
701 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
703 qlcnic_update_dma_mask(adapter);
705 qlcnic_check_options(adapter);
707 adapter->need_fw_reset = 0;
709 /* fall through and release firmware */
712 qlcnic_release_firmware(adapter);
717 qlcnic_request_irq(struct qlcnic_adapter *adapter)
719 irq_handler_t handler;
720 struct qlcnic_host_sds_ring *sds_ring;
723 unsigned long flags = 0;
724 struct net_device *netdev = adapter->netdev;
725 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
727 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
728 handler = qlcnic_tmp_intr;
729 if (!QLCNIC_IS_MSI_FAMILY(adapter))
730 flags |= IRQF_SHARED;
733 if (adapter->flags & QLCNIC_MSIX_ENABLED)
734 handler = qlcnic_msix_intr;
735 else if (adapter->flags & QLCNIC_MSI_ENABLED)
736 handler = qlcnic_msi_intr;
738 flags |= IRQF_SHARED;
739 handler = qlcnic_intr;
742 adapter->irq = netdev->irq;
744 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
745 sds_ring = &recv_ctx->sds_rings[ring];
746 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
747 err = request_irq(sds_ring->irq, handler,
748 flags, sds_ring->name, sds_ring);
757 qlcnic_free_irq(struct qlcnic_adapter *adapter)
760 struct qlcnic_host_sds_ring *sds_ring;
762 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
764 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
765 sds_ring = &recv_ctx->sds_rings[ring];
766 free_irq(sds_ring->irq, sds_ring);
771 qlcnic_init_coalesce_defaults(struct qlcnic_adapter *adapter)
773 adapter->coal.flags = QLCNIC_INTR_DEFAULT;
774 adapter->coal.normal.data.rx_time_us =
775 QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
776 adapter->coal.normal.data.rx_packets =
777 QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
778 adapter->coal.normal.data.tx_time_us =
779 QLCNIC_DEFAULT_INTR_COALESCE_TX_TIME_US;
780 adapter->coal.normal.data.tx_packets =
781 QLCNIC_DEFAULT_INTR_COALESCE_TX_PACKETS;
785 __qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
787 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
790 qlcnic_set_multi(netdev);
791 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
793 adapter->ahw.linkup = 0;
795 if (adapter->max_sds_rings > 1)
796 qlcnic_config_rss(adapter, 1);
798 qlcnic_config_intr_coalesce(adapter);
800 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
801 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
803 qlcnic_napi_enable(adapter);
805 qlcnic_linkevent_request(adapter, 1);
807 set_bit(__QLCNIC_DEV_UP, &adapter->state);
811 /* Usage: During resume and firmware recovery module.*/
814 qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
819 if (netif_running(netdev))
820 err = __qlcnic_up(adapter, netdev);
827 __qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
829 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
832 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
836 spin_lock(&adapter->tx_clean_lock);
837 netif_carrier_off(netdev);
838 netif_tx_disable(netdev);
840 qlcnic_free_mac_list(adapter);
842 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
844 qlcnic_napi_disable(adapter);
846 qlcnic_release_tx_buffers(adapter);
847 spin_unlock(&adapter->tx_clean_lock);
850 /* Usage: During suspend and firmware recovery module */
853 qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
856 if (netif_running(netdev))
857 __qlcnic_down(adapter, netdev);
863 qlcnic_attach(struct qlcnic_adapter *adapter)
865 struct net_device *netdev = adapter->netdev;
866 struct pci_dev *pdev = adapter->pdev;
868 struct qlcnic_host_rds_ring *rds_ring;
870 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
873 err = qlcnic_init_firmware(adapter);
877 err = qlcnic_napi_add(adapter, netdev);
881 err = qlcnic_alloc_sw_resources(adapter);
883 dev_err(&pdev->dev, "Error in setting sw resources\n");
887 err = qlcnic_alloc_hw_resources(adapter);
889 dev_err(&pdev->dev, "Error in setting hw resources\n");
890 goto err_out_free_sw;
894 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
895 rds_ring = &adapter->recv_ctx.rds_rings[ring];
896 qlcnic_post_rx_buffers(adapter, ring, rds_ring);
899 err = qlcnic_request_irq(adapter);
901 dev_err(&pdev->dev, "failed to setup interrupt\n");
902 goto err_out_free_rxbuf;
905 qlcnic_init_coalesce_defaults(adapter);
907 qlcnic_create_sysfs_entries(adapter);
909 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
913 qlcnic_release_rx_buffers(adapter);
914 qlcnic_free_hw_resources(adapter);
916 qlcnic_free_sw_resources(adapter);
921 qlcnic_detach(struct qlcnic_adapter *adapter)
923 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
926 qlcnic_remove_sysfs_entries(adapter);
928 qlcnic_free_hw_resources(adapter);
929 qlcnic_release_rx_buffers(adapter);
930 qlcnic_free_irq(adapter);
931 qlcnic_napi_del(adapter);
932 qlcnic_free_sw_resources(adapter);
937 void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
939 struct qlcnic_adapter *adapter = netdev_priv(netdev);
940 struct qlcnic_host_sds_ring *sds_ring;
943 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
944 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
945 sds_ring = &adapter->recv_ctx.sds_rings[ring];
946 qlcnic_disable_int(sds_ring);
950 qlcnic_detach(adapter);
952 adapter->diag_test = 0;
953 adapter->max_sds_rings = max_sds_rings;
955 if (qlcnic_attach(adapter))
958 if (netif_running(netdev))
959 __qlcnic_up(adapter, netdev);
961 netif_device_attach(netdev);
964 int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
966 struct qlcnic_adapter *adapter = netdev_priv(netdev);
967 struct qlcnic_host_sds_ring *sds_ring;
971 netif_device_detach(netdev);
973 if (netif_running(netdev))
974 __qlcnic_down(adapter, netdev);
976 qlcnic_detach(adapter);
978 adapter->max_sds_rings = 1;
979 adapter->diag_test = test;
981 ret = qlcnic_attach(adapter);
983 netif_device_attach(netdev);
987 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
988 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
989 sds_ring = &adapter->recv_ctx.sds_rings[ring];
990 qlcnic_enable_int(sds_ring);
998 qlcnic_reset_context(struct qlcnic_adapter *adapter)
1001 struct net_device *netdev = adapter->netdev;
1003 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1006 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1008 netif_device_detach(netdev);
1010 if (netif_running(netdev))
1011 __qlcnic_down(adapter, netdev);
1013 qlcnic_detach(adapter);
1015 if (netif_running(netdev)) {
1016 err = qlcnic_attach(adapter);
1018 __qlcnic_up(adapter, netdev);
1021 netif_device_attach(netdev);
1024 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1029 qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
1030 struct net_device *netdev)
1033 struct pci_dev *pdev = adapter->pdev;
1035 adapter->rx_csum = 1;
1036 adapter->mc_enabled = 0;
1037 adapter->max_mc_count = 38;
1039 netdev->netdev_ops = &qlcnic_netdev_ops;
1040 netdev->watchdog_timeo = 2*HZ;
1042 qlcnic_change_mtu(netdev, netdev->mtu);
1044 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1046 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1047 netdev->features |= (NETIF_F_GRO);
1048 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1050 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1051 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1053 if (adapter->pci_using_dac) {
1054 netdev->features |= NETIF_F_HIGHDMA;
1055 netdev->vlan_features |= NETIF_F_HIGHDMA;
1058 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1059 netdev->features |= (NETIF_F_HW_VLAN_TX);
1061 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1062 netdev->features |= NETIF_F_LRO;
1064 netdev->irq = adapter->msix_entries[0].vector;
1066 INIT_WORK(&adapter->tx_timeout_task, qlcnic_tx_timeout_task);
1068 if (qlcnic_read_mac_addr(adapter))
1069 dev_warn(&pdev->dev, "failed to read mac addr\n");
1071 netif_carrier_off(netdev);
1072 netif_stop_queue(netdev);
1074 err = register_netdev(netdev);
1076 dev_err(&pdev->dev, "failed to register net device\n");
1083 static int __devinit
1084 qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1086 struct net_device *netdev = NULL;
1087 struct qlcnic_adapter *adapter = NULL;
1089 int pci_func_id = PCI_FUNC(pdev->devfn);
1090 uint8_t revision_id;
1092 err = pci_enable_device(pdev);
1096 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1098 goto err_out_disable_pdev;
1101 err = pci_request_regions(pdev, qlcnic_driver_name);
1103 goto err_out_disable_pdev;
1105 pci_set_master(pdev);
1107 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1109 dev_err(&pdev->dev, "failed to allocate net_device\n");
1111 goto err_out_free_res;
1114 SET_NETDEV_DEV(netdev, &pdev->dev);
1116 adapter = netdev_priv(netdev);
1117 adapter->netdev = netdev;
1118 adapter->pdev = pdev;
1119 adapter->ahw.pci_func = pci_func_id;
1121 revision_id = pdev->revision;
1122 adapter->ahw.revision_id = revision_id;
1124 rwlock_init(&adapter->ahw.crb_lock);
1125 mutex_init(&adapter->ahw.mem_lock);
1127 spin_lock_init(&adapter->tx_clean_lock);
1128 INIT_LIST_HEAD(&adapter->mac_list);
1130 err = qlcnic_setup_pci_map(adapter);
1132 goto err_out_free_netdev;
1134 /* This will be reset for mezz cards */
1135 adapter->portnum = pci_func_id;
1137 err = qlcnic_get_board_info(adapter);
1139 dev_err(&pdev->dev, "Error getting board config info.\n");
1140 goto err_out_iounmap;
1143 qlcnic_setup_idc_param(adapter);
1145 err = qlcnic_start_firmware(adapter);
1147 goto err_out_decr_ref;
1150 * See if the firmware gave us a virtual-physical port mapping.
1152 adapter->physical_port = adapter->portnum;
1154 qlcnic_clear_stats(adapter);
1156 qlcnic_setup_intr(adapter);
1158 err = qlcnic_setup_netdev(adapter, netdev);
1160 goto err_out_disable_msi;
1162 pci_set_drvdata(pdev, adapter);
1164 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1166 switch (adapter->ahw.port_type) {
1168 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1169 adapter->netdev->name);
1172 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1173 adapter->netdev->name);
1177 qlcnic_create_diag_entries(adapter);
1181 err_out_disable_msi:
1182 qlcnic_teardown_intr(adapter);
1185 qlcnic_clr_all_drv_state(adapter);
1188 qlcnic_cleanup_pci_map(adapter);
1190 err_out_free_netdev:
1191 free_netdev(netdev);
1194 pci_release_regions(pdev);
1196 err_out_disable_pdev:
1197 pci_set_drvdata(pdev, NULL);
1198 pci_disable_device(pdev);
1202 static void __devexit qlcnic_remove(struct pci_dev *pdev)
1204 struct qlcnic_adapter *adapter;
1205 struct net_device *netdev;
1207 adapter = pci_get_drvdata(pdev);
1208 if (adapter == NULL)
1211 netdev = adapter->netdev;
1213 qlcnic_cancel_fw_work(adapter);
1215 unregister_netdev(netdev);
1217 cancel_work_sync(&adapter->tx_timeout_task);
1219 qlcnic_detach(adapter);
1221 qlcnic_clr_all_drv_state(adapter);
1223 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1225 qlcnic_teardown_intr(adapter);
1227 qlcnic_remove_diag_entries(adapter);
1229 qlcnic_cleanup_pci_map(adapter);
1231 qlcnic_release_firmware(adapter);
1233 pci_release_regions(pdev);
1234 pci_disable_device(pdev);
1235 pci_set_drvdata(pdev, NULL);
1237 free_netdev(netdev);
1239 static int __qlcnic_shutdown(struct pci_dev *pdev)
1241 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1242 struct net_device *netdev = adapter->netdev;
1245 netif_device_detach(netdev);
1247 qlcnic_cancel_fw_work(adapter);
1249 if (netif_running(netdev))
1250 qlcnic_down(adapter, netdev);
1252 cancel_work_sync(&adapter->tx_timeout_task);
1254 qlcnic_detach(adapter);
1256 qlcnic_clr_all_drv_state(adapter);
1258 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1260 retval = pci_save_state(pdev);
1264 if (qlcnic_wol_supported(adapter)) {
1265 pci_enable_wake(pdev, PCI_D3cold, 1);
1266 pci_enable_wake(pdev, PCI_D3hot, 1);
1272 static void qlcnic_shutdown(struct pci_dev *pdev)
1274 if (__qlcnic_shutdown(pdev))
1277 pci_disable_device(pdev);
1282 qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1286 retval = __qlcnic_shutdown(pdev);
1290 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1295 qlcnic_resume(struct pci_dev *pdev)
1297 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1298 struct net_device *netdev = adapter->netdev;
1301 err = pci_enable_device(pdev);
1305 pci_set_power_state(pdev, PCI_D0);
1306 pci_set_master(pdev);
1307 pci_restore_state(pdev);
1309 adapter->ahw.crb_win = -1;
1310 adapter->ahw.ocm_win = -1;
1312 err = qlcnic_start_firmware(adapter);
1314 dev_err(&pdev->dev, "failed to start firmware\n");
1318 if (netif_running(netdev)) {
1319 err = qlcnic_attach(adapter);
1323 err = qlcnic_up(adapter, netdev);
1325 goto err_out_detach;
1328 qlcnic_config_indev_addr(netdev, NETDEV_UP);
1331 netif_device_attach(netdev);
1332 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1336 qlcnic_detach(adapter);
1338 qlcnic_clr_all_drv_state(adapter);
1339 netif_device_attach(netdev);
1344 static int qlcnic_open(struct net_device *netdev)
1346 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1349 if (adapter->driver_mismatch)
1352 err = qlcnic_attach(adapter);
1356 err = __qlcnic_up(adapter, netdev);
1360 netif_start_queue(netdev);
1365 qlcnic_detach(adapter);
1370 * qlcnic_close - Disables a network interface entry point
1372 static int qlcnic_close(struct net_device *netdev)
1374 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1376 __qlcnic_down(adapter, netdev);
1381 qlcnic_tso_check(struct net_device *netdev,
1382 struct qlcnic_host_tx_ring *tx_ring,
1383 struct cmd_desc_type0 *first_desc,
1384 struct sk_buff *skb)
1386 u8 opcode = TX_ETHER_PKT;
1387 __be16 protocol = skb->protocol;
1388 u16 flags = 0, vid = 0;
1390 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1391 struct cmd_desc_type0 *hwdesc;
1392 struct vlan_ethhdr *vh;
1393 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1395 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1397 vh = (struct vlan_ethhdr *)skb->data;
1398 protocol = vh->h_vlan_encapsulated_proto;
1399 flags = FLAGS_VLAN_TAGGED;
1401 } else if (vlan_tx_tag_present(skb)) {
1403 flags = FLAGS_VLAN_OOB;
1404 vid = vlan_tx_tag_get(skb);
1405 qlcnic_set_tx_vlan_tci(first_desc, vid);
1409 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1410 skb_shinfo(skb)->gso_size > 0) {
1412 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1414 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1415 first_desc->total_hdr_length = hdr_len;
1417 first_desc->total_hdr_length += VLAN_HLEN;
1418 first_desc->tcp_hdr_offset = VLAN_HLEN;
1419 first_desc->ip_hdr_offset = VLAN_HLEN;
1420 /* Only in case of TSO on vlan device */
1421 flags |= FLAGS_VLAN_TAGGED;
1424 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1425 TX_TCP_LSO6 : TX_TCP_LSO;
1428 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1431 if (protocol == cpu_to_be16(ETH_P_IP)) {
1432 l4proto = ip_hdr(skb)->protocol;
1434 if (l4proto == IPPROTO_TCP)
1435 opcode = TX_TCP_PKT;
1436 else if (l4proto == IPPROTO_UDP)
1437 opcode = TX_UDP_PKT;
1438 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1439 l4proto = ipv6_hdr(skb)->nexthdr;
1441 if (l4proto == IPPROTO_TCP)
1442 opcode = TX_TCPV6_PKT;
1443 else if (l4proto == IPPROTO_UDP)
1444 opcode = TX_UDPV6_PKT;
1448 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1449 first_desc->ip_hdr_offset += skb_network_offset(skb);
1450 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
1455 /* For LSO, we need to copy the MAC/IP/TCP headers into
1456 * the descriptor ring
1458 producer = tx_ring->producer;
1463 /* Create a TSO vlan header template for firmware */
1465 hwdesc = &tx_ring->desc_head[producer];
1466 tx_ring->cmd_buf_arr[producer].skb = NULL;
1468 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1469 hdr_len + VLAN_HLEN);
1471 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1472 skb_copy_from_linear_data(skb, vh, 12);
1473 vh->h_vlan_proto = htons(ETH_P_8021Q);
1474 vh->h_vlan_TCI = htons(vid);
1475 skb_copy_from_linear_data_offset(skb, 12,
1476 (char *)vh + 16, copy_len - 16);
1478 copied = copy_len - VLAN_HLEN;
1481 producer = get_next_index(producer, tx_ring->num_desc);
1484 while (copied < hdr_len) {
1486 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1487 (hdr_len - copied));
1489 hwdesc = &tx_ring->desc_head[producer];
1490 tx_ring->cmd_buf_arr[producer].skb = NULL;
1492 skb_copy_from_linear_data_offset(skb, copied,
1493 (char *)hwdesc + offset, copy_len);
1498 producer = get_next_index(producer, tx_ring->num_desc);
1501 tx_ring->producer = producer;
1503 adapter->stats.lso_frames++;
1507 qlcnic_map_tx_skb(struct pci_dev *pdev,
1508 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
1510 struct qlcnic_skb_frag *nf;
1511 struct skb_frag_struct *frag;
1515 nr_frags = skb_shinfo(skb)->nr_frags;
1516 nf = &pbuf->frag_array[0];
1518 map = pci_map_single(pdev, skb->data,
1519 skb_headlen(skb), PCI_DMA_TODEVICE);
1520 if (pci_dma_mapping_error(pdev, map))
1524 nf->length = skb_headlen(skb);
1526 for (i = 0; i < nr_frags; i++) {
1527 frag = &skb_shinfo(skb)->frags[i];
1528 nf = &pbuf->frag_array[i+1];
1530 map = pci_map_page(pdev, frag->page, frag->page_offset,
1531 frag->size, PCI_DMA_TODEVICE);
1532 if (pci_dma_mapping_error(pdev, map))
1536 nf->length = frag->size;
1543 nf = &pbuf->frag_array[i+1];
1544 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1547 nf = &pbuf->frag_array[0];
1548 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1555 qlcnic_clear_cmddesc(u64 *desc)
1562 qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1564 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1565 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1566 struct qlcnic_cmd_buffer *pbuf;
1567 struct qlcnic_skb_frag *buffrag;
1568 struct cmd_desc_type0 *hwdesc, *first_desc;
1569 struct pci_dev *pdev;
1573 int frag_count, no_of_desc;
1574 u32 num_txd = tx_ring->num_desc;
1576 frag_count = skb_shinfo(skb)->nr_frags + 1;
1578 /* 4 fragments per cmd des */
1579 no_of_desc = (frag_count + 3) >> 2;
1581 if (unlikely(no_of_desc + 2 > qlcnic_tx_avail(tx_ring))) {
1582 netif_stop_queue(netdev);
1583 adapter->stats.xmit_off++;
1584 return NETDEV_TX_BUSY;
1587 producer = tx_ring->producer;
1588 pbuf = &tx_ring->cmd_buf_arr[producer];
1590 pdev = adapter->pdev;
1592 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
1593 adapter->stats.tx_dma_map_error++;
1598 pbuf->frag_count = frag_count;
1600 first_desc = hwdesc = &tx_ring->desc_head[producer];
1601 qlcnic_clear_cmddesc((u64 *)hwdesc);
1603 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
1604 qlcnic_set_tx_port(first_desc, adapter->portnum);
1606 for (i = 0; i < frag_count; i++) {
1610 if ((k == 0) && (i > 0)) {
1611 /* move to next desc.*/
1612 producer = get_next_index(producer, num_txd);
1613 hwdesc = &tx_ring->desc_head[producer];
1614 qlcnic_clear_cmddesc((u64 *)hwdesc);
1615 tx_ring->cmd_buf_arr[producer].skb = NULL;
1618 buffrag = &pbuf->frag_array[i];
1620 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
1623 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1626 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
1629 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
1632 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
1637 tx_ring->producer = get_next_index(producer, num_txd);
1639 qlcnic_tso_check(netdev, tx_ring, first_desc, skb);
1641 qlcnic_update_cmd_producer(adapter, tx_ring);
1643 adapter->stats.txbytes += skb->len;
1644 adapter->stats.xmitcalled++;
1646 return NETDEV_TX_OK;
1649 adapter->stats.txdropped++;
1650 dev_kfree_skb_any(skb);
1651 return NETDEV_TX_OK;
1654 static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
1656 struct net_device *netdev = adapter->netdev;
1657 u32 temp, temp_state, temp_val;
1660 temp = QLCRD32(adapter, CRB_TEMP_STATE);
1662 temp_state = qlcnic_get_temp_state(temp);
1663 temp_val = qlcnic_get_temp_val(temp);
1665 if (temp_state == QLCNIC_TEMP_PANIC) {
1666 dev_err(&netdev->dev,
1667 "Device temperature %d degrees C exceeds"
1668 " maximum allowed. Hardware has been shut down.\n",
1671 } else if (temp_state == QLCNIC_TEMP_WARN) {
1672 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
1673 dev_err(&netdev->dev,
1674 "Device temperature %d degrees C "
1675 "exceeds operating range."
1676 " Immediate action needed.\n",
1680 if (adapter->temp == QLCNIC_TEMP_WARN) {
1681 dev_info(&netdev->dev,
1682 "Device temperature is now %d degrees C"
1683 " in normal range.\n", temp_val);
1686 adapter->temp = temp_state;
1690 void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
1692 struct net_device *netdev = adapter->netdev;
1694 if (adapter->ahw.linkup && !linkup) {
1695 dev_info(&netdev->dev, "NIC Link is down\n");
1696 adapter->ahw.linkup = 0;
1697 if (netif_running(netdev)) {
1698 netif_carrier_off(netdev);
1699 netif_stop_queue(netdev);
1701 } else if (!adapter->ahw.linkup && linkup) {
1702 dev_info(&netdev->dev, "NIC Link is up\n");
1703 adapter->ahw.linkup = 1;
1704 if (netif_running(netdev)) {
1705 netif_carrier_on(netdev);
1706 netif_wake_queue(netdev);
1711 static void qlcnic_tx_timeout(struct net_device *netdev)
1713 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1715 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
1718 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
1719 schedule_work(&adapter->tx_timeout_task);
1722 static void qlcnic_tx_timeout_task(struct work_struct *work)
1724 struct qlcnic_adapter *adapter =
1725 container_of(work, struct qlcnic_adapter, tx_timeout_task);
1727 if (!netif_running(adapter->netdev))
1730 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1733 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
1736 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1737 if (!qlcnic_reset_context(adapter)) {
1738 adapter->netdev->trans_start = jiffies;
1741 /* context reset failed, fall through for fw reset */
1745 adapter->need_fw_reset = 1;
1746 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1747 QLCDB(adapter, DRV, "Resetting adapter\n");
1750 static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
1752 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1753 struct net_device_stats *stats = &netdev->stats;
1755 memset(stats, 0, sizeof(*stats));
1757 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
1758 stats->tx_packets = adapter->stats.xmitfinished;
1759 stats->rx_bytes = adapter->stats.rxbytes;
1760 stats->tx_bytes = adapter->stats.txbytes;
1761 stats->rx_dropped = adapter->stats.rxdropped;
1762 stats->tx_dropped = adapter->stats.txdropped;
1767 static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
1771 status = readl(adapter->isr_int_vec);
1773 if (!(status & adapter->int_vec_bit))
1776 /* check interrupt state machine, to be sure */
1777 status = readl(adapter->crb_int_state_reg);
1778 if (!ISR_LEGACY_INT_TRIGGERED(status))
1781 writel(0xffffffff, adapter->tgt_status_reg);
1782 /* read twice to ensure write is flushed */
1783 readl(adapter->isr_int_vec);
1784 readl(adapter->isr_int_vec);
1789 static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
1791 struct qlcnic_host_sds_ring *sds_ring = data;
1792 struct qlcnic_adapter *adapter = sds_ring->adapter;
1794 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1796 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
1797 writel(0xffffffff, adapter->tgt_status_reg);
1801 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
1805 adapter->diag_cnt++;
1806 qlcnic_enable_int(sds_ring);
1810 static irqreturn_t qlcnic_intr(int irq, void *data)
1812 struct qlcnic_host_sds_ring *sds_ring = data;
1813 struct qlcnic_adapter *adapter = sds_ring->adapter;
1815 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
1818 napi_schedule(&sds_ring->napi);
1823 static irqreturn_t qlcnic_msi_intr(int irq, void *data)
1825 struct qlcnic_host_sds_ring *sds_ring = data;
1826 struct qlcnic_adapter *adapter = sds_ring->adapter;
1828 /* clear interrupt */
1829 writel(0xffffffff, adapter->tgt_status_reg);
1831 napi_schedule(&sds_ring->napi);
1835 static irqreturn_t qlcnic_msix_intr(int irq, void *data)
1837 struct qlcnic_host_sds_ring *sds_ring = data;
1839 napi_schedule(&sds_ring->napi);
1843 static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
1845 u32 sw_consumer, hw_consumer;
1847 struct qlcnic_cmd_buffer *buffer;
1848 struct pci_dev *pdev = adapter->pdev;
1849 struct net_device *netdev = adapter->netdev;
1850 struct qlcnic_skb_frag *frag;
1852 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1854 if (!spin_trylock(&adapter->tx_clean_lock))
1857 sw_consumer = tx_ring->sw_consumer;
1858 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1860 while (sw_consumer != hw_consumer) {
1861 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
1863 frag = &buffer->frag_array[0];
1864 pci_unmap_single(pdev, frag->dma, frag->length,
1867 for (i = 1; i < buffer->frag_count; i++) {
1869 pci_unmap_page(pdev, frag->dma, frag->length,
1874 adapter->stats.xmitfinished++;
1875 dev_kfree_skb_any(buffer->skb);
1879 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
1880 if (++count >= MAX_STATUS_HANDLE)
1884 if (count && netif_running(netdev)) {
1885 tx_ring->sw_consumer = sw_consumer;
1889 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
1890 __netif_tx_lock(tx_ring->txq, smp_processor_id());
1891 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
1892 netif_wake_queue(netdev);
1893 adapter->tx_timeo_cnt = 0;
1894 adapter->stats.xmit_on++;
1896 __netif_tx_unlock(tx_ring->txq);
1900 * If everything is freed up to consumer then check if the ring is full
1901 * If the ring is full then check if more needs to be freed and
1902 * schedule the call back again.
1904 * This happens when there are 2 CPUs. One could be freeing and the
1905 * other filling it. If the ring is full when we get out of here and
1906 * the card has already interrupted the host then the host can miss the
1909 * There is still a possible race condition and the host could miss an
1910 * interrupt. The card has to take care of this.
1912 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1913 done = (sw_consumer == hw_consumer);
1914 spin_unlock(&adapter->tx_clean_lock);
1919 static int qlcnic_poll(struct napi_struct *napi, int budget)
1921 struct qlcnic_host_sds_ring *sds_ring =
1922 container_of(napi, struct qlcnic_host_sds_ring, napi);
1924 struct qlcnic_adapter *adapter = sds_ring->adapter;
1929 tx_complete = qlcnic_process_cmd_ring(adapter);
1931 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
1933 if ((work_done < budget) && tx_complete) {
1934 napi_complete(&sds_ring->napi);
1935 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1936 qlcnic_enable_int(sds_ring);
1942 #ifdef CONFIG_NET_POLL_CONTROLLER
1943 static void qlcnic_poll_controller(struct net_device *netdev)
1945 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1946 disable_irq(adapter->irq);
1947 qlcnic_intr(adapter->irq, adapter);
1948 enable_irq(adapter->irq);
1953 qlcnic_set_drv_state(struct qlcnic_adapter *adapter, int state)
1957 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
1958 state != QLCNIC_DEV_NEED_QUISCENT);
1960 if (qlcnic_api_lock(adapter))
1963 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
1965 if (state == QLCNIC_DEV_NEED_RESET)
1966 val |= ((u32)0x1 << (adapter->portnum * 4));
1967 else if (state == QLCNIC_DEV_NEED_QUISCENT)
1968 val |= ((u32)0x1 << ((adapter->portnum * 4) + 1));
1970 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
1972 qlcnic_api_unlock(adapter);
1976 qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
1980 if (qlcnic_api_lock(adapter))
1983 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
1984 val &= ~((u32)0x3 << (adapter->portnum * 4));
1985 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
1987 qlcnic_api_unlock(adapter);
1993 qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter)
1997 if (qlcnic_api_lock(adapter))
2000 val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
2001 val &= ~((u32)0x1 << (adapter->portnum * 4));
2002 QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
2004 if (!(val & 0x11111111))
2005 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2007 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2008 val &= ~((u32)0x3 << (adapter->portnum * 4));
2009 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2011 qlcnic_api_unlock(adapter);
2013 adapter->fw_fail_cnt = 0;
2014 clear_bit(__QLCNIC_START_FW, &adapter->state);
2015 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2019 qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2023 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2024 act = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
2026 if (((state & 0x11111111) == (act & 0x11111111)) ||
2027 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2034 qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2036 u32 val, prev_state;
2037 u8 dev_init_timeo = adapter->dev_init_timeo;
2038 int portnum = adapter->portnum;
2040 if (qlcnic_api_lock(adapter))
2043 val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
2044 if (!(val & ((int)0x1 << (portnum * 4)))) {
2045 val |= ((u32)0x1 << (portnum * 4));
2046 QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
2047 } else if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state)) {
2051 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2052 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
2054 switch (prev_state) {
2055 case QLCNIC_DEV_COLD:
2057 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
2058 qlcnic_api_unlock(adapter);
2061 case QLCNIC_DEV_READY:
2062 qlcnic_api_unlock(adapter);
2065 case QLCNIC_DEV_NEED_RESET:
2066 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2067 val |= ((u32)0x1 << (portnum * 4));
2068 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2071 case QLCNIC_DEV_NEED_QUISCENT:
2072 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2073 val |= ((u32)0x1 << ((portnum * 4) + 1));
2074 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2077 case QLCNIC_DEV_FAILED:
2078 qlcnic_api_unlock(adapter);
2081 case QLCNIC_DEV_INITIALIZING:
2082 case QLCNIC_DEV_QUISCENT:
2086 qlcnic_api_unlock(adapter);
2090 } while ((QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) != QLCNIC_DEV_READY)
2091 && --dev_init_timeo);
2093 if (!dev_init_timeo) {
2094 dev_err(&adapter->pdev->dev,
2095 "Waiting for device to initialize timeout\n");
2099 if (qlcnic_api_lock(adapter))
2102 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2103 val &= ~((u32)0x3 << (portnum * 4));
2104 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2106 qlcnic_api_unlock(adapter);
2112 qlcnic_fwinit_work(struct work_struct *work)
2114 struct qlcnic_adapter *adapter = container_of(work,
2115 struct qlcnic_adapter, fw_work.work);
2118 if (test_bit(__QLCNIC_START_FW, &adapter->state)) {
2120 if (qlcnic_check_drv_state(adapter) &&
2121 (adapter->fw_wait_cnt++ < adapter->reset_ack_timeo)) {
2122 qlcnic_schedule_work(adapter,
2123 qlcnic_fwinit_work, FW_POLL_DELAY);
2127 QLCDB(adapter, DRV, "Resetting FW\n");
2128 if (!qlcnic_start_firmware(adapter)) {
2129 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2136 if (adapter->fw_wait_cnt++ > (adapter->dev_init_timeo / 2)) {
2137 dev_err(&adapter->pdev->dev,
2138 "Waiting for device to reset timeout\n");
2142 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2143 QLCDB(adapter, HW, "Func waiting: Device state=%d\n", dev_state);
2145 switch (dev_state) {
2146 case QLCNIC_DEV_READY:
2147 if (!qlcnic_start_firmware(adapter)) {
2148 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2151 case QLCNIC_DEV_FAILED:
2155 qlcnic_schedule_work(adapter,
2156 qlcnic_fwinit_work, 2 * FW_POLL_DELAY);
2161 netif_device_attach(adapter->netdev);
2162 qlcnic_clr_all_drv_state(adapter);
2166 qlcnic_detach_work(struct work_struct *work)
2168 struct qlcnic_adapter *adapter = container_of(work,
2169 struct qlcnic_adapter, fw_work.work);
2170 struct net_device *netdev = adapter->netdev;
2173 netif_device_detach(netdev);
2175 qlcnic_down(adapter, netdev);
2178 qlcnic_detach(adapter);
2181 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2183 if (status & QLCNIC_RCODE_FATAL_ERROR)
2186 if (adapter->temp == QLCNIC_TEMP_PANIC)
2189 qlcnic_set_drv_state(adapter, adapter->dev_state);
2191 adapter->fw_wait_cnt = 0;
2193 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2198 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2199 status, adapter->temp);
2200 netif_device_attach(netdev);
2201 qlcnic_clr_all_drv_state(adapter);
2206 qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2210 if (qlcnic_api_lock(adapter))
2213 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2215 if (state != QLCNIC_DEV_INITIALIZING &&
2216 state != QLCNIC_DEV_NEED_RESET) {
2217 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
2218 set_bit(__QLCNIC_START_FW, &adapter->state);
2219 QLCDB(adapter, DRV, "NEED_RESET state set\n");
2222 qlcnic_api_unlock(adapter);
2226 qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2227 work_func_t func, int delay)
2229 INIT_DELAYED_WORK(&adapter->fw_work, func);
2230 schedule_delayed_work(&adapter->fw_work, round_jiffies_relative(delay));
2234 qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
2236 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2239 cancel_delayed_work_sync(&adapter->fw_work);
2243 qlcnic_attach_work(struct work_struct *work)
2245 struct qlcnic_adapter *adapter = container_of(work,
2246 struct qlcnic_adapter, fw_work.work);
2247 struct net_device *netdev = adapter->netdev;
2250 if (netif_running(netdev)) {
2251 err = qlcnic_attach(adapter);
2255 err = qlcnic_up(adapter, netdev);
2257 qlcnic_detach(adapter);
2261 qlcnic_config_indev_addr(netdev, NETDEV_UP);
2265 netif_device_attach(netdev);
2266 adapter->fw_fail_cnt = 0;
2267 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2269 if (!qlcnic_clr_drv_state(adapter))
2270 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2275 qlcnic_check_health(struct qlcnic_adapter *adapter)
2277 u32 state = 0, heartbit;
2278 struct net_device *netdev = adapter->netdev;
2280 if (qlcnic_check_temp(adapter))
2283 if (adapter->need_fw_reset) {
2284 qlcnic_dev_request_reset(adapter);
2288 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2289 if (state == QLCNIC_DEV_NEED_RESET || state == QLCNIC_DEV_NEED_QUISCENT)
2290 adapter->need_fw_reset = 1;
2292 heartbit = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
2293 if (heartbit != adapter->heartbit) {
2294 adapter->heartbit = heartbit;
2295 adapter->fw_fail_cnt = 0;
2296 if (adapter->need_fw_reset)
2301 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2304 qlcnic_dev_request_reset(adapter);
2306 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
2308 dev_info(&netdev->dev, "firmware hang detected\n");
2311 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
2312 QLCNIC_DEV_NEED_RESET;
2314 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
2315 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
2317 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
2318 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
2325 qlcnic_fw_poll_work(struct work_struct *work)
2327 struct qlcnic_adapter *adapter = container_of(work,
2328 struct qlcnic_adapter, fw_work.work);
2330 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2334 if (qlcnic_check_health(adapter))
2338 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
2342 qlcnic_store_bridged_mode(struct device *dev,
2343 struct device_attribute *attr, const char *buf, size_t len)
2345 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2349 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
2352 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2355 if (strict_strtoul(buf, 2, &new))
2358 if (!qlcnic_config_bridged_mode(adapter, !!new))
2366 qlcnic_show_bridged_mode(struct device *dev,
2367 struct device_attribute *attr, char *buf)
2369 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2370 int bridged_mode = 0;
2372 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2373 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
2375 return sprintf(buf, "%d\n", bridged_mode);
2378 static struct device_attribute dev_attr_bridged_mode = {
2379 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2380 .show = qlcnic_show_bridged_mode,
2381 .store = qlcnic_store_bridged_mode,
2385 qlcnic_store_diag_mode(struct device *dev,
2386 struct device_attribute *attr, const char *buf, size_t len)
2388 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2391 if (strict_strtoul(buf, 2, &new))
2394 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
2395 adapter->flags ^= QLCNIC_DIAG_ENABLED;
2401 qlcnic_show_diag_mode(struct device *dev,
2402 struct device_attribute *attr, char *buf)
2404 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2406 return sprintf(buf, "%d\n",
2407 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
2410 static struct device_attribute dev_attr_diag_mode = {
2411 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
2412 .show = qlcnic_show_diag_mode,
2413 .store = qlcnic_store_diag_mode,
2417 qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
2418 loff_t offset, size_t size)
2420 size_t crb_size = 4;
2422 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
2425 if (offset < QLCNIC_PCI_CRBSPACE) {
2426 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
2427 QLCNIC_PCI_CAMQM_END))
2433 if ((size != crb_size) || (offset & (crb_size-1)))
2440 qlcnic_sysfs_read_crb(struct kobject *kobj, struct bin_attribute *attr,
2441 char *buf, loff_t offset, size_t size)
2443 struct device *dev = container_of(kobj, struct device, kobj);
2444 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2449 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2453 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
2454 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
2455 memcpy(buf, &qmdata, size);
2457 data = QLCRD32(adapter, offset);
2458 memcpy(buf, &data, size);
2464 qlcnic_sysfs_write_crb(struct kobject *kobj, struct bin_attribute *attr,
2465 char *buf, loff_t offset, size_t size)
2467 struct device *dev = container_of(kobj, struct device, kobj);
2468 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2473 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2477 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
2478 memcpy(&qmdata, buf, size);
2479 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
2481 memcpy(&data, buf, size);
2482 QLCWR32(adapter, offset, data);
2488 qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
2489 loff_t offset, size_t size)
2491 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
2494 if ((size != 8) || (offset & 0x7))
2501 qlcnic_sysfs_read_mem(struct kobject *kobj, struct bin_attribute *attr,
2502 char *buf, loff_t offset, size_t size)
2504 struct device *dev = container_of(kobj, struct device, kobj);
2505 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2509 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
2513 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
2516 memcpy(buf, &data, size);
2522 qlcnic_sysfs_write_mem(struct kobject *kobj, struct bin_attribute *attr,
2523 char *buf, loff_t offset, size_t size)
2525 struct device *dev = container_of(kobj, struct device, kobj);
2526 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2530 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
2534 memcpy(&data, buf, size);
2536 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
2543 static struct bin_attribute bin_attr_crb = {
2544 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
2546 .read = qlcnic_sysfs_read_crb,
2547 .write = qlcnic_sysfs_write_crb,
2550 static struct bin_attribute bin_attr_mem = {
2551 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
2553 .read = qlcnic_sysfs_read_mem,
2554 .write = qlcnic_sysfs_write_mem,
2558 qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
2560 struct device *dev = &adapter->pdev->dev;
2562 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2563 if (device_create_file(dev, &dev_attr_bridged_mode))
2565 "failed to create bridged_mode sysfs entry\n");
2569 qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
2571 struct device *dev = &adapter->pdev->dev;
2573 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2574 device_remove_file(dev, &dev_attr_bridged_mode);
2578 qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
2580 struct device *dev = &adapter->pdev->dev;
2582 if (device_create_file(dev, &dev_attr_diag_mode))
2583 dev_info(dev, "failed to create diag_mode sysfs entry\n");
2584 if (device_create_bin_file(dev, &bin_attr_crb))
2585 dev_info(dev, "failed to create crb sysfs entry\n");
2586 if (device_create_bin_file(dev, &bin_attr_mem))
2587 dev_info(dev, "failed to create mem sysfs entry\n");
2592 qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
2594 struct device *dev = &adapter->pdev->dev;
2596 device_remove_file(dev, &dev_attr_diag_mode);
2597 device_remove_bin_file(dev, &bin_attr_crb);
2598 device_remove_bin_file(dev, &bin_attr_mem);
2603 #define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
2606 qlcnic_destip_supported(struct qlcnic_adapter *adapter)
2608 if (adapter->ahw.cut_through)
2615 qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
2617 struct in_device *indev;
2618 struct qlcnic_adapter *adapter = netdev_priv(dev);
2620 if (!qlcnic_destip_supported(adapter))
2623 indev = in_dev_get(dev);
2630 qlcnic_config_ipaddr(adapter,
2631 ifa->ifa_address, QLCNIC_IP_UP);
2634 qlcnic_config_ipaddr(adapter,
2635 ifa->ifa_address, QLCNIC_IP_DOWN);
2640 } endfor_ifa(indev);
2646 static int qlcnic_netdev_event(struct notifier_block *this,
2647 unsigned long event, void *ptr)
2649 struct qlcnic_adapter *adapter;
2650 struct net_device *dev = (struct net_device *)ptr;
2656 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2657 dev = vlan_dev_real_dev(dev);
2661 if (!is_qlcnic_netdev(dev))
2664 adapter = netdev_priv(dev);
2669 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2672 qlcnic_config_indev_addr(dev, event);
2678 qlcnic_inetaddr_event(struct notifier_block *this,
2679 unsigned long event, void *ptr)
2681 struct qlcnic_adapter *adapter;
2682 struct net_device *dev;
2684 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2686 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2689 if (dev == NULL || !netif_running(dev))
2692 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2693 dev = vlan_dev_real_dev(dev);
2697 if (!is_qlcnic_netdev(dev))
2700 adapter = netdev_priv(dev);
2702 if (!adapter || !qlcnic_destip_supported(adapter))
2705 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2710 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
2713 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
2723 static struct notifier_block qlcnic_netdev_cb = {
2724 .notifier_call = qlcnic_netdev_event,
2727 static struct notifier_block qlcnic_inetaddr_cb = {
2728 .notifier_call = qlcnic_inetaddr_event,
2732 qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
2736 static struct pci_driver qlcnic_driver = {
2737 .name = qlcnic_driver_name,
2738 .id_table = qlcnic_pci_tbl,
2739 .probe = qlcnic_probe,
2740 .remove = __devexit_p(qlcnic_remove),
2742 .suspend = qlcnic_suspend,
2743 .resume = qlcnic_resume,
2745 .shutdown = qlcnic_shutdown
2748 static int __init qlcnic_init_module(void)
2751 printk(KERN_INFO "%s\n", qlcnic_driver_string);
2754 register_netdevice_notifier(&qlcnic_netdev_cb);
2755 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
2759 return pci_register_driver(&qlcnic_driver);
2762 module_init(qlcnic_init_module);
2764 static void __exit qlcnic_exit_module(void)
2767 pci_unregister_driver(&qlcnic_driver);
2770 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
2771 unregister_netdevice_notifier(&qlcnic_netdev_cb);
2775 module_exit(qlcnic_exit_module);