OMAP3 PM: CPUFreq driver for OMAP3
[linux-ginger.git] / drivers / net / netxen / netxen_nic_main.c
blob7fc15e9e8adb89584c710067ff12d0709fd56d0e
1 /*
2 * Copyright (C) 2003 - 2009 NetXen, Inc.
3 * Copyright (C) 2009 - QLogic Corporation.
4 * All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.
26 #include <linux/vmalloc.h>
27 #include <linux/interrupt.h>
28 #include "netxen_nic_hw.h"
30 #include "netxen_nic.h"
32 #include <linux/dma-mapping.h>
33 #include <linux/if_vlan.h>
34 #include <net/ip.h>
35 #include <linux/ipv6.h>
36 #include <linux/inetdevice.h>
38 MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
39 MODULE_LICENSE("GPL");
40 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
42 char netxen_nic_driver_name[] = "netxen_nic";
43 static char netxen_nic_driver_string[] = "NetXen Network Driver version "
44 NETXEN_NIC_LINUX_VERSIONID;
46 static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
48 /* Default to restricted 1G auto-neg mode */
49 static int wol_port_mode = 5;
51 static int use_msi = 1;
53 static int use_msi_x = 1;
55 /* Local functions to NetXen NIC driver */
56 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
57 const struct pci_device_id *ent);
58 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
59 static int netxen_nic_open(struct net_device *netdev);
60 static int netxen_nic_close(struct net_device *netdev);
61 static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
62 struct net_device *);
63 static void netxen_tx_timeout(struct net_device *netdev);
64 static void netxen_tx_timeout_task(struct work_struct *work);
65 static void netxen_fw_poll_work(struct work_struct *work);
66 static void netxen_schedule_work(struct netxen_adapter *adapter,
67 work_func_t func, int delay);
68 static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
69 static int netxen_nic_poll(struct napi_struct *napi, int budget);
70 #ifdef CONFIG_NET_POLL_CONTROLLER
71 static void netxen_nic_poll_controller(struct net_device *netdev);
72 #endif
74 static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
75 static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
77 static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
78 static int netxen_can_start_firmware(struct netxen_adapter *adapter);
80 static irqreturn_t netxen_intr(int irq, void *data);
81 static irqreturn_t netxen_msi_intr(int irq, void *data);
82 static irqreturn_t netxen_msix_intr(int irq, void *data);
84 static void netxen_config_indev_addr(struct net_device *dev, unsigned long);
86 /* PCI Device ID Table */
87 #define ENTRY(device) \
88 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
89 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
91 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
92 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
93 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
94 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
95 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
96 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
97 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
98 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
99 ENTRY(PCI_DEVICE_ID_NX3031),
100 {0,}
103 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
105 static uint32_t crb_cmd_producer[4] = {
106 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
107 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
110 void
111 netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
112 struct nx_host_tx_ring *tx_ring)
114 NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
116 if (netxen_tx_avail(tx_ring) <= TX_STOP_THRESH) {
117 netif_stop_queue(adapter->netdev);
118 smp_mb();
122 static uint32_t crb_cmd_consumer[4] = {
123 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
124 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
127 static inline void
128 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
129 struct nx_host_tx_ring *tx_ring)
131 NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
134 static uint32_t msi_tgt_status[8] = {
135 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
136 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
137 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
138 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
141 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
143 static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
145 struct netxen_adapter *adapter = sds_ring->adapter;
147 NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
150 static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
152 struct netxen_adapter *adapter = sds_ring->adapter;
154 NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
156 if (!NETXEN_IS_MSI_FAMILY(adapter))
157 NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
160 static int
161 netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
163 int size = sizeof(struct nx_host_sds_ring) * count;
165 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
167 return (recv_ctx->sds_rings == NULL);
170 static void
171 netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
173 if (recv_ctx->sds_rings != NULL)
174 kfree(recv_ctx->sds_rings);
176 recv_ctx->sds_rings = NULL;
179 static int
180 netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
182 int ring;
183 struct nx_host_sds_ring *sds_ring;
184 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
186 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
187 return -ENOMEM;
189 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
190 sds_ring = &recv_ctx->sds_rings[ring];
191 netif_napi_add(netdev, &sds_ring->napi,
192 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
195 return 0;
198 static void
199 netxen_napi_del(struct netxen_adapter *adapter)
201 int ring;
202 struct nx_host_sds_ring *sds_ring;
203 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
205 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
206 sds_ring = &recv_ctx->sds_rings[ring];
207 netif_napi_del(&sds_ring->napi);
210 netxen_free_sds_rings(&adapter->recv_ctx);
213 static void
214 netxen_napi_enable(struct netxen_adapter *adapter)
216 int ring;
217 struct nx_host_sds_ring *sds_ring;
218 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
220 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
221 sds_ring = &recv_ctx->sds_rings[ring];
222 napi_enable(&sds_ring->napi);
223 netxen_nic_enable_int(sds_ring);
227 static void
228 netxen_napi_disable(struct netxen_adapter *adapter)
230 int ring;
231 struct nx_host_sds_ring *sds_ring;
232 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
234 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
235 sds_ring = &recv_ctx->sds_rings[ring];
236 netxen_nic_disable_int(sds_ring);
237 napi_synchronize(&sds_ring->napi);
238 napi_disable(&sds_ring->napi);
242 static int nx_set_dma_mask(struct netxen_adapter *adapter)
244 struct pci_dev *pdev = adapter->pdev;
245 uint64_t mask, cmask;
247 adapter->pci_using_dac = 0;
249 mask = DMA_BIT_MASK(32);
250 cmask = DMA_BIT_MASK(32);
252 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
253 #ifndef CONFIG_IA64
254 mask = DMA_BIT_MASK(35);
255 #endif
256 } else {
257 mask = DMA_BIT_MASK(39);
258 cmask = mask;
261 if (pci_set_dma_mask(pdev, mask) == 0 &&
262 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
263 adapter->pci_using_dac = 1;
264 return 0;
267 return -EIO;
270 /* Update addressable range if firmware supports it */
271 static int
272 nx_update_dma_mask(struct netxen_adapter *adapter)
274 int change, shift, err;
275 uint64_t mask, old_mask, old_cmask;
276 struct pci_dev *pdev = adapter->pdev;
278 change = 0;
280 shift = NXRD32(adapter, CRB_DMA_SHIFT);
281 if (shift > 32)
282 return 0;
284 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
285 change = 1;
286 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
287 change = 1;
289 if (change) {
290 old_mask = pdev->dma_mask;
291 old_cmask = pdev->dev.coherent_dma_mask;
293 mask = DMA_BIT_MASK(32+shift);
295 err = pci_set_dma_mask(pdev, mask);
296 if (err)
297 goto err_out;
299 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
301 err = pci_set_consistent_dma_mask(pdev, mask);
302 if (err)
303 goto err_out;
305 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
308 return 0;
310 err_out:
311 pci_set_dma_mask(pdev, old_mask);
312 pci_set_consistent_dma_mask(pdev, old_cmask);
313 return err;
316 static int
317 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
319 u32 val, timeout;
321 if (first_boot == 0x55555555) {
322 /* This is the first boot after power up */
323 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
325 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
326 return 0;
328 /* PCI bus master workaround */
329 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
330 if (!(first_boot & 0x4)) {
331 first_boot |= 0x4;
332 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
333 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
336 /* This is the first boot after power up */
337 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
338 if (first_boot != 0x80000f) {
339 /* clear the register for future unloads/loads */
340 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
341 return -EIO;
344 /* Start P2 boot loader */
345 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
346 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
347 timeout = 0;
348 do {
349 msleep(1);
350 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
352 if (++timeout > 5000)
353 return -EIO;
355 } while (val == NETXEN_BDINFO_MAGIC);
357 return 0;
360 static void netxen_set_port_mode(struct netxen_adapter *adapter)
362 u32 val, data;
364 val = adapter->ahw.board_type;
365 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
366 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
367 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
368 data = NETXEN_PORT_MODE_802_3_AP;
369 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
370 } else if (port_mode == NETXEN_PORT_MODE_XG) {
371 data = NETXEN_PORT_MODE_XG;
372 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
373 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
374 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
375 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
376 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
377 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
378 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
379 } else {
380 data = NETXEN_PORT_MODE_AUTO_NEG;
381 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
384 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
385 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
386 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
387 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
388 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
390 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
394 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
396 u32 control;
397 int pos;
399 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
400 if (pos) {
401 pci_read_config_dword(pdev, pos, &control);
402 if (enable)
403 control |= PCI_MSIX_FLAGS_ENABLE;
404 else
405 control = 0;
406 pci_write_config_dword(pdev, pos, control);
410 static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
412 int i;
414 for (i = 0; i < count; i++)
415 adapter->msix_entries[i].entry = i;
418 static int
419 netxen_read_mac_addr(struct netxen_adapter *adapter)
421 int i;
422 unsigned char *p;
423 __le64 mac_addr;
424 struct net_device *netdev = adapter->netdev;
425 struct pci_dev *pdev = adapter->pdev;
427 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
428 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
429 return -EIO;
430 } else {
431 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
432 return -EIO;
435 p = (unsigned char *)&mac_addr;
436 for (i = 0; i < 6; i++)
437 netdev->dev_addr[i] = *(p + 5 - i);
439 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
441 /* set station address */
443 if (!is_valid_ether_addr(netdev->perm_addr))
444 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
446 return 0;
449 int netxen_nic_set_mac(struct net_device *netdev, void *p)
451 struct netxen_adapter *adapter = netdev_priv(netdev);
452 struct sockaddr *addr = p;
454 if (!is_valid_ether_addr(addr->sa_data))
455 return -EINVAL;
457 if (netif_running(netdev)) {
458 netif_device_detach(netdev);
459 netxen_napi_disable(adapter);
462 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
463 adapter->macaddr_set(adapter, addr->sa_data);
465 if (netif_running(netdev)) {
466 netif_device_attach(netdev);
467 netxen_napi_enable(adapter);
469 return 0;
472 static void netxen_set_multicast_list(struct net_device *dev)
474 struct netxen_adapter *adapter = netdev_priv(dev);
476 adapter->set_multi(dev);
479 static const struct net_device_ops netxen_netdev_ops = {
480 .ndo_open = netxen_nic_open,
481 .ndo_stop = netxen_nic_close,
482 .ndo_start_xmit = netxen_nic_xmit_frame,
483 .ndo_get_stats = netxen_nic_get_stats,
484 .ndo_validate_addr = eth_validate_addr,
485 .ndo_set_multicast_list = netxen_set_multicast_list,
486 .ndo_set_mac_address = netxen_nic_set_mac,
487 .ndo_change_mtu = netxen_nic_change_mtu,
488 .ndo_tx_timeout = netxen_tx_timeout,
489 #ifdef CONFIG_NET_POLL_CONTROLLER
490 .ndo_poll_controller = netxen_nic_poll_controller,
491 #endif
494 static void
495 netxen_setup_intr(struct netxen_adapter *adapter)
497 struct netxen_legacy_intr_set *legacy_intrp;
498 struct pci_dev *pdev = adapter->pdev;
499 int err, num_msix;
501 if (adapter->rss_supported) {
502 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
503 MSIX_ENTRIES_PER_ADAPTER : 2;
504 } else
505 num_msix = 1;
507 adapter->max_sds_rings = 1;
509 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
511 if (adapter->ahw.revision_id >= NX_P3_B0)
512 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
513 else
514 legacy_intrp = &legacy_intr[0];
516 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
517 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
518 legacy_intrp->tgt_status_reg);
519 adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
520 legacy_intrp->tgt_mask_reg);
521 adapter->pci_int_reg = netxen_get_ioaddr(adapter,
522 legacy_intrp->pci_int_reg);
523 adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
525 if (adapter->ahw.revision_id >= NX_P3_B1)
526 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
527 ISR_INT_STATE_REG);
528 else
529 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
530 CRB_INT_VECTOR);
532 netxen_set_msix_bit(pdev, 0);
534 if (adapter->msix_supported) {
536 netxen_init_msix_entries(adapter, num_msix);
537 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
538 if (err == 0) {
539 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
540 netxen_set_msix_bit(pdev, 1);
542 if (adapter->rss_supported)
543 adapter->max_sds_rings = num_msix;
545 dev_info(&pdev->dev, "using msi-x interrupts\n");
546 return;
549 if (err > 0)
550 pci_disable_msix(pdev);
552 /* fall through for msi */
555 if (use_msi && !pci_enable_msi(pdev)) {
556 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
557 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
558 msi_tgt_status[adapter->ahw.pci_func]);
559 dev_info(&pdev->dev, "using msi interrupts\n");
560 adapter->msix_entries[0].vector = pdev->irq;
561 return;
564 dev_info(&pdev->dev, "using legacy interrupts\n");
565 adapter->msix_entries[0].vector = pdev->irq;
568 static void
569 netxen_teardown_intr(struct netxen_adapter *adapter)
571 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
572 pci_disable_msix(adapter->pdev);
573 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
574 pci_disable_msi(adapter->pdev);
577 static void
578 netxen_cleanup_pci_map(struct netxen_adapter *adapter)
580 if (adapter->ahw.db_base != NULL)
581 iounmap(adapter->ahw.db_base);
582 if (adapter->ahw.pci_base0 != NULL)
583 iounmap(adapter->ahw.pci_base0);
584 if (adapter->ahw.pci_base1 != NULL)
585 iounmap(adapter->ahw.pci_base1);
586 if (adapter->ahw.pci_base2 != NULL)
587 iounmap(adapter->ahw.pci_base2);
590 static int
591 netxen_setup_pci_map(struct netxen_adapter *adapter)
593 void __iomem *mem_ptr0 = NULL;
594 void __iomem *mem_ptr1 = NULL;
595 void __iomem *mem_ptr2 = NULL;
596 void __iomem *db_ptr = NULL;
598 resource_size_t mem_base, db_base;
599 unsigned long mem_len, db_len = 0, pci_len0 = 0;
601 struct pci_dev *pdev = adapter->pdev;
602 int pci_func = adapter->ahw.pci_func;
604 int err = 0;
607 * Set the CRB window to invalid. If any register in window 0 is
608 * accessed it should set the window to 0 and then reset it to 1.
610 adapter->curr_window = 255;
611 adapter->ahw.qdr_sn_window = -1;
612 adapter->ahw.ddr_mn_window = -1;
614 /* remap phys address */
615 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
616 mem_len = pci_resource_len(pdev, 0);
617 pci_len0 = 0;
619 /* 128 Meg of memory */
620 if (mem_len == NETXEN_PCI_128MB_SIZE) {
621 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
622 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
623 SECOND_PAGE_GROUP_SIZE);
624 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
625 THIRD_PAGE_GROUP_SIZE);
626 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
627 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
628 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
629 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
630 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
632 mem_ptr0 = pci_ioremap_bar(pdev, 0);
633 if (mem_ptr0 == NULL) {
634 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
635 return -EIO;
637 pci_len0 = mem_len;
639 adapter->ahw.ddr_mn_window = 0;
640 adapter->ahw.qdr_sn_window = 0;
642 adapter->ahw.mn_win_crb = NETXEN_PCI_CRBSPACE +
643 0x100000 + PCIX_MN_WINDOW + (pci_func * 0x20);
644 adapter->ahw.ms_win_crb = NETXEN_PCI_CRBSPACE +
645 0x100000 + PCIX_SN_WINDOW;
646 if (pci_func < 4)
647 adapter->ahw.ms_win_crb += (pci_func * 0x20);
648 else
649 adapter->ahw.ms_win_crb +=
650 0xA0 + ((pci_func - 4) * 0x10);
651 } else {
652 return -EIO;
655 netxen_setup_hwops(adapter);
657 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
659 adapter->ahw.pci_base0 = mem_ptr0;
660 adapter->ahw.pci_len0 = pci_len0;
661 adapter->ahw.pci_base1 = mem_ptr1;
662 adapter->ahw.pci_base2 = mem_ptr2;
664 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
665 goto skip_doorbell;
667 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
668 db_len = pci_resource_len(pdev, 4);
670 if (db_len == 0) {
671 printk(KERN_ERR "%s: doorbell is disabled\n",
672 netxen_nic_driver_name);
673 err = -EIO;
674 goto err_out;
677 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
678 if (!db_ptr) {
679 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
680 netxen_nic_driver_name);
681 err = -EIO;
682 goto err_out;
685 skip_doorbell:
686 adapter->ahw.db_base = db_ptr;
687 adapter->ahw.db_len = db_len;
688 return 0;
690 err_out:
691 netxen_cleanup_pci_map(adapter);
692 return err;
695 static void
696 netxen_check_options(struct netxen_adapter *adapter)
698 u32 fw_major, fw_minor, fw_build;
699 char brd_name[NETXEN_MAX_SHORT_NAME];
700 char serial_num[32];
701 int i, offset, val;
702 int *ptr32;
703 struct pci_dev *pdev = adapter->pdev;
705 adapter->driver_mismatch = 0;
707 ptr32 = (int *)&serial_num;
708 offset = NX_FW_SERIAL_NUM_OFFSET;
709 for (i = 0; i < 8; i++) {
710 if (netxen_rom_fast_read(adapter, offset, &val) == -1) {
711 dev_err(&pdev->dev, "error reading board info\n");
712 adapter->driver_mismatch = 1;
713 return;
715 ptr32[i] = cpu_to_le32(val);
716 offset += sizeof(u32);
719 fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
720 fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
721 fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
723 adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
725 if (adapter->portnum == 0) {
726 get_brd_name_by_type(adapter->ahw.board_type, brd_name);
728 printk(KERN_INFO "NetXen %s Board S/N %s Chip rev 0x%x\n",
729 brd_name, serial_num, adapter->ahw.revision_id);
732 if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
733 adapter->driver_mismatch = 1;
734 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
735 fw_major, fw_minor, fw_build);
736 return;
739 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
740 i = NXRD32(adapter, NETXEN_SRE_MISC);
741 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
744 dev_info(&pdev->dev, "firmware v%d.%d.%d [%s]\n",
745 fw_major, fw_minor, fw_build,
746 adapter->ahw.cut_through ? "cut-through" : "legacy");
748 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
749 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
751 adapter->flags &= ~NETXEN_NIC_LRO_ENABLED;
753 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
754 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
755 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
756 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
757 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
758 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
761 adapter->msix_supported = 0;
762 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
763 adapter->msix_supported = !!use_msi_x;
764 adapter->rss_supported = !!use_msi_x;
765 } else if (adapter->fw_version >= NETXEN_VERSION_CODE(3, 4, 336)) {
766 switch (adapter->ahw.board_type) {
767 case NETXEN_BRDTYPE_P2_SB31_10G:
768 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
769 adapter->msix_supported = !!use_msi_x;
770 adapter->rss_supported = !!use_msi_x;
771 break;
772 default:
773 break;
777 adapter->num_txd = MAX_CMD_DESCRIPTORS;
779 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
780 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
781 adapter->max_rds_rings = 3;
782 } else {
783 adapter->num_lro_rxd = 0;
784 adapter->max_rds_rings = 2;
788 static int
789 netxen_start_firmware(struct netxen_adapter *adapter)
791 int val, err, first_boot;
792 struct pci_dev *pdev = adapter->pdev;
794 /* required for NX2031 dummy dma */
795 err = nx_set_dma_mask(adapter);
796 if (err)
797 return err;
799 if (!netxen_can_start_firmware(adapter))
800 goto wait_init;
802 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
804 err = netxen_check_hw_init(adapter, first_boot);
805 if (err) {
806 dev_err(&pdev->dev, "error in init HW init sequence\n");
807 return err;
810 netxen_request_firmware(adapter);
812 err = netxen_need_fw_reset(adapter);
813 if (err < 0)
814 goto err_out;
815 if (err == 0)
816 goto ready;
818 if (first_boot != 0x55555555) {
819 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
820 netxen_pinit_from_rom(adapter, 0);
821 msleep(1);
824 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
825 NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
826 NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
828 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
829 netxen_set_port_mode(adapter);
831 err = netxen_load_firmware(adapter);
832 if (err)
833 goto err_out;
835 netxen_release_firmware(adapter);
837 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
839 /* Initialize multicast addr pool owners */
840 val = 0x7654;
841 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
842 val |= 0x0f000000;
843 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
847 err = netxen_init_dummy_dma(adapter);
848 if (err)
849 goto err_out;
852 * Tell the hardware our version number.
854 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
855 | ((_NETXEN_NIC_LINUX_MINOR << 8))
856 | (_NETXEN_NIC_LINUX_SUBVERSION);
857 NXWR32(adapter, CRB_DRIVER_VERSION, val);
859 ready:
860 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
862 wait_init:
863 /* Handshake with the card before we register the devices. */
864 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
865 if (err) {
866 netxen_free_dummy_dma(adapter);
867 goto err_out;
870 nx_update_dma_mask(adapter);
872 netxen_check_options(adapter);
874 adapter->need_fw_reset = 0;
876 /* fall through and release firmware */
878 err_out:
879 netxen_release_firmware(adapter);
880 return err;
883 static int
884 netxen_nic_request_irq(struct netxen_adapter *adapter)
886 irq_handler_t handler;
887 struct nx_host_sds_ring *sds_ring;
888 int err, ring;
890 unsigned long flags = IRQF_SAMPLE_RANDOM;
891 struct net_device *netdev = adapter->netdev;
892 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
894 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
895 handler = netxen_msix_intr;
896 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
897 handler = netxen_msi_intr;
898 else {
899 flags |= IRQF_SHARED;
900 handler = netxen_intr;
902 adapter->irq = netdev->irq;
904 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
905 sds_ring = &recv_ctx->sds_rings[ring];
906 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
907 err = request_irq(sds_ring->irq, handler,
908 flags, sds_ring->name, sds_ring);
909 if (err)
910 return err;
913 return 0;
916 static void
917 netxen_nic_free_irq(struct netxen_adapter *adapter)
919 int ring;
920 struct nx_host_sds_ring *sds_ring;
922 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
924 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
925 sds_ring = &recv_ctx->sds_rings[ring];
926 free_irq(sds_ring->irq, sds_ring);
930 static void
931 netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
933 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
934 adapter->coal.normal.data.rx_time_us =
935 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
936 adapter->coal.normal.data.rx_packets =
937 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
938 adapter->coal.normal.data.tx_time_us =
939 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
940 adapter->coal.normal.data.tx_packets =
941 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
944 static int
945 netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
947 int err;
949 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
950 return -EIO;
952 err = adapter->init_port(adapter, adapter->physical_port);
953 if (err) {
954 printk(KERN_ERR "%s: Failed to initialize port %d\n",
955 netxen_nic_driver_name, adapter->portnum);
956 return err;
958 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
959 adapter->macaddr_set(adapter, netdev->dev_addr);
961 adapter->set_multi(netdev);
962 adapter->set_mtu(adapter, netdev->mtu);
964 adapter->ahw.linkup = 0;
966 if (adapter->max_sds_rings > 1)
967 netxen_config_rss(adapter, 1);
969 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
970 netxen_config_intr_coalesce(adapter);
972 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
973 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
975 netxen_napi_enable(adapter);
977 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
978 netxen_linkevent_request(adapter, 1);
979 else
980 netxen_nic_set_link_parameters(adapter);
982 set_bit(__NX_DEV_UP, &adapter->state);
983 return 0;
986 static void
987 netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
989 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
990 return;
992 clear_bit(__NX_DEV_UP, &adapter->state);
994 spin_lock(&adapter->tx_clean_lock);
995 netif_carrier_off(netdev);
996 netif_tx_disable(netdev);
998 if (adapter->stop_port)
999 adapter->stop_port(adapter);
1001 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1002 netxen_p3_free_mac_list(adapter);
1004 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1006 netxen_napi_disable(adapter);
1008 netxen_release_tx_buffers(adapter);
1009 spin_unlock(&adapter->tx_clean_lock);
1013 static int
1014 netxen_nic_attach(struct netxen_adapter *adapter)
1016 struct net_device *netdev = adapter->netdev;
1017 struct pci_dev *pdev = adapter->pdev;
1018 int err, ring;
1019 struct nx_host_rds_ring *rds_ring;
1020 struct nx_host_tx_ring *tx_ring;
1022 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1023 return 0;
1025 err = netxen_init_firmware(adapter);
1026 if (err)
1027 return err;
1029 err = netxen_napi_add(adapter, netdev);
1030 if (err)
1031 return err;
1033 err = netxen_alloc_sw_resources(adapter);
1034 if (err) {
1035 printk(KERN_ERR "%s: Error in setting sw resources\n",
1036 netdev->name);
1037 return err;
1040 err = netxen_alloc_hw_resources(adapter);
1041 if (err) {
1042 printk(KERN_ERR "%s: Error in setting hw resources\n",
1043 netdev->name);
1044 goto err_out_free_sw;
1047 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1048 tx_ring = adapter->tx_ring;
1049 tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1050 crb_cmd_producer[adapter->portnum]);
1051 tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1052 crb_cmd_consumer[adapter->portnum]);
1054 tx_ring->producer = 0;
1055 tx_ring->sw_consumer = 0;
1057 netxen_nic_update_cmd_producer(adapter, tx_ring);
1058 netxen_nic_update_cmd_consumer(adapter, tx_ring);
1061 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1062 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1063 netxen_post_rx_buffers(adapter, ring, rds_ring);
1066 err = netxen_nic_request_irq(adapter);
1067 if (err) {
1068 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1069 netdev->name);
1070 goto err_out_free_rxbuf;
1073 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1074 netxen_nic_init_coalesce_defaults(adapter);
1076 netxen_create_sysfs_entries(adapter);
1078 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1079 return 0;
1081 err_out_free_rxbuf:
1082 netxen_release_rx_buffers(adapter);
1083 netxen_free_hw_resources(adapter);
1084 err_out_free_sw:
1085 netxen_free_sw_resources(adapter);
1086 return err;
1089 static void
1090 netxen_nic_detach(struct netxen_adapter *adapter)
1092 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1093 return;
1095 netxen_remove_sysfs_entries(adapter);
1097 netxen_free_hw_resources(adapter);
1098 netxen_release_rx_buffers(adapter);
1099 netxen_nic_free_irq(adapter);
1100 netxen_napi_del(adapter);
1101 netxen_free_sw_resources(adapter);
1103 adapter->is_up = 0;
1107 netxen_nic_reset_context(struct netxen_adapter *adapter)
1109 int err = 0;
1110 struct net_device *netdev = adapter->netdev;
1112 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1113 return -EBUSY;
1115 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1117 netif_device_detach(netdev);
1119 if (netif_running(netdev))
1120 netxen_nic_down(adapter, netdev);
1122 netxen_nic_detach(adapter);
1124 if (netif_running(netdev)) {
1125 err = netxen_nic_attach(adapter);
1126 if (!err)
1127 err = netxen_nic_up(adapter, netdev);
1129 if (err)
1130 goto done;
1133 netif_device_attach(netdev);
1136 done:
1137 clear_bit(__NX_RESETTING, &adapter->state);
1138 return err;
1141 static int
1142 netxen_setup_netdev(struct netxen_adapter *adapter,
1143 struct net_device *netdev)
1145 int err = 0;
1146 struct pci_dev *pdev = adapter->pdev;
1148 adapter->rx_csum = 1;
1149 adapter->mc_enabled = 0;
1150 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1151 adapter->max_mc_count = 38;
1152 else
1153 adapter->max_mc_count = 16;
1155 netdev->netdev_ops = &netxen_netdev_ops;
1156 netdev->watchdog_timeo = 2*HZ;
1158 netxen_nic_change_mtu(netdev, netdev->mtu);
1160 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
1162 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1163 netdev->features |= (NETIF_F_GRO);
1164 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1166 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1167 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1168 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1171 if (adapter->pci_using_dac) {
1172 netdev->features |= NETIF_F_HIGHDMA;
1173 netdev->vlan_features |= NETIF_F_HIGHDMA;
1176 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1177 netdev->features |= (NETIF_F_HW_VLAN_TX);
1179 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1180 netdev->features |= NETIF_F_LRO;
1182 netdev->irq = adapter->msix_entries[0].vector;
1184 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1186 if (netxen_read_mac_addr(adapter))
1187 dev_warn(&pdev->dev, "failed to read mac addr\n");
1189 netif_carrier_off(netdev);
1190 netif_stop_queue(netdev);
1192 err = register_netdev(netdev);
1193 if (err) {
1194 dev_err(&pdev->dev, "failed to register net device\n");
1195 return err;
1198 return 0;
1201 static int __devinit
1202 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1204 struct net_device *netdev = NULL;
1205 struct netxen_adapter *adapter = NULL;
1206 int i = 0, err;
1207 int pci_func_id = PCI_FUNC(pdev->devfn);
1208 uint8_t revision_id;
1210 if (pdev->class != 0x020000) {
1211 printk(KERN_DEBUG "NetXen function %d, class %x will not "
1212 "be enabled.\n",pci_func_id, pdev->class);
1213 return -ENODEV;
1216 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
1217 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
1218 "will not be enabled.\n",
1219 NX_P3_A0, NX_P3_B1);
1220 return -ENODEV;
1223 if ((err = pci_enable_device(pdev)))
1224 return err;
1226 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1227 err = -ENODEV;
1228 goto err_out_disable_pdev;
1231 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1232 goto err_out_disable_pdev;
1234 pci_set_master(pdev);
1236 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1237 if(!netdev) {
1238 dev_err(&pdev->dev, "failed to allocate net_device\n");
1239 err = -ENOMEM;
1240 goto err_out_free_res;
1243 SET_NETDEV_DEV(netdev, &pdev->dev);
1245 adapter = netdev_priv(netdev);
1246 adapter->netdev = netdev;
1247 adapter->pdev = pdev;
1248 adapter->ahw.pci_func = pci_func_id;
1250 revision_id = pdev->revision;
1251 adapter->ahw.revision_id = revision_id;
1253 rwlock_init(&adapter->adapter_lock);
1254 spin_lock_init(&adapter->tx_clean_lock);
1255 INIT_LIST_HEAD(&adapter->mac_list);
1257 err = netxen_setup_pci_map(adapter);
1258 if (err)
1259 goto err_out_free_netdev;
1261 /* This will be reset for mezz cards */
1262 adapter->portnum = pci_func_id;
1264 err = netxen_nic_get_board_info(adapter);
1265 if (err) {
1266 dev_err(&pdev->dev, "Error getting board config info.\n");
1267 goto err_out_iounmap;
1270 /* Mezz cards have PCI function 0,2,3 enabled */
1271 switch (adapter->ahw.board_type) {
1272 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1273 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1274 if (pci_func_id >= 2)
1275 adapter->portnum = pci_func_id - 2;
1276 break;
1277 default:
1278 break;
1281 err = netxen_start_firmware(adapter);
1282 if (err)
1283 goto err_out_iounmap;
1286 * See if the firmware gave us a virtual-physical port mapping.
1288 adapter->physical_port = adapter->portnum;
1289 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1290 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
1291 if (i != 0x55555555)
1292 adapter->physical_port = i;
1295 netxen_nic_clear_stats(adapter);
1297 netxen_setup_intr(adapter);
1299 err = netxen_setup_netdev(adapter, netdev);
1300 if (err)
1301 goto err_out_disable_msi;
1303 pci_set_drvdata(pdev, adapter);
1305 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1307 switch (adapter->ahw.port_type) {
1308 case NETXEN_NIC_GBE:
1309 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1310 adapter->netdev->name);
1311 break;
1312 case NETXEN_NIC_XGBE:
1313 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1314 adapter->netdev->name);
1315 break;
1318 return 0;
1320 err_out_disable_msi:
1321 netxen_teardown_intr(adapter);
1323 netxen_free_dummy_dma(adapter);
1325 nx_decr_dev_ref_cnt(adapter);
1327 err_out_iounmap:
1328 netxen_cleanup_pci_map(adapter);
1330 err_out_free_netdev:
1331 free_netdev(netdev);
1333 err_out_free_res:
1334 pci_release_regions(pdev);
1336 err_out_disable_pdev:
1337 pci_set_drvdata(pdev, NULL);
1338 pci_disable_device(pdev);
1339 return err;
1342 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1344 struct netxen_adapter *adapter;
1345 struct net_device *netdev;
1347 adapter = pci_get_drvdata(pdev);
1348 if (adapter == NULL)
1349 return;
1351 netdev = adapter->netdev;
1353 netxen_cancel_fw_work(adapter);
1355 unregister_netdev(netdev);
1357 cancel_work_sync(&adapter->tx_timeout_task);
1359 netxen_nic_detach(adapter);
1361 nx_decr_dev_ref_cnt(adapter);
1363 if (adapter->portnum == 0)
1364 netxen_free_dummy_dma(adapter);
1366 clear_bit(__NX_RESETTING, &adapter->state);
1368 netxen_teardown_intr(adapter);
1370 netxen_cleanup_pci_map(adapter);
1372 netxen_release_firmware(adapter);
1374 pci_release_regions(pdev);
1375 pci_disable_device(pdev);
1376 pci_set_drvdata(pdev, NULL);
1378 free_netdev(netdev);
1380 static int __netxen_nic_shutdown(struct pci_dev *pdev)
1382 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1383 struct net_device *netdev = adapter->netdev;
1384 int retval;
1386 netif_device_detach(netdev);
1388 netxen_cancel_fw_work(adapter);
1390 if (netif_running(netdev))
1391 netxen_nic_down(adapter, netdev);
1393 cancel_work_sync(&adapter->tx_timeout_task);
1395 netxen_nic_detach(adapter);
1397 if (adapter->portnum == 0)
1398 netxen_free_dummy_dma(adapter);
1400 nx_decr_dev_ref_cnt(adapter);
1402 clear_bit(__NX_RESETTING, &adapter->state);
1404 retval = pci_save_state(pdev);
1405 if (retval)
1406 return retval;
1408 if (netxen_nic_wol_supported(adapter)) {
1409 pci_enable_wake(pdev, PCI_D3cold, 1);
1410 pci_enable_wake(pdev, PCI_D3hot, 1);
1413 pci_disable_device(pdev);
1415 return 0;
1417 static void netxen_nic_shutdown(struct pci_dev *pdev)
1419 if (__netxen_nic_shutdown(pdev))
1420 return;
1422 #ifdef CONFIG_PM
1423 static int
1424 netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1426 int retval;
1428 retval = __netxen_nic_shutdown(pdev);
1429 if (retval)
1430 return retval;
1432 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1433 return 0;
1436 static int
1437 netxen_nic_resume(struct pci_dev *pdev)
1439 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1440 struct net_device *netdev = adapter->netdev;
1441 int err;
1443 pci_set_power_state(pdev, PCI_D0);
1444 pci_restore_state(pdev);
1446 err = pci_enable_device(pdev);
1447 if (err)
1448 return err;
1450 adapter->curr_window = 255;
1452 err = netxen_start_firmware(adapter);
1453 if (err) {
1454 dev_err(&pdev->dev, "failed to start firmware\n");
1455 return err;
1458 if (netif_running(netdev)) {
1459 err = netxen_nic_attach(adapter);
1460 if (err)
1461 goto err_out;
1463 err = netxen_nic_up(adapter, netdev);
1464 if (err)
1465 goto err_out_detach;
1467 netif_device_attach(netdev);
1469 netxen_config_indev_addr(netdev, NETDEV_UP);
1472 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1473 return 0;
1475 err_out_detach:
1476 netxen_nic_detach(adapter);
1477 err_out:
1478 nx_decr_dev_ref_cnt(adapter);
1479 return err;
1481 #endif
1483 static int netxen_nic_open(struct net_device *netdev)
1485 struct netxen_adapter *adapter = netdev_priv(netdev);
1486 int err = 0;
1488 if (adapter->driver_mismatch)
1489 return -EIO;
1491 err = netxen_nic_attach(adapter);
1492 if (err)
1493 return err;
1495 err = netxen_nic_up(adapter, netdev);
1496 if (err)
1497 goto err_out;
1499 netif_start_queue(netdev);
1501 return 0;
1503 err_out:
1504 netxen_nic_detach(adapter);
1505 return err;
1509 * netxen_nic_close - Disables a network interface entry point
1511 static int netxen_nic_close(struct net_device *netdev)
1513 struct netxen_adapter *adapter = netdev_priv(netdev);
1515 netxen_nic_down(adapter, netdev);
1516 return 0;
1519 static void
1520 netxen_tso_check(struct net_device *netdev,
1521 struct nx_host_tx_ring *tx_ring,
1522 struct cmd_desc_type0 *first_desc,
1523 struct sk_buff *skb)
1525 u8 opcode = TX_ETHER_PKT;
1526 __be16 protocol = skb->protocol;
1527 u16 flags = 0, vid = 0;
1528 u32 producer;
1529 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1530 struct cmd_desc_type0 *hwdesc;
1531 struct vlan_ethhdr *vh;
1533 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1535 vh = (struct vlan_ethhdr *)skb->data;
1536 protocol = vh->h_vlan_encapsulated_proto;
1537 flags = FLAGS_VLAN_TAGGED;
1539 } else if (vlan_tx_tag_present(skb)) {
1541 flags = FLAGS_VLAN_OOB;
1542 vid = vlan_tx_tag_get(skb);
1543 netxen_set_tx_vlan_tci(first_desc, vid);
1544 vlan_oob = 1;
1547 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1548 skb_shinfo(skb)->gso_size > 0) {
1550 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1552 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1553 first_desc->total_hdr_length = hdr_len;
1554 if (vlan_oob) {
1555 first_desc->total_hdr_length += VLAN_HLEN;
1556 first_desc->tcp_hdr_offset = VLAN_HLEN;
1557 first_desc->ip_hdr_offset = VLAN_HLEN;
1558 /* Only in case of TSO on vlan device */
1559 flags |= FLAGS_VLAN_TAGGED;
1562 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1563 TX_TCP_LSO6 : TX_TCP_LSO;
1564 tso = 1;
1566 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1567 u8 l4proto;
1569 if (protocol == cpu_to_be16(ETH_P_IP)) {
1570 l4proto = ip_hdr(skb)->protocol;
1572 if (l4proto == IPPROTO_TCP)
1573 opcode = TX_TCP_PKT;
1574 else if(l4proto == IPPROTO_UDP)
1575 opcode = TX_UDP_PKT;
1576 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1577 l4proto = ipv6_hdr(skb)->nexthdr;
1579 if (l4proto == IPPROTO_TCP)
1580 opcode = TX_TCPV6_PKT;
1581 else if(l4proto == IPPROTO_UDP)
1582 opcode = TX_UDPV6_PKT;
1586 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1587 first_desc->ip_hdr_offset += skb_network_offset(skb);
1588 netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1590 if (!tso)
1591 return;
1593 /* For LSO, we need to copy the MAC/IP/TCP headers into
1594 * the descriptor ring
1596 producer = tx_ring->producer;
1597 copied = 0;
1598 offset = 2;
1600 if (vlan_oob) {
1601 /* Create a TSO vlan header template for firmware */
1603 hwdesc = &tx_ring->desc_head[producer];
1604 tx_ring->cmd_buf_arr[producer].skb = NULL;
1606 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1607 hdr_len + VLAN_HLEN);
1609 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1610 skb_copy_from_linear_data(skb, vh, 12);
1611 vh->h_vlan_proto = htons(ETH_P_8021Q);
1612 vh->h_vlan_TCI = htons(vid);
1613 skb_copy_from_linear_data_offset(skb, 12,
1614 (char *)vh + 16, copy_len - 16);
1616 copied = copy_len - VLAN_HLEN;
1617 offset = 0;
1619 producer = get_next_index(producer, tx_ring->num_desc);
1622 while (copied < hdr_len) {
1624 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1625 (hdr_len - copied));
1627 hwdesc = &tx_ring->desc_head[producer];
1628 tx_ring->cmd_buf_arr[producer].skb = NULL;
1630 skb_copy_from_linear_data_offset(skb, copied,
1631 (char *)hwdesc + offset, copy_len);
1633 copied += copy_len;
1634 offset = 0;
1636 producer = get_next_index(producer, tx_ring->num_desc);
1639 tx_ring->producer = producer;
1640 barrier();
1643 static int
1644 netxen_map_tx_skb(struct pci_dev *pdev,
1645 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
1647 struct netxen_skb_frag *nf;
1648 struct skb_frag_struct *frag;
1649 int i, nr_frags;
1650 dma_addr_t map;
1652 nr_frags = skb_shinfo(skb)->nr_frags;
1653 nf = &pbuf->frag_array[0];
1655 map = pci_map_single(pdev, skb->data,
1656 skb_headlen(skb), PCI_DMA_TODEVICE);
1657 if (pci_dma_mapping_error(pdev, map))
1658 goto out_err;
1660 nf->dma = map;
1661 nf->length = skb_headlen(skb);
1663 for (i = 0; i < nr_frags; i++) {
1664 frag = &skb_shinfo(skb)->frags[i];
1665 nf = &pbuf->frag_array[i+1];
1667 map = pci_map_page(pdev, frag->page, frag->page_offset,
1668 frag->size, PCI_DMA_TODEVICE);
1669 if (pci_dma_mapping_error(pdev, map))
1670 goto unwind;
1672 nf->dma = map;
1673 nf->length = frag->size;
1676 return 0;
1678 unwind:
1679 while (--i >= 0) {
1680 nf = &pbuf->frag_array[i+1];
1681 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1684 nf = &pbuf->frag_array[0];
1685 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1687 out_err:
1688 return -ENOMEM;
1691 static inline void
1692 netxen_clear_cmddesc(u64 *desc)
1694 desc[0] = 0ULL;
1695 desc[2] = 0ULL;
1698 static netdev_tx_t
1699 netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1701 struct netxen_adapter *adapter = netdev_priv(netdev);
1702 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
1703 struct netxen_cmd_buffer *pbuf;
1704 struct netxen_skb_frag *buffrag;
1705 struct cmd_desc_type0 *hwdesc, *first_desc;
1706 struct pci_dev *pdev;
1707 int i, k;
1709 u32 producer;
1710 int frag_count, no_of_desc;
1711 u32 num_txd = tx_ring->num_desc;
1713 frag_count = skb_shinfo(skb)->nr_frags + 1;
1715 /* 4 fragments per cmd des */
1716 no_of_desc = (frag_count + 3) >> 2;
1718 if (unlikely(no_of_desc + 2 > netxen_tx_avail(tx_ring))) {
1719 netif_stop_queue(netdev);
1720 return NETDEV_TX_BUSY;
1723 producer = tx_ring->producer;
1724 pbuf = &tx_ring->cmd_buf_arr[producer];
1726 pdev = adapter->pdev;
1728 if (netxen_map_tx_skb(pdev, skb, pbuf))
1729 goto drop_packet;
1731 pbuf->skb = skb;
1732 pbuf->frag_count = frag_count;
1734 first_desc = hwdesc = &tx_ring->desc_head[producer];
1735 netxen_clear_cmddesc((u64 *)hwdesc);
1737 netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
1738 netxen_set_tx_port(first_desc, adapter->portnum);
1740 for (i = 0; i < frag_count; i++) {
1742 k = i % 4;
1744 if ((k == 0) && (i > 0)) {
1745 /* move to next desc.*/
1746 producer = get_next_index(producer, num_txd);
1747 hwdesc = &tx_ring->desc_head[producer];
1748 netxen_clear_cmddesc((u64 *)hwdesc);
1749 tx_ring->cmd_buf_arr[producer].skb = NULL;
1752 buffrag = &pbuf->frag_array[i];
1754 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
1755 switch (k) {
1756 case 0:
1757 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1758 break;
1759 case 1:
1760 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
1761 break;
1762 case 2:
1763 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
1764 break;
1765 case 3:
1766 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
1767 break;
1771 tx_ring->producer = get_next_index(producer, num_txd);
1773 netxen_tso_check(netdev, tx_ring, first_desc, skb);
1775 netxen_nic_update_cmd_producer(adapter, tx_ring);
1777 adapter->stats.txbytes += skb->len;
1778 adapter->stats.xmitcalled++;
1780 return NETDEV_TX_OK;
1782 drop_packet:
1783 adapter->stats.txdropped++;
1784 dev_kfree_skb_any(skb);
1785 return NETDEV_TX_OK;
1788 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1790 struct net_device *netdev = adapter->netdev;
1791 uint32_t temp, temp_state, temp_val;
1792 int rv = 0;
1794 temp = NXRD32(adapter, CRB_TEMP_STATE);
1796 temp_state = nx_get_temp_state(temp);
1797 temp_val = nx_get_temp_val(temp);
1799 if (temp_state == NX_TEMP_PANIC) {
1800 printk(KERN_ALERT
1801 "%s: Device temperature %d degrees C exceeds"
1802 " maximum allowed. Hardware has been shut down.\n",
1803 netdev->name, temp_val);
1804 rv = 1;
1805 } else if (temp_state == NX_TEMP_WARN) {
1806 if (adapter->temp == NX_TEMP_NORMAL) {
1807 printk(KERN_ALERT
1808 "%s: Device temperature %d degrees C "
1809 "exceeds operating range."
1810 " Immediate action needed.\n",
1811 netdev->name, temp_val);
1813 } else {
1814 if (adapter->temp == NX_TEMP_WARN) {
1815 printk(KERN_INFO
1816 "%s: Device temperature is now %d degrees C"
1817 " in normal range.\n", netdev->name,
1818 temp_val);
1821 adapter->temp = temp_state;
1822 return rv;
1825 void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
1827 struct net_device *netdev = adapter->netdev;
1829 if (adapter->ahw.linkup && !linkup) {
1830 printk(KERN_INFO "%s: %s NIC Link is down\n",
1831 netxen_nic_driver_name, netdev->name);
1832 adapter->ahw.linkup = 0;
1833 if (netif_running(netdev)) {
1834 netif_carrier_off(netdev);
1835 netif_stop_queue(netdev);
1837 adapter->link_changed = !adapter->has_link_events;
1838 } else if (!adapter->ahw.linkup && linkup) {
1839 printk(KERN_INFO "%s: %s NIC Link is up\n",
1840 netxen_nic_driver_name, netdev->name);
1841 adapter->ahw.linkup = 1;
1842 if (netif_running(netdev)) {
1843 netif_carrier_on(netdev);
1844 netif_wake_queue(netdev);
1846 adapter->link_changed = !adapter->has_link_events;
1850 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1852 u32 val, port, linkup;
1854 port = adapter->physical_port;
1856 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1857 val = NXRD32(adapter, CRB_XG_STATE_P3);
1858 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1859 linkup = (val == XG_LINK_UP_P3);
1860 } else {
1861 val = NXRD32(adapter, CRB_XG_STATE);
1862 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
1863 linkup = (val >> port) & 1;
1864 else {
1865 val = (val >> port*8) & 0xff;
1866 linkup = (val == XG_LINK_UP);
1870 netxen_advert_link_change(adapter, linkup);
1873 static void netxen_tx_timeout(struct net_device *netdev)
1875 struct netxen_adapter *adapter = netdev_priv(netdev);
1877 if (test_bit(__NX_RESETTING, &adapter->state))
1878 return;
1880 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
1881 schedule_work(&adapter->tx_timeout_task);
1884 static void netxen_tx_timeout_task(struct work_struct *work)
1886 struct netxen_adapter *adapter =
1887 container_of(work, struct netxen_adapter, tx_timeout_task);
1889 if (!netif_running(adapter->netdev))
1890 return;
1892 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1893 return;
1895 if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
1896 goto request_reset;
1898 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1899 /* try to scrub interrupt */
1900 netxen_napi_disable(adapter);
1902 adapter->netdev->trans_start = jiffies;
1904 netxen_napi_enable(adapter);
1906 netif_wake_queue(adapter->netdev);
1908 clear_bit(__NX_RESETTING, &adapter->state);
1910 } else {
1911 clear_bit(__NX_RESETTING, &adapter->state);
1912 if (!netxen_nic_reset_context(adapter)) {
1913 adapter->netdev->trans_start = jiffies;
1914 return;
1917 /* context reset failed, fall through for fw reset */
1920 request_reset:
1921 adapter->need_fw_reset = 1;
1924 struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1926 struct netxen_adapter *adapter = netdev_priv(netdev);
1927 struct net_device_stats *stats = &adapter->net_stats;
1929 memset(stats, 0, sizeof(*stats));
1931 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
1932 stats->tx_packets = adapter->stats.xmitfinished;
1933 stats->rx_bytes = adapter->stats.rxbytes;
1934 stats->tx_bytes = adapter->stats.txbytes;
1935 stats->rx_dropped = adapter->stats.rxdropped;
1936 stats->tx_dropped = adapter->stats.txdropped;
1938 return stats;
1941 static irqreturn_t netxen_intr(int irq, void *data)
1943 struct nx_host_sds_ring *sds_ring = data;
1944 struct netxen_adapter *adapter = sds_ring->adapter;
1945 u32 status = 0;
1947 status = readl(adapter->isr_int_vec);
1949 if (!(status & adapter->int_vec_bit))
1950 return IRQ_NONE;
1952 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1953 /* check interrupt state machine, to be sure */
1954 status = readl(adapter->crb_int_state_reg);
1955 if (!ISR_LEGACY_INT_TRIGGERED(status))
1956 return IRQ_NONE;
1958 } else {
1959 unsigned long our_int = 0;
1961 our_int = readl(adapter->crb_int_state_reg);
1963 /* not our interrupt */
1964 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
1965 return IRQ_NONE;
1967 /* claim interrupt */
1968 writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
1970 /* clear interrupt */
1971 netxen_nic_disable_int(sds_ring);
1974 writel(0xffffffff, adapter->tgt_status_reg);
1975 /* read twice to ensure write is flushed */
1976 readl(adapter->isr_int_vec);
1977 readl(adapter->isr_int_vec);
1979 napi_schedule(&sds_ring->napi);
1981 return IRQ_HANDLED;
1984 static irqreturn_t netxen_msi_intr(int irq, void *data)
1986 struct nx_host_sds_ring *sds_ring = data;
1987 struct netxen_adapter *adapter = sds_ring->adapter;
1989 /* clear interrupt */
1990 writel(0xffffffff, adapter->tgt_status_reg);
1992 napi_schedule(&sds_ring->napi);
1993 return IRQ_HANDLED;
1996 static irqreturn_t netxen_msix_intr(int irq, void *data)
1998 struct nx_host_sds_ring *sds_ring = data;
2000 napi_schedule(&sds_ring->napi);
2001 return IRQ_HANDLED;
2004 static int netxen_nic_poll(struct napi_struct *napi, int budget)
2006 struct nx_host_sds_ring *sds_ring =
2007 container_of(napi, struct nx_host_sds_ring, napi);
2009 struct netxen_adapter *adapter = sds_ring->adapter;
2011 int tx_complete;
2012 int work_done;
2014 tx_complete = netxen_process_cmd_ring(adapter);
2016 work_done = netxen_process_rcv_ring(sds_ring, budget);
2018 if ((work_done < budget) && tx_complete) {
2019 napi_complete(&sds_ring->napi);
2020 if (netif_running(adapter->netdev))
2021 netxen_nic_enable_int(sds_ring);
2024 return work_done;
2027 #ifdef CONFIG_NET_POLL_CONTROLLER
2028 static void netxen_nic_poll_controller(struct net_device *netdev)
2030 struct netxen_adapter *adapter = netdev_priv(netdev);
2031 disable_irq(adapter->irq);
2032 netxen_intr(adapter->irq, adapter);
2033 enable_irq(adapter->irq);
2035 #endif
2037 static int
2038 nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
2040 int count;
2041 if (netxen_api_lock(adapter))
2042 return -EIO;
2044 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2046 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2048 netxen_api_unlock(adapter);
2049 return count;
2052 static int
2053 nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
2055 int count;
2056 if (netxen_api_lock(adapter))
2057 return -EIO;
2059 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2060 WARN_ON(count == 0);
2062 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2064 if (count == 0)
2065 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2067 netxen_api_unlock(adapter);
2068 return count;
2071 static void
2072 nx_dev_request_reset(struct netxen_adapter *adapter)
2074 u32 state;
2076 if (netxen_api_lock(adapter))
2077 return;
2079 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2081 if (state != NX_DEV_INITALIZING)
2082 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
2084 netxen_api_unlock(adapter);
2087 static int
2088 netxen_can_start_firmware(struct netxen_adapter *adapter)
2090 int count;
2091 int can_start = 0;
2093 if (netxen_api_lock(adapter))
2094 return 0;
2096 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2098 if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2099 count = 0;
2101 if (count == 0) {
2102 can_start = 1;
2103 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2106 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2108 netxen_api_unlock(adapter);
2110 return can_start;
2113 static void
2114 netxen_schedule_work(struct netxen_adapter *adapter,
2115 work_func_t func, int delay)
2117 INIT_DELAYED_WORK(&adapter->fw_work, func);
2118 schedule_delayed_work(&adapter->fw_work, delay);
2121 static void
2122 netxen_cancel_fw_work(struct netxen_adapter *adapter)
2124 while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2125 msleep(10);
2127 cancel_delayed_work_sync(&adapter->fw_work);
2130 static void
2131 netxen_attach_work(struct work_struct *work)
2133 struct netxen_adapter *adapter = container_of(work,
2134 struct netxen_adapter, fw_work.work);
2135 struct net_device *netdev = adapter->netdev;
2136 int err = 0;
2138 if (netif_running(netdev)) {
2139 err = netxen_nic_attach(adapter);
2140 if (err)
2141 goto done;
2143 err = netxen_nic_up(adapter, netdev);
2144 if (err) {
2145 netxen_nic_detach(adapter);
2146 goto done;
2149 netxen_config_indev_addr(netdev, NETDEV_UP);
2152 netif_device_attach(netdev);
2154 done:
2155 adapter->fw_fail_cnt = 0;
2156 clear_bit(__NX_RESETTING, &adapter->state);
2157 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2160 static void
2161 netxen_fwinit_work(struct work_struct *work)
2163 struct netxen_adapter *adapter = container_of(work,
2164 struct netxen_adapter, fw_work.work);
2165 int dev_state;
2167 dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2169 switch (dev_state) {
2170 case NX_DEV_COLD:
2171 case NX_DEV_READY:
2172 if (!netxen_start_firmware(adapter)) {
2173 netxen_schedule_work(adapter, netxen_attach_work, 0);
2174 return;
2176 break;
2178 case NX_DEV_INITALIZING:
2179 if (++adapter->fw_wait_cnt < FW_POLL_THRESH) {
2180 netxen_schedule_work(adapter,
2181 netxen_fwinit_work, 2 * FW_POLL_DELAY);
2182 return;
2184 break;
2186 case NX_DEV_FAILED:
2187 default:
2188 break;
2191 nx_incr_dev_ref_cnt(adapter);
2192 clear_bit(__NX_RESETTING, &adapter->state);
2195 static void
2196 netxen_detach_work(struct work_struct *work)
2198 struct netxen_adapter *adapter = container_of(work,
2199 struct netxen_adapter, fw_work.work);
2200 struct net_device *netdev = adapter->netdev;
2201 int ref_cnt, delay;
2202 u32 status;
2204 netif_device_detach(netdev);
2206 if (netif_running(netdev))
2207 netxen_nic_down(adapter, netdev);
2209 netxen_nic_detach(adapter);
2211 status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2213 ref_cnt = nx_decr_dev_ref_cnt(adapter);
2215 if (status & NX_RCODE_FATAL_ERROR)
2216 return;
2218 if (adapter->temp == NX_TEMP_PANIC)
2219 return;
2221 delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2223 adapter->fw_wait_cnt = 0;
2224 netxen_schedule_work(adapter, netxen_fwinit_work, delay);
2227 static int
2228 netxen_check_health(struct netxen_adapter *adapter)
2230 u32 state, heartbit;
2231 struct net_device *netdev = adapter->netdev;
2233 if (netxen_nic_check_temp(adapter))
2234 goto detach;
2236 if (adapter->need_fw_reset) {
2237 nx_dev_request_reset(adapter);
2238 goto detach;
2241 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2242 if (state == NX_DEV_NEED_RESET)
2243 goto detach;
2245 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2246 return 0;
2248 heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2249 if (heartbit != adapter->heartbit) {
2250 adapter->heartbit = heartbit;
2251 adapter->fw_fail_cnt = 0;
2252 return 0;
2255 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2256 return 0;
2258 clear_bit(__NX_FW_ATTACHED, &adapter->state);
2260 dev_info(&netdev->dev, "firmware hang detected\n");
2262 detach:
2263 if (!test_and_set_bit(__NX_RESETTING, &adapter->state))
2264 netxen_schedule_work(adapter, netxen_detach_work, 0);
2265 return 1;
2268 static void
2269 netxen_fw_poll_work(struct work_struct *work)
2271 struct netxen_adapter *adapter = container_of(work,
2272 struct netxen_adapter, fw_work.work);
2274 if (test_bit(__NX_RESETTING, &adapter->state))
2275 goto reschedule;
2277 if (test_bit(__NX_DEV_UP, &adapter->state)) {
2278 if (!adapter->has_link_events) {
2280 netxen_nic_handle_phy_intr(adapter);
2282 if (adapter->link_changed)
2283 netxen_nic_set_link_parameters(adapter);
2287 if (netxen_check_health(adapter))
2288 return;
2290 reschedule:
2291 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2294 static ssize_t
2295 netxen_store_bridged_mode(struct device *dev,
2296 struct device_attribute *attr, const char *buf, size_t len)
2298 struct net_device *net = to_net_dev(dev);
2299 struct netxen_adapter *adapter = netdev_priv(net);
2300 unsigned long new;
2301 int ret = -EINVAL;
2303 if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2304 goto err_out;
2306 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2307 goto err_out;
2309 if (strict_strtoul(buf, 2, &new))
2310 goto err_out;
2312 if (!netxen_config_bridged_mode(adapter, !!new))
2313 ret = len;
2315 err_out:
2316 return ret;
2319 static ssize_t
2320 netxen_show_bridged_mode(struct device *dev,
2321 struct device_attribute *attr, char *buf)
2323 struct net_device *net = to_net_dev(dev);
2324 struct netxen_adapter *adapter;
2325 int bridged_mode = 0;
2327 adapter = netdev_priv(net);
2329 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2330 bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2332 return sprintf(buf, "%d\n", bridged_mode);
2335 static struct device_attribute dev_attr_bridged_mode = {
2336 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2337 .show = netxen_show_bridged_mode,
2338 .store = netxen_store_bridged_mode,
2341 static void
2342 netxen_create_sysfs_entries(struct netxen_adapter *adapter)
2344 struct net_device *netdev = adapter->netdev;
2345 struct device *dev = &netdev->dev;
2347 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
2348 /* bridged_mode control */
2349 if (device_create_file(dev, &dev_attr_bridged_mode)) {
2350 dev_warn(&netdev->dev,
2351 "failed to create bridged_mode sysfs entry\n");
2356 static void
2357 netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
2359 struct net_device *netdev = adapter->netdev;
2360 struct device *dev = &netdev->dev;
2362 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2363 device_remove_file(dev, &dev_attr_bridged_mode);
2366 #ifdef CONFIG_INET
2368 #define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
2370 static int
2371 netxen_destip_supported(struct netxen_adapter *adapter)
2373 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2374 return 0;
2376 if (adapter->ahw.cut_through)
2377 return 0;
2379 return 1;
2382 static void
2383 netxen_config_indev_addr(struct net_device *dev, unsigned long event)
2385 struct in_device *indev;
2386 struct netxen_adapter *adapter = netdev_priv(dev);
2388 if (!netxen_destip_supported(adapter))
2389 return;
2391 indev = in_dev_get(dev);
2392 if (!indev)
2393 return;
2395 for_ifa(indev) {
2396 switch (event) {
2397 case NETDEV_UP:
2398 netxen_config_ipaddr(adapter,
2399 ifa->ifa_address, NX_IP_UP);
2400 break;
2401 case NETDEV_DOWN:
2402 netxen_config_ipaddr(adapter,
2403 ifa->ifa_address, NX_IP_DOWN);
2404 break;
2405 default:
2406 break;
2408 } endfor_ifa(indev);
2410 in_dev_put(indev);
2411 return;
2414 static int netxen_netdev_event(struct notifier_block *this,
2415 unsigned long event, void *ptr)
2417 struct netxen_adapter *adapter;
2418 struct net_device *dev = (struct net_device *)ptr;
2420 recheck:
2421 if (dev == NULL)
2422 goto done;
2424 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2425 dev = vlan_dev_real_dev(dev);
2426 goto recheck;
2429 if (!is_netxen_netdev(dev))
2430 goto done;
2432 adapter = netdev_priv(dev);
2434 if (!adapter)
2435 goto done;
2437 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2438 goto done;
2440 netxen_config_indev_addr(dev, event);
2441 done:
2442 return NOTIFY_DONE;
2445 static int
2446 netxen_inetaddr_event(struct notifier_block *this,
2447 unsigned long event, void *ptr)
2449 struct netxen_adapter *adapter;
2450 struct net_device *dev;
2452 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2454 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2456 recheck:
2457 if (dev == NULL || !netif_running(dev))
2458 goto done;
2460 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2461 dev = vlan_dev_real_dev(dev);
2462 goto recheck;
2465 if (!is_netxen_netdev(dev))
2466 goto done;
2468 adapter = netdev_priv(dev);
2470 if (!adapter || !netxen_destip_supported(adapter))
2471 goto done;
2473 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2474 goto done;
2476 switch (event) {
2477 case NETDEV_UP:
2478 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
2479 break;
2480 case NETDEV_DOWN:
2481 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_DOWN);
2482 break;
2483 default:
2484 break;
2487 done:
2488 return NOTIFY_DONE;
2491 static struct notifier_block netxen_netdev_cb = {
2492 .notifier_call = netxen_netdev_event,
2495 static struct notifier_block netxen_inetaddr_cb = {
2496 .notifier_call = netxen_inetaddr_event,
2498 #else
2499 static void
2500 netxen_config_indev_addr(struct net_device *dev, unsigned long event)
2502 #endif
2504 static struct pci_driver netxen_driver = {
2505 .name = netxen_nic_driver_name,
2506 .id_table = netxen_pci_tbl,
2507 .probe = netxen_nic_probe,
2508 .remove = __devexit_p(netxen_nic_remove),
2509 #ifdef CONFIG_PM
2510 .suspend = netxen_nic_suspend,
2511 .resume = netxen_nic_resume,
2512 #endif
2513 .shutdown = netxen_nic_shutdown
2516 static int __init netxen_init_module(void)
2518 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
2520 #ifdef CONFIG_INET
2521 register_netdevice_notifier(&netxen_netdev_cb);
2522 register_inetaddr_notifier(&netxen_inetaddr_cb);
2523 #endif
2525 return pci_register_driver(&netxen_driver);
2528 module_init(netxen_init_module);
2530 static void __exit netxen_exit_module(void)
2532 pci_unregister_driver(&netxen_driver);
2534 #ifdef CONFIG_INET
2535 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
2536 unregister_netdevice_notifier(&netxen_netdev_cb);
2537 #endif
2540 module_exit(netxen_exit_module);