WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / ethernet / qlogic / netxen / netxen_nic_main.c
blobf21847739ef1fecbd37eacb588717304574ca2eb
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2003 - 2009 NetXen, Inc.
4 * Copyright (C) 2009 - QLogic Corporation.
5 * All rights reserved.
6 */
8 #include <linux/slab.h>
9 #include <linux/vmalloc.h>
10 #include <linux/interrupt.h>
11 #include "netxen_nic_hw.h"
13 #include "netxen_nic.h"
15 #include <linux/dma-mapping.h>
16 #include <linux/if_vlan.h>
17 #include <net/ip.h>
18 #include <linux/ipv6.h>
19 #include <linux/inetdevice.h>
20 #include <linux/sysfs.h>
21 #include <linux/aer.h>
23 MODULE_DESCRIPTION("QLogic/NetXen (1/10) GbE Intelligent Ethernet Driver");
24 MODULE_LICENSE("GPL");
25 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
26 MODULE_FIRMWARE(NX_UNIFIED_ROMIMAGE_NAME);
28 char netxen_nic_driver_name[] = "netxen_nic";
29 static char netxen_nic_driver_string[] = "QLogic/NetXen Network Driver v"
30 NETXEN_NIC_LINUX_VERSIONID;
32 static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
34 /* Default to restricted 1G auto-neg mode */
35 static int wol_port_mode = 5;
37 static int use_msi = 1;
39 static int use_msi_x = 1;
41 static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
42 module_param(auto_fw_reset, int, 0644);
43 MODULE_PARM_DESC(auto_fw_reset,"Auto firmware reset (0=disabled, 1=enabled");
45 static int netxen_nic_probe(struct pci_dev *pdev,
46 const struct pci_device_id *ent);
47 static void netxen_nic_remove(struct pci_dev *pdev);
48 static int netxen_nic_open(struct net_device *netdev);
49 static int netxen_nic_close(struct net_device *netdev);
50 static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
51 struct net_device *);
52 static void netxen_tx_timeout(struct net_device *netdev, unsigned int txqueue);
53 static void netxen_tx_timeout_task(struct work_struct *work);
54 static void netxen_fw_poll_work(struct work_struct *work);
55 static void netxen_schedule_work(struct netxen_adapter *adapter,
56 work_func_t func, int delay);
57 static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
58 static int netxen_nic_poll(struct napi_struct *napi, int budget);
60 static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
61 static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
62 static void netxen_create_diag_entries(struct netxen_adapter *adapter);
63 static void netxen_remove_diag_entries(struct netxen_adapter *adapter);
64 static int nx_dev_request_aer(struct netxen_adapter *adapter);
65 static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
66 static int netxen_can_start_firmware(struct netxen_adapter *adapter);
68 static irqreturn_t netxen_intr(int irq, void *data);
69 static irqreturn_t netxen_msi_intr(int irq, void *data);
70 static irqreturn_t netxen_msix_intr(int irq, void *data);
72 static void netxen_free_ip_list(struct netxen_adapter *, bool);
73 static void netxen_restore_indev_addr(struct net_device *dev, unsigned long);
74 static void netxen_nic_get_stats(struct net_device *dev,
75 struct rtnl_link_stats64 *stats);
76 static int netxen_nic_set_mac(struct net_device *netdev, void *p);
78 /* PCI Device ID Table */
79 #define ENTRY(device) \
80 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
81 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
83 static const struct pci_device_id netxen_pci_tbl[] = {
84 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
85 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
86 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
87 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
88 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
89 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
90 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
91 ENTRY(PCI_DEVICE_ID_NX3031),
92 {0,}
95 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
97 static uint32_t crb_cmd_producer[4] = {
98 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
99 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
102 void
103 netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
104 struct nx_host_tx_ring *tx_ring)
106 NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
109 static uint32_t crb_cmd_consumer[4] = {
110 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
111 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
114 static inline void
115 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
116 struct nx_host_tx_ring *tx_ring)
118 NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
121 static uint32_t msi_tgt_status[8] = {
122 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
123 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
124 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
125 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
128 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
130 static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
132 struct netxen_adapter *adapter = sds_ring->adapter;
134 NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
137 static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
139 struct netxen_adapter *adapter = sds_ring->adapter;
141 NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
143 if (!NETXEN_IS_MSI_FAMILY(adapter))
144 NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
147 static int
148 netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
150 int size = sizeof(struct nx_host_sds_ring) * count;
152 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
154 return recv_ctx->sds_rings == NULL;
157 static void
158 netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
160 kfree(recv_ctx->sds_rings);
161 recv_ctx->sds_rings = NULL;
164 static int
165 netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
167 int ring;
168 struct nx_host_sds_ring *sds_ring;
169 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
171 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
172 return -ENOMEM;
174 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
175 sds_ring = &recv_ctx->sds_rings[ring];
176 netif_napi_add(netdev, &sds_ring->napi,
177 netxen_nic_poll, NAPI_POLL_WEIGHT);
180 return 0;
183 static void
184 netxen_napi_del(struct netxen_adapter *adapter)
186 int ring;
187 struct nx_host_sds_ring *sds_ring;
188 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
190 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
191 sds_ring = &recv_ctx->sds_rings[ring];
192 netif_napi_del(&sds_ring->napi);
195 netxen_free_sds_rings(&adapter->recv_ctx);
198 static void
199 netxen_napi_enable(struct netxen_adapter *adapter)
201 int ring;
202 struct nx_host_sds_ring *sds_ring;
203 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
205 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
206 sds_ring = &recv_ctx->sds_rings[ring];
207 napi_enable(&sds_ring->napi);
208 netxen_nic_enable_int(sds_ring);
212 static void
213 netxen_napi_disable(struct netxen_adapter *adapter)
215 int ring;
216 struct nx_host_sds_ring *sds_ring;
217 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
219 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
220 sds_ring = &recv_ctx->sds_rings[ring];
221 netxen_nic_disable_int(sds_ring);
222 napi_synchronize(&sds_ring->napi);
223 napi_disable(&sds_ring->napi);
227 static int nx_set_dma_mask(struct netxen_adapter *adapter)
229 struct pci_dev *pdev = adapter->pdev;
230 uint64_t mask, cmask;
232 adapter->pci_using_dac = 0;
234 mask = DMA_BIT_MASK(32);
235 cmask = DMA_BIT_MASK(32);
237 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
238 #ifndef CONFIG_IA64
239 mask = DMA_BIT_MASK(35);
240 #endif
241 } else {
242 mask = DMA_BIT_MASK(39);
243 cmask = mask;
246 if (pci_set_dma_mask(pdev, mask) == 0 &&
247 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
248 adapter->pci_using_dac = 1;
249 return 0;
252 return -EIO;
255 /* Update addressable range if firmware supports it */
256 static int
257 nx_update_dma_mask(struct netxen_adapter *adapter)
259 int change, shift, err;
260 uint64_t mask, old_mask, old_cmask;
261 struct pci_dev *pdev = adapter->pdev;
263 change = 0;
265 shift = NXRD32(adapter, CRB_DMA_SHIFT);
266 if (shift > 32)
267 return 0;
269 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
270 change = 1;
271 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
272 change = 1;
274 if (change) {
275 old_mask = pdev->dma_mask;
276 old_cmask = pdev->dev.coherent_dma_mask;
278 mask = DMA_BIT_MASK(32+shift);
280 err = pci_set_dma_mask(pdev, mask);
281 if (err)
282 goto err_out;
284 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
286 err = pci_set_consistent_dma_mask(pdev, mask);
287 if (err)
288 goto err_out;
290 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
293 return 0;
295 err_out:
296 pci_set_dma_mask(pdev, old_mask);
297 pci_set_consistent_dma_mask(pdev, old_cmask);
298 return err;
301 static int
302 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
304 u32 val, timeout;
306 if (first_boot == 0x55555555) {
307 /* This is the first boot after power up */
308 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
310 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
311 return 0;
313 /* PCI bus master workaround */
314 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
315 if (!(first_boot & 0x4)) {
316 first_boot |= 0x4;
317 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
318 NXRD32(adapter, NETXEN_PCIE_REG(0x4));
321 /* This is the first boot after power up */
322 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
323 if (first_boot != 0x80000f) {
324 /* clear the register for future unloads/loads */
325 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
326 return -EIO;
329 /* Start P2 boot loader */
330 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
331 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
332 timeout = 0;
333 do {
334 msleep(1);
335 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
337 if (++timeout > 5000)
338 return -EIO;
340 } while (val == NETXEN_BDINFO_MAGIC);
342 return 0;
345 static void netxen_set_port_mode(struct netxen_adapter *adapter)
347 u32 val, data;
349 val = adapter->ahw.board_type;
350 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
351 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
352 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
353 data = NETXEN_PORT_MODE_802_3_AP;
354 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
355 } else if (port_mode == NETXEN_PORT_MODE_XG) {
356 data = NETXEN_PORT_MODE_XG;
357 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
358 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
359 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
360 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
361 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
362 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
363 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
364 } else {
365 data = NETXEN_PORT_MODE_AUTO_NEG;
366 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
369 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
370 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
371 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
372 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
373 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
375 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
379 #define PCI_CAP_ID_GEN 0x10
381 static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
383 u32 pdevfuncsave;
384 u32 c8c9value = 0;
385 u32 chicken = 0;
386 u32 control = 0;
387 int i, pos;
388 struct pci_dev *pdev;
390 pdev = adapter->pdev;
392 chicken = NXRD32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3));
393 /* clear chicken3.25:24 */
394 chicken &= 0xFCFFFFFF;
396 * if gen1 and B0, set F1020 - if gen 2, do nothing
397 * if gen2 set to F1000
399 pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
400 if (pos == 0xC0) {
401 pci_read_config_dword(pdev, pos + 0x10, &control);
402 if ((control & 0x000F0000) != 0x00020000) {
403 /* set chicken3.24 if gen1 */
404 chicken |= 0x01000000;
406 dev_info(&adapter->pdev->dev, "Gen2 strapping detected\n");
407 c8c9value = 0xF1000;
408 } else {
409 /* set chicken3.24 if gen1 */
410 chicken |= 0x01000000;
411 dev_info(&adapter->pdev->dev, "Gen1 strapping detected\n");
412 if (adapter->ahw.revision_id == NX_P3_B0)
413 c8c9value = 0xF1020;
414 else
415 c8c9value = 0;
418 NXWR32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3), chicken);
420 if (!c8c9value)
421 return;
423 pdevfuncsave = pdev->devfn;
424 if (pdevfuncsave & 0x07)
425 return;
427 for (i = 0; i < 8; i++) {
428 pci_read_config_dword(pdev, pos + 8, &control);
429 pci_read_config_dword(pdev, pos + 8, &control);
430 pci_write_config_dword(pdev, pos + 8, c8c9value);
431 pdev->devfn++;
433 pdev->devfn = pdevfuncsave;
436 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
438 u32 control;
440 if (pdev->msix_cap) {
441 pci_read_config_dword(pdev, pdev->msix_cap, &control);
442 if (enable)
443 control |= PCI_MSIX_FLAGS_ENABLE;
444 else
445 control = 0;
446 pci_write_config_dword(pdev, pdev->msix_cap, control);
450 static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
452 int i;
454 for (i = 0; i < count; i++)
455 adapter->msix_entries[i].entry = i;
458 static int
459 netxen_read_mac_addr(struct netxen_adapter *adapter)
461 int i;
462 unsigned char *p;
463 u64 mac_addr;
464 struct net_device *netdev = adapter->netdev;
465 struct pci_dev *pdev = adapter->pdev;
467 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
468 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
469 return -EIO;
470 } else {
471 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
472 return -EIO;
475 p = (unsigned char *)&mac_addr;
476 for (i = 0; i < 6; i++)
477 netdev->dev_addr[i] = *(p + 5 - i);
479 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
481 /* set station address */
483 if (!is_valid_ether_addr(netdev->dev_addr))
484 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
486 return 0;
489 static int netxen_nic_set_mac(struct net_device *netdev, void *p)
491 struct netxen_adapter *adapter = netdev_priv(netdev);
492 struct sockaddr *addr = p;
494 if (!is_valid_ether_addr(addr->sa_data))
495 return -EADDRNOTAVAIL;
497 if (netif_running(netdev)) {
498 netif_device_detach(netdev);
499 netxen_napi_disable(adapter);
502 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
503 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
504 adapter->macaddr_set(adapter, addr->sa_data);
506 if (netif_running(netdev)) {
507 netif_device_attach(netdev);
508 netxen_napi_enable(adapter);
510 return 0;
513 static void netxen_set_multicast_list(struct net_device *dev)
515 struct netxen_adapter *adapter = netdev_priv(dev);
517 adapter->set_multi(dev);
520 static netdev_features_t netxen_fix_features(struct net_device *dev,
521 netdev_features_t features)
523 if (!(features & NETIF_F_RXCSUM)) {
524 netdev_info(dev, "disabling LRO as RXCSUM is off\n");
526 features &= ~NETIF_F_LRO;
529 return features;
532 static int netxen_set_features(struct net_device *dev,
533 netdev_features_t features)
535 struct netxen_adapter *adapter = netdev_priv(dev);
536 int hw_lro;
538 if (!((dev->features ^ features) & NETIF_F_LRO))
539 return 0;
541 hw_lro = (features & NETIF_F_LRO) ? NETXEN_NIC_LRO_ENABLED
542 : NETXEN_NIC_LRO_DISABLED;
544 if (netxen_config_hw_lro(adapter, hw_lro))
545 return -EIO;
547 if (!(features & NETIF_F_LRO) && netxen_send_lro_cleanup(adapter))
548 return -EIO;
550 return 0;
553 static const struct net_device_ops netxen_netdev_ops = {
554 .ndo_open = netxen_nic_open,
555 .ndo_stop = netxen_nic_close,
556 .ndo_start_xmit = netxen_nic_xmit_frame,
557 .ndo_get_stats64 = netxen_nic_get_stats,
558 .ndo_validate_addr = eth_validate_addr,
559 .ndo_set_rx_mode = netxen_set_multicast_list,
560 .ndo_set_mac_address = netxen_nic_set_mac,
561 .ndo_change_mtu = netxen_nic_change_mtu,
562 .ndo_tx_timeout = netxen_tx_timeout,
563 .ndo_fix_features = netxen_fix_features,
564 .ndo_set_features = netxen_set_features,
567 static inline bool netxen_function_zero(struct pci_dev *pdev)
569 return (PCI_FUNC(pdev->devfn) == 0) ? true : false;
572 static inline void netxen_set_interrupt_mode(struct netxen_adapter *adapter,
573 u32 mode)
575 NXWR32(adapter, NETXEN_INTR_MODE_REG, mode);
578 static inline u32 netxen_get_interrupt_mode(struct netxen_adapter *adapter)
580 return NXRD32(adapter, NETXEN_INTR_MODE_REG);
583 static void
584 netxen_initialize_interrupt_registers(struct netxen_adapter *adapter)
586 struct netxen_legacy_intr_set *legacy_intrp;
587 u32 tgt_status_reg, int_state_reg;
589 if (adapter->ahw.revision_id >= NX_P3_B0)
590 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
591 else
592 legacy_intrp = &legacy_intr[0];
594 tgt_status_reg = legacy_intrp->tgt_status_reg;
595 int_state_reg = ISR_INT_STATE_REG;
597 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
598 adapter->tgt_status_reg = netxen_get_ioaddr(adapter, tgt_status_reg);
599 adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
600 legacy_intrp->tgt_mask_reg);
601 adapter->pci_int_reg = netxen_get_ioaddr(adapter,
602 legacy_intrp->pci_int_reg);
603 adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
605 if (adapter->ahw.revision_id >= NX_P3_B1)
606 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
607 int_state_reg);
608 else
609 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
610 CRB_INT_VECTOR);
613 static int netxen_setup_msi_interrupts(struct netxen_adapter *adapter,
614 int num_msix)
616 struct pci_dev *pdev = adapter->pdev;
617 u32 value;
618 int err;
620 if (adapter->msix_supported) {
621 netxen_init_msix_entries(adapter, num_msix);
622 err = pci_enable_msix_range(pdev, adapter->msix_entries,
623 num_msix, num_msix);
624 if (err > 0) {
625 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
626 netxen_set_msix_bit(pdev, 1);
628 if (adapter->rss_supported)
629 adapter->max_sds_rings = num_msix;
631 dev_info(&pdev->dev, "using msi-x interrupts\n");
632 return 0;
634 /* fall through for msi */
637 if (use_msi && !pci_enable_msi(pdev)) {
638 value = msi_tgt_status[adapter->ahw.pci_func];
639 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
640 adapter->tgt_status_reg = netxen_get_ioaddr(adapter, value);
641 adapter->msix_entries[0].vector = pdev->irq;
642 dev_info(&pdev->dev, "using msi interrupts\n");
643 return 0;
646 dev_err(&pdev->dev, "Failed to acquire MSI-X/MSI interrupt vector\n");
647 return -EIO;
650 static int netxen_setup_intr(struct netxen_adapter *adapter)
652 struct pci_dev *pdev = adapter->pdev;
653 int num_msix;
655 if (adapter->rss_supported)
656 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
657 MSIX_ENTRIES_PER_ADAPTER : 2;
658 else
659 num_msix = 1;
661 adapter->max_sds_rings = 1;
662 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
664 netxen_initialize_interrupt_registers(adapter);
665 netxen_set_msix_bit(pdev, 0);
667 if (netxen_function_zero(pdev)) {
668 if (!netxen_setup_msi_interrupts(adapter, num_msix))
669 netxen_set_interrupt_mode(adapter, NETXEN_MSI_MODE);
670 else
671 netxen_set_interrupt_mode(adapter, NETXEN_INTX_MODE);
672 } else {
673 if (netxen_get_interrupt_mode(adapter) == NETXEN_MSI_MODE &&
674 netxen_setup_msi_interrupts(adapter, num_msix)) {
675 dev_err(&pdev->dev, "Co-existence of MSI-X/MSI and INTx interrupts is not supported\n");
676 return -EIO;
680 if (!NETXEN_IS_MSI_FAMILY(adapter)) {
681 adapter->msix_entries[0].vector = pdev->irq;
682 dev_info(&pdev->dev, "using legacy interrupts\n");
684 return 0;
687 static void
688 netxen_teardown_intr(struct netxen_adapter *adapter)
690 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
691 pci_disable_msix(adapter->pdev);
692 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
693 pci_disable_msi(adapter->pdev);
696 static void
697 netxen_cleanup_pci_map(struct netxen_adapter *adapter)
699 if (adapter->ahw.db_base != NULL)
700 iounmap(adapter->ahw.db_base);
701 if (adapter->ahw.pci_base0 != NULL)
702 iounmap(adapter->ahw.pci_base0);
703 if (adapter->ahw.pci_base1 != NULL)
704 iounmap(adapter->ahw.pci_base1);
705 if (adapter->ahw.pci_base2 != NULL)
706 iounmap(adapter->ahw.pci_base2);
709 static int
710 netxen_setup_pci_map(struct netxen_adapter *adapter)
712 void __iomem *db_ptr = NULL;
714 resource_size_t mem_base, db_base;
715 unsigned long mem_len, db_len = 0;
717 struct pci_dev *pdev = adapter->pdev;
718 int pci_func = adapter->ahw.pci_func;
719 struct netxen_hardware_context *ahw = &adapter->ahw;
721 int err = 0;
724 * Set the CRB window to invalid. If any register in window 0 is
725 * accessed it should set the window to 0 and then reset it to 1.
727 adapter->ahw.crb_win = -1;
728 adapter->ahw.ocm_win = -1;
730 /* remap phys address */
731 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
732 mem_len = pci_resource_len(pdev, 0);
734 /* 128 Meg of memory */
735 if (mem_len == NETXEN_PCI_128MB_SIZE) {
737 ahw->pci_base0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
738 ahw->pci_base1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
739 SECOND_PAGE_GROUP_SIZE);
740 ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
741 THIRD_PAGE_GROUP_SIZE);
742 if (ahw->pci_base0 == NULL || ahw->pci_base1 == NULL ||
743 ahw->pci_base2 == NULL) {
744 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
745 err = -EIO;
746 goto err_out;
749 ahw->pci_len0 = FIRST_PAGE_GROUP_SIZE;
751 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
753 ahw->pci_base1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
754 ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
755 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
756 if (ahw->pci_base1 == NULL || ahw->pci_base2 == NULL) {
757 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
758 err = -EIO;
759 goto err_out;
762 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
764 ahw->pci_base0 = pci_ioremap_bar(pdev, 0);
765 if (ahw->pci_base0 == NULL) {
766 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
767 return -EIO;
769 ahw->pci_len0 = mem_len;
770 } else {
771 return -EIO;
774 netxen_setup_hwops(adapter);
776 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
778 if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
779 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
780 NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
782 } else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
783 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
784 NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func)));
787 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
788 goto skip_doorbell;
790 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
791 db_len = pci_resource_len(pdev, 4);
793 if (db_len == 0) {
794 printk(KERN_ERR "%s: doorbell is disabled\n",
795 netxen_nic_driver_name);
796 err = -EIO;
797 goto err_out;
800 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
801 if (!db_ptr) {
802 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
803 netxen_nic_driver_name);
804 err = -EIO;
805 goto err_out;
808 skip_doorbell:
809 adapter->ahw.db_base = db_ptr;
810 adapter->ahw.db_len = db_len;
811 return 0;
813 err_out:
814 netxen_cleanup_pci_map(adapter);
815 return err;
818 static void
819 netxen_check_options(struct netxen_adapter *adapter)
821 u32 fw_major, fw_minor, fw_build, prev_fw_version;
822 char brd_name[NETXEN_MAX_SHORT_NAME];
823 char serial_num[32];
824 int i, offset, val, err;
825 __le32 *ptr32;
826 struct pci_dev *pdev = adapter->pdev;
828 adapter->driver_mismatch = 0;
830 ptr32 = (__le32 *)&serial_num;
831 offset = NX_FW_SERIAL_NUM_OFFSET;
832 for (i = 0; i < 8; i++) {
833 err = netxen_rom_fast_read(adapter, offset, &val);
834 if (err) {
835 dev_err(&pdev->dev, "error reading board info\n");
836 adapter->driver_mismatch = 1;
837 return;
839 ptr32[i] = cpu_to_le32(val);
840 offset += sizeof(u32);
843 fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
844 fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
845 fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
846 prev_fw_version = adapter->fw_version;
847 adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
849 /* Get FW Mini Coredump template and store it */
850 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
851 if (adapter->mdump.md_template == NULL ||
852 adapter->fw_version > prev_fw_version) {
853 kfree(adapter->mdump.md_template);
854 adapter->mdump.md_template = NULL;
855 err = netxen_setup_minidump(adapter);
856 if (err)
857 dev_err(&adapter->pdev->dev,
858 "Failed to setup minidump rcode = %d\n", err);
862 if (adapter->portnum == 0) {
863 if (netxen_nic_get_brd_name_by_type(adapter->ahw.board_type,
864 brd_name))
865 strcpy(serial_num, "Unknown");
867 pr_info("%s: %s Board S/N %s Chip rev 0x%x\n",
868 module_name(THIS_MODULE),
869 brd_name, serial_num, adapter->ahw.revision_id);
872 if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
873 adapter->driver_mismatch = 1;
874 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
875 fw_major, fw_minor, fw_build);
876 return;
879 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
880 i = NXRD32(adapter, NETXEN_SRE_MISC);
881 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
884 dev_info(&pdev->dev, "Driver v%s, firmware v%d.%d.%d [%s]\n",
885 NETXEN_NIC_LINUX_VERSIONID, fw_major, fw_minor, fw_build,
886 adapter->ahw.cut_through ? "cut-through" : "legacy");
888 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
889 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
891 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
892 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
893 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
894 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
895 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
896 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
899 adapter->msix_supported = 0;
900 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
901 adapter->msix_supported = !!use_msi_x;
902 adapter->rss_supported = !!use_msi_x;
903 } else {
904 u32 flashed_ver = 0;
905 netxen_rom_fast_read(adapter,
906 NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
907 flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
909 if (flashed_ver >= NETXEN_VERSION_CODE(3, 4, 336)) {
910 switch (adapter->ahw.board_type) {
911 case NETXEN_BRDTYPE_P2_SB31_10G:
912 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
913 adapter->msix_supported = !!use_msi_x;
914 adapter->rss_supported = !!use_msi_x;
915 break;
916 default:
917 break;
922 adapter->num_txd = MAX_CMD_DESCRIPTORS;
924 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
925 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
926 adapter->max_rds_rings = 3;
927 } else {
928 adapter->num_lro_rxd = 0;
929 adapter->max_rds_rings = 2;
933 static int
934 netxen_start_firmware(struct netxen_adapter *adapter)
936 int val, err, first_boot;
937 struct pci_dev *pdev = adapter->pdev;
939 /* required for NX2031 dummy dma */
940 err = nx_set_dma_mask(adapter);
941 if (err)
942 return err;
944 err = netxen_can_start_firmware(adapter);
946 if (err < 0)
947 return err;
949 if (!err)
950 goto wait_init;
952 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
954 err = netxen_check_hw_init(adapter, first_boot);
955 if (err) {
956 dev_err(&pdev->dev, "error in init HW init sequence\n");
957 return err;
960 netxen_request_firmware(adapter);
962 err = netxen_need_fw_reset(adapter);
963 if (err < 0)
964 goto err_out;
965 if (err == 0)
966 goto pcie_strap_init;
968 if (first_boot != 0x55555555) {
969 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
970 netxen_pinit_from_rom(adapter);
971 msleep(1);
974 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
975 NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
976 NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
978 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
979 netxen_set_port_mode(adapter);
981 err = netxen_load_firmware(adapter);
982 if (err)
983 goto err_out;
985 netxen_release_firmware(adapter);
987 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
989 /* Initialize multicast addr pool owners */
990 val = 0x7654;
991 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
992 val |= 0x0f000000;
993 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
997 err = netxen_init_dummy_dma(adapter);
998 if (err)
999 goto err_out;
1002 * Tell the hardware our version number.
1004 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
1005 | ((_NETXEN_NIC_LINUX_MINOR << 8))
1006 | (_NETXEN_NIC_LINUX_SUBVERSION);
1007 NXWR32(adapter, CRB_DRIVER_VERSION, val);
1009 pcie_strap_init:
1010 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1011 netxen_pcie_strap_init(adapter);
1013 wait_init:
1014 /* Handshake with the card before we register the devices. */
1015 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
1016 if (err) {
1017 netxen_free_dummy_dma(adapter);
1018 goto err_out;
1021 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
1023 nx_update_dma_mask(adapter);
1025 netxen_check_options(adapter);
1027 adapter->need_fw_reset = 0;
1029 /* fall through and release firmware */
1031 err_out:
1032 netxen_release_firmware(adapter);
1033 return err;
1036 static int
1037 netxen_nic_request_irq(struct netxen_adapter *adapter)
1039 irq_handler_t handler;
1040 struct nx_host_sds_ring *sds_ring;
1041 int err, ring;
1043 unsigned long flags = 0;
1044 struct net_device *netdev = adapter->netdev;
1045 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1047 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1048 handler = netxen_msix_intr;
1049 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
1050 handler = netxen_msi_intr;
1051 else {
1052 flags |= IRQF_SHARED;
1053 handler = netxen_intr;
1055 adapter->irq = netdev->irq;
1057 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1058 sds_ring = &recv_ctx->sds_rings[ring];
1059 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1060 err = request_irq(sds_ring->irq, handler,
1061 flags, sds_ring->name, sds_ring);
1062 if (err)
1063 return err;
1066 return 0;
1069 static void
1070 netxen_nic_free_irq(struct netxen_adapter *adapter)
1072 int ring;
1073 struct nx_host_sds_ring *sds_ring;
1075 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1077 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1078 sds_ring = &recv_ctx->sds_rings[ring];
1079 free_irq(sds_ring->irq, sds_ring);
1083 static void
1084 netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
1086 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
1087 adapter->coal.normal.data.rx_time_us =
1088 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
1089 adapter->coal.normal.data.rx_packets =
1090 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
1091 adapter->coal.normal.data.tx_time_us =
1092 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
1093 adapter->coal.normal.data.tx_packets =
1094 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
1097 /* with rtnl_lock */
1098 static int
1099 __netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1101 int err;
1103 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1104 return -EIO;
1106 err = adapter->init_port(adapter, adapter->physical_port);
1107 if (err) {
1108 printk(KERN_ERR "%s: Failed to initialize port %d\n",
1109 netxen_nic_driver_name, adapter->portnum);
1110 return err;
1112 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
1113 adapter->macaddr_set(adapter, adapter->mac_addr);
1115 adapter->set_multi(netdev);
1116 adapter->set_mtu(adapter, netdev->mtu);
1118 adapter->ahw.linkup = 0;
1120 if (adapter->max_sds_rings > 1)
1121 netxen_config_rss(adapter, 1);
1123 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1124 netxen_config_intr_coalesce(adapter);
1126 if (netdev->features & NETIF_F_LRO)
1127 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
1129 netxen_napi_enable(adapter);
1131 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1132 netxen_linkevent_request(adapter, 1);
1133 else
1134 netxen_nic_set_link_parameters(adapter);
1136 set_bit(__NX_DEV_UP, &adapter->state);
1137 return 0;
1140 /* Usage: During resume and firmware recovery module.*/
1142 static inline int
1143 netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1145 int err = 0;
1147 rtnl_lock();
1148 if (netif_running(netdev))
1149 err = __netxen_nic_up(adapter, netdev);
1150 rtnl_unlock();
1152 return err;
1155 /* with rtnl_lock */
1156 static void
1157 __netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1159 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1160 return;
1162 if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state))
1163 return;
1165 smp_mb();
1166 netif_carrier_off(netdev);
1167 netif_tx_disable(netdev);
1169 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1170 netxen_linkevent_request(adapter, 0);
1172 if (adapter->stop_port)
1173 adapter->stop_port(adapter);
1175 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1176 netxen_p3_free_mac_list(adapter);
1178 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1180 netxen_napi_disable(adapter);
1182 netxen_release_tx_buffers(adapter);
1185 /* Usage: During suspend and firmware recovery module */
1187 static inline void
1188 netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1190 rtnl_lock();
1191 if (netif_running(netdev))
1192 __netxen_nic_down(adapter, netdev);
1193 rtnl_unlock();
1197 static int
1198 netxen_nic_attach(struct netxen_adapter *adapter)
1200 struct net_device *netdev = adapter->netdev;
1201 struct pci_dev *pdev = adapter->pdev;
1202 int err, ring;
1203 struct nx_host_rds_ring *rds_ring;
1204 struct nx_host_tx_ring *tx_ring;
1205 u32 capab2;
1207 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1208 return 0;
1210 err = netxen_init_firmware(adapter);
1211 if (err)
1212 return err;
1214 adapter->flags &= ~NETXEN_FW_MSS_CAP;
1215 if (adapter->capabilities & NX_FW_CAPABILITY_MORE_CAPS) {
1216 capab2 = NXRD32(adapter, CRB_FW_CAPABILITIES_2);
1217 if (capab2 & NX_FW_CAPABILITY_2_LRO_MAX_TCP_SEG)
1218 adapter->flags |= NETXEN_FW_MSS_CAP;
1221 err = netxen_napi_add(adapter, netdev);
1222 if (err)
1223 return err;
1225 err = netxen_alloc_sw_resources(adapter);
1226 if (err) {
1227 printk(KERN_ERR "%s: Error in setting sw resources\n",
1228 netdev->name);
1229 return err;
1232 err = netxen_alloc_hw_resources(adapter);
1233 if (err) {
1234 printk(KERN_ERR "%s: Error in setting hw resources\n",
1235 netdev->name);
1236 goto err_out_free_sw;
1239 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1240 tx_ring = adapter->tx_ring;
1241 tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1242 crb_cmd_producer[adapter->portnum]);
1243 tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1244 crb_cmd_consumer[adapter->portnum]);
1246 tx_ring->producer = 0;
1247 tx_ring->sw_consumer = 0;
1249 netxen_nic_update_cmd_producer(adapter, tx_ring);
1250 netxen_nic_update_cmd_consumer(adapter, tx_ring);
1253 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1254 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1255 netxen_post_rx_buffers(adapter, ring, rds_ring);
1258 err = netxen_nic_request_irq(adapter);
1259 if (err) {
1260 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1261 netdev->name);
1262 goto err_out_free_rxbuf;
1265 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1266 netxen_nic_init_coalesce_defaults(adapter);
1268 netxen_create_sysfs_entries(adapter);
1270 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1271 return 0;
1273 err_out_free_rxbuf:
1274 netxen_release_rx_buffers(adapter);
1275 netxen_free_hw_resources(adapter);
1276 err_out_free_sw:
1277 netxen_free_sw_resources(adapter);
1278 return err;
1281 static void
1282 netxen_nic_detach(struct netxen_adapter *adapter)
1284 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1285 return;
1287 netxen_remove_sysfs_entries(adapter);
1289 netxen_free_hw_resources(adapter);
1290 netxen_release_rx_buffers(adapter);
1291 netxen_nic_free_irq(adapter);
1292 netxen_napi_del(adapter);
1293 netxen_free_sw_resources(adapter);
1295 adapter->is_up = 0;
1299 netxen_nic_reset_context(struct netxen_adapter *adapter)
1301 int err = 0;
1302 struct net_device *netdev = adapter->netdev;
1304 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1305 return -EBUSY;
1307 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1309 netif_device_detach(netdev);
1311 if (netif_running(netdev))
1312 __netxen_nic_down(adapter, netdev);
1314 netxen_nic_detach(adapter);
1316 if (netif_running(netdev)) {
1317 err = netxen_nic_attach(adapter);
1318 if (!err)
1319 err = __netxen_nic_up(adapter, netdev);
1321 if (err)
1322 goto done;
1325 netif_device_attach(netdev);
1328 done:
1329 clear_bit(__NX_RESETTING, &adapter->state);
1330 return err;
1333 static int
1334 netxen_setup_netdev(struct netxen_adapter *adapter,
1335 struct net_device *netdev)
1337 int err = 0;
1338 struct pci_dev *pdev = adapter->pdev;
1340 adapter->mc_enabled = 0;
1341 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1342 adapter->max_mc_count = 38;
1343 else
1344 adapter->max_mc_count = 16;
1346 netdev->netdev_ops = &netxen_netdev_ops;
1347 netdev->watchdog_timeo = 5*HZ;
1349 netxen_nic_change_mtu(netdev, netdev->mtu);
1351 netdev->ethtool_ops = &netxen_nic_ethtool_ops;
1353 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
1354 NETIF_F_RXCSUM;
1356 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1357 netdev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
1359 netdev->vlan_features |= netdev->hw_features;
1361 if (adapter->pci_using_dac) {
1362 netdev->features |= NETIF_F_HIGHDMA;
1363 netdev->vlan_features |= NETIF_F_HIGHDMA;
1366 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1367 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
1369 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1370 netdev->hw_features |= NETIF_F_LRO;
1372 netdev->features |= netdev->hw_features;
1374 netdev->irq = adapter->msix_entries[0].vector;
1376 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1378 if (netxen_read_mac_addr(adapter))
1379 dev_warn(&pdev->dev, "failed to read mac addr\n");
1381 netif_carrier_off(netdev);
1383 err = register_netdev(netdev);
1384 if (err) {
1385 dev_err(&pdev->dev, "failed to register net device\n");
1386 return err;
1389 return 0;
1392 #define NETXEN_ULA_ADAPTER_KEY (0xdaddad01)
1393 #define NETXEN_NON_ULA_ADAPTER_KEY (0xdaddad00)
1395 static void netxen_read_ula_info(struct netxen_adapter *adapter)
1397 u32 temp;
1399 /* Print ULA info only once for an adapter */
1400 if (adapter->portnum != 0)
1401 return;
1403 temp = NXRD32(adapter, NETXEN_ULA_KEY);
1404 switch (temp) {
1405 case NETXEN_ULA_ADAPTER_KEY:
1406 dev_info(&adapter->pdev->dev, "ULA adapter");
1407 break;
1408 case NETXEN_NON_ULA_ADAPTER_KEY:
1409 dev_info(&adapter->pdev->dev, "non ULA adapter");
1410 break;
1411 default:
1412 break;
1415 return;
1418 #ifdef CONFIG_PCIEAER
1419 static void netxen_mask_aer_correctable(struct netxen_adapter *adapter)
1421 struct pci_dev *pdev = adapter->pdev;
1422 struct pci_dev *root = pdev->bus->self;
1423 u32 aer_pos;
1425 /* root bus? */
1426 if (!root)
1427 return;
1429 if (adapter->ahw.board_type != NETXEN_BRDTYPE_P3_4_GB_MM &&
1430 adapter->ahw.board_type != NETXEN_BRDTYPE_P3_10G_TP)
1431 return;
1433 if (pci_pcie_type(root) != PCI_EXP_TYPE_ROOT_PORT)
1434 return;
1436 aer_pos = pci_find_ext_capability(root, PCI_EXT_CAP_ID_ERR);
1437 if (!aer_pos)
1438 return;
1440 pci_write_config_dword(root, aer_pos + PCI_ERR_COR_MASK, 0xffff);
1442 #endif
1444 static int
1445 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1447 struct net_device *netdev = NULL;
1448 struct netxen_adapter *adapter = NULL;
1449 int i = 0, err;
1450 int pci_func_id = PCI_FUNC(pdev->devfn);
1451 uint8_t revision_id;
1452 u32 val;
1454 if (pdev->revision >= NX_P3_A0 && pdev->revision <= NX_P3_B1) {
1455 pr_warn("%s: chip revisions between 0x%x-0x%x will not be enabled\n",
1456 module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
1457 return -ENODEV;
1460 if ((err = pci_enable_device(pdev)))
1461 return err;
1463 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1464 err = -ENODEV;
1465 goto err_out_disable_pdev;
1468 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1469 goto err_out_disable_pdev;
1471 if (NX_IS_REVISION_P3(pdev->revision))
1472 pci_enable_pcie_error_reporting(pdev);
1474 pci_set_master(pdev);
1476 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1477 if(!netdev) {
1478 err = -ENOMEM;
1479 goto err_out_free_res;
1482 SET_NETDEV_DEV(netdev, &pdev->dev);
1484 adapter = netdev_priv(netdev);
1485 adapter->netdev = netdev;
1486 adapter->pdev = pdev;
1487 adapter->ahw.pci_func = pci_func_id;
1489 revision_id = pdev->revision;
1490 adapter->ahw.revision_id = revision_id;
1492 rwlock_init(&adapter->ahw.crb_lock);
1493 spin_lock_init(&adapter->ahw.mem_lock);
1495 spin_lock_init(&adapter->tx_clean_lock);
1496 INIT_LIST_HEAD(&adapter->mac_list);
1497 INIT_LIST_HEAD(&adapter->ip_list);
1499 err = netxen_setup_pci_map(adapter);
1500 if (err)
1501 goto err_out_free_netdev;
1503 /* This will be reset for mezz cards */
1504 adapter->portnum = pci_func_id;
1506 err = netxen_nic_get_board_info(adapter);
1507 if (err) {
1508 dev_err(&pdev->dev, "Error getting board config info.\n");
1509 goto err_out_iounmap;
1512 #ifdef CONFIG_PCIEAER
1513 netxen_mask_aer_correctable(adapter);
1514 #endif
1516 /* Mezz cards have PCI function 0,2,3 enabled */
1517 switch (adapter->ahw.board_type) {
1518 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1519 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1520 if (pci_func_id >= 2)
1521 adapter->portnum = pci_func_id - 2;
1522 break;
1523 default:
1524 break;
1527 err = netxen_check_flash_fw_compatibility(adapter);
1528 if (err)
1529 goto err_out_iounmap;
1531 if (adapter->portnum == 0) {
1532 val = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
1533 if (val != 0xffffffff && val != 0) {
1534 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, 0);
1535 adapter->need_fw_reset = 1;
1539 err = netxen_start_firmware(adapter);
1540 if (err)
1541 goto err_out_decr_ref;
1544 * See if the firmware gave us a virtual-physical port mapping.
1546 adapter->physical_port = adapter->portnum;
1547 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1548 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
1549 if (i != 0x55555555)
1550 adapter->physical_port = i;
1553 /* MTU range: 0 - 8000 (P2) or 9600 (P3) */
1554 netdev->min_mtu = 0;
1555 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1556 netdev->max_mtu = P3_MAX_MTU;
1557 else
1558 netdev->max_mtu = P2_MAX_MTU;
1560 netxen_nic_clear_stats(adapter);
1562 err = netxen_setup_intr(adapter);
1564 if (err) {
1565 dev_err(&adapter->pdev->dev,
1566 "Failed to setup interrupts, error = %d\n", err);
1567 goto err_out_disable_msi;
1570 netxen_read_ula_info(adapter);
1572 err = netxen_setup_netdev(adapter, netdev);
1573 if (err)
1574 goto err_out_disable_msi;
1576 pci_set_drvdata(pdev, adapter);
1578 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1580 switch (adapter->ahw.port_type) {
1581 case NETXEN_NIC_GBE:
1582 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1583 adapter->netdev->name);
1584 break;
1585 case NETXEN_NIC_XGBE:
1586 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1587 adapter->netdev->name);
1588 break;
1591 netxen_create_diag_entries(adapter);
1593 return 0;
1595 err_out_disable_msi:
1596 netxen_teardown_intr(adapter);
1598 netxen_free_dummy_dma(adapter);
1600 err_out_decr_ref:
1601 nx_decr_dev_ref_cnt(adapter);
1603 err_out_iounmap:
1604 netxen_cleanup_pci_map(adapter);
1606 err_out_free_netdev:
1607 free_netdev(netdev);
1609 err_out_free_res:
1610 pci_release_regions(pdev);
1612 err_out_disable_pdev:
1613 pci_disable_device(pdev);
1614 return err;
1617 static
1618 void netxen_cleanup_minidump(struct netxen_adapter *adapter)
1620 kfree(adapter->mdump.md_template);
1621 adapter->mdump.md_template = NULL;
1623 if (adapter->mdump.md_capture_buff) {
1624 vfree(adapter->mdump.md_capture_buff);
1625 adapter->mdump.md_capture_buff = NULL;
1629 static void netxen_nic_remove(struct pci_dev *pdev)
1631 struct netxen_adapter *adapter;
1632 struct net_device *netdev;
1634 adapter = pci_get_drvdata(pdev);
1635 if (adapter == NULL)
1636 return;
1638 netdev = adapter->netdev;
1640 netxen_cancel_fw_work(adapter);
1642 unregister_netdev(netdev);
1644 cancel_work_sync(&adapter->tx_timeout_task);
1646 netxen_free_ip_list(adapter, false);
1647 netxen_nic_detach(adapter);
1649 nx_decr_dev_ref_cnt(adapter);
1651 if (adapter->portnum == 0)
1652 netxen_free_dummy_dma(adapter);
1654 clear_bit(__NX_RESETTING, &adapter->state);
1656 netxen_teardown_intr(adapter);
1657 netxen_set_interrupt_mode(adapter, 0);
1658 netxen_remove_diag_entries(adapter);
1660 netxen_cleanup_pci_map(adapter);
1662 netxen_release_firmware(adapter);
1664 if (NX_IS_REVISION_P3(pdev->revision)) {
1665 netxen_cleanup_minidump(adapter);
1666 pci_disable_pcie_error_reporting(pdev);
1669 pci_release_regions(pdev);
1670 pci_disable_device(pdev);
1672 free_netdev(netdev);
1675 static void netxen_nic_detach_func(struct netxen_adapter *adapter)
1677 struct net_device *netdev = adapter->netdev;
1679 netif_device_detach(netdev);
1681 netxen_cancel_fw_work(adapter);
1683 if (netif_running(netdev))
1684 netxen_nic_down(adapter, netdev);
1686 cancel_work_sync(&adapter->tx_timeout_task);
1688 netxen_nic_detach(adapter);
1690 if (adapter->portnum == 0)
1691 netxen_free_dummy_dma(adapter);
1693 nx_decr_dev_ref_cnt(adapter);
1695 clear_bit(__NX_RESETTING, &adapter->state);
1698 static int netxen_nic_attach_late_func(struct pci_dev *pdev)
1700 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1701 struct net_device *netdev = adapter->netdev;
1702 int err;
1704 pci_set_master(pdev);
1706 adapter->ahw.crb_win = -1;
1707 adapter->ahw.ocm_win = -1;
1709 err = netxen_start_firmware(adapter);
1710 if (err) {
1711 dev_err(&pdev->dev, "failed to start firmware\n");
1712 return err;
1715 if (netif_running(netdev)) {
1716 err = netxen_nic_attach(adapter);
1717 if (err)
1718 goto err_out;
1720 err = netxen_nic_up(adapter, netdev);
1721 if (err)
1722 goto err_out_detach;
1724 netxen_restore_indev_addr(netdev, NETDEV_UP);
1727 netif_device_attach(netdev);
1728 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1729 return 0;
1731 err_out_detach:
1732 netxen_nic_detach(adapter);
1733 err_out:
1734 nx_decr_dev_ref_cnt(adapter);
1735 return err;
1738 static int netxen_nic_attach_func(struct pci_dev *pdev)
1740 int err;
1742 err = pci_enable_device(pdev);
1743 if (err)
1744 return err;
1746 pci_set_power_state(pdev, PCI_D0);
1747 pci_restore_state(pdev);
1749 return netxen_nic_attach_late_func(pdev);
1752 static pci_ers_result_t netxen_io_error_detected(struct pci_dev *pdev,
1753 pci_channel_state_t state)
1755 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1757 if (state == pci_channel_io_perm_failure)
1758 return PCI_ERS_RESULT_DISCONNECT;
1760 if (nx_dev_request_aer(adapter))
1761 return PCI_ERS_RESULT_RECOVERED;
1763 netxen_nic_detach_func(adapter);
1765 pci_disable_device(pdev);
1767 return PCI_ERS_RESULT_NEED_RESET;
1770 static pci_ers_result_t netxen_io_slot_reset(struct pci_dev *pdev)
1772 int err = 0;
1774 err = netxen_nic_attach_func(pdev);
1776 return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
1779 static void netxen_nic_shutdown(struct pci_dev *pdev)
1781 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1783 netxen_nic_detach_func(adapter);
1785 if (pci_save_state(pdev))
1786 return;
1788 if (netxen_nic_wol_supported(adapter)) {
1789 pci_enable_wake(pdev, PCI_D3cold, 1);
1790 pci_enable_wake(pdev, PCI_D3hot, 1);
1793 pci_disable_device(pdev);
1796 static int __maybe_unused
1797 netxen_nic_suspend(struct device *dev_d)
1799 struct netxen_adapter *adapter = dev_get_drvdata(dev_d);
1801 netxen_nic_detach_func(adapter);
1803 if (netxen_nic_wol_supported(adapter))
1804 device_wakeup_enable(dev_d);
1806 return 0;
1809 static int __maybe_unused
1810 netxen_nic_resume(struct device *dev_d)
1812 return netxen_nic_attach_late_func(to_pci_dev(dev_d));
1815 static int netxen_nic_open(struct net_device *netdev)
1817 struct netxen_adapter *adapter = netdev_priv(netdev);
1818 int err = 0;
1820 if (adapter->driver_mismatch)
1821 return -EIO;
1823 err = netxen_nic_attach(adapter);
1824 if (err)
1825 return err;
1827 err = __netxen_nic_up(adapter, netdev);
1828 if (err)
1829 goto err_out;
1831 netif_start_queue(netdev);
1833 return 0;
1835 err_out:
1836 netxen_nic_detach(adapter);
1837 return err;
1841 * netxen_nic_close - Disables a network interface entry point
1843 static int netxen_nic_close(struct net_device *netdev)
1845 struct netxen_adapter *adapter = netdev_priv(netdev);
1847 __netxen_nic_down(adapter, netdev);
1848 return 0;
1851 static void
1852 netxen_tso_check(struct net_device *netdev,
1853 struct nx_host_tx_ring *tx_ring,
1854 struct cmd_desc_type0 *first_desc,
1855 struct sk_buff *skb)
1857 u8 opcode = TX_ETHER_PKT;
1858 __be16 protocol = skb->protocol;
1859 u16 flags = 0, vid = 0;
1860 u32 producer;
1861 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1862 struct cmd_desc_type0 *hwdesc;
1863 struct vlan_ethhdr *vh;
1865 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1867 vh = (struct vlan_ethhdr *)skb->data;
1868 protocol = vh->h_vlan_encapsulated_proto;
1869 flags = FLAGS_VLAN_TAGGED;
1871 } else if (skb_vlan_tag_present(skb)) {
1872 flags = FLAGS_VLAN_OOB;
1873 vid = skb_vlan_tag_get(skb);
1874 netxen_set_tx_vlan_tci(first_desc, vid);
1875 vlan_oob = 1;
1878 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1879 skb_shinfo(skb)->gso_size > 0) {
1881 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1883 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1884 first_desc->total_hdr_length = hdr_len;
1885 if (vlan_oob) {
1886 first_desc->total_hdr_length += VLAN_HLEN;
1887 first_desc->tcp_hdr_offset = VLAN_HLEN;
1888 first_desc->ip_hdr_offset = VLAN_HLEN;
1889 /* Only in case of TSO on vlan device */
1890 flags |= FLAGS_VLAN_TAGGED;
1893 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1894 TX_TCP_LSO6 : TX_TCP_LSO;
1895 tso = 1;
1897 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1898 u8 l4proto;
1900 if (protocol == cpu_to_be16(ETH_P_IP)) {
1901 l4proto = ip_hdr(skb)->protocol;
1903 if (l4proto == IPPROTO_TCP)
1904 opcode = TX_TCP_PKT;
1905 else if(l4proto == IPPROTO_UDP)
1906 opcode = TX_UDP_PKT;
1907 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1908 l4proto = ipv6_hdr(skb)->nexthdr;
1910 if (l4proto == IPPROTO_TCP)
1911 opcode = TX_TCPV6_PKT;
1912 else if(l4proto == IPPROTO_UDP)
1913 opcode = TX_UDPV6_PKT;
1917 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1918 first_desc->ip_hdr_offset += skb_network_offset(skb);
1919 netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1921 if (!tso)
1922 return;
1924 /* For LSO, we need to copy the MAC/IP/TCP headers into
1925 * the descriptor ring
1927 producer = tx_ring->producer;
1928 copied = 0;
1929 offset = 2;
1931 if (vlan_oob) {
1932 /* Create a TSO vlan header template for firmware */
1934 hwdesc = &tx_ring->desc_head[producer];
1935 tx_ring->cmd_buf_arr[producer].skb = NULL;
1937 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1938 hdr_len + VLAN_HLEN);
1940 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1941 skb_copy_from_linear_data(skb, vh, 12);
1942 vh->h_vlan_proto = htons(ETH_P_8021Q);
1943 vh->h_vlan_TCI = htons(vid);
1944 skb_copy_from_linear_data_offset(skb, 12,
1945 (char *)vh + 16, copy_len - 16);
1947 copied = copy_len - VLAN_HLEN;
1948 offset = 0;
1950 producer = get_next_index(producer, tx_ring->num_desc);
1953 while (copied < hdr_len) {
1955 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1956 (hdr_len - copied));
1958 hwdesc = &tx_ring->desc_head[producer];
1959 tx_ring->cmd_buf_arr[producer].skb = NULL;
1961 skb_copy_from_linear_data_offset(skb, copied,
1962 (char *)hwdesc + offset, copy_len);
1964 copied += copy_len;
1965 offset = 0;
1967 producer = get_next_index(producer, tx_ring->num_desc);
1970 tx_ring->producer = producer;
1971 barrier();
1974 static int
1975 netxen_map_tx_skb(struct pci_dev *pdev,
1976 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
1978 struct netxen_skb_frag *nf;
1979 skb_frag_t *frag;
1980 int i, nr_frags;
1981 dma_addr_t map;
1983 nr_frags = skb_shinfo(skb)->nr_frags;
1984 nf = &pbuf->frag_array[0];
1986 map = pci_map_single(pdev, skb->data,
1987 skb_headlen(skb), PCI_DMA_TODEVICE);
1988 if (pci_dma_mapping_error(pdev, map))
1989 goto out_err;
1991 nf->dma = map;
1992 nf->length = skb_headlen(skb);
1994 for (i = 0; i < nr_frags; i++) {
1995 frag = &skb_shinfo(skb)->frags[i];
1996 nf = &pbuf->frag_array[i+1];
1998 map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
1999 DMA_TO_DEVICE);
2000 if (dma_mapping_error(&pdev->dev, map))
2001 goto unwind;
2003 nf->dma = map;
2004 nf->length = skb_frag_size(frag);
2007 return 0;
2009 unwind:
2010 while (--i >= 0) {
2011 nf = &pbuf->frag_array[i+1];
2012 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2013 nf->dma = 0ULL;
2016 nf = &pbuf->frag_array[0];
2017 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2018 nf->dma = 0ULL;
2020 out_err:
2021 return -ENOMEM;
2024 static inline void
2025 netxen_clear_cmddesc(u64 *desc)
2027 desc[0] = 0ULL;
2028 desc[2] = 0ULL;
2031 static netdev_tx_t
2032 netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2034 struct netxen_adapter *adapter = netdev_priv(netdev);
2035 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
2036 struct netxen_cmd_buffer *pbuf;
2037 struct netxen_skb_frag *buffrag;
2038 struct cmd_desc_type0 *hwdesc, *first_desc;
2039 struct pci_dev *pdev;
2040 int i, k;
2041 int delta = 0;
2042 skb_frag_t *frag;
2044 u32 producer;
2045 int frag_count;
2046 u32 num_txd = tx_ring->num_desc;
2048 frag_count = skb_shinfo(skb)->nr_frags + 1;
2050 /* 14 frags supported for normal packet and
2051 * 32 frags supported for TSO packet
2053 if (!skb_is_gso(skb) && frag_count > NETXEN_MAX_FRAGS_PER_TX) {
2055 for (i = 0; i < (frag_count - NETXEN_MAX_FRAGS_PER_TX); i++) {
2056 frag = &skb_shinfo(skb)->frags[i];
2057 delta += skb_frag_size(frag);
2060 if (!__pskb_pull_tail(skb, delta))
2061 goto drop_packet;
2063 frag_count = 1 + skb_shinfo(skb)->nr_frags;
2066 if (unlikely(netxen_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
2067 netif_stop_queue(netdev);
2068 smp_mb();
2069 if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH)
2070 netif_start_queue(netdev);
2071 else
2072 return NETDEV_TX_BUSY;
2075 producer = tx_ring->producer;
2076 pbuf = &tx_ring->cmd_buf_arr[producer];
2078 pdev = adapter->pdev;
2080 if (netxen_map_tx_skb(pdev, skb, pbuf))
2081 goto drop_packet;
2083 pbuf->skb = skb;
2084 pbuf->frag_count = frag_count;
2086 first_desc = hwdesc = &tx_ring->desc_head[producer];
2087 netxen_clear_cmddesc((u64 *)hwdesc);
2089 netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
2090 netxen_set_tx_port(first_desc, adapter->portnum);
2092 for (i = 0; i < frag_count; i++) {
2094 k = i % 4;
2096 if ((k == 0) && (i > 0)) {
2097 /* move to next desc.*/
2098 producer = get_next_index(producer, num_txd);
2099 hwdesc = &tx_ring->desc_head[producer];
2100 netxen_clear_cmddesc((u64 *)hwdesc);
2101 tx_ring->cmd_buf_arr[producer].skb = NULL;
2104 buffrag = &pbuf->frag_array[i];
2106 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2107 switch (k) {
2108 case 0:
2109 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2110 break;
2111 case 1:
2112 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2113 break;
2114 case 2:
2115 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2116 break;
2117 case 3:
2118 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2119 break;
2123 tx_ring->producer = get_next_index(producer, num_txd);
2125 netxen_tso_check(netdev, tx_ring, first_desc, skb);
2127 adapter->stats.txbytes += skb->len;
2128 adapter->stats.xmitcalled++;
2130 netxen_nic_update_cmd_producer(adapter, tx_ring);
2132 return NETDEV_TX_OK;
2134 drop_packet:
2135 adapter->stats.txdropped++;
2136 dev_kfree_skb_any(skb);
2137 return NETDEV_TX_OK;
2140 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
2142 struct net_device *netdev = adapter->netdev;
2143 uint32_t temp, temp_state, temp_val;
2144 int rv = 0;
2146 temp = NXRD32(adapter, CRB_TEMP_STATE);
2148 temp_state = nx_get_temp_state(temp);
2149 temp_val = nx_get_temp_val(temp);
2151 if (temp_state == NX_TEMP_PANIC) {
2152 printk(KERN_ALERT
2153 "%s: Device temperature %d degrees C exceeds"
2154 " maximum allowed. Hardware has been shut down.\n",
2155 netdev->name, temp_val);
2156 rv = 1;
2157 } else if (temp_state == NX_TEMP_WARN) {
2158 if (adapter->temp == NX_TEMP_NORMAL) {
2159 printk(KERN_ALERT
2160 "%s: Device temperature %d degrees C "
2161 "exceeds operating range."
2162 " Immediate action needed.\n",
2163 netdev->name, temp_val);
2165 } else {
2166 if (adapter->temp == NX_TEMP_WARN) {
2167 printk(KERN_INFO
2168 "%s: Device temperature is now %d degrees C"
2169 " in normal range.\n", netdev->name,
2170 temp_val);
2173 adapter->temp = temp_state;
2174 return rv;
2177 void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
2179 struct net_device *netdev = adapter->netdev;
2181 if (adapter->ahw.linkup && !linkup) {
2182 printk(KERN_INFO "%s: %s NIC Link is down\n",
2183 netxen_nic_driver_name, netdev->name);
2184 adapter->ahw.linkup = 0;
2185 if (netif_running(netdev)) {
2186 netif_carrier_off(netdev);
2187 netif_stop_queue(netdev);
2189 adapter->link_changed = !adapter->has_link_events;
2190 } else if (!adapter->ahw.linkup && linkup) {
2191 printk(KERN_INFO "%s: %s NIC Link is up\n",
2192 netxen_nic_driver_name, netdev->name);
2193 adapter->ahw.linkup = 1;
2194 if (netif_running(netdev)) {
2195 netif_carrier_on(netdev);
2196 netif_wake_queue(netdev);
2198 adapter->link_changed = !adapter->has_link_events;
2202 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
2204 u32 val, port, linkup;
2206 port = adapter->physical_port;
2208 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2209 val = NXRD32(adapter, CRB_XG_STATE_P3);
2210 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
2211 linkup = (val == XG_LINK_UP_P3);
2212 } else {
2213 val = NXRD32(adapter, CRB_XG_STATE);
2214 val = (val >> port*8) & 0xff;
2215 linkup = (val == XG_LINK_UP);
2218 netxen_advert_link_change(adapter, linkup);
2221 static void netxen_tx_timeout(struct net_device *netdev, unsigned int txqueue)
2223 struct netxen_adapter *adapter = netdev_priv(netdev);
2225 if (test_bit(__NX_RESETTING, &adapter->state))
2226 return;
2228 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
2229 schedule_work(&adapter->tx_timeout_task);
2232 static void netxen_tx_timeout_task(struct work_struct *work)
2234 struct netxen_adapter *adapter =
2235 container_of(work, struct netxen_adapter, tx_timeout_task);
2237 if (!netif_running(adapter->netdev))
2238 return;
2240 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
2241 return;
2243 if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
2244 goto request_reset;
2246 rtnl_lock();
2247 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
2248 /* try to scrub interrupt */
2249 netxen_napi_disable(adapter);
2251 netxen_napi_enable(adapter);
2253 netif_wake_queue(adapter->netdev);
2255 clear_bit(__NX_RESETTING, &adapter->state);
2256 } else {
2257 clear_bit(__NX_RESETTING, &adapter->state);
2258 if (netxen_nic_reset_context(adapter)) {
2259 rtnl_unlock();
2260 goto request_reset;
2263 netif_trans_update(adapter->netdev);
2264 rtnl_unlock();
2265 return;
2267 request_reset:
2268 adapter->need_fw_reset = 1;
2269 clear_bit(__NX_RESETTING, &adapter->state);
2272 static void netxen_nic_get_stats(struct net_device *netdev,
2273 struct rtnl_link_stats64 *stats)
2275 struct netxen_adapter *adapter = netdev_priv(netdev);
2277 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2278 stats->tx_packets = adapter->stats.xmitfinished;
2279 stats->rx_bytes = adapter->stats.rxbytes;
2280 stats->tx_bytes = adapter->stats.txbytes;
2281 stats->rx_dropped = adapter->stats.rxdropped;
2282 stats->tx_dropped = adapter->stats.txdropped;
2285 static irqreturn_t netxen_intr(int irq, void *data)
2287 struct nx_host_sds_ring *sds_ring = data;
2288 struct netxen_adapter *adapter = sds_ring->adapter;
2289 u32 status = 0;
2291 status = readl(adapter->isr_int_vec);
2293 if (!(status & adapter->int_vec_bit))
2294 return IRQ_NONE;
2296 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2297 /* check interrupt state machine, to be sure */
2298 status = readl(adapter->crb_int_state_reg);
2299 if (!ISR_LEGACY_INT_TRIGGERED(status))
2300 return IRQ_NONE;
2302 } else {
2303 unsigned long our_int = 0;
2305 our_int = readl(adapter->crb_int_state_reg);
2307 /* not our interrupt */
2308 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
2309 return IRQ_NONE;
2311 /* claim interrupt */
2312 writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
2314 /* clear interrupt */
2315 netxen_nic_disable_int(sds_ring);
2318 writel(0xffffffff, adapter->tgt_status_reg);
2319 /* read twice to ensure write is flushed */
2320 readl(adapter->isr_int_vec);
2321 readl(adapter->isr_int_vec);
2323 napi_schedule(&sds_ring->napi);
2325 return IRQ_HANDLED;
2328 static irqreturn_t netxen_msi_intr(int irq, void *data)
2330 struct nx_host_sds_ring *sds_ring = data;
2331 struct netxen_adapter *adapter = sds_ring->adapter;
2333 /* clear interrupt */
2334 writel(0xffffffff, adapter->tgt_status_reg);
2336 napi_schedule(&sds_ring->napi);
2337 return IRQ_HANDLED;
2340 static irqreturn_t netxen_msix_intr(int irq, void *data)
2342 struct nx_host_sds_ring *sds_ring = data;
2344 napi_schedule(&sds_ring->napi);
2345 return IRQ_HANDLED;
2348 static int netxen_nic_poll(struct napi_struct *napi, int budget)
2350 struct nx_host_sds_ring *sds_ring =
2351 container_of(napi, struct nx_host_sds_ring, napi);
2353 struct netxen_adapter *adapter = sds_ring->adapter;
2355 int tx_complete;
2356 int work_done;
2358 tx_complete = netxen_process_cmd_ring(adapter);
2360 work_done = netxen_process_rcv_ring(sds_ring, budget);
2362 if (!tx_complete)
2363 work_done = budget;
2365 if (work_done < budget) {
2366 napi_complete_done(&sds_ring->napi, work_done);
2367 if (test_bit(__NX_DEV_UP, &adapter->state))
2368 netxen_nic_enable_int(sds_ring);
2371 return work_done;
2374 static int
2375 nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
2377 int count;
2378 if (netxen_api_lock(adapter))
2379 return -EIO;
2381 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2383 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2385 netxen_api_unlock(adapter);
2386 return count;
2389 static int
2390 nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
2392 int count, state;
2393 if (netxen_api_lock(adapter))
2394 return -EIO;
2396 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2397 WARN_ON(count == 0);
2399 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2400 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2402 if (count == 0 && state != NX_DEV_FAILED)
2403 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2405 netxen_api_unlock(adapter);
2406 return count;
2409 static int
2410 nx_dev_request_aer(struct netxen_adapter *adapter)
2412 u32 state;
2413 int ret = -EINVAL;
2415 if (netxen_api_lock(adapter))
2416 return ret;
2418 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2420 if (state == NX_DEV_NEED_AER)
2421 ret = 0;
2422 else if (state == NX_DEV_READY) {
2423 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_AER);
2424 ret = 0;
2427 netxen_api_unlock(adapter);
2428 return ret;
2432 nx_dev_request_reset(struct netxen_adapter *adapter)
2434 u32 state;
2435 int ret = -EINVAL;
2437 if (netxen_api_lock(adapter))
2438 return ret;
2440 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2442 if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED)
2443 ret = 0;
2444 else if (state != NX_DEV_INITALIZING && state != NX_DEV_NEED_AER) {
2445 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
2446 adapter->flags |= NETXEN_FW_RESET_OWNER;
2447 ret = 0;
2450 netxen_api_unlock(adapter);
2452 return ret;
2455 static int
2456 netxen_can_start_firmware(struct netxen_adapter *adapter)
2458 int count;
2459 int can_start = 0;
2461 if (netxen_api_lock(adapter)) {
2462 nx_incr_dev_ref_cnt(adapter);
2463 return -1;
2466 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2468 if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2469 count = 0;
2471 if (count == 0) {
2472 can_start = 1;
2473 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2476 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2478 netxen_api_unlock(adapter);
2480 return can_start;
2483 static void
2484 netxen_schedule_work(struct netxen_adapter *adapter,
2485 work_func_t func, int delay)
2487 INIT_DELAYED_WORK(&adapter->fw_work, func);
2488 schedule_delayed_work(&adapter->fw_work, delay);
2491 static void
2492 netxen_cancel_fw_work(struct netxen_adapter *adapter)
2494 while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2495 msleep(10);
2497 cancel_delayed_work_sync(&adapter->fw_work);
2500 static void
2501 netxen_attach_work(struct work_struct *work)
2503 struct netxen_adapter *adapter = container_of(work,
2504 struct netxen_adapter, fw_work.work);
2505 struct net_device *netdev = adapter->netdev;
2506 int err = 0;
2508 if (netif_running(netdev)) {
2509 err = netxen_nic_attach(adapter);
2510 if (err)
2511 goto done;
2513 err = netxen_nic_up(adapter, netdev);
2514 if (err) {
2515 netxen_nic_detach(adapter);
2516 goto done;
2519 netxen_restore_indev_addr(netdev, NETDEV_UP);
2522 netif_device_attach(netdev);
2524 done:
2525 adapter->fw_fail_cnt = 0;
2526 clear_bit(__NX_RESETTING, &adapter->state);
2527 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2530 static void
2531 netxen_fwinit_work(struct work_struct *work)
2533 struct netxen_adapter *adapter = container_of(work,
2534 struct netxen_adapter, fw_work.work);
2535 int dev_state;
2536 int count;
2537 dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2538 if (adapter->flags & NETXEN_FW_RESET_OWNER) {
2539 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2540 WARN_ON(count == 0);
2541 if (count == 1) {
2542 if (adapter->mdump.md_enabled) {
2543 rtnl_lock();
2544 netxen_dump_fw(adapter);
2545 rtnl_unlock();
2547 adapter->flags &= ~NETXEN_FW_RESET_OWNER;
2548 if (netxen_api_lock(adapter)) {
2549 clear_bit(__NX_RESETTING, &adapter->state);
2550 NXWR32(adapter, NX_CRB_DEV_STATE,
2551 NX_DEV_FAILED);
2552 return;
2554 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2555 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2556 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2557 dev_state = NX_DEV_COLD;
2558 netxen_api_unlock(adapter);
2562 switch (dev_state) {
2563 case NX_DEV_COLD:
2564 case NX_DEV_READY:
2565 if (!netxen_start_firmware(adapter)) {
2566 netxen_schedule_work(adapter, netxen_attach_work, 0);
2567 return;
2569 break;
2571 case NX_DEV_NEED_RESET:
2572 case NX_DEV_INITALIZING:
2573 netxen_schedule_work(adapter,
2574 netxen_fwinit_work, 2 * FW_POLL_DELAY);
2575 return;
2577 case NX_DEV_FAILED:
2578 default:
2579 nx_incr_dev_ref_cnt(adapter);
2580 break;
2583 if (netxen_api_lock(adapter)) {
2584 clear_bit(__NX_RESETTING, &adapter->state);
2585 return;
2587 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_FAILED);
2588 netxen_api_unlock(adapter);
2589 dev_err(&adapter->pdev->dev, "%s: Device initialization Failed\n",
2590 adapter->netdev->name);
2592 clear_bit(__NX_RESETTING, &adapter->state);
2595 static void
2596 netxen_detach_work(struct work_struct *work)
2598 struct netxen_adapter *adapter = container_of(work,
2599 struct netxen_adapter, fw_work.work);
2600 struct net_device *netdev = adapter->netdev;
2601 int ref_cnt = 0, delay;
2602 u32 status;
2604 netif_device_detach(netdev);
2606 netxen_nic_down(adapter, netdev);
2608 rtnl_lock();
2609 netxen_nic_detach(adapter);
2610 rtnl_unlock();
2612 status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2614 if (status & NX_RCODE_FATAL_ERROR)
2615 goto err_ret;
2617 if (adapter->temp == NX_TEMP_PANIC)
2618 goto err_ret;
2620 if (!(adapter->flags & NETXEN_FW_RESET_OWNER))
2621 ref_cnt = nx_decr_dev_ref_cnt(adapter);
2623 if (ref_cnt == -EIO)
2624 goto err_ret;
2626 delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2628 adapter->fw_wait_cnt = 0;
2629 netxen_schedule_work(adapter, netxen_fwinit_work, delay);
2631 return;
2633 err_ret:
2634 clear_bit(__NX_RESETTING, &adapter->state);
2637 static int
2638 netxen_check_health(struct netxen_adapter *adapter)
2640 u32 state, heartbit;
2641 u32 peg_status;
2642 struct net_device *netdev = adapter->netdev;
2644 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2645 if (state == NX_DEV_NEED_AER)
2646 return 0;
2648 if (netxen_nic_check_temp(adapter))
2649 goto detach;
2651 if (adapter->need_fw_reset) {
2652 if (nx_dev_request_reset(adapter))
2653 return 0;
2654 goto detach;
2657 /* NX_DEV_NEED_RESET, this state can be marked in two cases
2658 * 1. Tx timeout 2. Fw hang
2659 * Send request to destroy context in case of tx timeout only
2660 * and doesn't required in case of Fw hang
2662 if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED) {
2663 adapter->need_fw_reset = 1;
2664 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2665 goto detach;
2668 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2669 return 0;
2671 heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2672 if (heartbit != adapter->heartbit) {
2673 adapter->heartbit = heartbit;
2674 adapter->fw_fail_cnt = 0;
2675 if (adapter->need_fw_reset)
2676 goto detach;
2677 return 0;
2680 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2681 return 0;
2683 if (nx_dev_request_reset(adapter))
2684 return 0;
2686 clear_bit(__NX_FW_ATTACHED, &adapter->state);
2688 dev_err(&netdev->dev, "firmware hang detected\n");
2689 peg_status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2690 dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
2691 "PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
2692 "PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
2693 "PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
2694 "PEG_NET_4_PC: 0x%x\n",
2695 peg_status,
2696 NXRD32(adapter, NETXEN_PEG_HALT_STATUS2),
2697 NXRD32(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c),
2698 NXRD32(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c),
2699 NXRD32(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c),
2700 NXRD32(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c),
2701 NXRD32(adapter, NETXEN_CRB_PEG_NET_4 + 0x3c));
2702 if (NX_FWERROR_PEGSTAT1(peg_status) == 0x67)
2703 dev_err(&adapter->pdev->dev,
2704 "Firmware aborted with error code 0x00006700. "
2705 "Device is being reset.\n");
2706 detach:
2707 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
2708 !test_and_set_bit(__NX_RESETTING, &adapter->state))
2709 netxen_schedule_work(adapter, netxen_detach_work, 0);
2710 return 1;
2713 static void
2714 netxen_fw_poll_work(struct work_struct *work)
2716 struct netxen_adapter *adapter = container_of(work,
2717 struct netxen_adapter, fw_work.work);
2719 if (test_bit(__NX_RESETTING, &adapter->state))
2720 goto reschedule;
2722 if (test_bit(__NX_DEV_UP, &adapter->state) &&
2723 !(adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)) {
2724 if (!adapter->has_link_events) {
2726 netxen_nic_handle_phy_intr(adapter);
2728 if (adapter->link_changed)
2729 netxen_nic_set_link_parameters(adapter);
2733 if (netxen_check_health(adapter))
2734 return;
2736 reschedule:
2737 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2740 static ssize_t
2741 netxen_store_bridged_mode(struct device *dev,
2742 struct device_attribute *attr, const char *buf, size_t len)
2744 struct net_device *net = to_net_dev(dev);
2745 struct netxen_adapter *adapter = netdev_priv(net);
2746 unsigned long new;
2747 int ret = -EINVAL;
2749 if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2750 goto err_out;
2752 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2753 goto err_out;
2755 if (kstrtoul(buf, 2, &new))
2756 goto err_out;
2758 if (!netxen_config_bridged_mode(adapter, !!new))
2759 ret = len;
2761 err_out:
2762 return ret;
2765 static ssize_t
2766 netxen_show_bridged_mode(struct device *dev,
2767 struct device_attribute *attr, char *buf)
2769 struct net_device *net = to_net_dev(dev);
2770 struct netxen_adapter *adapter;
2771 int bridged_mode = 0;
2773 adapter = netdev_priv(net);
2775 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2776 bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2778 return sprintf(buf, "%d\n", bridged_mode);
2781 static const struct device_attribute dev_attr_bridged_mode = {
2782 .attr = { .name = "bridged_mode", .mode = 0644 },
2783 .show = netxen_show_bridged_mode,
2784 .store = netxen_store_bridged_mode,
2787 static ssize_t
2788 netxen_store_diag_mode(struct device *dev,
2789 struct device_attribute *attr, const char *buf, size_t len)
2791 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2792 unsigned long new;
2794 if (kstrtoul(buf, 2, &new))
2795 return -EINVAL;
2797 if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2798 adapter->flags ^= NETXEN_NIC_DIAG_ENABLED;
2800 return len;
2803 static ssize_t
2804 netxen_show_diag_mode(struct device *dev,
2805 struct device_attribute *attr, char *buf)
2807 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2809 return sprintf(buf, "%d\n",
2810 !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED));
2813 static const struct device_attribute dev_attr_diag_mode = {
2814 .attr = { .name = "diag_mode", .mode = 0644 },
2815 .show = netxen_show_diag_mode,
2816 .store = netxen_store_diag_mode,
2819 static int
2820 netxen_sysfs_validate_crb(struct netxen_adapter *adapter,
2821 loff_t offset, size_t size)
2823 size_t crb_size = 4;
2825 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2826 return -EIO;
2828 if (offset < NETXEN_PCI_CRBSPACE) {
2829 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2830 return -EINVAL;
2832 if (ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2833 NETXEN_PCI_CAMQM_2M_END))
2834 crb_size = 8;
2835 else
2836 return -EINVAL;
2839 if ((size != crb_size) || (offset & (crb_size-1)))
2840 return -EINVAL;
2842 return 0;
2845 static ssize_t
2846 netxen_sysfs_read_crb(struct file *filp, struct kobject *kobj,
2847 struct bin_attribute *attr,
2848 char *buf, loff_t offset, size_t size)
2850 struct device *dev = kobj_to_dev(kobj);
2851 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2852 u32 data;
2853 u64 qmdata;
2854 int ret;
2856 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2857 if (ret != 0)
2858 return ret;
2860 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
2861 ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2862 NETXEN_PCI_CAMQM_2M_END)) {
2863 netxen_pci_camqm_read_2M(adapter, offset, &qmdata);
2864 memcpy(buf, &qmdata, size);
2865 } else {
2866 data = NXRD32(adapter, offset);
2867 memcpy(buf, &data, size);
2870 return size;
2873 static ssize_t
2874 netxen_sysfs_write_crb(struct file *filp, struct kobject *kobj,
2875 struct bin_attribute *attr,
2876 char *buf, loff_t offset, size_t size)
2878 struct device *dev = kobj_to_dev(kobj);
2879 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2880 u32 data;
2881 u64 qmdata;
2882 int ret;
2884 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2885 if (ret != 0)
2886 return ret;
2888 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
2889 ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2890 NETXEN_PCI_CAMQM_2M_END)) {
2891 memcpy(&qmdata, buf, size);
2892 netxen_pci_camqm_write_2M(adapter, offset, qmdata);
2893 } else {
2894 memcpy(&data, buf, size);
2895 NXWR32(adapter, offset, data);
2898 return size;
2901 static int
2902 netxen_sysfs_validate_mem(struct netxen_adapter *adapter,
2903 loff_t offset, size_t size)
2905 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2906 return -EIO;
2908 if ((size != 8) || (offset & 0x7))
2909 return -EIO;
2911 return 0;
2914 static ssize_t
2915 netxen_sysfs_read_mem(struct file *filp, struct kobject *kobj,
2916 struct bin_attribute *attr,
2917 char *buf, loff_t offset, size_t size)
2919 struct device *dev = kobj_to_dev(kobj);
2920 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2921 u64 data;
2922 int ret;
2924 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2925 if (ret != 0)
2926 return ret;
2928 if (adapter->pci_mem_read(adapter, offset, &data))
2929 return -EIO;
2931 memcpy(buf, &data, size);
2933 return size;
2936 static ssize_t netxen_sysfs_write_mem(struct file *filp, struct kobject *kobj,
2937 struct bin_attribute *attr, char *buf,
2938 loff_t offset, size_t size)
2940 struct device *dev = kobj_to_dev(kobj);
2941 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2942 u64 data;
2943 int ret;
2945 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2946 if (ret != 0)
2947 return ret;
2949 memcpy(&data, buf, size);
2951 if (adapter->pci_mem_write(adapter, offset, data))
2952 return -EIO;
2954 return size;
2958 static const struct bin_attribute bin_attr_crb = {
2959 .attr = { .name = "crb", .mode = 0644 },
2960 .size = 0,
2961 .read = netxen_sysfs_read_crb,
2962 .write = netxen_sysfs_write_crb,
2965 static const struct bin_attribute bin_attr_mem = {
2966 .attr = { .name = "mem", .mode = 0644 },
2967 .size = 0,
2968 .read = netxen_sysfs_read_mem,
2969 .write = netxen_sysfs_write_mem,
2972 static ssize_t
2973 netxen_sysfs_read_dimm(struct file *filp, struct kobject *kobj,
2974 struct bin_attribute *attr,
2975 char *buf, loff_t offset, size_t size)
2977 struct device *dev = kobj_to_dev(kobj);
2978 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2979 struct net_device *netdev = adapter->netdev;
2980 struct netxen_dimm_cfg dimm;
2981 u8 dw, rows, cols, banks, ranks;
2982 u32 val;
2984 if (size < attr->size) {
2985 netdev_err(netdev, "Invalid size\n");
2986 return -EINVAL;
2989 memset(&dimm, 0, sizeof(struct netxen_dimm_cfg));
2990 val = NXRD32(adapter, NETXEN_DIMM_CAPABILITY);
2992 /* Checks if DIMM info is valid. */
2993 if (val & NETXEN_DIMM_VALID_FLAG) {
2994 netdev_err(netdev, "Invalid DIMM flag\n");
2995 dimm.presence = 0xff;
2996 goto out;
2999 rows = NETXEN_DIMM_NUMROWS(val);
3000 cols = NETXEN_DIMM_NUMCOLS(val);
3001 ranks = NETXEN_DIMM_NUMRANKS(val);
3002 banks = NETXEN_DIMM_NUMBANKS(val);
3003 dw = NETXEN_DIMM_DATAWIDTH(val);
3005 dimm.presence = (val & NETXEN_DIMM_PRESENT);
3007 /* Checks if DIMM info is present. */
3008 if (!dimm.presence) {
3009 netdev_err(netdev, "DIMM not present\n");
3010 goto out;
3013 dimm.dimm_type = NETXEN_DIMM_TYPE(val);
3015 switch (dimm.dimm_type) {
3016 case NETXEN_DIMM_TYPE_RDIMM:
3017 case NETXEN_DIMM_TYPE_UDIMM:
3018 case NETXEN_DIMM_TYPE_SO_DIMM:
3019 case NETXEN_DIMM_TYPE_Micro_DIMM:
3020 case NETXEN_DIMM_TYPE_Mini_RDIMM:
3021 case NETXEN_DIMM_TYPE_Mini_UDIMM:
3022 break;
3023 default:
3024 netdev_err(netdev, "Invalid DIMM type %x\n", dimm.dimm_type);
3025 goto out;
3028 if (val & NETXEN_DIMM_MEMTYPE_DDR2_SDRAM)
3029 dimm.mem_type = NETXEN_DIMM_MEM_DDR2_SDRAM;
3030 else
3031 dimm.mem_type = NETXEN_DIMM_MEMTYPE(val);
3033 if (val & NETXEN_DIMM_SIZE) {
3034 dimm.size = NETXEN_DIMM_STD_MEM_SIZE;
3035 goto out;
3038 if (!rows) {
3039 netdev_err(netdev, "Invalid no of rows %x\n", rows);
3040 goto out;
3043 if (!cols) {
3044 netdev_err(netdev, "Invalid no of columns %x\n", cols);
3045 goto out;
3048 if (!banks) {
3049 netdev_err(netdev, "Invalid no of banks %x\n", banks);
3050 goto out;
3053 ranks += 1;
3055 switch (dw) {
3056 case 0x0:
3057 dw = 32;
3058 break;
3059 case 0x1:
3060 dw = 33;
3061 break;
3062 case 0x2:
3063 dw = 36;
3064 break;
3065 case 0x3:
3066 dw = 64;
3067 break;
3068 case 0x4:
3069 dw = 72;
3070 break;
3071 case 0x5:
3072 dw = 80;
3073 break;
3074 case 0x6:
3075 dw = 128;
3076 break;
3077 case 0x7:
3078 dw = 144;
3079 break;
3080 default:
3081 netdev_err(netdev, "Invalid data-width %x\n", dw);
3082 goto out;
3085 dimm.size = ((1 << rows) * (1 << cols) * dw * banks * ranks) / 8;
3086 /* Size returned in MB. */
3087 dimm.size = (dimm.size) / 0x100000;
3088 out:
3089 memcpy(buf, &dimm, sizeof(struct netxen_dimm_cfg));
3090 return sizeof(struct netxen_dimm_cfg);
3094 static const struct bin_attribute bin_attr_dimm = {
3095 .attr = { .name = "dimm", .mode = 0644 },
3096 .size = sizeof(struct netxen_dimm_cfg),
3097 .read = netxen_sysfs_read_dimm,
3101 static void
3102 netxen_create_sysfs_entries(struct netxen_adapter *adapter)
3104 struct device *dev = &adapter->pdev->dev;
3106 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
3107 /* bridged_mode control */
3108 if (device_create_file(dev, &dev_attr_bridged_mode)) {
3109 dev_warn(dev,
3110 "failed to create bridged_mode sysfs entry\n");
3115 static void
3116 netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
3118 struct device *dev = &adapter->pdev->dev;
3120 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
3121 device_remove_file(dev, &dev_attr_bridged_mode);
3124 static void
3125 netxen_create_diag_entries(struct netxen_adapter *adapter)
3127 struct pci_dev *pdev = adapter->pdev;
3128 struct device *dev;
3130 dev = &pdev->dev;
3131 if (device_create_file(dev, &dev_attr_diag_mode))
3132 dev_info(dev, "failed to create diag_mode sysfs entry\n");
3133 if (device_create_bin_file(dev, &bin_attr_crb))
3134 dev_info(dev, "failed to create crb sysfs entry\n");
3135 if (device_create_bin_file(dev, &bin_attr_mem))
3136 dev_info(dev, "failed to create mem sysfs entry\n");
3137 if (device_create_bin_file(dev, &bin_attr_dimm))
3138 dev_info(dev, "failed to create dimm sysfs entry\n");
3142 static void
3143 netxen_remove_diag_entries(struct netxen_adapter *adapter)
3145 struct pci_dev *pdev = adapter->pdev;
3146 struct device *dev = &pdev->dev;
3148 device_remove_file(dev, &dev_attr_diag_mode);
3149 device_remove_bin_file(dev, &bin_attr_crb);
3150 device_remove_bin_file(dev, &bin_attr_mem);
3151 device_remove_bin_file(dev, &bin_attr_dimm);
3154 #ifdef CONFIG_INET
3156 #define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
3158 static int
3159 netxen_destip_supported(struct netxen_adapter *adapter)
3161 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
3162 return 0;
3164 if (adapter->ahw.cut_through)
3165 return 0;
3167 return 1;
3170 static void
3171 netxen_free_ip_list(struct netxen_adapter *adapter, bool master)
3173 struct nx_ip_list *cur, *tmp_cur;
3175 list_for_each_entry_safe(cur, tmp_cur, &adapter->ip_list, list) {
3176 if (master) {
3177 if (cur->master) {
3178 netxen_config_ipaddr(adapter, cur->ip_addr,
3179 NX_IP_DOWN);
3180 list_del(&cur->list);
3181 kfree(cur);
3183 } else {
3184 netxen_config_ipaddr(adapter, cur->ip_addr, NX_IP_DOWN);
3185 list_del(&cur->list);
3186 kfree(cur);
3191 static bool
3192 netxen_list_config_ip(struct netxen_adapter *adapter,
3193 struct in_ifaddr *ifa, unsigned long event)
3195 struct net_device *dev;
3196 struct nx_ip_list *cur, *tmp_cur;
3197 struct list_head *head;
3198 bool ret = false;
3200 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
3202 if (dev == NULL)
3203 goto out;
3205 switch (event) {
3206 case NX_IP_UP:
3207 list_for_each(head, &adapter->ip_list) {
3208 cur = list_entry(head, struct nx_ip_list, list);
3210 if (cur->ip_addr == ifa->ifa_address)
3211 goto out;
3214 cur = kzalloc(sizeof(struct nx_ip_list), GFP_ATOMIC);
3215 if (cur == NULL)
3216 goto out;
3217 if (is_vlan_dev(dev))
3218 dev = vlan_dev_real_dev(dev);
3219 cur->master = !!netif_is_bond_master(dev);
3220 cur->ip_addr = ifa->ifa_address;
3221 list_add_tail(&cur->list, &adapter->ip_list);
3222 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
3223 ret = true;
3224 break;
3225 case NX_IP_DOWN:
3226 list_for_each_entry_safe(cur, tmp_cur,
3227 &adapter->ip_list, list) {
3228 if (cur->ip_addr == ifa->ifa_address) {
3229 list_del(&cur->list);
3230 kfree(cur);
3231 netxen_config_ipaddr(adapter, ifa->ifa_address,
3232 NX_IP_DOWN);
3233 ret = true;
3234 break;
3238 out:
3239 return ret;
3242 static void
3243 netxen_config_indev_addr(struct netxen_adapter *adapter,
3244 struct net_device *dev, unsigned long event)
3246 struct in_device *indev;
3247 struct in_ifaddr *ifa;
3249 if (!netxen_destip_supported(adapter))
3250 return;
3252 indev = in_dev_get(dev);
3253 if (!indev)
3254 return;
3256 rcu_read_lock();
3257 in_dev_for_each_ifa_rcu(ifa, indev) {
3258 switch (event) {
3259 case NETDEV_UP:
3260 netxen_list_config_ip(adapter, ifa, NX_IP_UP);
3261 break;
3262 case NETDEV_DOWN:
3263 netxen_list_config_ip(adapter, ifa, NX_IP_DOWN);
3264 break;
3265 default:
3266 break;
3269 rcu_read_unlock();
3270 in_dev_put(indev);
3273 static void
3274 netxen_restore_indev_addr(struct net_device *netdev, unsigned long event)
3277 struct netxen_adapter *adapter = netdev_priv(netdev);
3278 struct nx_ip_list *pos, *tmp_pos;
3279 unsigned long ip_event;
3281 ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
3282 netxen_config_indev_addr(adapter, netdev, event);
3284 list_for_each_entry_safe(pos, tmp_pos, &adapter->ip_list, list) {
3285 netxen_config_ipaddr(adapter, pos->ip_addr, ip_event);
3289 static inline bool
3290 netxen_config_checkdev(struct net_device *dev)
3292 struct netxen_adapter *adapter;
3294 if (!is_netxen_netdev(dev))
3295 return false;
3296 adapter = netdev_priv(dev);
3297 if (!adapter)
3298 return false;
3299 if (!netxen_destip_supported(adapter))
3300 return false;
3301 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
3302 return false;
3304 return true;
3308 * netxen_config_master - configure addresses based on master
3309 * @dev: netxen device
3310 * @event: netdev event
3312 static void netxen_config_master(struct net_device *dev, unsigned long event)
3314 struct net_device *master, *slave;
3315 struct netxen_adapter *adapter = netdev_priv(dev);
3317 rcu_read_lock();
3318 master = netdev_master_upper_dev_get_rcu(dev);
3320 * This is the case where the netxen nic is being
3321 * enslaved and is dev_open()ed in bond_enslave()
3322 * Now we should program the bond's (and its vlans')
3323 * addresses in the netxen NIC.
3325 if (master && netif_is_bond_master(master) &&
3326 !netif_is_bond_slave(dev)) {
3327 netxen_config_indev_addr(adapter, master, event);
3328 for_each_netdev_rcu(&init_net, slave)
3329 if (is_vlan_dev(slave) &&
3330 vlan_dev_real_dev(slave) == master)
3331 netxen_config_indev_addr(adapter, slave, event);
3333 rcu_read_unlock();
3335 * This is the case where the netxen nic is being
3336 * released and is dev_close()ed in bond_release()
3337 * just before IFF_BONDING is stripped.
3339 if (!master && dev->priv_flags & IFF_BONDING)
3340 netxen_free_ip_list(adapter, true);
3343 static int netxen_netdev_event(struct notifier_block *this,
3344 unsigned long event, void *ptr)
3346 struct netxen_adapter *adapter;
3347 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3348 struct net_device *orig_dev = dev;
3349 struct net_device *slave;
3351 recheck:
3352 if (dev == NULL)
3353 goto done;
3355 if (is_vlan_dev(dev)) {
3356 dev = vlan_dev_real_dev(dev);
3357 goto recheck;
3359 if (event == NETDEV_UP || event == NETDEV_DOWN) {
3360 /* If this is a bonding device, look for netxen-based slaves*/
3361 if (netif_is_bond_master(dev)) {
3362 rcu_read_lock();
3363 for_each_netdev_in_bond_rcu(dev, slave) {
3364 if (!netxen_config_checkdev(slave))
3365 continue;
3366 adapter = netdev_priv(slave);
3367 netxen_config_indev_addr(adapter,
3368 orig_dev, event);
3370 rcu_read_unlock();
3371 } else {
3372 if (!netxen_config_checkdev(dev))
3373 goto done;
3374 adapter = netdev_priv(dev);
3375 /* Act only if the actual netxen is the target */
3376 if (orig_dev == dev)
3377 netxen_config_master(dev, event);
3378 netxen_config_indev_addr(adapter, orig_dev, event);
3381 done:
3382 return NOTIFY_DONE;
3385 static int
3386 netxen_inetaddr_event(struct notifier_block *this,
3387 unsigned long event, void *ptr)
3389 struct netxen_adapter *adapter;
3390 struct net_device *dev, *slave;
3391 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
3392 unsigned long ip_event;
3394 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
3395 ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
3396 recheck:
3397 if (dev == NULL)
3398 goto done;
3400 if (is_vlan_dev(dev)) {
3401 dev = vlan_dev_real_dev(dev);
3402 goto recheck;
3404 if (event == NETDEV_UP || event == NETDEV_DOWN) {
3405 /* If this is a bonding device, look for netxen-based slaves*/
3406 if (netif_is_bond_master(dev)) {
3407 rcu_read_lock();
3408 for_each_netdev_in_bond_rcu(dev, slave) {
3409 if (!netxen_config_checkdev(slave))
3410 continue;
3411 adapter = netdev_priv(slave);
3412 netxen_list_config_ip(adapter, ifa, ip_event);
3414 rcu_read_unlock();
3415 } else {
3416 if (!netxen_config_checkdev(dev))
3417 goto done;
3418 adapter = netdev_priv(dev);
3419 netxen_list_config_ip(adapter, ifa, ip_event);
3422 done:
3423 return NOTIFY_DONE;
3426 static struct notifier_block netxen_netdev_cb = {
3427 .notifier_call = netxen_netdev_event,
3430 static struct notifier_block netxen_inetaddr_cb = {
3431 .notifier_call = netxen_inetaddr_event,
3433 #else
3434 static void
3435 netxen_restore_indev_addr(struct net_device *dev, unsigned long event)
3437 static void
3438 netxen_free_ip_list(struct netxen_adapter *adapter, bool master)
3440 #endif
3442 static const struct pci_error_handlers netxen_err_handler = {
3443 .error_detected = netxen_io_error_detected,
3444 .slot_reset = netxen_io_slot_reset,
3447 static SIMPLE_DEV_PM_OPS(netxen_nic_pm_ops,
3448 netxen_nic_suspend,
3449 netxen_nic_resume);
3451 static struct pci_driver netxen_driver = {
3452 .name = netxen_nic_driver_name,
3453 .id_table = netxen_pci_tbl,
3454 .probe = netxen_nic_probe,
3455 .remove = netxen_nic_remove,
3456 .driver.pm = &netxen_nic_pm_ops,
3457 .shutdown = netxen_nic_shutdown,
3458 .err_handler = &netxen_err_handler
3461 static int __init netxen_init_module(void)
3463 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
3465 #ifdef CONFIG_INET
3466 register_netdevice_notifier(&netxen_netdev_cb);
3467 register_inetaddr_notifier(&netxen_inetaddr_cb);
3468 #endif
3469 return pci_register_driver(&netxen_driver);
3472 module_init(netxen_init_module);
3474 static void __exit netxen_exit_module(void)
3476 pci_unregister_driver(&netxen_driver);
3478 #ifdef CONFIG_INET
3479 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
3480 unregister_netdevice_notifier(&netxen_netdev_cb);
3481 #endif
3484 module_exit(netxen_exit_module);