spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / net / ethernet / qlogic / netxen / netxen_nic_main.c
blob7dd9a4b107e622980620d231b8f178335b1c526e
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 "COPYING".
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/interrupt.h>
29 #include "netxen_nic_hw.h"
31 #include "netxen_nic.h"
33 #include <linux/dma-mapping.h>
34 #include <linux/if_vlan.h>
35 #include <net/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/inetdevice.h>
38 #include <linux/sysfs.h>
39 #include <linux/aer.h>
41 MODULE_DESCRIPTION("QLogic/NetXen (1/10) GbE Intelligent Ethernet Driver");
42 MODULE_LICENSE("GPL");
43 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
44 MODULE_FIRMWARE(NX_UNIFIED_ROMIMAGE_NAME);
46 char netxen_nic_driver_name[] = "netxen_nic";
47 static char netxen_nic_driver_string[] = "QLogic/NetXen Network Driver v"
48 NETXEN_NIC_LINUX_VERSIONID;
50 static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
52 /* Default to restricted 1G auto-neg mode */
53 static int wol_port_mode = 5;
55 static int use_msi = 1;
57 static int use_msi_x = 1;
59 static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
60 module_param(auto_fw_reset, int, 0644);
61 MODULE_PARM_DESC(auto_fw_reset,"Auto firmware reset (0=disabled, 1=enabled");
63 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
64 const struct pci_device_id *ent);
65 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
66 static int netxen_nic_open(struct net_device *netdev);
67 static int netxen_nic_close(struct net_device *netdev);
68 static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
69 struct net_device *);
70 static void netxen_tx_timeout(struct net_device *netdev);
71 static void netxen_tx_timeout_task(struct work_struct *work);
72 static void netxen_fw_poll_work(struct work_struct *work);
73 static void netxen_schedule_work(struct netxen_adapter *adapter,
74 work_func_t func, int delay);
75 static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
76 static int netxen_nic_poll(struct napi_struct *napi, int budget);
77 #ifdef CONFIG_NET_POLL_CONTROLLER
78 static void netxen_nic_poll_controller(struct net_device *netdev);
79 #endif
81 static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
82 static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
83 static void netxen_create_diag_entries(struct netxen_adapter *adapter);
84 static void netxen_remove_diag_entries(struct netxen_adapter *adapter);
86 static int nx_dev_request_aer(struct netxen_adapter *adapter);
87 static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
88 static int netxen_can_start_firmware(struct netxen_adapter *adapter);
90 static irqreturn_t netxen_intr(int irq, void *data);
91 static irqreturn_t netxen_msi_intr(int irq, void *data);
92 static irqreturn_t netxen_msix_intr(int irq, void *data);
94 static void netxen_free_vlan_ip_list(struct netxen_adapter *);
95 static void netxen_restore_indev_addr(struct net_device *dev, unsigned long);
96 static struct rtnl_link_stats64 *netxen_nic_get_stats(struct net_device *dev,
97 struct rtnl_link_stats64 *stats);
98 static int netxen_nic_set_mac(struct net_device *netdev, void *p);
100 /* PCI Device ID Table */
101 #define ENTRY(device) \
102 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
103 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
105 static DEFINE_PCI_DEVICE_TABLE(netxen_pci_tbl) = {
106 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
107 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
108 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
109 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
110 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
111 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
112 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
113 ENTRY(PCI_DEVICE_ID_NX3031),
114 {0,}
117 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
119 static uint32_t crb_cmd_producer[4] = {
120 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
121 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
124 void
125 netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
126 struct nx_host_tx_ring *tx_ring)
128 NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
131 static uint32_t crb_cmd_consumer[4] = {
132 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
133 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
136 static inline void
137 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
138 struct nx_host_tx_ring *tx_ring)
140 NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
143 static uint32_t msi_tgt_status[8] = {
144 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
145 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
146 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
147 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
150 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
152 static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
154 struct netxen_adapter *adapter = sds_ring->adapter;
156 NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
159 static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
161 struct netxen_adapter *adapter = sds_ring->adapter;
163 NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
165 if (!NETXEN_IS_MSI_FAMILY(adapter))
166 NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
169 static int
170 netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
172 int size = sizeof(struct nx_host_sds_ring) * count;
174 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
176 return recv_ctx->sds_rings == NULL;
179 static void
180 netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
182 if (recv_ctx->sds_rings != NULL)
183 kfree(recv_ctx->sds_rings);
185 recv_ctx->sds_rings = NULL;
188 static int
189 netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
191 int ring;
192 struct nx_host_sds_ring *sds_ring;
193 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
195 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
196 return -ENOMEM;
198 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
199 sds_ring = &recv_ctx->sds_rings[ring];
200 netif_napi_add(netdev, &sds_ring->napi,
201 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
204 return 0;
207 static void
208 netxen_napi_del(struct netxen_adapter *adapter)
210 int ring;
211 struct nx_host_sds_ring *sds_ring;
212 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
214 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
215 sds_ring = &recv_ctx->sds_rings[ring];
216 netif_napi_del(&sds_ring->napi);
219 netxen_free_sds_rings(&adapter->recv_ctx);
222 static void
223 netxen_napi_enable(struct netxen_adapter *adapter)
225 int ring;
226 struct nx_host_sds_ring *sds_ring;
227 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
229 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
230 sds_ring = &recv_ctx->sds_rings[ring];
231 napi_enable(&sds_ring->napi);
232 netxen_nic_enable_int(sds_ring);
236 static void
237 netxen_napi_disable(struct netxen_adapter *adapter)
239 int ring;
240 struct nx_host_sds_ring *sds_ring;
241 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
243 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
244 sds_ring = &recv_ctx->sds_rings[ring];
245 netxen_nic_disable_int(sds_ring);
246 napi_synchronize(&sds_ring->napi);
247 napi_disable(&sds_ring->napi);
251 static int nx_set_dma_mask(struct netxen_adapter *adapter)
253 struct pci_dev *pdev = adapter->pdev;
254 uint64_t mask, cmask;
256 adapter->pci_using_dac = 0;
258 mask = DMA_BIT_MASK(32);
259 cmask = DMA_BIT_MASK(32);
261 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
262 #ifndef CONFIG_IA64
263 mask = DMA_BIT_MASK(35);
264 #endif
265 } else {
266 mask = DMA_BIT_MASK(39);
267 cmask = mask;
270 if (pci_set_dma_mask(pdev, mask) == 0 &&
271 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
272 adapter->pci_using_dac = 1;
273 return 0;
276 return -EIO;
279 /* Update addressable range if firmware supports it */
280 static int
281 nx_update_dma_mask(struct netxen_adapter *adapter)
283 int change, shift, err;
284 uint64_t mask, old_mask, old_cmask;
285 struct pci_dev *pdev = adapter->pdev;
287 change = 0;
289 shift = NXRD32(adapter, CRB_DMA_SHIFT);
290 if (shift > 32)
291 return 0;
293 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
294 change = 1;
295 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
296 change = 1;
298 if (change) {
299 old_mask = pdev->dma_mask;
300 old_cmask = pdev->dev.coherent_dma_mask;
302 mask = DMA_BIT_MASK(32+shift);
304 err = pci_set_dma_mask(pdev, mask);
305 if (err)
306 goto err_out;
308 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
310 err = pci_set_consistent_dma_mask(pdev, mask);
311 if (err)
312 goto err_out;
314 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
317 return 0;
319 err_out:
320 pci_set_dma_mask(pdev, old_mask);
321 pci_set_consistent_dma_mask(pdev, old_cmask);
322 return err;
325 static int
326 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
328 u32 val, timeout;
330 if (first_boot == 0x55555555) {
331 /* This is the first boot after power up */
332 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
334 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
335 return 0;
337 /* PCI bus master workaround */
338 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
339 if (!(first_boot & 0x4)) {
340 first_boot |= 0x4;
341 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
342 NXRD32(adapter, NETXEN_PCIE_REG(0x4));
345 /* This is the first boot after power up */
346 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
347 if (first_boot != 0x80000f) {
348 /* clear the register for future unloads/loads */
349 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
350 return -EIO;
353 /* Start P2 boot loader */
354 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
355 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
356 timeout = 0;
357 do {
358 msleep(1);
359 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
361 if (++timeout > 5000)
362 return -EIO;
364 } while (val == NETXEN_BDINFO_MAGIC);
366 return 0;
369 static void netxen_set_port_mode(struct netxen_adapter *adapter)
371 u32 val, data;
373 val = adapter->ahw.board_type;
374 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
375 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
376 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
377 data = NETXEN_PORT_MODE_802_3_AP;
378 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
379 } else if (port_mode == NETXEN_PORT_MODE_XG) {
380 data = NETXEN_PORT_MODE_XG;
381 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
382 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
383 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
384 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
385 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
386 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
387 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
388 } else {
389 data = NETXEN_PORT_MODE_AUTO_NEG;
390 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
393 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
394 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
395 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
396 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
397 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
399 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
403 #define PCI_CAP_ID_GEN 0x10
405 static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
407 u32 pdevfuncsave;
408 u32 c8c9value = 0;
409 u32 chicken = 0;
410 u32 control = 0;
411 int i, pos;
412 struct pci_dev *pdev;
414 pdev = adapter->pdev;
416 chicken = NXRD32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3));
417 /* clear chicken3.25:24 */
418 chicken &= 0xFCFFFFFF;
420 * if gen1 and B0, set F1020 - if gen 2, do nothing
421 * if gen2 set to F1000
423 pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
424 if (pos == 0xC0) {
425 pci_read_config_dword(pdev, pos + 0x10, &control);
426 if ((control & 0x000F0000) != 0x00020000) {
427 /* set chicken3.24 if gen1 */
428 chicken |= 0x01000000;
430 dev_info(&adapter->pdev->dev, "Gen2 strapping detected\n");
431 c8c9value = 0xF1000;
432 } else {
433 /* set chicken3.24 if gen1 */
434 chicken |= 0x01000000;
435 dev_info(&adapter->pdev->dev, "Gen1 strapping detected\n");
436 if (adapter->ahw.revision_id == NX_P3_B0)
437 c8c9value = 0xF1020;
438 else
439 c8c9value = 0;
442 NXWR32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3), chicken);
444 if (!c8c9value)
445 return;
447 pdevfuncsave = pdev->devfn;
448 if (pdevfuncsave & 0x07)
449 return;
451 for (i = 0; i < 8; i++) {
452 pci_read_config_dword(pdev, pos + 8, &control);
453 pci_read_config_dword(pdev, pos + 8, &control);
454 pci_write_config_dword(pdev, pos + 8, c8c9value);
455 pdev->devfn++;
457 pdev->devfn = pdevfuncsave;
460 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
462 u32 control;
463 int pos;
465 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
466 if (pos) {
467 pci_read_config_dword(pdev, pos, &control);
468 if (enable)
469 control |= PCI_MSIX_FLAGS_ENABLE;
470 else
471 control = 0;
472 pci_write_config_dword(pdev, pos, control);
476 static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
478 int i;
480 for (i = 0; i < count; i++)
481 adapter->msix_entries[i].entry = i;
484 static int
485 netxen_read_mac_addr(struct netxen_adapter *adapter)
487 int i;
488 unsigned char *p;
489 u64 mac_addr;
490 struct net_device *netdev = adapter->netdev;
491 struct pci_dev *pdev = adapter->pdev;
493 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
494 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
495 return -EIO;
496 } else {
497 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
498 return -EIO;
501 p = (unsigned char *)&mac_addr;
502 for (i = 0; i < 6; i++)
503 netdev->dev_addr[i] = *(p + 5 - i);
505 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
506 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
508 /* set station address */
510 if (!is_valid_ether_addr(netdev->perm_addr))
511 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
513 return 0;
516 static int netxen_nic_set_mac(struct net_device *netdev, void *p)
518 struct netxen_adapter *adapter = netdev_priv(netdev);
519 struct sockaddr *addr = p;
521 if (!is_valid_ether_addr(addr->sa_data))
522 return -EINVAL;
524 if (netif_running(netdev)) {
525 netif_device_detach(netdev);
526 netxen_napi_disable(adapter);
529 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
530 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
531 adapter->macaddr_set(adapter, addr->sa_data);
533 if (netif_running(netdev)) {
534 netif_device_attach(netdev);
535 netxen_napi_enable(adapter);
537 return 0;
540 static void netxen_set_multicast_list(struct net_device *dev)
542 struct netxen_adapter *adapter = netdev_priv(dev);
544 adapter->set_multi(dev);
547 static netdev_features_t netxen_fix_features(struct net_device *dev,
548 netdev_features_t features)
550 if (!(features & NETIF_F_RXCSUM)) {
551 netdev_info(dev, "disabling LRO as RXCSUM is off\n");
553 features &= ~NETIF_F_LRO;
556 return features;
559 static int netxen_set_features(struct net_device *dev,
560 netdev_features_t features)
562 struct netxen_adapter *adapter = netdev_priv(dev);
563 int hw_lro;
565 if (!((dev->features ^ features) & NETIF_F_LRO))
566 return 0;
568 hw_lro = (features & NETIF_F_LRO) ? NETXEN_NIC_LRO_ENABLED
569 : NETXEN_NIC_LRO_DISABLED;
571 if (netxen_config_hw_lro(adapter, hw_lro))
572 return -EIO;
574 if (!(features & NETIF_F_LRO) && netxen_send_lro_cleanup(adapter))
575 return -EIO;
577 return 0;
580 static const struct net_device_ops netxen_netdev_ops = {
581 .ndo_open = netxen_nic_open,
582 .ndo_stop = netxen_nic_close,
583 .ndo_start_xmit = netxen_nic_xmit_frame,
584 .ndo_get_stats64 = netxen_nic_get_stats,
585 .ndo_validate_addr = eth_validate_addr,
586 .ndo_set_rx_mode = netxen_set_multicast_list,
587 .ndo_set_mac_address = netxen_nic_set_mac,
588 .ndo_change_mtu = netxen_nic_change_mtu,
589 .ndo_tx_timeout = netxen_tx_timeout,
590 .ndo_fix_features = netxen_fix_features,
591 .ndo_set_features = netxen_set_features,
592 #ifdef CONFIG_NET_POLL_CONTROLLER
593 .ndo_poll_controller = netxen_nic_poll_controller,
594 #endif
597 static void
598 netxen_setup_intr(struct netxen_adapter *adapter)
600 struct netxen_legacy_intr_set *legacy_intrp;
601 struct pci_dev *pdev = adapter->pdev;
602 int err, num_msix;
604 if (adapter->rss_supported) {
605 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
606 MSIX_ENTRIES_PER_ADAPTER : 2;
607 } else
608 num_msix = 1;
610 adapter->max_sds_rings = 1;
612 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
614 if (adapter->ahw.revision_id >= NX_P3_B0)
615 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
616 else
617 legacy_intrp = &legacy_intr[0];
619 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
620 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
621 legacy_intrp->tgt_status_reg);
622 adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
623 legacy_intrp->tgt_mask_reg);
624 adapter->pci_int_reg = netxen_get_ioaddr(adapter,
625 legacy_intrp->pci_int_reg);
626 adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
628 if (adapter->ahw.revision_id >= NX_P3_B1)
629 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
630 ISR_INT_STATE_REG);
631 else
632 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
633 CRB_INT_VECTOR);
635 netxen_set_msix_bit(pdev, 0);
637 if (adapter->msix_supported) {
639 netxen_init_msix_entries(adapter, num_msix);
640 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
641 if (err == 0) {
642 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
643 netxen_set_msix_bit(pdev, 1);
645 if (adapter->rss_supported)
646 adapter->max_sds_rings = num_msix;
648 dev_info(&pdev->dev, "using msi-x interrupts\n");
649 return;
652 if (err > 0)
653 pci_disable_msix(pdev);
655 /* fall through for msi */
658 if (use_msi && !pci_enable_msi(pdev)) {
659 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
660 adapter->tgt_status_reg = netxen_get_ioaddr(adapter,
661 msi_tgt_status[adapter->ahw.pci_func]);
662 dev_info(&pdev->dev, "using msi interrupts\n");
663 adapter->msix_entries[0].vector = pdev->irq;
664 return;
667 dev_info(&pdev->dev, "using legacy interrupts\n");
668 adapter->msix_entries[0].vector = pdev->irq;
671 static void
672 netxen_teardown_intr(struct netxen_adapter *adapter)
674 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
675 pci_disable_msix(adapter->pdev);
676 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
677 pci_disable_msi(adapter->pdev);
680 static void
681 netxen_cleanup_pci_map(struct netxen_adapter *adapter)
683 if (adapter->ahw.db_base != NULL)
684 iounmap(adapter->ahw.db_base);
685 if (adapter->ahw.pci_base0 != NULL)
686 iounmap(adapter->ahw.pci_base0);
687 if (adapter->ahw.pci_base1 != NULL)
688 iounmap(adapter->ahw.pci_base1);
689 if (adapter->ahw.pci_base2 != NULL)
690 iounmap(adapter->ahw.pci_base2);
693 static int
694 netxen_setup_pci_map(struct netxen_adapter *adapter)
696 void __iomem *db_ptr = NULL;
698 resource_size_t mem_base, db_base;
699 unsigned long mem_len, db_len = 0;
701 struct pci_dev *pdev = adapter->pdev;
702 int pci_func = adapter->ahw.pci_func;
703 struct netxen_hardware_context *ahw = &adapter->ahw;
705 int err = 0;
708 * Set the CRB window to invalid. If any register in window 0 is
709 * accessed it should set the window to 0 and then reset it to 1.
711 adapter->ahw.crb_win = -1;
712 adapter->ahw.ocm_win = -1;
714 /* remap phys address */
715 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
716 mem_len = pci_resource_len(pdev, 0);
718 /* 128 Meg of memory */
719 if (mem_len == NETXEN_PCI_128MB_SIZE) {
721 ahw->pci_base0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
722 ahw->pci_base1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
723 SECOND_PAGE_GROUP_SIZE);
724 ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
725 THIRD_PAGE_GROUP_SIZE);
726 if (ahw->pci_base0 == NULL || ahw->pci_base1 == NULL ||
727 ahw->pci_base2 == NULL) {
728 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
729 err = -EIO;
730 goto err_out;
733 ahw->pci_len0 = FIRST_PAGE_GROUP_SIZE;
735 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
737 ahw->pci_base1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
738 ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
739 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
740 if (ahw->pci_base1 == NULL || ahw->pci_base2 == NULL) {
741 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
742 err = -EIO;
743 goto err_out;
746 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
748 ahw->pci_base0 = pci_ioremap_bar(pdev, 0);
749 if (ahw->pci_base0 == NULL) {
750 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
751 return -EIO;
753 ahw->pci_len0 = mem_len;
754 } else {
755 return -EIO;
758 netxen_setup_hwops(adapter);
760 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
762 if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
763 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
764 NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
766 } else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
767 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
768 NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func)));
771 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
772 goto skip_doorbell;
774 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
775 db_len = pci_resource_len(pdev, 4);
777 if (db_len == 0) {
778 printk(KERN_ERR "%s: doorbell is disabled\n",
779 netxen_nic_driver_name);
780 err = -EIO;
781 goto err_out;
784 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
785 if (!db_ptr) {
786 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
787 netxen_nic_driver_name);
788 err = -EIO;
789 goto err_out;
792 skip_doorbell:
793 adapter->ahw.db_base = db_ptr;
794 adapter->ahw.db_len = db_len;
795 return 0;
797 err_out:
798 netxen_cleanup_pci_map(adapter);
799 return err;
802 static void
803 netxen_check_options(struct netxen_adapter *adapter)
805 u32 fw_major, fw_minor, fw_build;
806 char brd_name[NETXEN_MAX_SHORT_NAME];
807 char serial_num[32];
808 int i, offset, val;
809 int *ptr32;
810 struct pci_dev *pdev = adapter->pdev;
812 adapter->driver_mismatch = 0;
814 ptr32 = (int *)&serial_num;
815 offset = NX_FW_SERIAL_NUM_OFFSET;
816 for (i = 0; i < 8; i++) {
817 if (netxen_rom_fast_read(adapter, offset, &val) == -1) {
818 dev_err(&pdev->dev, "error reading board info\n");
819 adapter->driver_mismatch = 1;
820 return;
822 ptr32[i] = cpu_to_le32(val);
823 offset += sizeof(u32);
826 fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
827 fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
828 fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
830 adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
832 if (adapter->portnum == 0) {
833 get_brd_name_by_type(adapter->ahw.board_type, brd_name);
835 pr_info("%s: %s Board S/N %s Chip rev 0x%x\n",
836 module_name(THIS_MODULE),
837 brd_name, serial_num, adapter->ahw.revision_id);
840 if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
841 adapter->driver_mismatch = 1;
842 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
843 fw_major, fw_minor, fw_build);
844 return;
847 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
848 i = NXRD32(adapter, NETXEN_SRE_MISC);
849 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
852 dev_info(&pdev->dev, "firmware v%d.%d.%d [%s]\n",
853 fw_major, fw_minor, fw_build,
854 adapter->ahw.cut_through ? "cut-through" : "legacy");
856 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
857 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
859 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
860 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
861 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
862 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
863 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
864 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
867 adapter->msix_supported = 0;
868 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
869 adapter->msix_supported = !!use_msi_x;
870 adapter->rss_supported = !!use_msi_x;
871 } else {
872 u32 flashed_ver = 0;
873 netxen_rom_fast_read(adapter,
874 NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
875 flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
877 if (flashed_ver >= NETXEN_VERSION_CODE(3, 4, 336)) {
878 switch (adapter->ahw.board_type) {
879 case NETXEN_BRDTYPE_P2_SB31_10G:
880 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
881 adapter->msix_supported = !!use_msi_x;
882 adapter->rss_supported = !!use_msi_x;
883 break;
884 default:
885 break;
890 adapter->num_txd = MAX_CMD_DESCRIPTORS;
892 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
893 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
894 adapter->max_rds_rings = 3;
895 } else {
896 adapter->num_lro_rxd = 0;
897 adapter->max_rds_rings = 2;
901 static int
902 netxen_start_firmware(struct netxen_adapter *adapter)
904 int val, err, first_boot;
905 struct pci_dev *pdev = adapter->pdev;
907 /* required for NX2031 dummy dma */
908 err = nx_set_dma_mask(adapter);
909 if (err)
910 return err;
912 if (!netxen_can_start_firmware(adapter))
913 goto wait_init;
915 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
917 err = netxen_check_hw_init(adapter, first_boot);
918 if (err) {
919 dev_err(&pdev->dev, "error in init HW init sequence\n");
920 return err;
923 netxen_request_firmware(adapter);
925 err = netxen_need_fw_reset(adapter);
926 if (err < 0)
927 goto err_out;
928 if (err == 0)
929 goto pcie_strap_init;
931 if (first_boot != 0x55555555) {
932 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
933 netxen_pinit_from_rom(adapter);
934 msleep(1);
937 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
938 NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
939 NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
941 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
942 netxen_set_port_mode(adapter);
944 err = netxen_load_firmware(adapter);
945 if (err)
946 goto err_out;
948 netxen_release_firmware(adapter);
950 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
952 /* Initialize multicast addr pool owners */
953 val = 0x7654;
954 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
955 val |= 0x0f000000;
956 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
960 err = netxen_init_dummy_dma(adapter);
961 if (err)
962 goto err_out;
965 * Tell the hardware our version number.
967 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
968 | ((_NETXEN_NIC_LINUX_MINOR << 8))
969 | (_NETXEN_NIC_LINUX_SUBVERSION);
970 NXWR32(adapter, CRB_DRIVER_VERSION, val);
972 pcie_strap_init:
973 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
974 netxen_pcie_strap_init(adapter);
976 wait_init:
977 /* Handshake with the card before we register the devices. */
978 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
979 if (err) {
980 netxen_free_dummy_dma(adapter);
981 goto err_out;
984 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
986 nx_update_dma_mask(adapter);
988 netxen_check_options(adapter);
990 adapter->need_fw_reset = 0;
992 /* fall through and release firmware */
994 err_out:
995 netxen_release_firmware(adapter);
996 return err;
999 static int
1000 netxen_nic_request_irq(struct netxen_adapter *adapter)
1002 irq_handler_t handler;
1003 struct nx_host_sds_ring *sds_ring;
1004 int err, ring;
1006 unsigned long flags = 0;
1007 struct net_device *netdev = adapter->netdev;
1008 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1010 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1011 handler = netxen_msix_intr;
1012 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
1013 handler = netxen_msi_intr;
1014 else {
1015 flags |= IRQF_SHARED;
1016 handler = netxen_intr;
1018 adapter->irq = netdev->irq;
1020 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1021 sds_ring = &recv_ctx->sds_rings[ring];
1022 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1023 err = request_irq(sds_ring->irq, handler,
1024 flags, sds_ring->name, sds_ring);
1025 if (err)
1026 return err;
1029 return 0;
1032 static void
1033 netxen_nic_free_irq(struct netxen_adapter *adapter)
1035 int ring;
1036 struct nx_host_sds_ring *sds_ring;
1038 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1040 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1041 sds_ring = &recv_ctx->sds_rings[ring];
1042 free_irq(sds_ring->irq, sds_ring);
1046 static void
1047 netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
1049 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
1050 adapter->coal.normal.data.rx_time_us =
1051 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
1052 adapter->coal.normal.data.rx_packets =
1053 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
1054 adapter->coal.normal.data.tx_time_us =
1055 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
1056 adapter->coal.normal.data.tx_packets =
1057 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
1060 /* with rtnl_lock */
1061 static int
1062 __netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1064 int err;
1066 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1067 return -EIO;
1069 err = adapter->init_port(adapter, adapter->physical_port);
1070 if (err) {
1071 printk(KERN_ERR "%s: Failed to initialize port %d\n",
1072 netxen_nic_driver_name, adapter->portnum);
1073 return err;
1075 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
1076 adapter->macaddr_set(adapter, adapter->mac_addr);
1078 adapter->set_multi(netdev);
1079 adapter->set_mtu(adapter, netdev->mtu);
1081 adapter->ahw.linkup = 0;
1083 if (adapter->max_sds_rings > 1)
1084 netxen_config_rss(adapter, 1);
1086 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1087 netxen_config_intr_coalesce(adapter);
1089 if (netdev->features & NETIF_F_LRO)
1090 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
1092 netxen_napi_enable(adapter);
1094 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1095 netxen_linkevent_request(adapter, 1);
1096 else
1097 netxen_nic_set_link_parameters(adapter);
1099 set_bit(__NX_DEV_UP, &adapter->state);
1100 return 0;
1103 /* Usage: During resume and firmware recovery module.*/
1105 static inline int
1106 netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1108 int err = 0;
1110 rtnl_lock();
1111 if (netif_running(netdev))
1112 err = __netxen_nic_up(adapter, netdev);
1113 rtnl_unlock();
1115 return err;
1118 /* with rtnl_lock */
1119 static void
1120 __netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1122 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1123 return;
1125 if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state))
1126 return;
1128 smp_mb();
1129 spin_lock(&adapter->tx_clean_lock);
1130 netif_carrier_off(netdev);
1131 netif_tx_disable(netdev);
1133 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1134 netxen_linkevent_request(adapter, 0);
1136 if (adapter->stop_port)
1137 adapter->stop_port(adapter);
1139 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1140 netxen_p3_free_mac_list(adapter);
1142 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1144 netxen_napi_disable(adapter);
1146 netxen_release_tx_buffers(adapter);
1147 spin_unlock(&adapter->tx_clean_lock);
1150 /* Usage: During suspend and firmware recovery module */
1152 static inline void
1153 netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1155 rtnl_lock();
1156 if (netif_running(netdev))
1157 __netxen_nic_down(adapter, netdev);
1158 rtnl_unlock();
1162 static int
1163 netxen_nic_attach(struct netxen_adapter *adapter)
1165 struct net_device *netdev = adapter->netdev;
1166 struct pci_dev *pdev = adapter->pdev;
1167 int err, ring;
1168 struct nx_host_rds_ring *rds_ring;
1169 struct nx_host_tx_ring *tx_ring;
1171 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1172 return 0;
1174 err = netxen_init_firmware(adapter);
1175 if (err)
1176 return err;
1178 err = netxen_napi_add(adapter, netdev);
1179 if (err)
1180 return err;
1182 err = netxen_alloc_sw_resources(adapter);
1183 if (err) {
1184 printk(KERN_ERR "%s: Error in setting sw resources\n",
1185 netdev->name);
1186 return err;
1189 err = netxen_alloc_hw_resources(adapter);
1190 if (err) {
1191 printk(KERN_ERR "%s: Error in setting hw resources\n",
1192 netdev->name);
1193 goto err_out_free_sw;
1196 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1197 tx_ring = adapter->tx_ring;
1198 tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1199 crb_cmd_producer[adapter->portnum]);
1200 tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1201 crb_cmd_consumer[adapter->portnum]);
1203 tx_ring->producer = 0;
1204 tx_ring->sw_consumer = 0;
1206 netxen_nic_update_cmd_producer(adapter, tx_ring);
1207 netxen_nic_update_cmd_consumer(adapter, tx_ring);
1210 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1211 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1212 netxen_post_rx_buffers(adapter, ring, rds_ring);
1215 err = netxen_nic_request_irq(adapter);
1216 if (err) {
1217 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1218 netdev->name);
1219 goto err_out_free_rxbuf;
1222 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1223 netxen_nic_init_coalesce_defaults(adapter);
1225 netxen_create_sysfs_entries(adapter);
1227 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1228 return 0;
1230 err_out_free_rxbuf:
1231 netxen_release_rx_buffers(adapter);
1232 netxen_free_hw_resources(adapter);
1233 err_out_free_sw:
1234 netxen_free_sw_resources(adapter);
1235 return err;
1238 static void
1239 netxen_nic_detach(struct netxen_adapter *adapter)
1241 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1242 return;
1244 netxen_remove_sysfs_entries(adapter);
1246 netxen_free_hw_resources(adapter);
1247 netxen_release_rx_buffers(adapter);
1248 netxen_nic_free_irq(adapter);
1249 netxen_napi_del(adapter);
1250 netxen_free_sw_resources(adapter);
1252 adapter->is_up = 0;
1256 netxen_nic_reset_context(struct netxen_adapter *adapter)
1258 int err = 0;
1259 struct net_device *netdev = adapter->netdev;
1261 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1262 return -EBUSY;
1264 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1266 netif_device_detach(netdev);
1268 if (netif_running(netdev))
1269 __netxen_nic_down(adapter, netdev);
1271 netxen_nic_detach(adapter);
1273 if (netif_running(netdev)) {
1274 err = netxen_nic_attach(adapter);
1275 if (!err)
1276 err = __netxen_nic_up(adapter, netdev);
1278 if (err)
1279 goto done;
1282 netif_device_attach(netdev);
1285 done:
1286 clear_bit(__NX_RESETTING, &adapter->state);
1287 return err;
1290 static int
1291 netxen_setup_netdev(struct netxen_adapter *adapter,
1292 struct net_device *netdev)
1294 int err = 0;
1295 struct pci_dev *pdev = adapter->pdev;
1297 adapter->mc_enabled = 0;
1298 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1299 adapter->max_mc_count = 38;
1300 else
1301 adapter->max_mc_count = 16;
1303 netdev->netdev_ops = &netxen_netdev_ops;
1304 netdev->watchdog_timeo = 5*HZ;
1306 netxen_nic_change_mtu(netdev, netdev->mtu);
1308 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
1310 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
1311 NETIF_F_RXCSUM;
1313 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1314 netdev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
1316 netdev->vlan_features |= netdev->hw_features;
1318 if (adapter->pci_using_dac) {
1319 netdev->features |= NETIF_F_HIGHDMA;
1320 netdev->vlan_features |= NETIF_F_HIGHDMA;
1323 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1324 netdev->hw_features |= NETIF_F_HW_VLAN_TX;
1326 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1327 netdev->hw_features |= NETIF_F_LRO;
1329 netdev->features |= netdev->hw_features;
1331 netdev->irq = adapter->msix_entries[0].vector;
1333 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1335 if (netxen_read_mac_addr(adapter))
1336 dev_warn(&pdev->dev, "failed to read mac addr\n");
1338 netif_carrier_off(netdev);
1340 err = register_netdev(netdev);
1341 if (err) {
1342 dev_err(&pdev->dev, "failed to register net device\n");
1343 return err;
1346 return 0;
1349 #ifdef CONFIG_PCIEAER
1350 static void netxen_mask_aer_correctable(struct netxen_adapter *adapter)
1352 struct pci_dev *pdev = adapter->pdev;
1353 struct pci_dev *root = pdev->bus->self;
1354 u32 aer_pos;
1356 if (adapter->ahw.board_type != NETXEN_BRDTYPE_P3_4_GB_MM &&
1357 adapter->ahw.board_type != NETXEN_BRDTYPE_P3_10G_TP)
1358 return;
1360 if (root->pcie_type != PCI_EXP_TYPE_ROOT_PORT)
1361 return;
1363 aer_pos = pci_find_ext_capability(root, PCI_EXT_CAP_ID_ERR);
1364 if (!aer_pos)
1365 return;
1367 pci_write_config_dword(root, aer_pos + PCI_ERR_COR_MASK, 0xffff);
1369 #endif
1371 static int __devinit
1372 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1374 struct net_device *netdev = NULL;
1375 struct netxen_adapter *adapter = NULL;
1376 int i = 0, err;
1377 int pci_func_id = PCI_FUNC(pdev->devfn);
1378 uint8_t revision_id;
1379 u32 val;
1381 if (pdev->revision >= NX_P3_A0 && pdev->revision <= NX_P3_B1) {
1382 pr_warning("%s: chip revisions between 0x%x-0x%x "
1383 "will not be enabled.\n",
1384 module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
1385 return -ENODEV;
1388 if ((err = pci_enable_device(pdev)))
1389 return err;
1391 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1392 err = -ENODEV;
1393 goto err_out_disable_pdev;
1396 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1397 goto err_out_disable_pdev;
1399 if (NX_IS_REVISION_P3(pdev->revision))
1400 pci_enable_pcie_error_reporting(pdev);
1402 pci_set_master(pdev);
1404 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1405 if(!netdev) {
1406 dev_err(&pdev->dev, "failed to allocate net_device\n");
1407 err = -ENOMEM;
1408 goto err_out_free_res;
1411 SET_NETDEV_DEV(netdev, &pdev->dev);
1413 adapter = netdev_priv(netdev);
1414 adapter->netdev = netdev;
1415 adapter->pdev = pdev;
1416 adapter->ahw.pci_func = pci_func_id;
1418 revision_id = pdev->revision;
1419 adapter->ahw.revision_id = revision_id;
1421 rwlock_init(&adapter->ahw.crb_lock);
1422 spin_lock_init(&adapter->ahw.mem_lock);
1424 spin_lock_init(&adapter->tx_clean_lock);
1425 INIT_LIST_HEAD(&adapter->mac_list);
1426 INIT_LIST_HEAD(&adapter->vlan_ip_list);
1428 err = netxen_setup_pci_map(adapter);
1429 if (err)
1430 goto err_out_free_netdev;
1432 /* This will be reset for mezz cards */
1433 adapter->portnum = pci_func_id;
1435 err = netxen_nic_get_board_info(adapter);
1436 if (err) {
1437 dev_err(&pdev->dev, "Error getting board config info.\n");
1438 goto err_out_iounmap;
1441 #ifdef CONFIG_PCIEAER
1442 netxen_mask_aer_correctable(adapter);
1443 #endif
1445 /* Mezz cards have PCI function 0,2,3 enabled */
1446 switch (adapter->ahw.board_type) {
1447 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1448 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1449 if (pci_func_id >= 2)
1450 adapter->portnum = pci_func_id - 2;
1451 break;
1452 default:
1453 break;
1456 err = netxen_check_flash_fw_compatibility(adapter);
1457 if (err)
1458 goto err_out_iounmap;
1460 if (adapter->portnum == 0) {
1461 val = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
1462 if (val != 0xffffffff && val != 0) {
1463 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, 0);
1464 adapter->need_fw_reset = 1;
1468 err = netxen_start_firmware(adapter);
1469 if (err)
1470 goto err_out_decr_ref;
1473 * See if the firmware gave us a virtual-physical port mapping.
1475 adapter->physical_port = adapter->portnum;
1476 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1477 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
1478 if (i != 0x55555555)
1479 adapter->physical_port = i;
1482 netxen_nic_clear_stats(adapter);
1484 netxen_setup_intr(adapter);
1486 err = netxen_setup_netdev(adapter, netdev);
1487 if (err)
1488 goto err_out_disable_msi;
1490 pci_set_drvdata(pdev, adapter);
1492 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1494 switch (adapter->ahw.port_type) {
1495 case NETXEN_NIC_GBE:
1496 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1497 adapter->netdev->name);
1498 break;
1499 case NETXEN_NIC_XGBE:
1500 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1501 adapter->netdev->name);
1502 break;
1505 netxen_create_diag_entries(adapter);
1507 return 0;
1509 err_out_disable_msi:
1510 netxen_teardown_intr(adapter);
1512 netxen_free_dummy_dma(adapter);
1514 err_out_decr_ref:
1515 nx_decr_dev_ref_cnt(adapter);
1517 err_out_iounmap:
1518 netxen_cleanup_pci_map(adapter);
1520 err_out_free_netdev:
1521 free_netdev(netdev);
1523 err_out_free_res:
1524 pci_release_regions(pdev);
1526 err_out_disable_pdev:
1527 pci_set_drvdata(pdev, NULL);
1528 pci_disable_device(pdev);
1529 return err;
1532 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1534 struct netxen_adapter *adapter;
1535 struct net_device *netdev;
1537 adapter = pci_get_drvdata(pdev);
1538 if (adapter == NULL)
1539 return;
1541 netdev = adapter->netdev;
1543 netxen_cancel_fw_work(adapter);
1545 unregister_netdev(netdev);
1547 cancel_work_sync(&adapter->tx_timeout_task);
1549 netxen_free_vlan_ip_list(adapter);
1550 netxen_nic_detach(adapter);
1552 nx_decr_dev_ref_cnt(adapter);
1554 if (adapter->portnum == 0)
1555 netxen_free_dummy_dma(adapter);
1557 clear_bit(__NX_RESETTING, &adapter->state);
1559 netxen_teardown_intr(adapter);
1561 netxen_remove_diag_entries(adapter);
1563 netxen_cleanup_pci_map(adapter);
1565 netxen_release_firmware(adapter);
1567 if (NX_IS_REVISION_P3(pdev->revision))
1568 pci_disable_pcie_error_reporting(pdev);
1570 pci_release_regions(pdev);
1571 pci_disable_device(pdev);
1572 pci_set_drvdata(pdev, NULL);
1574 free_netdev(netdev);
1577 static void netxen_nic_detach_func(struct netxen_adapter *adapter)
1579 struct net_device *netdev = adapter->netdev;
1581 netif_device_detach(netdev);
1583 netxen_cancel_fw_work(adapter);
1585 if (netif_running(netdev))
1586 netxen_nic_down(adapter, netdev);
1588 cancel_work_sync(&adapter->tx_timeout_task);
1590 netxen_nic_detach(adapter);
1592 if (adapter->portnum == 0)
1593 netxen_free_dummy_dma(adapter);
1595 nx_decr_dev_ref_cnt(adapter);
1597 clear_bit(__NX_RESETTING, &adapter->state);
1600 static int netxen_nic_attach_func(struct pci_dev *pdev)
1602 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1603 struct net_device *netdev = adapter->netdev;
1604 int err;
1606 err = pci_enable_device(pdev);
1607 if (err)
1608 return err;
1610 pci_set_power_state(pdev, PCI_D0);
1611 pci_set_master(pdev);
1612 pci_restore_state(pdev);
1614 adapter->ahw.crb_win = -1;
1615 adapter->ahw.ocm_win = -1;
1617 err = netxen_start_firmware(adapter);
1618 if (err) {
1619 dev_err(&pdev->dev, "failed to start firmware\n");
1620 return err;
1623 if (netif_running(netdev)) {
1624 err = netxen_nic_attach(adapter);
1625 if (err)
1626 goto err_out;
1628 err = netxen_nic_up(adapter, netdev);
1629 if (err)
1630 goto err_out_detach;
1632 netxen_restore_indev_addr(netdev, NETDEV_UP);
1635 netif_device_attach(netdev);
1636 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1637 return 0;
1639 err_out_detach:
1640 netxen_nic_detach(adapter);
1641 err_out:
1642 nx_decr_dev_ref_cnt(adapter);
1643 return err;
1646 static pci_ers_result_t netxen_io_error_detected(struct pci_dev *pdev,
1647 pci_channel_state_t state)
1649 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1651 if (state == pci_channel_io_perm_failure)
1652 return PCI_ERS_RESULT_DISCONNECT;
1654 if (nx_dev_request_aer(adapter))
1655 return PCI_ERS_RESULT_RECOVERED;
1657 netxen_nic_detach_func(adapter);
1659 pci_disable_device(pdev);
1661 return PCI_ERS_RESULT_NEED_RESET;
1664 static pci_ers_result_t netxen_io_slot_reset(struct pci_dev *pdev)
1666 int err = 0;
1668 err = netxen_nic_attach_func(pdev);
1670 return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
1673 static void netxen_io_resume(struct pci_dev *pdev)
1675 pci_cleanup_aer_uncorrect_error_status(pdev);
1678 static void netxen_nic_shutdown(struct pci_dev *pdev)
1680 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1682 netxen_nic_detach_func(adapter);
1684 if (pci_save_state(pdev))
1685 return;
1687 if (netxen_nic_wol_supported(adapter)) {
1688 pci_enable_wake(pdev, PCI_D3cold, 1);
1689 pci_enable_wake(pdev, PCI_D3hot, 1);
1692 pci_disable_device(pdev);
1695 #ifdef CONFIG_PM
1696 static int
1697 netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1699 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1700 int retval;
1702 netxen_nic_detach_func(adapter);
1704 retval = pci_save_state(pdev);
1705 if (retval)
1706 return retval;
1708 if (netxen_nic_wol_supported(adapter)) {
1709 pci_enable_wake(pdev, PCI_D3cold, 1);
1710 pci_enable_wake(pdev, PCI_D3hot, 1);
1713 pci_disable_device(pdev);
1714 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1716 return 0;
1719 static int
1720 netxen_nic_resume(struct pci_dev *pdev)
1722 return netxen_nic_attach_func(pdev);
1724 #endif
1726 static int netxen_nic_open(struct net_device *netdev)
1728 struct netxen_adapter *adapter = netdev_priv(netdev);
1729 int err = 0;
1731 if (adapter->driver_mismatch)
1732 return -EIO;
1734 err = netxen_nic_attach(adapter);
1735 if (err)
1736 return err;
1738 err = __netxen_nic_up(adapter, netdev);
1739 if (err)
1740 goto err_out;
1742 netif_start_queue(netdev);
1744 return 0;
1746 err_out:
1747 netxen_nic_detach(adapter);
1748 return err;
1752 * netxen_nic_close - Disables a network interface entry point
1754 static int netxen_nic_close(struct net_device *netdev)
1756 struct netxen_adapter *adapter = netdev_priv(netdev);
1758 __netxen_nic_down(adapter, netdev);
1759 return 0;
1762 static void
1763 netxen_tso_check(struct net_device *netdev,
1764 struct nx_host_tx_ring *tx_ring,
1765 struct cmd_desc_type0 *first_desc,
1766 struct sk_buff *skb)
1768 u8 opcode = TX_ETHER_PKT;
1769 __be16 protocol = skb->protocol;
1770 u16 flags = 0, vid = 0;
1771 u32 producer;
1772 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1773 struct cmd_desc_type0 *hwdesc;
1774 struct vlan_ethhdr *vh;
1776 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1778 vh = (struct vlan_ethhdr *)skb->data;
1779 protocol = vh->h_vlan_encapsulated_proto;
1780 flags = FLAGS_VLAN_TAGGED;
1782 } else if (vlan_tx_tag_present(skb)) {
1784 flags = FLAGS_VLAN_OOB;
1785 vid = vlan_tx_tag_get(skb);
1786 netxen_set_tx_vlan_tci(first_desc, vid);
1787 vlan_oob = 1;
1790 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1791 skb_shinfo(skb)->gso_size > 0) {
1793 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1795 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1796 first_desc->total_hdr_length = hdr_len;
1797 if (vlan_oob) {
1798 first_desc->total_hdr_length += VLAN_HLEN;
1799 first_desc->tcp_hdr_offset = VLAN_HLEN;
1800 first_desc->ip_hdr_offset = VLAN_HLEN;
1801 /* Only in case of TSO on vlan device */
1802 flags |= FLAGS_VLAN_TAGGED;
1805 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1806 TX_TCP_LSO6 : TX_TCP_LSO;
1807 tso = 1;
1809 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1810 u8 l4proto;
1812 if (protocol == cpu_to_be16(ETH_P_IP)) {
1813 l4proto = ip_hdr(skb)->protocol;
1815 if (l4proto == IPPROTO_TCP)
1816 opcode = TX_TCP_PKT;
1817 else if(l4proto == IPPROTO_UDP)
1818 opcode = TX_UDP_PKT;
1819 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1820 l4proto = ipv6_hdr(skb)->nexthdr;
1822 if (l4proto == IPPROTO_TCP)
1823 opcode = TX_TCPV6_PKT;
1824 else if(l4proto == IPPROTO_UDP)
1825 opcode = TX_UDPV6_PKT;
1829 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1830 first_desc->ip_hdr_offset += skb_network_offset(skb);
1831 netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1833 if (!tso)
1834 return;
1836 /* For LSO, we need to copy the MAC/IP/TCP headers into
1837 * the descriptor ring
1839 producer = tx_ring->producer;
1840 copied = 0;
1841 offset = 2;
1843 if (vlan_oob) {
1844 /* Create a TSO vlan header template for firmware */
1846 hwdesc = &tx_ring->desc_head[producer];
1847 tx_ring->cmd_buf_arr[producer].skb = NULL;
1849 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1850 hdr_len + VLAN_HLEN);
1852 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1853 skb_copy_from_linear_data(skb, vh, 12);
1854 vh->h_vlan_proto = htons(ETH_P_8021Q);
1855 vh->h_vlan_TCI = htons(vid);
1856 skb_copy_from_linear_data_offset(skb, 12,
1857 (char *)vh + 16, copy_len - 16);
1859 copied = copy_len - VLAN_HLEN;
1860 offset = 0;
1862 producer = get_next_index(producer, tx_ring->num_desc);
1865 while (copied < hdr_len) {
1867 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1868 (hdr_len - copied));
1870 hwdesc = &tx_ring->desc_head[producer];
1871 tx_ring->cmd_buf_arr[producer].skb = NULL;
1873 skb_copy_from_linear_data_offset(skb, copied,
1874 (char *)hwdesc + offset, copy_len);
1876 copied += copy_len;
1877 offset = 0;
1879 producer = get_next_index(producer, tx_ring->num_desc);
1882 tx_ring->producer = producer;
1883 barrier();
1886 static int
1887 netxen_map_tx_skb(struct pci_dev *pdev,
1888 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
1890 struct netxen_skb_frag *nf;
1891 struct skb_frag_struct *frag;
1892 int i, nr_frags;
1893 dma_addr_t map;
1895 nr_frags = skb_shinfo(skb)->nr_frags;
1896 nf = &pbuf->frag_array[0];
1898 map = pci_map_single(pdev, skb->data,
1899 skb_headlen(skb), PCI_DMA_TODEVICE);
1900 if (pci_dma_mapping_error(pdev, map))
1901 goto out_err;
1903 nf->dma = map;
1904 nf->length = skb_headlen(skb);
1906 for (i = 0; i < nr_frags; i++) {
1907 frag = &skb_shinfo(skb)->frags[i];
1908 nf = &pbuf->frag_array[i+1];
1910 map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
1911 DMA_TO_DEVICE);
1912 if (dma_mapping_error(&pdev->dev, map))
1913 goto unwind;
1915 nf->dma = map;
1916 nf->length = skb_frag_size(frag);
1919 return 0;
1921 unwind:
1922 while (--i >= 0) {
1923 nf = &pbuf->frag_array[i+1];
1924 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1927 nf = &pbuf->frag_array[0];
1928 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1930 out_err:
1931 return -ENOMEM;
1934 static inline void
1935 netxen_clear_cmddesc(u64 *desc)
1937 desc[0] = 0ULL;
1938 desc[2] = 0ULL;
1941 static netdev_tx_t
1942 netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1944 struct netxen_adapter *adapter = netdev_priv(netdev);
1945 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
1946 struct netxen_cmd_buffer *pbuf;
1947 struct netxen_skb_frag *buffrag;
1948 struct cmd_desc_type0 *hwdesc, *first_desc;
1949 struct pci_dev *pdev;
1950 int i, k;
1951 int delta = 0;
1952 struct skb_frag_struct *frag;
1954 u32 producer;
1955 int frag_count, no_of_desc;
1956 u32 num_txd = tx_ring->num_desc;
1958 frag_count = skb_shinfo(skb)->nr_frags + 1;
1960 /* 14 frags supported for normal packet and
1961 * 32 frags supported for TSO packet
1963 if (!skb_is_gso(skb) && frag_count > NETXEN_MAX_FRAGS_PER_TX) {
1965 for (i = 0; i < (frag_count - NETXEN_MAX_FRAGS_PER_TX); i++) {
1966 frag = &skb_shinfo(skb)->frags[i];
1967 delta += skb_frag_size(frag);
1970 if (!__pskb_pull_tail(skb, delta))
1971 goto drop_packet;
1973 frag_count = 1 + skb_shinfo(skb)->nr_frags;
1975 /* 4 fragments per cmd des */
1976 no_of_desc = (frag_count + 3) >> 2;
1978 if (unlikely(netxen_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
1979 netif_stop_queue(netdev);
1980 smp_mb();
1981 if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH)
1982 netif_start_queue(netdev);
1983 else
1984 return NETDEV_TX_BUSY;
1987 producer = tx_ring->producer;
1988 pbuf = &tx_ring->cmd_buf_arr[producer];
1990 pdev = adapter->pdev;
1992 if (netxen_map_tx_skb(pdev, skb, pbuf))
1993 goto drop_packet;
1995 pbuf->skb = skb;
1996 pbuf->frag_count = frag_count;
1998 first_desc = hwdesc = &tx_ring->desc_head[producer];
1999 netxen_clear_cmddesc((u64 *)hwdesc);
2001 netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
2002 netxen_set_tx_port(first_desc, adapter->portnum);
2004 for (i = 0; i < frag_count; i++) {
2006 k = i % 4;
2008 if ((k == 0) && (i > 0)) {
2009 /* move to next desc.*/
2010 producer = get_next_index(producer, num_txd);
2011 hwdesc = &tx_ring->desc_head[producer];
2012 netxen_clear_cmddesc((u64 *)hwdesc);
2013 tx_ring->cmd_buf_arr[producer].skb = NULL;
2016 buffrag = &pbuf->frag_array[i];
2018 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2019 switch (k) {
2020 case 0:
2021 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2022 break;
2023 case 1:
2024 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2025 break;
2026 case 2:
2027 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2028 break;
2029 case 3:
2030 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2031 break;
2035 tx_ring->producer = get_next_index(producer, num_txd);
2037 netxen_tso_check(netdev, tx_ring, first_desc, skb);
2039 adapter->stats.txbytes += skb->len;
2040 adapter->stats.xmitcalled++;
2042 netxen_nic_update_cmd_producer(adapter, tx_ring);
2044 return NETDEV_TX_OK;
2046 drop_packet:
2047 adapter->stats.txdropped++;
2048 dev_kfree_skb_any(skb);
2049 return NETDEV_TX_OK;
2052 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
2054 struct net_device *netdev = adapter->netdev;
2055 uint32_t temp, temp_state, temp_val;
2056 int rv = 0;
2058 temp = NXRD32(adapter, CRB_TEMP_STATE);
2060 temp_state = nx_get_temp_state(temp);
2061 temp_val = nx_get_temp_val(temp);
2063 if (temp_state == NX_TEMP_PANIC) {
2064 printk(KERN_ALERT
2065 "%s: Device temperature %d degrees C exceeds"
2066 " maximum allowed. Hardware has been shut down.\n",
2067 netdev->name, temp_val);
2068 rv = 1;
2069 } else if (temp_state == NX_TEMP_WARN) {
2070 if (adapter->temp == NX_TEMP_NORMAL) {
2071 printk(KERN_ALERT
2072 "%s: Device temperature %d degrees C "
2073 "exceeds operating range."
2074 " Immediate action needed.\n",
2075 netdev->name, temp_val);
2077 } else {
2078 if (adapter->temp == NX_TEMP_WARN) {
2079 printk(KERN_INFO
2080 "%s: Device temperature is now %d degrees C"
2081 " in normal range.\n", netdev->name,
2082 temp_val);
2085 adapter->temp = temp_state;
2086 return rv;
2089 void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
2091 struct net_device *netdev = adapter->netdev;
2093 if (adapter->ahw.linkup && !linkup) {
2094 printk(KERN_INFO "%s: %s NIC Link is down\n",
2095 netxen_nic_driver_name, netdev->name);
2096 adapter->ahw.linkup = 0;
2097 if (netif_running(netdev)) {
2098 netif_carrier_off(netdev);
2099 netif_stop_queue(netdev);
2101 adapter->link_changed = !adapter->has_link_events;
2102 } else if (!adapter->ahw.linkup && linkup) {
2103 printk(KERN_INFO "%s: %s NIC Link is up\n",
2104 netxen_nic_driver_name, netdev->name);
2105 adapter->ahw.linkup = 1;
2106 if (netif_running(netdev)) {
2107 netif_carrier_on(netdev);
2108 netif_wake_queue(netdev);
2110 adapter->link_changed = !adapter->has_link_events;
2114 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
2116 u32 val, port, linkup;
2118 port = adapter->physical_port;
2120 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2121 val = NXRD32(adapter, CRB_XG_STATE_P3);
2122 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
2123 linkup = (val == XG_LINK_UP_P3);
2124 } else {
2125 val = NXRD32(adapter, CRB_XG_STATE);
2126 val = (val >> port*8) & 0xff;
2127 linkup = (val == XG_LINK_UP);
2130 netxen_advert_link_change(adapter, linkup);
2133 static void netxen_tx_timeout(struct net_device *netdev)
2135 struct netxen_adapter *adapter = netdev_priv(netdev);
2137 if (test_bit(__NX_RESETTING, &adapter->state))
2138 return;
2140 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
2141 schedule_work(&adapter->tx_timeout_task);
2144 static void netxen_tx_timeout_task(struct work_struct *work)
2146 struct netxen_adapter *adapter =
2147 container_of(work, struct netxen_adapter, tx_timeout_task);
2149 if (!netif_running(adapter->netdev))
2150 return;
2152 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
2153 return;
2155 if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
2156 goto request_reset;
2158 rtnl_lock();
2159 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
2160 /* try to scrub interrupt */
2161 netxen_napi_disable(adapter);
2163 netxen_napi_enable(adapter);
2165 netif_wake_queue(adapter->netdev);
2167 clear_bit(__NX_RESETTING, &adapter->state);
2168 } else {
2169 clear_bit(__NX_RESETTING, &adapter->state);
2170 if (netxen_nic_reset_context(adapter)) {
2171 rtnl_unlock();
2172 goto request_reset;
2175 adapter->netdev->trans_start = jiffies;
2176 rtnl_unlock();
2177 return;
2179 request_reset:
2180 adapter->need_fw_reset = 1;
2181 clear_bit(__NX_RESETTING, &adapter->state);
2184 static struct rtnl_link_stats64 *netxen_nic_get_stats(struct net_device *netdev,
2185 struct rtnl_link_stats64 *stats)
2187 struct netxen_adapter *adapter = netdev_priv(netdev);
2189 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2190 stats->tx_packets = adapter->stats.xmitfinished;
2191 stats->rx_bytes = adapter->stats.rxbytes;
2192 stats->tx_bytes = adapter->stats.txbytes;
2193 stats->rx_dropped = adapter->stats.rxdropped;
2194 stats->tx_dropped = adapter->stats.txdropped;
2196 return stats;
2199 static irqreturn_t netxen_intr(int irq, void *data)
2201 struct nx_host_sds_ring *sds_ring = data;
2202 struct netxen_adapter *adapter = sds_ring->adapter;
2203 u32 status = 0;
2205 status = readl(adapter->isr_int_vec);
2207 if (!(status & adapter->int_vec_bit))
2208 return IRQ_NONE;
2210 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2211 /* check interrupt state machine, to be sure */
2212 status = readl(adapter->crb_int_state_reg);
2213 if (!ISR_LEGACY_INT_TRIGGERED(status))
2214 return IRQ_NONE;
2216 } else {
2217 unsigned long our_int = 0;
2219 our_int = readl(adapter->crb_int_state_reg);
2221 /* not our interrupt */
2222 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
2223 return IRQ_NONE;
2225 /* claim interrupt */
2226 writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
2228 /* clear interrupt */
2229 netxen_nic_disable_int(sds_ring);
2232 writel(0xffffffff, adapter->tgt_status_reg);
2233 /* read twice to ensure write is flushed */
2234 readl(adapter->isr_int_vec);
2235 readl(adapter->isr_int_vec);
2237 napi_schedule(&sds_ring->napi);
2239 return IRQ_HANDLED;
2242 static irqreturn_t netxen_msi_intr(int irq, void *data)
2244 struct nx_host_sds_ring *sds_ring = data;
2245 struct netxen_adapter *adapter = sds_ring->adapter;
2247 /* clear interrupt */
2248 writel(0xffffffff, adapter->tgt_status_reg);
2250 napi_schedule(&sds_ring->napi);
2251 return IRQ_HANDLED;
2254 static irqreturn_t netxen_msix_intr(int irq, void *data)
2256 struct nx_host_sds_ring *sds_ring = data;
2258 napi_schedule(&sds_ring->napi);
2259 return IRQ_HANDLED;
2262 static int netxen_nic_poll(struct napi_struct *napi, int budget)
2264 struct nx_host_sds_ring *sds_ring =
2265 container_of(napi, struct nx_host_sds_ring, napi);
2267 struct netxen_adapter *adapter = sds_ring->adapter;
2269 int tx_complete;
2270 int work_done;
2272 tx_complete = netxen_process_cmd_ring(adapter);
2274 work_done = netxen_process_rcv_ring(sds_ring, budget);
2276 if ((work_done < budget) && tx_complete) {
2277 napi_complete(&sds_ring->napi);
2278 if (test_bit(__NX_DEV_UP, &adapter->state))
2279 netxen_nic_enable_int(sds_ring);
2282 return work_done;
2285 #ifdef CONFIG_NET_POLL_CONTROLLER
2286 static void netxen_nic_poll_controller(struct net_device *netdev)
2288 int ring;
2289 struct nx_host_sds_ring *sds_ring;
2290 struct netxen_adapter *adapter = netdev_priv(netdev);
2291 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
2293 disable_irq(adapter->irq);
2294 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2295 sds_ring = &recv_ctx->sds_rings[ring];
2296 netxen_intr(adapter->irq, sds_ring);
2298 enable_irq(adapter->irq);
2300 #endif
2302 static int
2303 nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
2305 int count;
2306 if (netxen_api_lock(adapter))
2307 return -EIO;
2309 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2311 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2313 netxen_api_unlock(adapter);
2314 return count;
2317 static int
2318 nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
2320 int count;
2321 if (netxen_api_lock(adapter))
2322 return -EIO;
2324 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2325 WARN_ON(count == 0);
2327 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2329 if (count == 0)
2330 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2332 netxen_api_unlock(adapter);
2333 return count;
2336 static int
2337 nx_dev_request_aer(struct netxen_adapter *adapter)
2339 u32 state;
2340 int ret = -EINVAL;
2342 if (netxen_api_lock(adapter))
2343 return ret;
2345 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2347 if (state == NX_DEV_NEED_AER)
2348 ret = 0;
2349 else if (state == NX_DEV_READY) {
2350 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_AER);
2351 ret = 0;
2354 netxen_api_unlock(adapter);
2355 return ret;
2358 static int
2359 nx_dev_request_reset(struct netxen_adapter *adapter)
2361 u32 state;
2362 int ret = -EINVAL;
2364 if (netxen_api_lock(adapter))
2365 return ret;
2367 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2369 if (state == NX_DEV_NEED_RESET)
2370 ret = 0;
2371 else if (state != NX_DEV_INITALIZING && state != NX_DEV_NEED_AER) {
2372 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
2373 ret = 0;
2376 netxen_api_unlock(adapter);
2378 return ret;
2381 static int
2382 netxen_can_start_firmware(struct netxen_adapter *adapter)
2384 int count;
2385 int can_start = 0;
2387 if (netxen_api_lock(adapter))
2388 return 0;
2390 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2392 if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2393 count = 0;
2395 if (count == 0) {
2396 can_start = 1;
2397 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2400 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2402 netxen_api_unlock(adapter);
2404 return can_start;
2407 static void
2408 netxen_schedule_work(struct netxen_adapter *adapter,
2409 work_func_t func, int delay)
2411 INIT_DELAYED_WORK(&adapter->fw_work, func);
2412 schedule_delayed_work(&adapter->fw_work, delay);
2415 static void
2416 netxen_cancel_fw_work(struct netxen_adapter *adapter)
2418 while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2419 msleep(10);
2421 cancel_delayed_work_sync(&adapter->fw_work);
2424 static void
2425 netxen_attach_work(struct work_struct *work)
2427 struct netxen_adapter *adapter = container_of(work,
2428 struct netxen_adapter, fw_work.work);
2429 struct net_device *netdev = adapter->netdev;
2430 int err = 0;
2432 if (netif_running(netdev)) {
2433 err = netxen_nic_attach(adapter);
2434 if (err)
2435 goto done;
2437 err = netxen_nic_up(adapter, netdev);
2438 if (err) {
2439 netxen_nic_detach(adapter);
2440 goto done;
2443 netxen_restore_indev_addr(netdev, NETDEV_UP);
2446 netif_device_attach(netdev);
2448 done:
2449 adapter->fw_fail_cnt = 0;
2450 clear_bit(__NX_RESETTING, &adapter->state);
2451 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2454 static void
2455 netxen_fwinit_work(struct work_struct *work)
2457 struct netxen_adapter *adapter = container_of(work,
2458 struct netxen_adapter, fw_work.work);
2459 int dev_state;
2461 dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2463 switch (dev_state) {
2464 case NX_DEV_COLD:
2465 case NX_DEV_READY:
2466 if (!netxen_start_firmware(adapter)) {
2467 netxen_schedule_work(adapter, netxen_attach_work, 0);
2468 return;
2470 break;
2472 case NX_DEV_NEED_RESET:
2473 case NX_DEV_INITALIZING:
2474 if (++adapter->fw_wait_cnt < FW_POLL_THRESH) {
2475 netxen_schedule_work(adapter,
2476 netxen_fwinit_work, 2 * FW_POLL_DELAY);
2477 return;
2480 case NX_DEV_FAILED:
2481 default:
2482 nx_incr_dev_ref_cnt(adapter);
2483 break;
2486 clear_bit(__NX_RESETTING, &adapter->state);
2489 static void
2490 netxen_detach_work(struct work_struct *work)
2492 struct netxen_adapter *adapter = container_of(work,
2493 struct netxen_adapter, fw_work.work);
2494 struct net_device *netdev = adapter->netdev;
2495 int ref_cnt, delay;
2496 u32 status;
2498 netif_device_detach(netdev);
2500 netxen_nic_down(adapter, netdev);
2502 rtnl_lock();
2503 netxen_nic_detach(adapter);
2504 rtnl_unlock();
2506 status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2508 if (status & NX_RCODE_FATAL_ERROR)
2509 goto err_ret;
2511 if (adapter->temp == NX_TEMP_PANIC)
2512 goto err_ret;
2514 ref_cnt = nx_decr_dev_ref_cnt(adapter);
2516 if (ref_cnt == -EIO)
2517 goto err_ret;
2519 delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2521 adapter->fw_wait_cnt = 0;
2522 netxen_schedule_work(adapter, netxen_fwinit_work, delay);
2524 return;
2526 err_ret:
2527 clear_bit(__NX_RESETTING, &adapter->state);
2530 static int
2531 netxen_check_health(struct netxen_adapter *adapter)
2533 u32 state, heartbit;
2534 struct net_device *netdev = adapter->netdev;
2536 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2537 if (state == NX_DEV_NEED_AER)
2538 return 0;
2540 if (netxen_nic_check_temp(adapter))
2541 goto detach;
2543 if (adapter->need_fw_reset) {
2544 if (nx_dev_request_reset(adapter))
2545 return 0;
2546 goto detach;
2549 /* NX_DEV_NEED_RESET, this state can be marked in two cases
2550 * 1. Tx timeout 2. Fw hang
2551 * Send request to destroy context in case of tx timeout only
2552 * and doesn't required in case of Fw hang
2554 if (state == NX_DEV_NEED_RESET) {
2555 adapter->need_fw_reset = 1;
2556 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2557 goto detach;
2560 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2561 return 0;
2563 heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2564 if (heartbit != adapter->heartbit) {
2565 adapter->heartbit = heartbit;
2566 adapter->fw_fail_cnt = 0;
2567 if (adapter->need_fw_reset)
2568 goto detach;
2569 return 0;
2572 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2573 return 0;
2575 if (nx_dev_request_reset(adapter))
2576 return 0;
2578 clear_bit(__NX_FW_ATTACHED, &adapter->state);
2580 dev_info(&netdev->dev, "firmware hang detected\n");
2582 detach:
2583 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
2584 !test_and_set_bit(__NX_RESETTING, &adapter->state))
2585 netxen_schedule_work(adapter, netxen_detach_work, 0);
2586 return 1;
2589 static void
2590 netxen_fw_poll_work(struct work_struct *work)
2592 struct netxen_adapter *adapter = container_of(work,
2593 struct netxen_adapter, fw_work.work);
2595 if (test_bit(__NX_RESETTING, &adapter->state))
2596 goto reschedule;
2598 if (test_bit(__NX_DEV_UP, &adapter->state)) {
2599 if (!adapter->has_link_events) {
2601 netxen_nic_handle_phy_intr(adapter);
2603 if (adapter->link_changed)
2604 netxen_nic_set_link_parameters(adapter);
2608 if (netxen_check_health(adapter))
2609 return;
2611 reschedule:
2612 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2615 static ssize_t
2616 netxen_store_bridged_mode(struct device *dev,
2617 struct device_attribute *attr, const char *buf, size_t len)
2619 struct net_device *net = to_net_dev(dev);
2620 struct netxen_adapter *adapter = netdev_priv(net);
2621 unsigned long new;
2622 int ret = -EINVAL;
2624 if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2625 goto err_out;
2627 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2628 goto err_out;
2630 if (strict_strtoul(buf, 2, &new))
2631 goto err_out;
2633 if (!netxen_config_bridged_mode(adapter, !!new))
2634 ret = len;
2636 err_out:
2637 return ret;
2640 static ssize_t
2641 netxen_show_bridged_mode(struct device *dev,
2642 struct device_attribute *attr, char *buf)
2644 struct net_device *net = to_net_dev(dev);
2645 struct netxen_adapter *adapter;
2646 int bridged_mode = 0;
2648 adapter = netdev_priv(net);
2650 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2651 bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2653 return sprintf(buf, "%d\n", bridged_mode);
2656 static struct device_attribute dev_attr_bridged_mode = {
2657 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2658 .show = netxen_show_bridged_mode,
2659 .store = netxen_store_bridged_mode,
2662 static ssize_t
2663 netxen_store_diag_mode(struct device *dev,
2664 struct device_attribute *attr, const char *buf, size_t len)
2666 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2667 unsigned long new;
2669 if (strict_strtoul(buf, 2, &new))
2670 return -EINVAL;
2672 if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2673 adapter->flags ^= NETXEN_NIC_DIAG_ENABLED;
2675 return len;
2678 static ssize_t
2679 netxen_show_diag_mode(struct device *dev,
2680 struct device_attribute *attr, char *buf)
2682 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2684 return sprintf(buf, "%d\n",
2685 !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED));
2688 static struct device_attribute dev_attr_diag_mode = {
2689 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
2690 .show = netxen_show_diag_mode,
2691 .store = netxen_store_diag_mode,
2694 static int
2695 netxen_sysfs_validate_crb(struct netxen_adapter *adapter,
2696 loff_t offset, size_t size)
2698 size_t crb_size = 4;
2700 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2701 return -EIO;
2703 if (offset < NETXEN_PCI_CRBSPACE) {
2704 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2705 return -EINVAL;
2707 if (ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2708 NETXEN_PCI_CAMQM_2M_END))
2709 crb_size = 8;
2710 else
2711 return -EINVAL;
2714 if ((size != crb_size) || (offset & (crb_size-1)))
2715 return -EINVAL;
2717 return 0;
2720 static ssize_t
2721 netxen_sysfs_read_crb(struct file *filp, struct kobject *kobj,
2722 struct bin_attribute *attr,
2723 char *buf, loff_t offset, size_t size)
2725 struct device *dev = container_of(kobj, struct device, kobj);
2726 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2727 u32 data;
2728 u64 qmdata;
2729 int ret;
2731 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2732 if (ret != 0)
2733 return ret;
2735 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
2736 ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2737 NETXEN_PCI_CAMQM_2M_END)) {
2738 netxen_pci_camqm_read_2M(adapter, offset, &qmdata);
2739 memcpy(buf, &qmdata, size);
2740 } else {
2741 data = NXRD32(adapter, offset);
2742 memcpy(buf, &data, size);
2745 return size;
2748 static ssize_t
2749 netxen_sysfs_write_crb(struct file *filp, struct kobject *kobj,
2750 struct bin_attribute *attr,
2751 char *buf, loff_t offset, size_t size)
2753 struct device *dev = container_of(kobj, struct device, kobj);
2754 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2755 u32 data;
2756 u64 qmdata;
2757 int ret;
2759 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2760 if (ret != 0)
2761 return ret;
2763 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
2764 ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2765 NETXEN_PCI_CAMQM_2M_END)) {
2766 memcpy(&qmdata, buf, size);
2767 netxen_pci_camqm_write_2M(adapter, offset, qmdata);
2768 } else {
2769 memcpy(&data, buf, size);
2770 NXWR32(adapter, offset, data);
2773 return size;
2776 static int
2777 netxen_sysfs_validate_mem(struct netxen_adapter *adapter,
2778 loff_t offset, size_t size)
2780 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2781 return -EIO;
2783 if ((size != 8) || (offset & 0x7))
2784 return -EIO;
2786 return 0;
2789 static ssize_t
2790 netxen_sysfs_read_mem(struct file *filp, struct kobject *kobj,
2791 struct bin_attribute *attr,
2792 char *buf, loff_t offset, size_t size)
2794 struct device *dev = container_of(kobj, struct device, kobj);
2795 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2796 u64 data;
2797 int ret;
2799 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2800 if (ret != 0)
2801 return ret;
2803 if (adapter->pci_mem_read(adapter, offset, &data))
2804 return -EIO;
2806 memcpy(buf, &data, size);
2808 return size;
2811 static ssize_t netxen_sysfs_write_mem(struct file *filp, struct kobject *kobj,
2812 struct bin_attribute *attr, char *buf,
2813 loff_t offset, size_t size)
2815 struct device *dev = container_of(kobj, struct device, kobj);
2816 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2817 u64 data;
2818 int ret;
2820 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2821 if (ret != 0)
2822 return ret;
2824 memcpy(&data, buf, size);
2826 if (adapter->pci_mem_write(adapter, offset, data))
2827 return -EIO;
2829 return size;
2833 static struct bin_attribute bin_attr_crb = {
2834 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
2835 .size = 0,
2836 .read = netxen_sysfs_read_crb,
2837 .write = netxen_sysfs_write_crb,
2840 static struct bin_attribute bin_attr_mem = {
2841 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
2842 .size = 0,
2843 .read = netxen_sysfs_read_mem,
2844 .write = netxen_sysfs_write_mem,
2848 static void
2849 netxen_create_sysfs_entries(struct netxen_adapter *adapter)
2851 struct net_device *netdev = adapter->netdev;
2852 struct device *dev = &netdev->dev;
2854 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
2855 /* bridged_mode control */
2856 if (device_create_file(dev, &dev_attr_bridged_mode)) {
2857 dev_warn(&netdev->dev,
2858 "failed to create bridged_mode sysfs entry\n");
2863 static void
2864 netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
2866 struct net_device *netdev = adapter->netdev;
2867 struct device *dev = &netdev->dev;
2869 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2870 device_remove_file(dev, &dev_attr_bridged_mode);
2873 static void
2874 netxen_create_diag_entries(struct netxen_adapter *adapter)
2876 struct pci_dev *pdev = adapter->pdev;
2877 struct device *dev;
2879 dev = &pdev->dev;
2880 if (device_create_file(dev, &dev_attr_diag_mode))
2881 dev_info(dev, "failed to create diag_mode sysfs entry\n");
2882 if (device_create_bin_file(dev, &bin_attr_crb))
2883 dev_info(dev, "failed to create crb sysfs entry\n");
2884 if (device_create_bin_file(dev, &bin_attr_mem))
2885 dev_info(dev, "failed to create mem sysfs entry\n");
2889 static void
2890 netxen_remove_diag_entries(struct netxen_adapter *adapter)
2892 struct pci_dev *pdev = adapter->pdev;
2893 struct device *dev = &pdev->dev;
2895 device_remove_file(dev, &dev_attr_diag_mode);
2896 device_remove_bin_file(dev, &bin_attr_crb);
2897 device_remove_bin_file(dev, &bin_attr_mem);
2900 #ifdef CONFIG_INET
2902 #define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
2904 static int
2905 netxen_destip_supported(struct netxen_adapter *adapter)
2907 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2908 return 0;
2910 if (adapter->ahw.cut_through)
2911 return 0;
2913 return 1;
2916 static void
2917 netxen_free_vlan_ip_list(struct netxen_adapter *adapter)
2919 struct nx_vlan_ip_list *cur;
2920 struct list_head *head = &adapter->vlan_ip_list;
2922 while (!list_empty(head)) {
2923 cur = list_entry(head->next, struct nx_vlan_ip_list, list);
2924 netxen_config_ipaddr(adapter, cur->ip_addr, NX_IP_DOWN);
2925 list_del(&cur->list);
2926 kfree(cur);
2930 static void
2931 netxen_list_config_vlan_ip(struct netxen_adapter *adapter,
2932 struct in_ifaddr *ifa, unsigned long event)
2934 struct net_device *dev;
2935 struct nx_vlan_ip_list *cur, *tmp_cur;
2936 struct list_head *head;
2938 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2940 if (dev == NULL)
2941 return;
2943 if (!is_vlan_dev(dev))
2944 return;
2946 switch (event) {
2947 case NX_IP_UP:
2948 list_for_each(head, &adapter->vlan_ip_list) {
2949 cur = list_entry(head, struct nx_vlan_ip_list, list);
2951 if (cur->ip_addr == ifa->ifa_address)
2952 return;
2955 cur = kzalloc(sizeof(struct nx_vlan_ip_list), GFP_ATOMIC);
2956 if (cur == NULL) {
2957 printk(KERN_ERR "%s: failed to add vlan ip to list\n",
2958 adapter->netdev->name);
2959 return;
2962 cur->ip_addr = ifa->ifa_address;
2963 list_add_tail(&cur->list, &adapter->vlan_ip_list);
2964 break;
2965 case NX_IP_DOWN:
2966 list_for_each_entry_safe(cur, tmp_cur,
2967 &adapter->vlan_ip_list, list) {
2968 if (cur->ip_addr == ifa->ifa_address) {
2969 list_del(&cur->list);
2970 kfree(cur);
2971 break;
2976 static void
2977 netxen_config_indev_addr(struct netxen_adapter *adapter,
2978 struct net_device *dev, unsigned long event)
2980 struct in_device *indev;
2982 if (!netxen_destip_supported(adapter))
2983 return;
2985 indev = in_dev_get(dev);
2986 if (!indev)
2987 return;
2989 for_ifa(indev) {
2990 switch (event) {
2991 case NETDEV_UP:
2992 netxen_config_ipaddr(adapter,
2993 ifa->ifa_address, NX_IP_UP);
2994 netxen_list_config_vlan_ip(adapter, ifa, NX_IP_UP);
2995 break;
2996 case NETDEV_DOWN:
2997 netxen_config_ipaddr(adapter,
2998 ifa->ifa_address, NX_IP_DOWN);
2999 netxen_list_config_vlan_ip(adapter, ifa, NX_IP_DOWN);
3000 break;
3001 default:
3002 break;
3004 } endfor_ifa(indev);
3006 in_dev_put(indev);
3009 static void
3010 netxen_restore_indev_addr(struct net_device *netdev, unsigned long event)
3013 struct netxen_adapter *adapter = netdev_priv(netdev);
3014 struct nx_vlan_ip_list *pos, *tmp_pos;
3015 unsigned long ip_event;
3017 ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
3018 netxen_config_indev_addr(adapter, netdev, event);
3020 list_for_each_entry_safe(pos, tmp_pos, &adapter->vlan_ip_list, list) {
3021 netxen_config_ipaddr(adapter, pos->ip_addr, ip_event);
3025 static int netxen_netdev_event(struct notifier_block *this,
3026 unsigned long event, void *ptr)
3028 struct netxen_adapter *adapter;
3029 struct net_device *dev = (struct net_device *)ptr;
3030 struct net_device *orig_dev = dev;
3032 recheck:
3033 if (dev == NULL)
3034 goto done;
3036 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3037 dev = vlan_dev_real_dev(dev);
3038 goto recheck;
3041 if (!is_netxen_netdev(dev))
3042 goto done;
3044 adapter = netdev_priv(dev);
3046 if (!adapter)
3047 goto done;
3049 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
3050 goto done;
3052 netxen_config_indev_addr(adapter, orig_dev, event);
3053 done:
3054 return NOTIFY_DONE;
3057 static int
3058 netxen_inetaddr_event(struct notifier_block *this,
3059 unsigned long event, void *ptr)
3061 struct netxen_adapter *adapter;
3062 struct net_device *dev;
3064 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
3066 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
3068 recheck:
3069 if (dev == NULL)
3070 goto done;
3072 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3073 dev = vlan_dev_real_dev(dev);
3074 goto recheck;
3077 if (!is_netxen_netdev(dev))
3078 goto done;
3080 adapter = netdev_priv(dev);
3082 if (!adapter || !netxen_destip_supported(adapter))
3083 goto done;
3085 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
3086 goto done;
3088 switch (event) {
3089 case NETDEV_UP:
3090 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
3091 netxen_list_config_vlan_ip(adapter, ifa, NX_IP_UP);
3092 break;
3093 case NETDEV_DOWN:
3094 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_DOWN);
3095 netxen_list_config_vlan_ip(adapter, ifa, NX_IP_DOWN);
3096 break;
3097 default:
3098 break;
3101 done:
3102 return NOTIFY_DONE;
3105 static struct notifier_block netxen_netdev_cb = {
3106 .notifier_call = netxen_netdev_event,
3109 static struct notifier_block netxen_inetaddr_cb = {
3110 .notifier_call = netxen_inetaddr_event,
3112 #else
3113 static void
3114 netxen_restore_indev_addr(struct net_device *dev, unsigned long event)
3116 static void
3117 netxen_free_vlan_ip_list(struct netxen_adapter *adapter)
3119 #endif
3121 static struct pci_error_handlers netxen_err_handler = {
3122 .error_detected = netxen_io_error_detected,
3123 .slot_reset = netxen_io_slot_reset,
3124 .resume = netxen_io_resume,
3127 static struct pci_driver netxen_driver = {
3128 .name = netxen_nic_driver_name,
3129 .id_table = netxen_pci_tbl,
3130 .probe = netxen_nic_probe,
3131 .remove = __devexit_p(netxen_nic_remove),
3132 #ifdef CONFIG_PM
3133 .suspend = netxen_nic_suspend,
3134 .resume = netxen_nic_resume,
3135 #endif
3136 .shutdown = netxen_nic_shutdown,
3137 .err_handler = &netxen_err_handler
3140 static int __init netxen_init_module(void)
3142 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
3144 #ifdef CONFIG_INET
3145 register_netdevice_notifier(&netxen_netdev_cb);
3146 register_inetaddr_notifier(&netxen_inetaddr_cb);
3147 #endif
3148 return pci_register_driver(&netxen_driver);
3151 module_init(netxen_init_module);
3153 static void __exit netxen_exit_module(void)
3155 pci_unregister_driver(&netxen_driver);
3157 #ifdef CONFIG_INET
3158 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
3159 unregister_netdevice_notifier(&netxen_netdev_cb);
3160 #endif
3163 module_exit(netxen_exit_module);