netxen: implement pci driver shutdown
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / netxen / netxen_nic_main.c
blob794c19b2254f3978881b60e77022e5a3b423a3b9
1 /*
2 * Copyright (C) 2003 - 2009 NetXen, Inc.
3 * All rights reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
23 * Contact Information:
24 * info@netxen.com
25 * NetXen Inc,
26 * 18922 Forge Drive
27 * Cupertino, CA 95014-0701
31 #include <linux/vmalloc.h>
32 #include <linux/interrupt.h>
33 #include "netxen_nic_hw.h"
35 #include "netxen_nic.h"
36 #include "netxen_nic_phan_reg.h"
38 #include <linux/dma-mapping.h>
39 #include <linux/if_vlan.h>
40 #include <net/ip.h>
41 #include <linux/ipv6.h>
42 #include <linux/inetdevice.h>
44 MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
45 MODULE_LICENSE("GPL");
46 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
48 char netxen_nic_driver_name[] = "netxen_nic";
49 static char netxen_nic_driver_string[] = "NetXen Network Driver version "
50 NETXEN_NIC_LINUX_VERSIONID;
52 static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
54 /* Default to restricted 1G auto-neg mode */
55 static int wol_port_mode = 5;
57 static int use_msi = 1;
59 static int use_msi_x = 1;
61 /* Local functions to NetXen NIC driver */
62 static int __devinit netxen_nic_probe(struct pci_dev *pdev,
63 const struct pci_device_id *ent);
64 static void __devexit netxen_nic_remove(struct pci_dev *pdev);
65 static int netxen_nic_open(struct net_device *netdev);
66 static int netxen_nic_close(struct net_device *netdev);
67 static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
68 static void netxen_tx_timeout(struct net_device *netdev);
69 static void netxen_reset_task(struct work_struct *work);
70 static void netxen_watchdog(unsigned long);
71 static int netxen_nic_poll(struct napi_struct *napi, int budget);
72 #ifdef CONFIG_NET_POLL_CONTROLLER
73 static void netxen_nic_poll_controller(struct net_device *netdev);
74 #endif
75 static irqreturn_t netxen_intr(int irq, void *data);
76 static irqreturn_t netxen_msi_intr(int irq, void *data);
77 static irqreturn_t netxen_msix_intr(int irq, void *data);
79 /* PCI Device ID Table */
80 #define ENTRY(device) \
81 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
82 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
84 static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
85 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
86 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
87 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
88 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
89 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
90 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
91 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
92 ENTRY(PCI_DEVICE_ID_NX3031),
93 {0,}
96 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
98 static void netxen_watchdog(unsigned long);
100 static uint32_t crb_cmd_producer[4] = {
101 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
102 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
105 void
106 netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
107 struct nx_host_tx_ring *tx_ring)
109 NXWR32(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
111 if (netxen_tx_avail(tx_ring) <= TX_STOP_THRESH) {
112 netif_stop_queue(adapter->netdev);
113 smp_mb();
117 static uint32_t crb_cmd_consumer[4] = {
118 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
119 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
122 static inline void
123 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
124 struct nx_host_tx_ring *tx_ring)
126 NXWR32(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
129 static uint32_t msi_tgt_status[8] = {
130 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
131 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
132 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
133 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
136 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
138 static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
140 struct netxen_adapter *adapter = sds_ring->adapter;
142 NXWR32(adapter, sds_ring->crb_intr_mask, 0);
145 static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
147 struct netxen_adapter *adapter = sds_ring->adapter;
149 NXWR32(adapter, sds_ring->crb_intr_mask, 0x1);
151 if (!NETXEN_IS_MSI_FAMILY(adapter))
152 adapter->pci_write_immediate(adapter,
153 adapter->legacy_intr.tgt_mask_reg, 0xfbff);
156 static int
157 netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
159 int size = sizeof(struct nx_host_sds_ring) * count;
161 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
163 return (recv_ctx->sds_rings == NULL);
166 static void
167 netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
169 if (recv_ctx->sds_rings != NULL)
170 kfree(recv_ctx->sds_rings);
172 recv_ctx->sds_rings = NULL;
175 static int
176 netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
178 int ring;
179 struct nx_host_sds_ring *sds_ring;
180 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
182 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
183 return -ENOMEM;
185 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
186 sds_ring = &recv_ctx->sds_rings[ring];
187 netif_napi_add(netdev, &sds_ring->napi,
188 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
191 return 0;
194 static void
195 netxen_napi_del(struct netxen_adapter *adapter)
197 int ring;
198 struct nx_host_sds_ring *sds_ring;
199 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
201 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
202 sds_ring = &recv_ctx->sds_rings[ring];
203 netif_napi_del(&sds_ring->napi);
206 netxen_free_sds_rings(&adapter->recv_ctx);
209 static void
210 netxen_napi_enable(struct netxen_adapter *adapter)
212 int ring;
213 struct nx_host_sds_ring *sds_ring;
214 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
216 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
217 sds_ring = &recv_ctx->sds_rings[ring];
218 napi_enable(&sds_ring->napi);
219 netxen_nic_enable_int(sds_ring);
223 static void
224 netxen_napi_disable(struct netxen_adapter *adapter)
226 int ring;
227 struct nx_host_sds_ring *sds_ring;
228 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
230 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
231 sds_ring = &recv_ctx->sds_rings[ring];
232 netxen_nic_disable_int(sds_ring);
233 napi_synchronize(&sds_ring->napi);
234 napi_disable(&sds_ring->napi);
238 static int nx_set_dma_mask(struct netxen_adapter *adapter)
240 struct pci_dev *pdev = adapter->pdev;
241 uint64_t mask, cmask;
243 adapter->pci_using_dac = 0;
245 mask = DMA_BIT_MASK(32);
246 cmask = DMA_BIT_MASK(32);
248 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
249 #ifndef CONFIG_IA64
250 mask = DMA_BIT_MASK(35);
251 #endif
252 } else {
253 mask = DMA_BIT_MASK(39);
254 cmask = mask;
257 if (pci_set_dma_mask(pdev, mask) == 0 &&
258 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
259 adapter->pci_using_dac = 1;
260 return 0;
263 return -EIO;
266 /* Update addressable range if firmware supports it */
267 static int
268 nx_update_dma_mask(struct netxen_adapter *adapter)
270 int change, shift, err;
271 uint64_t mask, old_mask, old_cmask;
272 struct pci_dev *pdev = adapter->pdev;
274 change = 0;
276 shift = NXRD32(adapter, CRB_DMA_SHIFT);
277 if (shift > 32)
278 return 0;
280 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
281 change = 1;
282 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
283 change = 1;
285 if (change) {
286 old_mask = pdev->dma_mask;
287 old_cmask = pdev->dev.coherent_dma_mask;
289 mask = DMA_BIT_MASK(32+shift);
291 err = pci_set_dma_mask(pdev, mask);
292 if (err)
293 goto err_out;
295 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
297 err = pci_set_consistent_dma_mask(pdev, mask);
298 if (err)
299 goto err_out;
301 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
304 return 0;
306 err_out:
307 pci_set_dma_mask(pdev, old_mask);
308 pci_set_consistent_dma_mask(pdev, old_cmask);
309 return err;
312 static void
313 netxen_check_options(struct netxen_adapter *adapter)
315 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
316 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
317 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
318 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
319 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
320 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
323 adapter->msix_supported = 0;
324 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
325 adapter->msix_supported = !!use_msi_x;
326 adapter->rss_supported = !!use_msi_x;
327 } else if (adapter->fw_version >= NETXEN_VERSION_CODE(3, 4, 336)) {
328 switch (adapter->ahw.board_type) {
329 case NETXEN_BRDTYPE_P2_SB31_10G:
330 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
331 adapter->msix_supported = !!use_msi_x;
332 adapter->rss_supported = !!use_msi_x;
333 break;
334 default:
335 break;
339 adapter->num_txd = MAX_CMD_DESCRIPTORS;
341 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
342 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
343 adapter->max_rds_rings = 3;
344 } else {
345 adapter->num_lro_rxd = 0;
346 adapter->max_rds_rings = 2;
350 static int
351 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
353 u32 val, timeout;
355 if (first_boot == 0x55555555) {
356 /* This is the first boot after power up */
357 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
359 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
360 return 0;
362 /* PCI bus master workaround */
363 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
364 if (!(first_boot & 0x4)) {
365 first_boot |= 0x4;
366 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
367 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
370 /* This is the first boot after power up */
371 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
372 if (first_boot != 0x80000f) {
373 /* clear the register for future unloads/loads */
374 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
375 return -EIO;
378 /* Start P2 boot loader */
379 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
380 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
381 timeout = 0;
382 do {
383 msleep(1);
384 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
386 if (++timeout > 5000)
387 return -EIO;
389 } while (val == NETXEN_BDINFO_MAGIC);
391 return 0;
394 static void netxen_set_port_mode(struct netxen_adapter *adapter)
396 u32 val, data;
398 val = adapter->ahw.board_type;
399 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
400 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
401 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
402 data = NETXEN_PORT_MODE_802_3_AP;
403 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
404 } else if (port_mode == NETXEN_PORT_MODE_XG) {
405 data = NETXEN_PORT_MODE_XG;
406 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
407 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
408 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
409 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
410 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
411 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
412 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
413 } else {
414 data = NETXEN_PORT_MODE_AUTO_NEG;
415 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
418 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
419 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
420 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
421 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
422 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
424 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
428 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
430 u32 control;
431 int pos;
433 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
434 if (pos) {
435 pci_read_config_dword(pdev, pos, &control);
436 if (enable)
437 control |= PCI_MSIX_FLAGS_ENABLE;
438 else
439 control = 0;
440 pci_write_config_dword(pdev, pos, control);
444 static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
446 int i;
448 for (i = 0; i < count; i++)
449 adapter->msix_entries[i].entry = i;
452 static int
453 netxen_read_mac_addr(struct netxen_adapter *adapter)
455 int i;
456 unsigned char *p;
457 __le64 mac_addr;
458 struct net_device *netdev = adapter->netdev;
459 struct pci_dev *pdev = adapter->pdev;
461 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
462 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
463 return -EIO;
464 } else {
465 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
466 return -EIO;
469 p = (unsigned char *)&mac_addr;
470 for (i = 0; i < 6; i++)
471 netdev->dev_addr[i] = *(p + 5 - i);
473 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
475 /* set station address */
477 if (!is_valid_ether_addr(netdev->perm_addr))
478 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
480 return 0;
483 int netxen_nic_set_mac(struct net_device *netdev, void *p)
485 struct netxen_adapter *adapter = netdev_priv(netdev);
486 struct sockaddr *addr = p;
488 if (!is_valid_ether_addr(addr->sa_data))
489 return -EINVAL;
491 if (netif_running(netdev)) {
492 netif_device_detach(netdev);
493 netxen_napi_disable(adapter);
496 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
497 adapter->macaddr_set(adapter, addr->sa_data);
499 if (netif_running(netdev)) {
500 netif_device_attach(netdev);
501 netxen_napi_enable(adapter);
503 return 0;
506 static void netxen_set_multicast_list(struct net_device *dev)
508 struct netxen_adapter *adapter = netdev_priv(dev);
510 adapter->set_multi(dev);
513 static const struct net_device_ops netxen_netdev_ops = {
514 .ndo_open = netxen_nic_open,
515 .ndo_stop = netxen_nic_close,
516 .ndo_start_xmit = netxen_nic_xmit_frame,
517 .ndo_get_stats = netxen_nic_get_stats,
518 .ndo_validate_addr = eth_validate_addr,
519 .ndo_set_multicast_list = netxen_set_multicast_list,
520 .ndo_set_mac_address = netxen_nic_set_mac,
521 .ndo_change_mtu = netxen_nic_change_mtu,
522 .ndo_tx_timeout = netxen_tx_timeout,
523 #ifdef CONFIG_NET_POLL_CONTROLLER
524 .ndo_poll_controller = netxen_nic_poll_controller,
525 #endif
528 static void
529 netxen_setup_intr(struct netxen_adapter *adapter)
531 struct netxen_legacy_intr_set *legacy_intrp;
532 struct pci_dev *pdev = adapter->pdev;
533 int err, num_msix;
535 if (adapter->rss_supported) {
536 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
537 MSIX_ENTRIES_PER_ADAPTER : 2;
538 } else
539 num_msix = 1;
541 adapter->max_sds_rings = 1;
543 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
545 if (adapter->ahw.revision_id >= NX_P3_B0)
546 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
547 else
548 legacy_intrp = &legacy_intr[0];
549 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
550 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
551 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
552 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
554 netxen_set_msix_bit(pdev, 0);
556 if (adapter->msix_supported) {
558 netxen_init_msix_entries(adapter, num_msix);
559 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
560 if (err == 0) {
561 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
562 netxen_set_msix_bit(pdev, 1);
564 if (adapter->rss_supported)
565 adapter->max_sds_rings = num_msix;
567 dev_info(&pdev->dev, "using msi-x interrupts\n");
568 return;
571 if (err > 0)
572 pci_disable_msix(pdev);
574 /* fall through for msi */
577 if (use_msi && !pci_enable_msi(pdev)) {
578 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
579 adapter->msi_tgt_status =
580 msi_tgt_status[adapter->ahw.pci_func];
581 dev_info(&pdev->dev, "using msi interrupts\n");
582 adapter->msix_entries[0].vector = pdev->irq;
583 return;
586 dev_info(&pdev->dev, "using legacy interrupts\n");
587 adapter->msix_entries[0].vector = pdev->irq;
590 static void
591 netxen_teardown_intr(struct netxen_adapter *adapter)
593 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
594 pci_disable_msix(adapter->pdev);
595 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
596 pci_disable_msi(adapter->pdev);
599 static void
600 netxen_cleanup_pci_map(struct netxen_adapter *adapter)
602 if (adapter->ahw.db_base != NULL)
603 iounmap(adapter->ahw.db_base);
604 if (adapter->ahw.pci_base0 != NULL)
605 iounmap(adapter->ahw.pci_base0);
606 if (adapter->ahw.pci_base1 != NULL)
607 iounmap(adapter->ahw.pci_base1);
608 if (adapter->ahw.pci_base2 != NULL)
609 iounmap(adapter->ahw.pci_base2);
612 static int
613 netxen_setup_pci_map(struct netxen_adapter *adapter)
615 void __iomem *mem_ptr0 = NULL;
616 void __iomem *mem_ptr1 = NULL;
617 void __iomem *mem_ptr2 = NULL;
618 void __iomem *db_ptr = NULL;
620 unsigned long mem_base, mem_len, db_base, db_len = 0, pci_len0 = 0;
622 struct pci_dev *pdev = adapter->pdev;
623 int pci_func = adapter->ahw.pci_func;
625 int err = 0;
628 * Set the CRB window to invalid. If any register in window 0 is
629 * accessed it should set the window to 0 and then reset it to 1.
631 adapter->curr_window = 255;
632 adapter->ahw.qdr_sn_window = -1;
633 adapter->ahw.ddr_mn_window = -1;
635 /* remap phys address */
636 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
637 mem_len = pci_resource_len(pdev, 0);
638 pci_len0 = 0;
640 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
641 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
642 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
643 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
644 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
645 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
646 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
648 /* 128 Meg of memory */
649 if (mem_len == NETXEN_PCI_128MB_SIZE) {
650 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
651 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
652 SECOND_PAGE_GROUP_SIZE);
653 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
654 THIRD_PAGE_GROUP_SIZE);
655 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
656 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
657 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
658 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
659 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
660 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
661 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
662 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
663 adapter->pci_write_immediate =
664 netxen_nic_pci_write_immediate_2M;
665 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
666 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
667 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
669 mem_ptr0 = pci_ioremap_bar(pdev, 0);
670 if (mem_ptr0 == NULL) {
671 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
672 return -EIO;
674 pci_len0 = mem_len;
676 adapter->ahw.ddr_mn_window = 0;
677 adapter->ahw.qdr_sn_window = 0;
679 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
680 (pci_func * 0x20);
681 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
682 if (pci_func < 4)
683 adapter->ahw.ms_win_crb += (pci_func * 0x20);
684 else
685 adapter->ahw.ms_win_crb +=
686 0xA0 + ((pci_func - 4) * 0x10);
687 } else {
688 return -EIO;
691 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
693 adapter->ahw.pci_base0 = mem_ptr0;
694 adapter->ahw.pci_len0 = pci_len0;
695 adapter->ahw.pci_base1 = mem_ptr1;
696 adapter->ahw.pci_base2 = mem_ptr2;
698 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
699 goto skip_doorbell;
701 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
702 db_len = pci_resource_len(pdev, 4);
704 if (db_len == 0) {
705 printk(KERN_ERR "%s: doorbell is disabled\n",
706 netxen_nic_driver_name);
707 err = -EIO;
708 goto err_out;
711 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
712 if (!db_ptr) {
713 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
714 netxen_nic_driver_name);
715 err = -EIO;
716 goto err_out;
719 skip_doorbell:
720 adapter->ahw.db_base = db_ptr;
721 adapter->ahw.db_len = db_len;
722 return 0;
724 err_out:
725 netxen_cleanup_pci_map(adapter);
726 return err;
729 static int
730 netxen_start_firmware(struct netxen_adapter *adapter, int request_fw)
732 int val, err, first_boot;
733 struct pci_dev *pdev = adapter->pdev;
735 int first_driver = 0;
737 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
738 first_driver = (adapter->portnum == 0);
739 else
740 first_driver = (adapter->ahw.pci_func == 0);
742 if (!first_driver)
743 goto wait_init;
745 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
747 err = netxen_check_hw_init(adapter, first_boot);
748 if (err) {
749 dev_err(&pdev->dev, "error in init HW init sequence\n");
750 return err;
753 if (request_fw)
754 netxen_request_firmware(adapter);
756 err = netxen_need_fw_reset(adapter);
757 if (err < 0)
758 return err;
759 if (err == 0)
760 goto wait_init;
762 if (first_boot != 0x55555555) {
763 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
764 netxen_pinit_from_rom(adapter, 0);
765 msleep(1);
768 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
769 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
770 netxen_set_port_mode(adapter);
772 netxen_load_firmware(adapter);
774 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
776 /* Initialize multicast addr pool owners */
777 val = 0x7654;
778 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
779 val |= 0x0f000000;
780 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
784 err = netxen_init_dummy_dma(adapter);
785 if (err)
786 return err;
789 * Tell the hardware our version number.
791 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
792 | ((_NETXEN_NIC_LINUX_MINOR << 8))
793 | (_NETXEN_NIC_LINUX_SUBVERSION);
794 NXWR32(adapter, CRB_DRIVER_VERSION, val);
796 wait_init:
797 /* Handshake with the card before we register the devices. */
798 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
799 if (err) {
800 netxen_free_dummy_dma(adapter);
801 return err;
804 nx_update_dma_mask(adapter);
806 netxen_nic_get_firmware_info(adapter);
808 return 0;
811 static int
812 netxen_nic_request_irq(struct netxen_adapter *adapter)
814 irq_handler_t handler;
815 struct nx_host_sds_ring *sds_ring;
816 int err, ring;
818 unsigned long flags = IRQF_SAMPLE_RANDOM;
819 struct net_device *netdev = adapter->netdev;
820 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
822 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
823 handler = netxen_msix_intr;
824 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
825 handler = netxen_msi_intr;
826 else {
827 flags |= IRQF_SHARED;
828 handler = netxen_intr;
830 adapter->irq = netdev->irq;
832 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
833 sds_ring = &recv_ctx->sds_rings[ring];
834 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
835 err = request_irq(sds_ring->irq, handler,
836 flags, sds_ring->name, sds_ring);
837 if (err)
838 return err;
841 return 0;
844 static void
845 netxen_nic_free_irq(struct netxen_adapter *adapter)
847 int ring;
848 struct nx_host_sds_ring *sds_ring;
850 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
852 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
853 sds_ring = &recv_ctx->sds_rings[ring];
854 free_irq(sds_ring->irq, sds_ring);
858 static void
859 netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
861 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
862 adapter->coal.normal.data.rx_time_us =
863 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
864 adapter->coal.normal.data.rx_packets =
865 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
866 adapter->coal.normal.data.tx_time_us =
867 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
868 adapter->coal.normal.data.tx_packets =
869 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
872 static int
873 netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
875 int err;
877 err = adapter->init_port(adapter, adapter->physical_port);
878 if (err) {
879 printk(KERN_ERR "%s: Failed to initialize port %d\n",
880 netxen_nic_driver_name, adapter->portnum);
881 return err;
883 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
884 adapter->macaddr_set(adapter, netdev->dev_addr);
886 adapter->set_multi(netdev);
887 adapter->set_mtu(adapter, netdev->mtu);
889 adapter->ahw.linkup = 0;
891 if (adapter->max_sds_rings > 1)
892 netxen_config_rss(adapter, 1);
894 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
895 netxen_config_intr_coalesce(adapter);
897 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
898 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
900 netxen_napi_enable(adapter);
902 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
903 netxen_linkevent_request(adapter, 1);
904 else
905 netxen_nic_set_link_parameters(adapter);
907 mod_timer(&adapter->watchdog_timer, jiffies);
909 return 0;
912 static void
913 netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
915 spin_lock(&adapter->tx_clean_lock);
916 netif_carrier_off(netdev);
917 netif_tx_disable(netdev);
919 if (adapter->stop_port)
920 adapter->stop_port(adapter);
922 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
923 netxen_p3_free_mac_list(adapter);
925 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
927 netxen_napi_disable(adapter);
929 netxen_release_tx_buffers(adapter);
930 spin_unlock(&adapter->tx_clean_lock);
932 del_timer_sync(&adapter->watchdog_timer);
936 static int
937 netxen_nic_attach(struct netxen_adapter *adapter)
939 struct net_device *netdev = adapter->netdev;
940 struct pci_dev *pdev = adapter->pdev;
941 int err, ring;
942 struct nx_host_rds_ring *rds_ring;
943 struct nx_host_tx_ring *tx_ring;
945 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
946 return 0;
948 err = netxen_init_firmware(adapter);
949 if (err)
950 return err;
952 err = netxen_napi_add(adapter, netdev);
953 if (err)
954 return err;
956 err = netxen_alloc_sw_resources(adapter);
957 if (err) {
958 printk(KERN_ERR "%s: Error in setting sw resources\n",
959 netdev->name);
960 return err;
963 netxen_nic_clear_stats(adapter);
965 err = netxen_alloc_hw_resources(adapter);
966 if (err) {
967 printk(KERN_ERR "%s: Error in setting hw resources\n",
968 netdev->name);
969 goto err_out_free_sw;
972 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
973 tx_ring = adapter->tx_ring;
974 tx_ring->crb_cmd_producer = crb_cmd_producer[adapter->portnum];
975 tx_ring->crb_cmd_consumer = crb_cmd_consumer[adapter->portnum];
977 tx_ring->producer = 0;
978 tx_ring->sw_consumer = 0;
980 netxen_nic_update_cmd_producer(adapter, tx_ring);
981 netxen_nic_update_cmd_consumer(adapter, tx_ring);
984 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
985 rds_ring = &adapter->recv_ctx.rds_rings[ring];
986 netxen_post_rx_buffers(adapter, ring, rds_ring);
989 err = netxen_nic_request_irq(adapter);
990 if (err) {
991 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
992 netdev->name);
993 goto err_out_free_rxbuf;
996 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
997 netxen_nic_init_coalesce_defaults(adapter);
999 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1000 return 0;
1002 err_out_free_rxbuf:
1003 netxen_release_rx_buffers(adapter);
1004 netxen_free_hw_resources(adapter);
1005 err_out_free_sw:
1006 netxen_free_sw_resources(adapter);
1007 return err;
1010 static void
1011 netxen_nic_detach(struct netxen_adapter *adapter)
1013 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1014 return;
1016 netxen_free_hw_resources(adapter);
1017 netxen_release_rx_buffers(adapter);
1018 netxen_nic_free_irq(adapter);
1019 netxen_napi_del(adapter);
1020 netxen_free_sw_resources(adapter);
1022 adapter->is_up = 0;
1026 netxen_nic_reset_context(struct netxen_adapter *adapter)
1028 int err = 0;
1029 struct net_device *netdev = adapter->netdev;
1031 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1033 if (netif_running(netdev))
1034 netxen_nic_down(adapter, netdev);
1036 netxen_nic_detach(adapter);
1038 err = netxen_nic_attach(adapter);
1039 if (err)
1040 goto done;
1042 if (netif_running(netdev))
1043 err = netxen_nic_up(adapter, netdev);
1045 done:
1046 return err;
1049 static int
1050 netxen_setup_netdev(struct netxen_adapter *adapter,
1051 struct net_device *netdev)
1053 int err = 0;
1054 struct pci_dev *pdev = adapter->pdev;
1056 adapter->rx_csum = 1;
1057 adapter->mc_enabled = 0;
1058 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1059 adapter->max_mc_count = 38;
1060 else
1061 adapter->max_mc_count = 16;
1063 netdev->netdev_ops = &netxen_netdev_ops;
1064 netdev->watchdog_timeo = 2*HZ;
1066 netxen_nic_change_mtu(netdev, netdev->mtu);
1068 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
1070 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1071 netdev->features |= (NETIF_F_GRO);
1072 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
1074 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1075 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1076 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1079 if (adapter->pci_using_dac) {
1080 netdev->features |= NETIF_F_HIGHDMA;
1081 netdev->vlan_features |= NETIF_F_HIGHDMA;
1084 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1085 netdev->features |= (NETIF_F_HW_VLAN_TX);
1087 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1088 netdev->features |= NETIF_F_LRO;
1090 netdev->irq = adapter->msix_entries[0].vector;
1092 err = netxen_napi_add(adapter, netdev);
1093 if (err)
1094 return err;
1096 init_timer(&adapter->watchdog_timer);
1097 adapter->watchdog_timer.function = &netxen_watchdog;
1098 adapter->watchdog_timer.data = (unsigned long)adapter;
1099 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
1100 INIT_WORK(&adapter->tx_timeout_task, netxen_reset_task);
1102 if (netxen_read_mac_addr(adapter))
1103 dev_warn(&pdev->dev, "failed to read mac addr\n");
1105 netif_carrier_off(netdev);
1106 netif_stop_queue(netdev);
1108 err = register_netdev(netdev);
1109 if (err) {
1110 dev_err(&pdev->dev, "failed to register net device\n");
1111 return err;
1114 return 0;
1117 static int __devinit
1118 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1120 struct net_device *netdev = NULL;
1121 struct netxen_adapter *adapter = NULL;
1122 int i = 0, err;
1123 int pci_func_id = PCI_FUNC(pdev->devfn);
1124 uint8_t revision_id;
1126 if (pdev->class != 0x020000) {
1127 printk(KERN_DEBUG "NetXen function %d, class %x will not "
1128 "be enabled.\n",pci_func_id, pdev->class);
1129 return -ENODEV;
1132 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
1133 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
1134 "will not be enabled.\n",
1135 NX_P3_A0, NX_P3_B1);
1136 return -ENODEV;
1139 if ((err = pci_enable_device(pdev)))
1140 return err;
1142 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1143 err = -ENODEV;
1144 goto err_out_disable_pdev;
1147 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1148 goto err_out_disable_pdev;
1150 pci_set_master(pdev);
1152 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1153 if(!netdev) {
1154 dev_err(&pdev->dev, "failed to allocate net_device\n");
1155 err = -ENOMEM;
1156 goto err_out_free_res;
1159 SET_NETDEV_DEV(netdev, &pdev->dev);
1161 adapter = netdev_priv(netdev);
1162 adapter->netdev = netdev;
1163 adapter->pdev = pdev;
1164 adapter->ahw.pci_func = pci_func_id;
1166 revision_id = pdev->revision;
1167 adapter->ahw.revision_id = revision_id;
1169 err = nx_set_dma_mask(adapter);
1170 if (err)
1171 goto err_out_free_netdev;
1173 rwlock_init(&adapter->adapter_lock);
1174 spin_lock_init(&adapter->tx_clean_lock);
1175 INIT_LIST_HEAD(&adapter->mac_list);
1177 err = netxen_setup_pci_map(adapter);
1178 if (err)
1179 goto err_out_free_netdev;
1181 /* This will be reset for mezz cards */
1182 adapter->portnum = pci_func_id;
1184 err = netxen_nic_get_board_info(adapter);
1185 if (err) {
1186 dev_err(&pdev->dev, "Error getting board config info.\n");
1187 goto err_out_iounmap;
1190 netxen_initialize_adapter_ops(adapter);
1192 /* Mezz cards have PCI function 0,2,3 enabled */
1193 switch (adapter->ahw.board_type) {
1194 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1195 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1196 if (pci_func_id >= 2)
1197 adapter->portnum = pci_func_id - 2;
1198 break;
1199 default:
1200 break;
1203 err = netxen_start_firmware(adapter, 1);
1204 if (err)
1205 goto err_out_iounmap;
1208 * See if the firmware gave us a virtual-physical port mapping.
1210 adapter->physical_port = adapter->portnum;
1211 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1212 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
1213 if (i != 0x55555555)
1214 adapter->physical_port = i;
1217 netxen_check_options(adapter);
1219 netxen_setup_intr(adapter);
1221 err = netxen_setup_netdev(adapter, netdev);
1222 if (err)
1223 goto err_out_disable_msi;
1225 pci_set_drvdata(pdev, adapter);
1227 switch (adapter->ahw.port_type) {
1228 case NETXEN_NIC_GBE:
1229 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1230 adapter->netdev->name);
1231 break;
1232 case NETXEN_NIC_XGBE:
1233 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1234 adapter->netdev->name);
1235 break;
1238 return 0;
1240 err_out_disable_msi:
1241 netxen_teardown_intr(adapter);
1243 netxen_free_dummy_dma(adapter);
1245 err_out_iounmap:
1246 netxen_cleanup_pci_map(adapter);
1248 err_out_free_netdev:
1249 free_netdev(netdev);
1251 err_out_free_res:
1252 pci_release_regions(pdev);
1254 err_out_disable_pdev:
1255 pci_set_drvdata(pdev, NULL);
1256 pci_disable_device(pdev);
1257 return err;
1260 static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1262 struct netxen_adapter *adapter;
1263 struct net_device *netdev;
1265 adapter = pci_get_drvdata(pdev);
1266 if (adapter == NULL)
1267 return;
1269 netdev = adapter->netdev;
1271 unregister_netdev(netdev);
1273 cancel_work_sync(&adapter->watchdog_task);
1274 cancel_work_sync(&adapter->tx_timeout_task);
1276 netxen_nic_detach(adapter);
1278 if (adapter->portnum == 0)
1279 netxen_free_dummy_dma(adapter);
1281 netxen_teardown_intr(adapter);
1283 netxen_cleanup_pci_map(adapter);
1285 netxen_release_firmware(adapter);
1287 pci_release_regions(pdev);
1288 pci_disable_device(pdev);
1289 pci_set_drvdata(pdev, NULL);
1291 free_netdev(netdev);
1293 static int __netxen_nic_shutdown(struct pci_dev *pdev)
1295 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1296 struct net_device *netdev = adapter->netdev;
1297 int retval;
1299 netif_device_detach(netdev);
1301 if (netif_running(netdev))
1302 netxen_nic_down(adapter, netdev);
1304 cancel_work_sync(&adapter->watchdog_task);
1305 cancel_work_sync(&adapter->tx_timeout_task);
1307 netxen_nic_detach(adapter);
1309 if (adapter->portnum == 0)
1310 netxen_free_dummy_dma(adapter);
1312 retval = pci_save_state(pdev);
1313 if (retval)
1314 return retval;
1316 if (netxen_nic_wol_supported(adapter)) {
1317 pci_enable_wake(pdev, PCI_D3cold, 1);
1318 pci_enable_wake(pdev, PCI_D3hot, 1);
1321 pci_disable_device(pdev);
1323 return 0;
1325 static void netxen_nic_shutdown(struct pci_dev *pdev)
1327 if (__netxen_nic_shutdown(pdev))
1328 return;
1330 #ifdef CONFIG_PM
1331 static int
1332 netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1334 int retval;
1336 retval = __netxen_nic_shutdown(pdev);
1337 if (retval)
1338 return retval;
1340 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1341 return 0;
1344 static int
1345 netxen_nic_resume(struct pci_dev *pdev)
1347 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1348 struct net_device *netdev = adapter->netdev;
1349 int err;
1351 pci_set_power_state(pdev, PCI_D0);
1352 pci_restore_state(pdev);
1354 err = pci_enable_device(pdev);
1355 if (err)
1356 return err;
1358 adapter->curr_window = 255;
1360 err = netxen_start_firmware(adapter, 0);
1361 if (err) {
1362 dev_err(&pdev->dev, "failed to start firmware\n");
1363 return err;
1366 if (netif_running(netdev)) {
1367 err = netxen_nic_attach(adapter);
1368 if (err)
1369 return err;
1371 err = netxen_nic_up(adapter, netdev);
1372 if (err)
1373 return err;
1375 netif_device_attach(netdev);
1378 return 0;
1380 #endif
1382 static int netxen_nic_open(struct net_device *netdev)
1384 struct netxen_adapter *adapter = netdev_priv(netdev);
1385 int err = 0;
1387 if (adapter->driver_mismatch)
1388 return -EIO;
1390 err = netxen_nic_attach(adapter);
1391 if (err)
1392 return err;
1394 err = netxen_nic_up(adapter, netdev);
1395 if (err)
1396 goto err_out;
1398 netif_start_queue(netdev);
1400 return 0;
1402 err_out:
1403 netxen_nic_detach(adapter);
1404 return err;
1408 * netxen_nic_close - Disables a network interface entry point
1410 static int netxen_nic_close(struct net_device *netdev)
1412 struct netxen_adapter *adapter = netdev_priv(netdev);
1414 netxen_nic_down(adapter, netdev);
1415 return 0;
1418 static void
1419 netxen_tso_check(struct net_device *netdev,
1420 struct nx_host_tx_ring *tx_ring,
1421 struct cmd_desc_type0 *first_desc,
1422 struct sk_buff *skb)
1424 u8 opcode = TX_ETHER_PKT;
1425 __be16 protocol = skb->protocol;
1426 u16 flags = 0, vid = 0;
1427 u32 producer;
1428 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1429 struct cmd_desc_type0 *hwdesc;
1430 struct vlan_ethhdr *vh;
1432 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1434 vh = (struct vlan_ethhdr *)skb->data;
1435 protocol = vh->h_vlan_encapsulated_proto;
1436 flags = FLAGS_VLAN_TAGGED;
1438 } else if (vlan_tx_tag_present(skb)) {
1440 flags = FLAGS_VLAN_OOB;
1441 vid = vlan_tx_tag_get(skb);
1442 netxen_set_tx_vlan_tci(first_desc, vid);
1443 vlan_oob = 1;
1446 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1447 skb_shinfo(skb)->gso_size > 0) {
1449 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1451 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1452 first_desc->total_hdr_length = hdr_len;
1453 if (vlan_oob) {
1454 first_desc->total_hdr_length += VLAN_HLEN;
1455 first_desc->tcp_hdr_offset = VLAN_HLEN;
1456 first_desc->ip_hdr_offset = VLAN_HLEN;
1457 /* Only in case of TSO on vlan device */
1458 flags |= FLAGS_VLAN_TAGGED;
1461 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1462 TX_TCP_LSO6 : TX_TCP_LSO;
1463 tso = 1;
1465 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1466 u8 l4proto;
1468 if (protocol == cpu_to_be16(ETH_P_IP)) {
1469 l4proto = ip_hdr(skb)->protocol;
1471 if (l4proto == IPPROTO_TCP)
1472 opcode = TX_TCP_PKT;
1473 else if(l4proto == IPPROTO_UDP)
1474 opcode = TX_UDP_PKT;
1475 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1476 l4proto = ipv6_hdr(skb)->nexthdr;
1478 if (l4proto == IPPROTO_TCP)
1479 opcode = TX_TCPV6_PKT;
1480 else if(l4proto == IPPROTO_UDP)
1481 opcode = TX_UDPV6_PKT;
1485 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1486 first_desc->ip_hdr_offset += skb_network_offset(skb);
1487 netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1489 if (!tso)
1490 return;
1492 /* For LSO, we need to copy the MAC/IP/TCP headers into
1493 * the descriptor ring
1495 producer = tx_ring->producer;
1496 copied = 0;
1497 offset = 2;
1499 if (vlan_oob) {
1500 /* Create a TSO vlan header template for firmware */
1502 hwdesc = &tx_ring->desc_head[producer];
1503 tx_ring->cmd_buf_arr[producer].skb = NULL;
1505 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1506 hdr_len + VLAN_HLEN);
1508 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1509 skb_copy_from_linear_data(skb, vh, 12);
1510 vh->h_vlan_proto = htons(ETH_P_8021Q);
1511 vh->h_vlan_TCI = htons(vid);
1512 skb_copy_from_linear_data_offset(skb, 12,
1513 (char *)vh + 16, copy_len - 16);
1515 copied = copy_len - VLAN_HLEN;
1516 offset = 0;
1518 producer = get_next_index(producer, tx_ring->num_desc);
1521 while (copied < hdr_len) {
1523 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1524 (hdr_len - copied));
1526 hwdesc = &tx_ring->desc_head[producer];
1527 tx_ring->cmd_buf_arr[producer].skb = NULL;
1529 skb_copy_from_linear_data_offset(skb, copied,
1530 (char *)hwdesc + offset, copy_len);
1532 copied += copy_len;
1533 offset = 0;
1535 producer = get_next_index(producer, tx_ring->num_desc);
1538 tx_ring->producer = producer;
1539 barrier();
1542 static int
1543 netxen_map_tx_skb(struct pci_dev *pdev,
1544 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
1546 struct netxen_skb_frag *nf;
1547 struct skb_frag_struct *frag;
1548 int i, nr_frags;
1549 dma_addr_t map;
1551 nr_frags = skb_shinfo(skb)->nr_frags;
1552 nf = &pbuf->frag_array[0];
1554 map = pci_map_single(pdev, skb->data,
1555 skb_headlen(skb), PCI_DMA_TODEVICE);
1556 if (pci_dma_mapping_error(pdev, map))
1557 goto out_err;
1559 nf->dma = map;
1560 nf->length = skb_headlen(skb);
1562 for (i = 0; i < nr_frags; i++) {
1563 frag = &skb_shinfo(skb)->frags[i];
1564 nf = &pbuf->frag_array[i+1];
1566 map = pci_map_page(pdev, frag->page, frag->page_offset,
1567 frag->size, PCI_DMA_TODEVICE);
1568 if (pci_dma_mapping_error(pdev, map))
1569 goto unwind;
1571 nf->dma = map;
1572 nf->length = frag->size;
1575 return 0;
1577 unwind:
1578 while (i > 0) {
1579 nf = &pbuf->frag_array[i];
1580 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1583 nf = &pbuf->frag_array[0];
1584 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1586 out_err:
1587 return -ENOMEM;
1590 static inline void
1591 netxen_clear_cmddesc(u64 *desc)
1593 desc[0] = 0ULL;
1594 desc[2] = 0ULL;
1597 static int
1598 netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1600 struct netxen_adapter *adapter = netdev_priv(netdev);
1601 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
1602 struct netxen_cmd_buffer *pbuf;
1603 struct netxen_skb_frag *buffrag;
1604 struct cmd_desc_type0 *hwdesc, *first_desc;
1605 struct pci_dev *pdev;
1606 int i, k;
1608 u32 producer;
1609 int frag_count, no_of_desc;
1610 u32 num_txd = tx_ring->num_desc;
1612 frag_count = skb_shinfo(skb)->nr_frags + 1;
1614 /* 4 fragments per cmd des */
1615 no_of_desc = (frag_count + 3) >> 2;
1617 if (unlikely(no_of_desc + 2) > netxen_tx_avail(tx_ring)) {
1618 netif_stop_queue(netdev);
1619 return NETDEV_TX_BUSY;
1622 producer = tx_ring->producer;
1623 pbuf = &tx_ring->cmd_buf_arr[producer];
1625 pdev = adapter->pdev;
1627 if (netxen_map_tx_skb(pdev, skb, pbuf))
1628 goto drop_packet;
1630 pbuf->skb = skb;
1631 pbuf->frag_count = frag_count;
1633 first_desc = hwdesc = &tx_ring->desc_head[producer];
1634 netxen_clear_cmddesc((u64 *)hwdesc);
1636 netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
1637 netxen_set_tx_port(first_desc, adapter->portnum);
1639 for (i = 0; i < frag_count; i++) {
1641 k = i % 4;
1643 if ((k == 0) && (i > 0)) {
1644 /* move to next desc.*/
1645 producer = get_next_index(producer, num_txd);
1646 hwdesc = &tx_ring->desc_head[producer];
1647 netxen_clear_cmddesc((u64 *)hwdesc);
1648 tx_ring->cmd_buf_arr[producer].skb = NULL;
1651 buffrag = &pbuf->frag_array[i];
1653 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
1654 switch (k) {
1655 case 0:
1656 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1657 break;
1658 case 1:
1659 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
1660 break;
1661 case 2:
1662 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
1663 break;
1664 case 3:
1665 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
1666 break;
1670 tx_ring->producer = get_next_index(producer, num_txd);
1672 netxen_tso_check(netdev, tx_ring, first_desc, skb);
1674 netxen_nic_update_cmd_producer(adapter, tx_ring);
1676 adapter->stats.txbytes += skb->len;
1677 adapter->stats.xmitcalled++;
1679 return NETDEV_TX_OK;
1681 drop_packet:
1682 adapter->stats.txdropped++;
1683 dev_kfree_skb_any(skb);
1684 return NETDEV_TX_OK;
1687 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1689 struct net_device *netdev = adapter->netdev;
1690 uint32_t temp, temp_state, temp_val;
1691 int rv = 0;
1693 temp = NXRD32(adapter, CRB_TEMP_STATE);
1695 temp_state = nx_get_temp_state(temp);
1696 temp_val = nx_get_temp_val(temp);
1698 if (temp_state == NX_TEMP_PANIC) {
1699 printk(KERN_ALERT
1700 "%s: Device temperature %d degrees C exceeds"
1701 " maximum allowed. Hardware has been shut down.\n",
1702 netdev->name, temp_val);
1703 rv = 1;
1704 } else if (temp_state == NX_TEMP_WARN) {
1705 if (adapter->temp == NX_TEMP_NORMAL) {
1706 printk(KERN_ALERT
1707 "%s: Device temperature %d degrees C "
1708 "exceeds operating range."
1709 " Immediate action needed.\n",
1710 netdev->name, temp_val);
1712 } else {
1713 if (adapter->temp == NX_TEMP_WARN) {
1714 printk(KERN_INFO
1715 "%s: Device temperature is now %d degrees C"
1716 " in normal range.\n", netdev->name,
1717 temp_val);
1720 adapter->temp = temp_state;
1721 return rv;
1724 void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
1726 struct net_device *netdev = adapter->netdev;
1728 if (adapter->ahw.linkup && !linkup) {
1729 printk(KERN_INFO "%s: %s NIC Link is down\n",
1730 netxen_nic_driver_name, netdev->name);
1731 adapter->ahw.linkup = 0;
1732 if (netif_running(netdev)) {
1733 netif_carrier_off(netdev);
1734 netif_stop_queue(netdev);
1736 adapter->link_changed = !adapter->has_link_events;
1737 } else if (!adapter->ahw.linkup && linkup) {
1738 printk(KERN_INFO "%s: %s NIC Link is up\n",
1739 netxen_nic_driver_name, netdev->name);
1740 adapter->ahw.linkup = 1;
1741 if (netif_running(netdev)) {
1742 netif_carrier_on(netdev);
1743 netif_wake_queue(netdev);
1745 adapter->link_changed = !adapter->has_link_events;
1749 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1751 u32 val, port, linkup;
1753 port = adapter->physical_port;
1755 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1756 val = NXRD32(adapter, CRB_XG_STATE_P3);
1757 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1758 linkup = (val == XG_LINK_UP_P3);
1759 } else {
1760 val = NXRD32(adapter, CRB_XG_STATE);
1761 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
1762 linkup = (val >> port) & 1;
1763 else {
1764 val = (val >> port*8) & 0xff;
1765 linkup = (val == XG_LINK_UP);
1769 netxen_advert_link_change(adapter, linkup);
1772 static void netxen_nic_thermal_shutdown(struct netxen_adapter *adapter)
1774 struct net_device *netdev = adapter->netdev;
1776 netif_device_detach(netdev);
1777 netxen_nic_down(adapter, netdev);
1778 netxen_nic_detach(adapter);
1781 static void netxen_watchdog(unsigned long v)
1783 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
1785 if (netxen_nic_check_temp(adapter))
1786 goto do_sched;
1788 if (!adapter->has_link_events) {
1789 netxen_nic_handle_phy_intr(adapter);
1791 if (adapter->link_changed)
1792 goto do_sched;
1795 if (netif_running(adapter->netdev))
1796 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1798 return;
1800 do_sched:
1801 schedule_work(&adapter->watchdog_task);
1804 void netxen_watchdog_task(struct work_struct *work)
1806 struct netxen_adapter *adapter =
1807 container_of(work, struct netxen_adapter, watchdog_task);
1809 if (adapter->temp == NX_TEMP_PANIC) {
1810 netxen_nic_thermal_shutdown(adapter);
1811 return;
1814 if (adapter->link_changed)
1815 netxen_nic_set_link_parameters(adapter);
1817 if (netif_running(adapter->netdev))
1818 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1821 static void netxen_tx_timeout(struct net_device *netdev)
1823 struct netxen_adapter *adapter = netdev_priv(netdev);
1825 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
1826 schedule_work(&adapter->tx_timeout_task);
1829 static void netxen_reset_task(struct work_struct *work)
1831 struct netxen_adapter *adapter =
1832 container_of(work, struct netxen_adapter, tx_timeout_task);
1834 if (!netif_running(adapter->netdev))
1835 return;
1837 netxen_napi_disable(adapter);
1839 adapter->netdev->trans_start = jiffies;
1841 netxen_napi_enable(adapter);
1842 netif_wake_queue(adapter->netdev);
1845 struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1847 struct netxen_adapter *adapter = netdev_priv(netdev);
1848 struct net_device_stats *stats = &adapter->net_stats;
1850 memset(stats, 0, sizeof(*stats));
1852 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
1853 stats->tx_packets = adapter->stats.xmitfinished;
1854 stats->rx_bytes = adapter->stats.rxbytes;
1855 stats->tx_bytes = adapter->stats.txbytes;
1856 stats->rx_dropped = adapter->stats.rxdropped;
1857 stats->tx_dropped = adapter->stats.txdropped;
1859 return stats;
1862 static irqreturn_t netxen_intr(int irq, void *data)
1864 struct nx_host_sds_ring *sds_ring = data;
1865 struct netxen_adapter *adapter = sds_ring->adapter;
1866 u32 status = 0;
1868 status = adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1870 if (!(status & adapter->legacy_intr.int_vec_bit))
1871 return IRQ_NONE;
1873 if (adapter->ahw.revision_id >= NX_P3_B1) {
1874 /* check interrupt state machine, to be sure */
1875 status = adapter->pci_read_immediate(adapter,
1876 ISR_INT_STATE_REG);
1877 if (!ISR_LEGACY_INT_TRIGGERED(status))
1878 return IRQ_NONE;
1880 } else {
1881 unsigned long our_int = 0;
1883 our_int = NXRD32(adapter, CRB_INT_VECTOR);
1885 /* not our interrupt */
1886 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
1887 return IRQ_NONE;
1889 /* claim interrupt */
1890 NXWR32(adapter, CRB_INT_VECTOR, (our_int & 0xffffffff));
1893 /* clear interrupt */
1894 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
1895 netxen_nic_disable_int(sds_ring);
1897 adapter->pci_write_immediate(adapter,
1898 adapter->legacy_intr.tgt_status_reg,
1899 0xffffffff);
1900 /* read twice to ensure write is flushed */
1901 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1902 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1904 napi_schedule(&sds_ring->napi);
1906 return IRQ_HANDLED;
1909 static irqreturn_t netxen_msi_intr(int irq, void *data)
1911 struct nx_host_sds_ring *sds_ring = data;
1912 struct netxen_adapter *adapter = sds_ring->adapter;
1914 /* clear interrupt */
1915 adapter->pci_write_immediate(adapter,
1916 adapter->msi_tgt_status, 0xffffffff);
1918 napi_schedule(&sds_ring->napi);
1919 return IRQ_HANDLED;
1922 static irqreturn_t netxen_msix_intr(int irq, void *data)
1924 struct nx_host_sds_ring *sds_ring = data;
1926 napi_schedule(&sds_ring->napi);
1927 return IRQ_HANDLED;
1930 static int netxen_nic_poll(struct napi_struct *napi, int budget)
1932 struct nx_host_sds_ring *sds_ring =
1933 container_of(napi, struct nx_host_sds_ring, napi);
1935 struct netxen_adapter *adapter = sds_ring->adapter;
1937 int tx_complete;
1938 int work_done;
1940 tx_complete = netxen_process_cmd_ring(adapter);
1942 work_done = netxen_process_rcv_ring(sds_ring, budget);
1944 if ((work_done < budget) && tx_complete) {
1945 napi_complete(&sds_ring->napi);
1946 if (netif_running(adapter->netdev))
1947 netxen_nic_enable_int(sds_ring);
1950 return work_done;
1953 #ifdef CONFIG_NET_POLL_CONTROLLER
1954 static void netxen_nic_poll_controller(struct net_device *netdev)
1956 struct netxen_adapter *adapter = netdev_priv(netdev);
1957 disable_irq(adapter->irq);
1958 netxen_intr(adapter->irq, adapter);
1959 enable_irq(adapter->irq);
1961 #endif
1963 #ifdef CONFIG_INET
1965 #define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
1967 static int
1968 netxen_destip_supported(struct netxen_adapter *adapter)
1970 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
1971 return 0;
1973 if (adapter->ahw.cut_through)
1974 return 0;
1976 return 1;
1979 static int netxen_netdev_event(struct notifier_block *this,
1980 unsigned long event, void *ptr)
1982 struct netxen_adapter *adapter;
1983 struct net_device *dev = (struct net_device *)ptr;
1984 struct in_device *indev;
1986 recheck:
1987 if (dev == NULL)
1988 goto done;
1990 if (dev->priv_flags & IFF_802_1Q_VLAN) {
1991 dev = vlan_dev_real_dev(dev);
1992 goto recheck;
1995 if (!is_netxen_netdev(dev))
1996 goto done;
1998 adapter = netdev_priv(dev);
2000 if (!adapter || !netxen_destip_supported(adapter))
2001 goto done;
2003 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2004 goto done;
2006 indev = in_dev_get(dev);
2007 if (!indev)
2008 goto done;
2010 for_ifa(indev) {
2011 switch (event) {
2012 case NETDEV_UP:
2013 netxen_config_ipaddr(adapter,
2014 ifa->ifa_address, NX_IP_UP);
2015 break;
2016 case NETDEV_DOWN:
2017 netxen_config_ipaddr(adapter,
2018 ifa->ifa_address, NX_IP_DOWN);
2019 break;
2020 default:
2021 break;
2023 } endfor_ifa(indev);
2025 in_dev_put(indev);
2026 done:
2027 return NOTIFY_DONE;
2030 static int
2031 netxen_inetaddr_event(struct notifier_block *this,
2032 unsigned long event, void *ptr)
2034 struct netxen_adapter *adapter;
2035 struct net_device *dev;
2037 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2039 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2041 recheck:
2042 if (dev == NULL || !netif_running(dev))
2043 goto done;
2045 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2046 dev = vlan_dev_real_dev(dev);
2047 goto recheck;
2050 if (!is_netxen_netdev(dev))
2051 goto done;
2053 adapter = netdev_priv(dev);
2055 if (!adapter || !netxen_destip_supported(adapter))
2056 goto done;
2058 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2059 goto done;
2061 switch (event) {
2062 case NETDEV_UP:
2063 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
2064 break;
2065 case NETDEV_DOWN:
2066 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_DOWN);
2067 break;
2068 default:
2069 break;
2072 done:
2073 return NOTIFY_DONE;
2076 static struct notifier_block netxen_netdev_cb = {
2077 .notifier_call = netxen_netdev_event,
2080 static struct notifier_block netxen_inetaddr_cb = {
2081 .notifier_call = netxen_inetaddr_event,
2083 #endif
2085 static struct pci_driver netxen_driver = {
2086 .name = netxen_nic_driver_name,
2087 .id_table = netxen_pci_tbl,
2088 .probe = netxen_nic_probe,
2089 .remove = __devexit_p(netxen_nic_remove),
2090 #ifdef CONFIG_PM
2091 .suspend = netxen_nic_suspend,
2092 .resume = netxen_nic_resume,
2093 #endif
2094 .shutdown = netxen_nic_shutdown
2097 static int __init netxen_init_module(void)
2099 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
2101 #ifdef CONFIG_INET
2102 register_netdevice_notifier(&netxen_netdev_cb);
2103 register_inetaddr_notifier(&netxen_inetaddr_cb);
2104 #endif
2106 return pci_register_driver(&netxen_driver);
2109 module_init(netxen_init_module);
2111 static void __exit netxen_exit_module(void)
2113 pci_unregister_driver(&netxen_driver);
2115 #ifdef CONFIG_INET
2116 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
2117 unregister_netdevice_notifier(&netxen_netdev_cb);
2118 #endif
2121 module_exit(netxen_exit_module);