Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / ethernet / qlogic / netxen / netxen_nic_main.c
blob59c70be22a84c11262388529cf0ddf09887cea96
1 /*
2 * Copyright (C) 2003 - 2009 NetXen, Inc.
3 * Copyright (C) 2009 - QLogic Corporation.
4 * All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 * The full GNU General Public License is included in this distribution
20 * in the file called "COPYING".
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/interrupt.h>
27 #include "netxen_nic_hw.h"
29 #include "netxen_nic.h"
31 #include <linux/dma-mapping.h>
32 #include <linux/if_vlan.h>
33 #include <net/ip.h>
34 #include <linux/ipv6.h>
35 #include <linux/inetdevice.h>
36 #include <linux/sysfs.h>
37 #include <linux/aer.h>
39 MODULE_DESCRIPTION("QLogic/NetXen (1/10) GbE Intelligent Ethernet Driver");
40 MODULE_LICENSE("GPL");
41 MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
42 MODULE_FIRMWARE(NX_UNIFIED_ROMIMAGE_NAME);
44 char netxen_nic_driver_name[] = "netxen_nic";
45 static char netxen_nic_driver_string[] = "QLogic/NetXen Network Driver v"
46 NETXEN_NIC_LINUX_VERSIONID;
48 static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
50 /* Default to restricted 1G auto-neg mode */
51 static int wol_port_mode = 5;
53 static int use_msi = 1;
55 static int use_msi_x = 1;
57 static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
58 module_param(auto_fw_reset, int, 0644);
59 MODULE_PARM_DESC(auto_fw_reset,"Auto firmware reset (0=disabled, 1=enabled");
61 static int netxen_nic_probe(struct pci_dev *pdev,
62 const struct pci_device_id *ent);
63 static void netxen_nic_remove(struct pci_dev *pdev);
64 static int netxen_nic_open(struct net_device *netdev);
65 static int netxen_nic_close(struct net_device *netdev);
66 static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
67 struct net_device *);
68 static void netxen_tx_timeout(struct net_device *netdev);
69 static void netxen_tx_timeout_task(struct work_struct *work);
70 static void netxen_fw_poll_work(struct work_struct *work);
71 static void netxen_schedule_work(struct netxen_adapter *adapter,
72 work_func_t func, int delay);
73 static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
74 static int netxen_nic_poll(struct napi_struct *napi, int budget);
76 static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
77 static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
78 static void netxen_create_diag_entries(struct netxen_adapter *adapter);
79 static void netxen_remove_diag_entries(struct netxen_adapter *adapter);
80 static int nx_dev_request_aer(struct netxen_adapter *adapter);
81 static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
82 static int netxen_can_start_firmware(struct netxen_adapter *adapter);
84 static irqreturn_t netxen_intr(int irq, void *data);
85 static irqreturn_t netxen_msi_intr(int irq, void *data);
86 static irqreturn_t netxen_msix_intr(int irq, void *data);
88 static void netxen_free_ip_list(struct netxen_adapter *, bool);
89 static void netxen_restore_indev_addr(struct net_device *dev, unsigned long);
90 static void netxen_nic_get_stats(struct net_device *dev,
91 struct rtnl_link_stats64 *stats);
92 static int netxen_nic_set_mac(struct net_device *netdev, void *p);
94 /* PCI Device ID Table */
95 #define ENTRY(device) \
96 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
97 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
99 static const struct pci_device_id netxen_pci_tbl[] = {
100 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
101 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
102 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
103 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
104 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
105 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
106 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
107 ENTRY(PCI_DEVICE_ID_NX3031),
108 {0,}
111 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
113 static uint32_t crb_cmd_producer[4] = {
114 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
115 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
118 void
119 netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
120 struct nx_host_tx_ring *tx_ring)
122 NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
125 static uint32_t crb_cmd_consumer[4] = {
126 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
127 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
130 static inline void
131 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
132 struct nx_host_tx_ring *tx_ring)
134 NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
137 static uint32_t msi_tgt_status[8] = {
138 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
139 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
140 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
141 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
144 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
146 static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
148 struct netxen_adapter *adapter = sds_ring->adapter;
150 NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
153 static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
155 struct netxen_adapter *adapter = sds_ring->adapter;
157 NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
159 if (!NETXEN_IS_MSI_FAMILY(adapter))
160 NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
163 static int
164 netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
166 int size = sizeof(struct nx_host_sds_ring) * count;
168 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
170 return recv_ctx->sds_rings == NULL;
173 static void
174 netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
176 kfree(recv_ctx->sds_rings);
177 recv_ctx->sds_rings = NULL;
180 static int
181 netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
183 int ring;
184 struct nx_host_sds_ring *sds_ring;
185 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
187 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
188 return -ENOMEM;
190 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
191 sds_ring = &recv_ctx->sds_rings[ring];
192 netif_napi_add(netdev, &sds_ring->napi,
193 netxen_nic_poll, NAPI_POLL_WEIGHT);
196 return 0;
199 static void
200 netxen_napi_del(struct netxen_adapter *adapter)
202 int ring;
203 struct nx_host_sds_ring *sds_ring;
204 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
206 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
207 sds_ring = &recv_ctx->sds_rings[ring];
208 netif_napi_del(&sds_ring->napi);
211 netxen_free_sds_rings(&adapter->recv_ctx);
214 static void
215 netxen_napi_enable(struct netxen_adapter *adapter)
217 int ring;
218 struct nx_host_sds_ring *sds_ring;
219 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
221 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
222 sds_ring = &recv_ctx->sds_rings[ring];
223 napi_enable(&sds_ring->napi);
224 netxen_nic_enable_int(sds_ring);
228 static void
229 netxen_napi_disable(struct netxen_adapter *adapter)
231 int ring;
232 struct nx_host_sds_ring *sds_ring;
233 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
235 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
236 sds_ring = &recv_ctx->sds_rings[ring];
237 netxen_nic_disable_int(sds_ring);
238 napi_synchronize(&sds_ring->napi);
239 napi_disable(&sds_ring->napi);
243 static int nx_set_dma_mask(struct netxen_adapter *adapter)
245 struct pci_dev *pdev = adapter->pdev;
246 uint64_t mask, cmask;
248 adapter->pci_using_dac = 0;
250 mask = DMA_BIT_MASK(32);
251 cmask = DMA_BIT_MASK(32);
253 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
254 #ifndef CONFIG_IA64
255 mask = DMA_BIT_MASK(35);
256 #endif
257 } else {
258 mask = DMA_BIT_MASK(39);
259 cmask = mask;
262 if (pci_set_dma_mask(pdev, mask) == 0 &&
263 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
264 adapter->pci_using_dac = 1;
265 return 0;
268 return -EIO;
271 /* Update addressable range if firmware supports it */
272 static int
273 nx_update_dma_mask(struct netxen_adapter *adapter)
275 int change, shift, err;
276 uint64_t mask, old_mask, old_cmask;
277 struct pci_dev *pdev = adapter->pdev;
279 change = 0;
281 shift = NXRD32(adapter, CRB_DMA_SHIFT);
282 if (shift > 32)
283 return 0;
285 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
286 change = 1;
287 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
288 change = 1;
290 if (change) {
291 old_mask = pdev->dma_mask;
292 old_cmask = pdev->dev.coherent_dma_mask;
294 mask = DMA_BIT_MASK(32+shift);
296 err = pci_set_dma_mask(pdev, mask);
297 if (err)
298 goto err_out;
300 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
302 err = pci_set_consistent_dma_mask(pdev, mask);
303 if (err)
304 goto err_out;
306 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
309 return 0;
311 err_out:
312 pci_set_dma_mask(pdev, old_mask);
313 pci_set_consistent_dma_mask(pdev, old_cmask);
314 return err;
317 static int
318 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
320 u32 val, timeout;
322 if (first_boot == 0x55555555) {
323 /* This is the first boot after power up */
324 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
326 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
327 return 0;
329 /* PCI bus master workaround */
330 first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
331 if (!(first_boot & 0x4)) {
332 first_boot |= 0x4;
333 NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
334 NXRD32(adapter, NETXEN_PCIE_REG(0x4));
337 /* This is the first boot after power up */
338 first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
339 if (first_boot != 0x80000f) {
340 /* clear the register for future unloads/loads */
341 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
342 return -EIO;
345 /* Start P2 boot loader */
346 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
347 NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
348 timeout = 0;
349 do {
350 msleep(1);
351 val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
353 if (++timeout > 5000)
354 return -EIO;
356 } while (val == NETXEN_BDINFO_MAGIC);
358 return 0;
361 static void netxen_set_port_mode(struct netxen_adapter *adapter)
363 u32 val, data;
365 val = adapter->ahw.board_type;
366 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
367 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
368 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
369 data = NETXEN_PORT_MODE_802_3_AP;
370 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
371 } else if (port_mode == NETXEN_PORT_MODE_XG) {
372 data = NETXEN_PORT_MODE_XG;
373 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
374 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
375 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
376 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
377 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
378 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
379 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
380 } else {
381 data = NETXEN_PORT_MODE_AUTO_NEG;
382 NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
385 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
386 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
387 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
388 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
389 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
391 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
395 #define PCI_CAP_ID_GEN 0x10
397 static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
399 u32 pdevfuncsave;
400 u32 c8c9value = 0;
401 u32 chicken = 0;
402 u32 control = 0;
403 int i, pos;
404 struct pci_dev *pdev;
406 pdev = adapter->pdev;
408 chicken = NXRD32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3));
409 /* clear chicken3.25:24 */
410 chicken &= 0xFCFFFFFF;
412 * if gen1 and B0, set F1020 - if gen 2, do nothing
413 * if gen2 set to F1000
415 pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
416 if (pos == 0xC0) {
417 pci_read_config_dword(pdev, pos + 0x10, &control);
418 if ((control & 0x000F0000) != 0x00020000) {
419 /* set chicken3.24 if gen1 */
420 chicken |= 0x01000000;
422 dev_info(&adapter->pdev->dev, "Gen2 strapping detected\n");
423 c8c9value = 0xF1000;
424 } else {
425 /* set chicken3.24 if gen1 */
426 chicken |= 0x01000000;
427 dev_info(&adapter->pdev->dev, "Gen1 strapping detected\n");
428 if (adapter->ahw.revision_id == NX_P3_B0)
429 c8c9value = 0xF1020;
430 else
431 c8c9value = 0;
434 NXWR32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3), chicken);
436 if (!c8c9value)
437 return;
439 pdevfuncsave = pdev->devfn;
440 if (pdevfuncsave & 0x07)
441 return;
443 for (i = 0; i < 8; i++) {
444 pci_read_config_dword(pdev, pos + 8, &control);
445 pci_read_config_dword(pdev, pos + 8, &control);
446 pci_write_config_dword(pdev, pos + 8, c8c9value);
447 pdev->devfn++;
449 pdev->devfn = pdevfuncsave;
452 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
454 u32 control;
456 if (pdev->msix_cap) {
457 pci_read_config_dword(pdev, pdev->msix_cap, &control);
458 if (enable)
459 control |= PCI_MSIX_FLAGS_ENABLE;
460 else
461 control = 0;
462 pci_write_config_dword(pdev, pdev->msix_cap, control);
466 static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
468 int i;
470 for (i = 0; i < count; i++)
471 adapter->msix_entries[i].entry = i;
474 static int
475 netxen_read_mac_addr(struct netxen_adapter *adapter)
477 int i;
478 unsigned char *p;
479 u64 mac_addr;
480 struct net_device *netdev = adapter->netdev;
481 struct pci_dev *pdev = adapter->pdev;
483 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
484 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
485 return -EIO;
486 } else {
487 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
488 return -EIO;
491 p = (unsigned char *)&mac_addr;
492 for (i = 0; i < 6; i++)
493 netdev->dev_addr[i] = *(p + 5 - i);
495 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
497 /* set station address */
499 if (!is_valid_ether_addr(netdev->dev_addr))
500 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
502 return 0;
505 static int netxen_nic_set_mac(struct net_device *netdev, void *p)
507 struct netxen_adapter *adapter = netdev_priv(netdev);
508 struct sockaddr *addr = p;
510 if (!is_valid_ether_addr(addr->sa_data))
511 return -EADDRNOTAVAIL;
513 if (netif_running(netdev)) {
514 netif_device_detach(netdev);
515 netxen_napi_disable(adapter);
518 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
519 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
520 adapter->macaddr_set(adapter, addr->sa_data);
522 if (netif_running(netdev)) {
523 netif_device_attach(netdev);
524 netxen_napi_enable(adapter);
526 return 0;
529 static void netxen_set_multicast_list(struct net_device *dev)
531 struct netxen_adapter *adapter = netdev_priv(dev);
533 adapter->set_multi(dev);
536 static netdev_features_t netxen_fix_features(struct net_device *dev,
537 netdev_features_t features)
539 if (!(features & NETIF_F_RXCSUM)) {
540 netdev_info(dev, "disabling LRO as RXCSUM is off\n");
542 features &= ~NETIF_F_LRO;
545 return features;
548 static int netxen_set_features(struct net_device *dev,
549 netdev_features_t features)
551 struct netxen_adapter *adapter = netdev_priv(dev);
552 int hw_lro;
554 if (!((dev->features ^ features) & NETIF_F_LRO))
555 return 0;
557 hw_lro = (features & NETIF_F_LRO) ? NETXEN_NIC_LRO_ENABLED
558 : NETXEN_NIC_LRO_DISABLED;
560 if (netxen_config_hw_lro(adapter, hw_lro))
561 return -EIO;
563 if (!(features & NETIF_F_LRO) && netxen_send_lro_cleanup(adapter))
564 return -EIO;
566 return 0;
569 static const struct net_device_ops netxen_netdev_ops = {
570 .ndo_open = netxen_nic_open,
571 .ndo_stop = netxen_nic_close,
572 .ndo_start_xmit = netxen_nic_xmit_frame,
573 .ndo_get_stats64 = netxen_nic_get_stats,
574 .ndo_validate_addr = eth_validate_addr,
575 .ndo_set_rx_mode = netxen_set_multicast_list,
576 .ndo_set_mac_address = netxen_nic_set_mac,
577 .ndo_change_mtu = netxen_nic_change_mtu,
578 .ndo_tx_timeout = netxen_tx_timeout,
579 .ndo_fix_features = netxen_fix_features,
580 .ndo_set_features = netxen_set_features,
583 static inline bool netxen_function_zero(struct pci_dev *pdev)
585 return (PCI_FUNC(pdev->devfn) == 0) ? true : false;
588 static inline void netxen_set_interrupt_mode(struct netxen_adapter *adapter,
589 u32 mode)
591 NXWR32(adapter, NETXEN_INTR_MODE_REG, mode);
594 static inline u32 netxen_get_interrupt_mode(struct netxen_adapter *adapter)
596 return NXRD32(adapter, NETXEN_INTR_MODE_REG);
599 static void
600 netxen_initialize_interrupt_registers(struct netxen_adapter *adapter)
602 struct netxen_legacy_intr_set *legacy_intrp;
603 u32 tgt_status_reg, int_state_reg;
605 if (adapter->ahw.revision_id >= NX_P3_B0)
606 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
607 else
608 legacy_intrp = &legacy_intr[0];
610 tgt_status_reg = legacy_intrp->tgt_status_reg;
611 int_state_reg = ISR_INT_STATE_REG;
613 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
614 adapter->tgt_status_reg = netxen_get_ioaddr(adapter, tgt_status_reg);
615 adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
616 legacy_intrp->tgt_mask_reg);
617 adapter->pci_int_reg = netxen_get_ioaddr(adapter,
618 legacy_intrp->pci_int_reg);
619 adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
621 if (adapter->ahw.revision_id >= NX_P3_B1)
622 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
623 int_state_reg);
624 else
625 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
626 CRB_INT_VECTOR);
629 static int netxen_setup_msi_interrupts(struct netxen_adapter *adapter,
630 int num_msix)
632 struct pci_dev *pdev = adapter->pdev;
633 u32 value;
634 int err;
636 if (adapter->msix_supported) {
637 netxen_init_msix_entries(adapter, num_msix);
638 err = pci_enable_msix_range(pdev, adapter->msix_entries,
639 num_msix, num_msix);
640 if (err > 0) {
641 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
642 netxen_set_msix_bit(pdev, 1);
644 if (adapter->rss_supported)
645 adapter->max_sds_rings = num_msix;
647 dev_info(&pdev->dev, "using msi-x interrupts\n");
648 return 0;
650 /* fall through for msi */
653 if (use_msi && !pci_enable_msi(pdev)) {
654 value = msi_tgt_status[adapter->ahw.pci_func];
655 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
656 adapter->tgt_status_reg = netxen_get_ioaddr(adapter, value);
657 adapter->msix_entries[0].vector = pdev->irq;
658 dev_info(&pdev->dev, "using msi interrupts\n");
659 return 0;
662 dev_err(&pdev->dev, "Failed to acquire MSI-X/MSI interrupt vector\n");
663 return -EIO;
666 static int netxen_setup_intr(struct netxen_adapter *adapter)
668 struct pci_dev *pdev = adapter->pdev;
669 int num_msix;
671 if (adapter->rss_supported)
672 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
673 MSIX_ENTRIES_PER_ADAPTER : 2;
674 else
675 num_msix = 1;
677 adapter->max_sds_rings = 1;
678 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
680 netxen_initialize_interrupt_registers(adapter);
681 netxen_set_msix_bit(pdev, 0);
683 if (netxen_function_zero(pdev)) {
684 if (!netxen_setup_msi_interrupts(adapter, num_msix))
685 netxen_set_interrupt_mode(adapter, NETXEN_MSI_MODE);
686 else
687 netxen_set_interrupt_mode(adapter, NETXEN_INTX_MODE);
688 } else {
689 if (netxen_get_interrupt_mode(adapter) == NETXEN_MSI_MODE &&
690 netxen_setup_msi_interrupts(adapter, num_msix)) {
691 dev_err(&pdev->dev, "Co-existence of MSI-X/MSI and INTx interrupts is not supported\n");
692 return -EIO;
696 if (!NETXEN_IS_MSI_FAMILY(adapter)) {
697 adapter->msix_entries[0].vector = pdev->irq;
698 dev_info(&pdev->dev, "using legacy interrupts\n");
700 return 0;
703 static void
704 netxen_teardown_intr(struct netxen_adapter *adapter)
706 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
707 pci_disable_msix(adapter->pdev);
708 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
709 pci_disable_msi(adapter->pdev);
712 static void
713 netxen_cleanup_pci_map(struct netxen_adapter *adapter)
715 if (adapter->ahw.db_base != NULL)
716 iounmap(adapter->ahw.db_base);
717 if (adapter->ahw.pci_base0 != NULL)
718 iounmap(adapter->ahw.pci_base0);
719 if (adapter->ahw.pci_base1 != NULL)
720 iounmap(adapter->ahw.pci_base1);
721 if (adapter->ahw.pci_base2 != NULL)
722 iounmap(adapter->ahw.pci_base2);
725 static int
726 netxen_setup_pci_map(struct netxen_adapter *adapter)
728 void __iomem *db_ptr = NULL;
730 resource_size_t mem_base, db_base;
731 unsigned long mem_len, db_len = 0;
733 struct pci_dev *pdev = adapter->pdev;
734 int pci_func = adapter->ahw.pci_func;
735 struct netxen_hardware_context *ahw = &adapter->ahw;
737 int err = 0;
740 * Set the CRB window to invalid. If any register in window 0 is
741 * accessed it should set the window to 0 and then reset it to 1.
743 adapter->ahw.crb_win = -1;
744 adapter->ahw.ocm_win = -1;
746 /* remap phys address */
747 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
748 mem_len = pci_resource_len(pdev, 0);
750 /* 128 Meg of memory */
751 if (mem_len == NETXEN_PCI_128MB_SIZE) {
753 ahw->pci_base0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
754 ahw->pci_base1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
755 SECOND_PAGE_GROUP_SIZE);
756 ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
757 THIRD_PAGE_GROUP_SIZE);
758 if (ahw->pci_base0 == NULL || ahw->pci_base1 == NULL ||
759 ahw->pci_base2 == NULL) {
760 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
761 err = -EIO;
762 goto err_out;
765 ahw->pci_len0 = FIRST_PAGE_GROUP_SIZE;
767 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
769 ahw->pci_base1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
770 ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
771 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
772 if (ahw->pci_base1 == NULL || ahw->pci_base2 == NULL) {
773 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
774 err = -EIO;
775 goto err_out;
778 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
780 ahw->pci_base0 = pci_ioremap_bar(pdev, 0);
781 if (ahw->pci_base0 == NULL) {
782 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
783 return -EIO;
785 ahw->pci_len0 = mem_len;
786 } else {
787 return -EIO;
790 netxen_setup_hwops(adapter);
792 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
794 if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
795 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
796 NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
798 } else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
799 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
800 NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func)));
803 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
804 goto skip_doorbell;
806 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
807 db_len = pci_resource_len(pdev, 4);
809 if (db_len == 0) {
810 printk(KERN_ERR "%s: doorbell is disabled\n",
811 netxen_nic_driver_name);
812 err = -EIO;
813 goto err_out;
816 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
817 if (!db_ptr) {
818 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
819 netxen_nic_driver_name);
820 err = -EIO;
821 goto err_out;
824 skip_doorbell:
825 adapter->ahw.db_base = db_ptr;
826 adapter->ahw.db_len = db_len;
827 return 0;
829 err_out:
830 netxen_cleanup_pci_map(adapter);
831 return err;
834 static void
835 netxen_check_options(struct netxen_adapter *adapter)
837 u32 fw_major, fw_minor, fw_build, prev_fw_version;
838 char brd_name[NETXEN_MAX_SHORT_NAME];
839 char serial_num[32];
840 int i, offset, val, err;
841 __le32 *ptr32;
842 struct pci_dev *pdev = adapter->pdev;
844 adapter->driver_mismatch = 0;
846 ptr32 = (__le32 *)&serial_num;
847 offset = NX_FW_SERIAL_NUM_OFFSET;
848 for (i = 0; i < 8; i++) {
849 err = netxen_rom_fast_read(adapter, offset, &val);
850 if (err) {
851 dev_err(&pdev->dev, "error reading board info\n");
852 adapter->driver_mismatch = 1;
853 return;
855 ptr32[i] = cpu_to_le32(val);
856 offset += sizeof(u32);
859 fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
860 fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
861 fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
862 prev_fw_version = adapter->fw_version;
863 adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
865 /* Get FW Mini Coredump template and store it */
866 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
867 if (adapter->mdump.md_template == NULL ||
868 adapter->fw_version > prev_fw_version) {
869 kfree(adapter->mdump.md_template);
870 adapter->mdump.md_template = NULL;
871 err = netxen_setup_minidump(adapter);
872 if (err)
873 dev_err(&adapter->pdev->dev,
874 "Failed to setup minidump rcode = %d\n", err);
878 if (adapter->portnum == 0) {
879 if (netxen_nic_get_brd_name_by_type(adapter->ahw.board_type,
880 brd_name))
881 strcpy(serial_num, "Unknown");
883 pr_info("%s: %s Board S/N %s Chip rev 0x%x\n",
884 module_name(THIS_MODULE),
885 brd_name, serial_num, adapter->ahw.revision_id);
888 if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
889 adapter->driver_mismatch = 1;
890 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
891 fw_major, fw_minor, fw_build);
892 return;
895 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
896 i = NXRD32(adapter, NETXEN_SRE_MISC);
897 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
900 dev_info(&pdev->dev, "Driver v%s, firmware v%d.%d.%d [%s]\n",
901 NETXEN_NIC_LINUX_VERSIONID, fw_major, fw_minor, fw_build,
902 adapter->ahw.cut_through ? "cut-through" : "legacy");
904 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
905 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
907 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
908 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
909 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
910 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
911 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
912 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
915 adapter->msix_supported = 0;
916 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
917 adapter->msix_supported = !!use_msi_x;
918 adapter->rss_supported = !!use_msi_x;
919 } else {
920 u32 flashed_ver = 0;
921 netxen_rom_fast_read(adapter,
922 NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
923 flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
925 if (flashed_ver >= NETXEN_VERSION_CODE(3, 4, 336)) {
926 switch (adapter->ahw.board_type) {
927 case NETXEN_BRDTYPE_P2_SB31_10G:
928 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
929 adapter->msix_supported = !!use_msi_x;
930 adapter->rss_supported = !!use_msi_x;
931 break;
932 default:
933 break;
938 adapter->num_txd = MAX_CMD_DESCRIPTORS;
940 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
941 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
942 adapter->max_rds_rings = 3;
943 } else {
944 adapter->num_lro_rxd = 0;
945 adapter->max_rds_rings = 2;
949 static int
950 netxen_start_firmware(struct netxen_adapter *adapter)
952 int val, err, first_boot;
953 struct pci_dev *pdev = adapter->pdev;
955 /* required for NX2031 dummy dma */
956 err = nx_set_dma_mask(adapter);
957 if (err)
958 return err;
960 err = netxen_can_start_firmware(adapter);
962 if (err < 0)
963 return err;
965 if (!err)
966 goto wait_init;
968 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
970 err = netxen_check_hw_init(adapter, first_boot);
971 if (err) {
972 dev_err(&pdev->dev, "error in init HW init sequence\n");
973 return err;
976 netxen_request_firmware(adapter);
978 err = netxen_need_fw_reset(adapter);
979 if (err < 0)
980 goto err_out;
981 if (err == 0)
982 goto pcie_strap_init;
984 if (first_boot != 0x55555555) {
985 NXWR32(adapter, CRB_CMDPEG_STATE, 0);
986 netxen_pinit_from_rom(adapter);
987 msleep(1);
990 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
991 NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
992 NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
994 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
995 netxen_set_port_mode(adapter);
997 err = netxen_load_firmware(adapter);
998 if (err)
999 goto err_out;
1001 netxen_release_firmware(adapter);
1003 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1005 /* Initialize multicast addr pool owners */
1006 val = 0x7654;
1007 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
1008 val |= 0x0f000000;
1009 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
1013 err = netxen_init_dummy_dma(adapter);
1014 if (err)
1015 goto err_out;
1018 * Tell the hardware our version number.
1020 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
1021 | ((_NETXEN_NIC_LINUX_MINOR << 8))
1022 | (_NETXEN_NIC_LINUX_SUBVERSION);
1023 NXWR32(adapter, CRB_DRIVER_VERSION, val);
1025 pcie_strap_init:
1026 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1027 netxen_pcie_strap_init(adapter);
1029 wait_init:
1030 /* Handshake with the card before we register the devices. */
1031 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
1032 if (err) {
1033 netxen_free_dummy_dma(adapter);
1034 goto err_out;
1037 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
1039 nx_update_dma_mask(adapter);
1041 netxen_check_options(adapter);
1043 adapter->need_fw_reset = 0;
1045 /* fall through and release firmware */
1047 err_out:
1048 netxen_release_firmware(adapter);
1049 return err;
1052 static int
1053 netxen_nic_request_irq(struct netxen_adapter *adapter)
1055 irq_handler_t handler;
1056 struct nx_host_sds_ring *sds_ring;
1057 int err, ring;
1059 unsigned long flags = 0;
1060 struct net_device *netdev = adapter->netdev;
1061 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1063 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1064 handler = netxen_msix_intr;
1065 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
1066 handler = netxen_msi_intr;
1067 else {
1068 flags |= IRQF_SHARED;
1069 handler = netxen_intr;
1071 adapter->irq = netdev->irq;
1073 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1074 sds_ring = &recv_ctx->sds_rings[ring];
1075 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1076 err = request_irq(sds_ring->irq, handler,
1077 flags, sds_ring->name, sds_ring);
1078 if (err)
1079 return err;
1082 return 0;
1085 static void
1086 netxen_nic_free_irq(struct netxen_adapter *adapter)
1088 int ring;
1089 struct nx_host_sds_ring *sds_ring;
1091 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
1093 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1094 sds_ring = &recv_ctx->sds_rings[ring];
1095 free_irq(sds_ring->irq, sds_ring);
1099 static void
1100 netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
1102 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
1103 adapter->coal.normal.data.rx_time_us =
1104 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
1105 adapter->coal.normal.data.rx_packets =
1106 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
1107 adapter->coal.normal.data.tx_time_us =
1108 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
1109 adapter->coal.normal.data.tx_packets =
1110 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
1113 /* with rtnl_lock */
1114 static int
1115 __netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1117 int err;
1119 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1120 return -EIO;
1122 err = adapter->init_port(adapter, adapter->physical_port);
1123 if (err) {
1124 printk(KERN_ERR "%s: Failed to initialize port %d\n",
1125 netxen_nic_driver_name, adapter->portnum);
1126 return err;
1128 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
1129 adapter->macaddr_set(adapter, adapter->mac_addr);
1131 adapter->set_multi(netdev);
1132 adapter->set_mtu(adapter, netdev->mtu);
1134 adapter->ahw.linkup = 0;
1136 if (adapter->max_sds_rings > 1)
1137 netxen_config_rss(adapter, 1);
1139 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1140 netxen_config_intr_coalesce(adapter);
1142 if (netdev->features & NETIF_F_LRO)
1143 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
1145 netxen_napi_enable(adapter);
1147 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1148 netxen_linkevent_request(adapter, 1);
1149 else
1150 netxen_nic_set_link_parameters(adapter);
1152 set_bit(__NX_DEV_UP, &adapter->state);
1153 return 0;
1156 /* Usage: During resume and firmware recovery module.*/
1158 static inline int
1159 netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
1161 int err = 0;
1163 rtnl_lock();
1164 if (netif_running(netdev))
1165 err = __netxen_nic_up(adapter, netdev);
1166 rtnl_unlock();
1168 return err;
1171 /* with rtnl_lock */
1172 static void
1173 __netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1175 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1176 return;
1178 if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state))
1179 return;
1181 smp_mb();
1182 netif_carrier_off(netdev);
1183 netif_tx_disable(netdev);
1185 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
1186 netxen_linkevent_request(adapter, 0);
1188 if (adapter->stop_port)
1189 adapter->stop_port(adapter);
1191 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1192 netxen_p3_free_mac_list(adapter);
1194 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
1196 netxen_napi_disable(adapter);
1198 netxen_release_tx_buffers(adapter);
1201 /* Usage: During suspend and firmware recovery module */
1203 static inline void
1204 netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
1206 rtnl_lock();
1207 if (netif_running(netdev))
1208 __netxen_nic_down(adapter, netdev);
1209 rtnl_unlock();
1213 static int
1214 netxen_nic_attach(struct netxen_adapter *adapter)
1216 struct net_device *netdev = adapter->netdev;
1217 struct pci_dev *pdev = adapter->pdev;
1218 int err, ring;
1219 struct nx_host_rds_ring *rds_ring;
1220 struct nx_host_tx_ring *tx_ring;
1221 u32 capab2;
1223 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
1224 return 0;
1226 err = netxen_init_firmware(adapter);
1227 if (err)
1228 return err;
1230 adapter->flags &= ~NETXEN_FW_MSS_CAP;
1231 if (adapter->capabilities & NX_FW_CAPABILITY_MORE_CAPS) {
1232 capab2 = NXRD32(adapter, CRB_FW_CAPABILITIES_2);
1233 if (capab2 & NX_FW_CAPABILITY_2_LRO_MAX_TCP_SEG)
1234 adapter->flags |= NETXEN_FW_MSS_CAP;
1237 err = netxen_napi_add(adapter, netdev);
1238 if (err)
1239 return err;
1241 err = netxen_alloc_sw_resources(adapter);
1242 if (err) {
1243 printk(KERN_ERR "%s: Error in setting sw resources\n",
1244 netdev->name);
1245 return err;
1248 err = netxen_alloc_hw_resources(adapter);
1249 if (err) {
1250 printk(KERN_ERR "%s: Error in setting hw resources\n",
1251 netdev->name);
1252 goto err_out_free_sw;
1255 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1256 tx_ring = adapter->tx_ring;
1257 tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
1258 crb_cmd_producer[adapter->portnum]);
1259 tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
1260 crb_cmd_consumer[adapter->portnum]);
1262 tx_ring->producer = 0;
1263 tx_ring->sw_consumer = 0;
1265 netxen_nic_update_cmd_producer(adapter, tx_ring);
1266 netxen_nic_update_cmd_consumer(adapter, tx_ring);
1269 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1270 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1271 netxen_post_rx_buffers(adapter, ring, rds_ring);
1274 err = netxen_nic_request_irq(adapter);
1275 if (err) {
1276 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
1277 netdev->name);
1278 goto err_out_free_rxbuf;
1281 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1282 netxen_nic_init_coalesce_defaults(adapter);
1284 netxen_create_sysfs_entries(adapter);
1286 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
1287 return 0;
1289 err_out_free_rxbuf:
1290 netxen_release_rx_buffers(adapter);
1291 netxen_free_hw_resources(adapter);
1292 err_out_free_sw:
1293 netxen_free_sw_resources(adapter);
1294 return err;
1297 static void
1298 netxen_nic_detach(struct netxen_adapter *adapter)
1300 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
1301 return;
1303 netxen_remove_sysfs_entries(adapter);
1305 netxen_free_hw_resources(adapter);
1306 netxen_release_rx_buffers(adapter);
1307 netxen_nic_free_irq(adapter);
1308 netxen_napi_del(adapter);
1309 netxen_free_sw_resources(adapter);
1311 adapter->is_up = 0;
1315 netxen_nic_reset_context(struct netxen_adapter *adapter)
1317 int err = 0;
1318 struct net_device *netdev = adapter->netdev;
1320 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
1321 return -EBUSY;
1323 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
1325 netif_device_detach(netdev);
1327 if (netif_running(netdev))
1328 __netxen_nic_down(adapter, netdev);
1330 netxen_nic_detach(adapter);
1332 if (netif_running(netdev)) {
1333 err = netxen_nic_attach(adapter);
1334 if (!err)
1335 err = __netxen_nic_up(adapter, netdev);
1337 if (err)
1338 goto done;
1341 netif_device_attach(netdev);
1344 done:
1345 clear_bit(__NX_RESETTING, &adapter->state);
1346 return err;
1349 static int
1350 netxen_setup_netdev(struct netxen_adapter *adapter,
1351 struct net_device *netdev)
1353 int err = 0;
1354 struct pci_dev *pdev = adapter->pdev;
1356 adapter->mc_enabled = 0;
1357 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1358 adapter->max_mc_count = 38;
1359 else
1360 adapter->max_mc_count = 16;
1362 netdev->netdev_ops = &netxen_netdev_ops;
1363 netdev->watchdog_timeo = 5*HZ;
1365 netxen_nic_change_mtu(netdev, netdev->mtu);
1367 netdev->ethtool_ops = &netxen_nic_ethtool_ops;
1369 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
1370 NETIF_F_RXCSUM;
1372 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1373 netdev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
1375 netdev->vlan_features |= netdev->hw_features;
1377 if (adapter->pci_using_dac) {
1378 netdev->features |= NETIF_F_HIGHDMA;
1379 netdev->vlan_features |= NETIF_F_HIGHDMA;
1382 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
1383 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
1385 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
1386 netdev->hw_features |= NETIF_F_LRO;
1388 netdev->features |= netdev->hw_features;
1390 netdev->irq = adapter->msix_entries[0].vector;
1392 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1394 if (netxen_read_mac_addr(adapter))
1395 dev_warn(&pdev->dev, "failed to read mac addr\n");
1397 netif_carrier_off(netdev);
1399 err = register_netdev(netdev);
1400 if (err) {
1401 dev_err(&pdev->dev, "failed to register net device\n");
1402 return err;
1405 return 0;
1408 #define NETXEN_ULA_ADAPTER_KEY (0xdaddad01)
1409 #define NETXEN_NON_ULA_ADAPTER_KEY (0xdaddad00)
1411 static void netxen_read_ula_info(struct netxen_adapter *adapter)
1413 u32 temp;
1415 /* Print ULA info only once for an adapter */
1416 if (adapter->portnum != 0)
1417 return;
1419 temp = NXRD32(adapter, NETXEN_ULA_KEY);
1420 switch (temp) {
1421 case NETXEN_ULA_ADAPTER_KEY:
1422 dev_info(&adapter->pdev->dev, "ULA adapter");
1423 break;
1424 case NETXEN_NON_ULA_ADAPTER_KEY:
1425 dev_info(&adapter->pdev->dev, "non ULA adapter");
1426 break;
1427 default:
1428 break;
1431 return;
1434 #ifdef CONFIG_PCIEAER
1435 static void netxen_mask_aer_correctable(struct netxen_adapter *adapter)
1437 struct pci_dev *pdev = adapter->pdev;
1438 struct pci_dev *root = pdev->bus->self;
1439 u32 aer_pos;
1441 /* root bus? */
1442 if (!root)
1443 return;
1445 if (adapter->ahw.board_type != NETXEN_BRDTYPE_P3_4_GB_MM &&
1446 adapter->ahw.board_type != NETXEN_BRDTYPE_P3_10G_TP)
1447 return;
1449 if (pci_pcie_type(root) != PCI_EXP_TYPE_ROOT_PORT)
1450 return;
1452 aer_pos = pci_find_ext_capability(root, PCI_EXT_CAP_ID_ERR);
1453 if (!aer_pos)
1454 return;
1456 pci_write_config_dword(root, aer_pos + PCI_ERR_COR_MASK, 0xffff);
1458 #endif
1460 static int
1461 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1463 struct net_device *netdev = NULL;
1464 struct netxen_adapter *adapter = NULL;
1465 int i = 0, err;
1466 int pci_func_id = PCI_FUNC(pdev->devfn);
1467 uint8_t revision_id;
1468 u32 val;
1470 if (pdev->revision >= NX_P3_A0 && pdev->revision <= NX_P3_B1) {
1471 pr_warn("%s: chip revisions between 0x%x-0x%x will not be enabled\n",
1472 module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
1473 return -ENODEV;
1476 if ((err = pci_enable_device(pdev)))
1477 return err;
1479 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1480 err = -ENODEV;
1481 goto err_out_disable_pdev;
1484 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
1485 goto err_out_disable_pdev;
1487 if (NX_IS_REVISION_P3(pdev->revision))
1488 pci_enable_pcie_error_reporting(pdev);
1490 pci_set_master(pdev);
1492 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
1493 if(!netdev) {
1494 err = -ENOMEM;
1495 goto err_out_free_res;
1498 SET_NETDEV_DEV(netdev, &pdev->dev);
1500 adapter = netdev_priv(netdev);
1501 adapter->netdev = netdev;
1502 adapter->pdev = pdev;
1503 adapter->ahw.pci_func = pci_func_id;
1505 revision_id = pdev->revision;
1506 adapter->ahw.revision_id = revision_id;
1508 rwlock_init(&adapter->ahw.crb_lock);
1509 spin_lock_init(&adapter->ahw.mem_lock);
1511 spin_lock_init(&adapter->tx_clean_lock);
1512 INIT_LIST_HEAD(&adapter->mac_list);
1513 INIT_LIST_HEAD(&adapter->ip_list);
1515 err = netxen_setup_pci_map(adapter);
1516 if (err)
1517 goto err_out_free_netdev;
1519 /* This will be reset for mezz cards */
1520 adapter->portnum = pci_func_id;
1522 err = netxen_nic_get_board_info(adapter);
1523 if (err) {
1524 dev_err(&pdev->dev, "Error getting board config info.\n");
1525 goto err_out_iounmap;
1528 #ifdef CONFIG_PCIEAER
1529 netxen_mask_aer_correctable(adapter);
1530 #endif
1532 /* Mezz cards have PCI function 0,2,3 enabled */
1533 switch (adapter->ahw.board_type) {
1534 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
1535 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
1536 if (pci_func_id >= 2)
1537 adapter->portnum = pci_func_id - 2;
1538 break;
1539 default:
1540 break;
1543 err = netxen_check_flash_fw_compatibility(adapter);
1544 if (err)
1545 goto err_out_iounmap;
1547 if (adapter->portnum == 0) {
1548 val = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
1549 if (val != 0xffffffff && val != 0) {
1550 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, 0);
1551 adapter->need_fw_reset = 1;
1555 err = netxen_start_firmware(adapter);
1556 if (err)
1557 goto err_out_decr_ref;
1560 * See if the firmware gave us a virtual-physical port mapping.
1562 adapter->physical_port = adapter->portnum;
1563 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1564 i = NXRD32(adapter, CRB_V2P(adapter->portnum));
1565 if (i != 0x55555555)
1566 adapter->physical_port = i;
1569 /* MTU range: 0 - 8000 (P2) or 9600 (P3) */
1570 netdev->min_mtu = 0;
1571 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1572 netdev->max_mtu = P3_MAX_MTU;
1573 else
1574 netdev->max_mtu = P2_MAX_MTU;
1576 netxen_nic_clear_stats(adapter);
1578 err = netxen_setup_intr(adapter);
1580 if (err) {
1581 dev_err(&adapter->pdev->dev,
1582 "Failed to setup interrupts, error = %d\n", err);
1583 goto err_out_disable_msi;
1586 netxen_read_ula_info(adapter);
1588 err = netxen_setup_netdev(adapter, netdev);
1589 if (err)
1590 goto err_out_disable_msi;
1592 pci_set_drvdata(pdev, adapter);
1594 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1596 switch (adapter->ahw.port_type) {
1597 case NETXEN_NIC_GBE:
1598 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1599 adapter->netdev->name);
1600 break;
1601 case NETXEN_NIC_XGBE:
1602 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1603 adapter->netdev->name);
1604 break;
1607 netxen_create_diag_entries(adapter);
1609 return 0;
1611 err_out_disable_msi:
1612 netxen_teardown_intr(adapter);
1614 netxen_free_dummy_dma(adapter);
1616 err_out_decr_ref:
1617 nx_decr_dev_ref_cnt(adapter);
1619 err_out_iounmap:
1620 netxen_cleanup_pci_map(adapter);
1622 err_out_free_netdev:
1623 free_netdev(netdev);
1625 err_out_free_res:
1626 pci_release_regions(pdev);
1628 err_out_disable_pdev:
1629 pci_disable_device(pdev);
1630 return err;
1633 static
1634 void netxen_cleanup_minidump(struct netxen_adapter *adapter)
1636 kfree(adapter->mdump.md_template);
1637 adapter->mdump.md_template = NULL;
1639 if (adapter->mdump.md_capture_buff) {
1640 vfree(adapter->mdump.md_capture_buff);
1641 adapter->mdump.md_capture_buff = NULL;
1645 static void netxen_nic_remove(struct pci_dev *pdev)
1647 struct netxen_adapter *adapter;
1648 struct net_device *netdev;
1650 adapter = pci_get_drvdata(pdev);
1651 if (adapter == NULL)
1652 return;
1654 netdev = adapter->netdev;
1656 netxen_cancel_fw_work(adapter);
1658 unregister_netdev(netdev);
1660 cancel_work_sync(&adapter->tx_timeout_task);
1662 netxen_free_ip_list(adapter, false);
1663 netxen_nic_detach(adapter);
1665 nx_decr_dev_ref_cnt(adapter);
1667 if (adapter->portnum == 0)
1668 netxen_free_dummy_dma(adapter);
1670 clear_bit(__NX_RESETTING, &adapter->state);
1672 netxen_teardown_intr(adapter);
1673 netxen_set_interrupt_mode(adapter, 0);
1674 netxen_remove_diag_entries(adapter);
1676 netxen_cleanup_pci_map(adapter);
1678 netxen_release_firmware(adapter);
1680 if (NX_IS_REVISION_P3(pdev->revision)) {
1681 netxen_cleanup_minidump(adapter);
1682 pci_disable_pcie_error_reporting(pdev);
1685 pci_release_regions(pdev);
1686 pci_disable_device(pdev);
1688 free_netdev(netdev);
1691 static void netxen_nic_detach_func(struct netxen_adapter *adapter)
1693 struct net_device *netdev = adapter->netdev;
1695 netif_device_detach(netdev);
1697 netxen_cancel_fw_work(adapter);
1699 if (netif_running(netdev))
1700 netxen_nic_down(adapter, netdev);
1702 cancel_work_sync(&adapter->tx_timeout_task);
1704 netxen_nic_detach(adapter);
1706 if (adapter->portnum == 0)
1707 netxen_free_dummy_dma(adapter);
1709 nx_decr_dev_ref_cnt(adapter);
1711 clear_bit(__NX_RESETTING, &adapter->state);
1714 static int netxen_nic_attach_func(struct pci_dev *pdev)
1716 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1717 struct net_device *netdev = adapter->netdev;
1718 int err;
1720 err = pci_enable_device(pdev);
1721 if (err)
1722 return err;
1724 pci_set_power_state(pdev, PCI_D0);
1725 pci_set_master(pdev);
1726 pci_restore_state(pdev);
1728 adapter->ahw.crb_win = -1;
1729 adapter->ahw.ocm_win = -1;
1731 err = netxen_start_firmware(adapter);
1732 if (err) {
1733 dev_err(&pdev->dev, "failed to start firmware\n");
1734 return err;
1737 if (netif_running(netdev)) {
1738 err = netxen_nic_attach(adapter);
1739 if (err)
1740 goto err_out;
1742 err = netxen_nic_up(adapter, netdev);
1743 if (err)
1744 goto err_out_detach;
1746 netxen_restore_indev_addr(netdev, NETDEV_UP);
1749 netif_device_attach(netdev);
1750 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
1751 return 0;
1753 err_out_detach:
1754 netxen_nic_detach(adapter);
1755 err_out:
1756 nx_decr_dev_ref_cnt(adapter);
1757 return err;
1760 static pci_ers_result_t netxen_io_error_detected(struct pci_dev *pdev,
1761 pci_channel_state_t state)
1763 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1765 if (state == pci_channel_io_perm_failure)
1766 return PCI_ERS_RESULT_DISCONNECT;
1768 if (nx_dev_request_aer(adapter))
1769 return PCI_ERS_RESULT_RECOVERED;
1771 netxen_nic_detach_func(adapter);
1773 pci_disable_device(pdev);
1775 return PCI_ERS_RESULT_NEED_RESET;
1778 static pci_ers_result_t netxen_io_slot_reset(struct pci_dev *pdev)
1780 int err = 0;
1782 err = netxen_nic_attach_func(pdev);
1784 return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
1787 static void netxen_io_resume(struct pci_dev *pdev)
1789 pci_cleanup_aer_uncorrect_error_status(pdev);
1792 static void netxen_nic_shutdown(struct pci_dev *pdev)
1794 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1796 netxen_nic_detach_func(adapter);
1798 if (pci_save_state(pdev))
1799 return;
1801 if (netxen_nic_wol_supported(adapter)) {
1802 pci_enable_wake(pdev, PCI_D3cold, 1);
1803 pci_enable_wake(pdev, PCI_D3hot, 1);
1806 pci_disable_device(pdev);
1809 #ifdef CONFIG_PM
1810 static int
1811 netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
1813 struct netxen_adapter *adapter = pci_get_drvdata(pdev);
1814 int retval;
1816 netxen_nic_detach_func(adapter);
1818 retval = pci_save_state(pdev);
1819 if (retval)
1820 return retval;
1822 if (netxen_nic_wol_supported(adapter)) {
1823 pci_enable_wake(pdev, PCI_D3cold, 1);
1824 pci_enable_wake(pdev, PCI_D3hot, 1);
1827 pci_disable_device(pdev);
1828 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1830 return 0;
1833 static int
1834 netxen_nic_resume(struct pci_dev *pdev)
1836 return netxen_nic_attach_func(pdev);
1838 #endif
1840 static int netxen_nic_open(struct net_device *netdev)
1842 struct netxen_adapter *adapter = netdev_priv(netdev);
1843 int err = 0;
1845 if (adapter->driver_mismatch)
1846 return -EIO;
1848 err = netxen_nic_attach(adapter);
1849 if (err)
1850 return err;
1852 err = __netxen_nic_up(adapter, netdev);
1853 if (err)
1854 goto err_out;
1856 netif_start_queue(netdev);
1858 return 0;
1860 err_out:
1861 netxen_nic_detach(adapter);
1862 return err;
1866 * netxen_nic_close - Disables a network interface entry point
1868 static int netxen_nic_close(struct net_device *netdev)
1870 struct netxen_adapter *adapter = netdev_priv(netdev);
1872 __netxen_nic_down(adapter, netdev);
1873 return 0;
1876 static void
1877 netxen_tso_check(struct net_device *netdev,
1878 struct nx_host_tx_ring *tx_ring,
1879 struct cmd_desc_type0 *first_desc,
1880 struct sk_buff *skb)
1882 u8 opcode = TX_ETHER_PKT;
1883 __be16 protocol = skb->protocol;
1884 u16 flags = 0, vid = 0;
1885 u32 producer;
1886 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1887 struct cmd_desc_type0 *hwdesc;
1888 struct vlan_ethhdr *vh;
1890 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1892 vh = (struct vlan_ethhdr *)skb->data;
1893 protocol = vh->h_vlan_encapsulated_proto;
1894 flags = FLAGS_VLAN_TAGGED;
1896 } else if (skb_vlan_tag_present(skb)) {
1897 flags = FLAGS_VLAN_OOB;
1898 vid = skb_vlan_tag_get(skb);
1899 netxen_set_tx_vlan_tci(first_desc, vid);
1900 vlan_oob = 1;
1903 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1904 skb_shinfo(skb)->gso_size > 0) {
1906 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1908 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1909 first_desc->total_hdr_length = hdr_len;
1910 if (vlan_oob) {
1911 first_desc->total_hdr_length += VLAN_HLEN;
1912 first_desc->tcp_hdr_offset = VLAN_HLEN;
1913 first_desc->ip_hdr_offset = VLAN_HLEN;
1914 /* Only in case of TSO on vlan device */
1915 flags |= FLAGS_VLAN_TAGGED;
1918 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1919 TX_TCP_LSO6 : TX_TCP_LSO;
1920 tso = 1;
1922 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1923 u8 l4proto;
1925 if (protocol == cpu_to_be16(ETH_P_IP)) {
1926 l4proto = ip_hdr(skb)->protocol;
1928 if (l4proto == IPPROTO_TCP)
1929 opcode = TX_TCP_PKT;
1930 else if(l4proto == IPPROTO_UDP)
1931 opcode = TX_UDP_PKT;
1932 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1933 l4proto = ipv6_hdr(skb)->nexthdr;
1935 if (l4proto == IPPROTO_TCP)
1936 opcode = TX_TCPV6_PKT;
1937 else if(l4proto == IPPROTO_UDP)
1938 opcode = TX_UDPV6_PKT;
1942 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1943 first_desc->ip_hdr_offset += skb_network_offset(skb);
1944 netxen_set_tx_flags_opcode(first_desc, flags, opcode);
1946 if (!tso)
1947 return;
1949 /* For LSO, we need to copy the MAC/IP/TCP headers into
1950 * the descriptor ring
1952 producer = tx_ring->producer;
1953 copied = 0;
1954 offset = 2;
1956 if (vlan_oob) {
1957 /* Create a TSO vlan header template for firmware */
1959 hwdesc = &tx_ring->desc_head[producer];
1960 tx_ring->cmd_buf_arr[producer].skb = NULL;
1962 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1963 hdr_len + VLAN_HLEN);
1965 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1966 skb_copy_from_linear_data(skb, vh, 12);
1967 vh->h_vlan_proto = htons(ETH_P_8021Q);
1968 vh->h_vlan_TCI = htons(vid);
1969 skb_copy_from_linear_data_offset(skb, 12,
1970 (char *)vh + 16, copy_len - 16);
1972 copied = copy_len - VLAN_HLEN;
1973 offset = 0;
1975 producer = get_next_index(producer, tx_ring->num_desc);
1978 while (copied < hdr_len) {
1980 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1981 (hdr_len - copied));
1983 hwdesc = &tx_ring->desc_head[producer];
1984 tx_ring->cmd_buf_arr[producer].skb = NULL;
1986 skb_copy_from_linear_data_offset(skb, copied,
1987 (char *)hwdesc + offset, copy_len);
1989 copied += copy_len;
1990 offset = 0;
1992 producer = get_next_index(producer, tx_ring->num_desc);
1995 tx_ring->producer = producer;
1996 barrier();
1999 static int
2000 netxen_map_tx_skb(struct pci_dev *pdev,
2001 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
2003 struct netxen_skb_frag *nf;
2004 struct skb_frag_struct *frag;
2005 int i, nr_frags;
2006 dma_addr_t map;
2008 nr_frags = skb_shinfo(skb)->nr_frags;
2009 nf = &pbuf->frag_array[0];
2011 map = pci_map_single(pdev, skb->data,
2012 skb_headlen(skb), PCI_DMA_TODEVICE);
2013 if (pci_dma_mapping_error(pdev, map))
2014 goto out_err;
2016 nf->dma = map;
2017 nf->length = skb_headlen(skb);
2019 for (i = 0; i < nr_frags; i++) {
2020 frag = &skb_shinfo(skb)->frags[i];
2021 nf = &pbuf->frag_array[i+1];
2023 map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
2024 DMA_TO_DEVICE);
2025 if (dma_mapping_error(&pdev->dev, map))
2026 goto unwind;
2028 nf->dma = map;
2029 nf->length = skb_frag_size(frag);
2032 return 0;
2034 unwind:
2035 while (--i >= 0) {
2036 nf = &pbuf->frag_array[i+1];
2037 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2038 nf->dma = 0ULL;
2041 nf = &pbuf->frag_array[0];
2042 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2043 nf->dma = 0ULL;
2045 out_err:
2046 return -ENOMEM;
2049 static inline void
2050 netxen_clear_cmddesc(u64 *desc)
2052 desc[0] = 0ULL;
2053 desc[2] = 0ULL;
2056 static netdev_tx_t
2057 netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2059 struct netxen_adapter *adapter = netdev_priv(netdev);
2060 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
2061 struct netxen_cmd_buffer *pbuf;
2062 struct netxen_skb_frag *buffrag;
2063 struct cmd_desc_type0 *hwdesc, *first_desc;
2064 struct pci_dev *pdev;
2065 int i, k;
2066 int delta = 0;
2067 struct skb_frag_struct *frag;
2069 u32 producer;
2070 int frag_count;
2071 u32 num_txd = tx_ring->num_desc;
2073 frag_count = skb_shinfo(skb)->nr_frags + 1;
2075 /* 14 frags supported for normal packet and
2076 * 32 frags supported for TSO packet
2078 if (!skb_is_gso(skb) && frag_count > NETXEN_MAX_FRAGS_PER_TX) {
2080 for (i = 0; i < (frag_count - NETXEN_MAX_FRAGS_PER_TX); i++) {
2081 frag = &skb_shinfo(skb)->frags[i];
2082 delta += skb_frag_size(frag);
2085 if (!__pskb_pull_tail(skb, delta))
2086 goto drop_packet;
2088 frag_count = 1 + skb_shinfo(skb)->nr_frags;
2091 if (unlikely(netxen_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
2092 netif_stop_queue(netdev);
2093 smp_mb();
2094 if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH)
2095 netif_start_queue(netdev);
2096 else
2097 return NETDEV_TX_BUSY;
2100 producer = tx_ring->producer;
2101 pbuf = &tx_ring->cmd_buf_arr[producer];
2103 pdev = adapter->pdev;
2105 if (netxen_map_tx_skb(pdev, skb, pbuf))
2106 goto drop_packet;
2108 pbuf->skb = skb;
2109 pbuf->frag_count = frag_count;
2111 first_desc = hwdesc = &tx_ring->desc_head[producer];
2112 netxen_clear_cmddesc((u64 *)hwdesc);
2114 netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
2115 netxen_set_tx_port(first_desc, adapter->portnum);
2117 for (i = 0; i < frag_count; i++) {
2119 k = i % 4;
2121 if ((k == 0) && (i > 0)) {
2122 /* move to next desc.*/
2123 producer = get_next_index(producer, num_txd);
2124 hwdesc = &tx_ring->desc_head[producer];
2125 netxen_clear_cmddesc((u64 *)hwdesc);
2126 tx_ring->cmd_buf_arr[producer].skb = NULL;
2129 buffrag = &pbuf->frag_array[i];
2131 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2132 switch (k) {
2133 case 0:
2134 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2135 break;
2136 case 1:
2137 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2138 break;
2139 case 2:
2140 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2141 break;
2142 case 3:
2143 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2144 break;
2148 tx_ring->producer = get_next_index(producer, num_txd);
2150 netxen_tso_check(netdev, tx_ring, first_desc, skb);
2152 adapter->stats.txbytes += skb->len;
2153 adapter->stats.xmitcalled++;
2155 netxen_nic_update_cmd_producer(adapter, tx_ring);
2157 return NETDEV_TX_OK;
2159 drop_packet:
2160 adapter->stats.txdropped++;
2161 dev_kfree_skb_any(skb);
2162 return NETDEV_TX_OK;
2165 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
2167 struct net_device *netdev = adapter->netdev;
2168 uint32_t temp, temp_state, temp_val;
2169 int rv = 0;
2171 temp = NXRD32(adapter, CRB_TEMP_STATE);
2173 temp_state = nx_get_temp_state(temp);
2174 temp_val = nx_get_temp_val(temp);
2176 if (temp_state == NX_TEMP_PANIC) {
2177 printk(KERN_ALERT
2178 "%s: Device temperature %d degrees C exceeds"
2179 " maximum allowed. Hardware has been shut down.\n",
2180 netdev->name, temp_val);
2181 rv = 1;
2182 } else if (temp_state == NX_TEMP_WARN) {
2183 if (adapter->temp == NX_TEMP_NORMAL) {
2184 printk(KERN_ALERT
2185 "%s: Device temperature %d degrees C "
2186 "exceeds operating range."
2187 " Immediate action needed.\n",
2188 netdev->name, temp_val);
2190 } else {
2191 if (adapter->temp == NX_TEMP_WARN) {
2192 printk(KERN_INFO
2193 "%s: Device temperature is now %d degrees C"
2194 " in normal range.\n", netdev->name,
2195 temp_val);
2198 adapter->temp = temp_state;
2199 return rv;
2202 void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
2204 struct net_device *netdev = adapter->netdev;
2206 if (adapter->ahw.linkup && !linkup) {
2207 printk(KERN_INFO "%s: %s NIC Link is down\n",
2208 netxen_nic_driver_name, netdev->name);
2209 adapter->ahw.linkup = 0;
2210 if (netif_running(netdev)) {
2211 netif_carrier_off(netdev);
2212 netif_stop_queue(netdev);
2214 adapter->link_changed = !adapter->has_link_events;
2215 } else if (!adapter->ahw.linkup && linkup) {
2216 printk(KERN_INFO "%s: %s NIC Link is up\n",
2217 netxen_nic_driver_name, netdev->name);
2218 adapter->ahw.linkup = 1;
2219 if (netif_running(netdev)) {
2220 netif_carrier_on(netdev);
2221 netif_wake_queue(netdev);
2223 adapter->link_changed = !adapter->has_link_events;
2227 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
2229 u32 val, port, linkup;
2231 port = adapter->physical_port;
2233 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2234 val = NXRD32(adapter, CRB_XG_STATE_P3);
2235 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
2236 linkup = (val == XG_LINK_UP_P3);
2237 } else {
2238 val = NXRD32(adapter, CRB_XG_STATE);
2239 val = (val >> port*8) & 0xff;
2240 linkup = (val == XG_LINK_UP);
2243 netxen_advert_link_change(adapter, linkup);
2246 static void netxen_tx_timeout(struct net_device *netdev)
2248 struct netxen_adapter *adapter = netdev_priv(netdev);
2250 if (test_bit(__NX_RESETTING, &adapter->state))
2251 return;
2253 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
2254 schedule_work(&adapter->tx_timeout_task);
2257 static void netxen_tx_timeout_task(struct work_struct *work)
2259 struct netxen_adapter *adapter =
2260 container_of(work, struct netxen_adapter, tx_timeout_task);
2262 if (!netif_running(adapter->netdev))
2263 return;
2265 if (test_and_set_bit(__NX_RESETTING, &adapter->state))
2266 return;
2268 if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
2269 goto request_reset;
2271 rtnl_lock();
2272 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
2273 /* try to scrub interrupt */
2274 netxen_napi_disable(adapter);
2276 netxen_napi_enable(adapter);
2278 netif_wake_queue(adapter->netdev);
2280 clear_bit(__NX_RESETTING, &adapter->state);
2281 } else {
2282 clear_bit(__NX_RESETTING, &adapter->state);
2283 if (netxen_nic_reset_context(adapter)) {
2284 rtnl_unlock();
2285 goto request_reset;
2288 netif_trans_update(adapter->netdev);
2289 rtnl_unlock();
2290 return;
2292 request_reset:
2293 adapter->need_fw_reset = 1;
2294 clear_bit(__NX_RESETTING, &adapter->state);
2297 static void netxen_nic_get_stats(struct net_device *netdev,
2298 struct rtnl_link_stats64 *stats)
2300 struct netxen_adapter *adapter = netdev_priv(netdev);
2302 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2303 stats->tx_packets = adapter->stats.xmitfinished;
2304 stats->rx_bytes = adapter->stats.rxbytes;
2305 stats->tx_bytes = adapter->stats.txbytes;
2306 stats->rx_dropped = adapter->stats.rxdropped;
2307 stats->tx_dropped = adapter->stats.txdropped;
2310 static irqreturn_t netxen_intr(int irq, void *data)
2312 struct nx_host_sds_ring *sds_ring = data;
2313 struct netxen_adapter *adapter = sds_ring->adapter;
2314 u32 status = 0;
2316 status = readl(adapter->isr_int_vec);
2318 if (!(status & adapter->int_vec_bit))
2319 return IRQ_NONE;
2321 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
2322 /* check interrupt state machine, to be sure */
2323 status = readl(adapter->crb_int_state_reg);
2324 if (!ISR_LEGACY_INT_TRIGGERED(status))
2325 return IRQ_NONE;
2327 } else {
2328 unsigned long our_int = 0;
2330 our_int = readl(adapter->crb_int_state_reg);
2332 /* not our interrupt */
2333 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
2334 return IRQ_NONE;
2336 /* claim interrupt */
2337 writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
2339 /* clear interrupt */
2340 netxen_nic_disable_int(sds_ring);
2343 writel(0xffffffff, adapter->tgt_status_reg);
2344 /* read twice to ensure write is flushed */
2345 readl(adapter->isr_int_vec);
2346 readl(adapter->isr_int_vec);
2348 napi_schedule(&sds_ring->napi);
2350 return IRQ_HANDLED;
2353 static irqreturn_t netxen_msi_intr(int irq, void *data)
2355 struct nx_host_sds_ring *sds_ring = data;
2356 struct netxen_adapter *adapter = sds_ring->adapter;
2358 /* clear interrupt */
2359 writel(0xffffffff, adapter->tgt_status_reg);
2361 napi_schedule(&sds_ring->napi);
2362 return IRQ_HANDLED;
2365 static irqreturn_t netxen_msix_intr(int irq, void *data)
2367 struct nx_host_sds_ring *sds_ring = data;
2369 napi_schedule(&sds_ring->napi);
2370 return IRQ_HANDLED;
2373 static int netxen_nic_poll(struct napi_struct *napi, int budget)
2375 struct nx_host_sds_ring *sds_ring =
2376 container_of(napi, struct nx_host_sds_ring, napi);
2378 struct netxen_adapter *adapter = sds_ring->adapter;
2380 int tx_complete;
2381 int work_done;
2383 tx_complete = netxen_process_cmd_ring(adapter);
2385 work_done = netxen_process_rcv_ring(sds_ring, budget);
2387 if (!tx_complete)
2388 work_done = budget;
2390 if (work_done < budget) {
2391 napi_complete_done(&sds_ring->napi, work_done);
2392 if (test_bit(__NX_DEV_UP, &adapter->state))
2393 netxen_nic_enable_int(sds_ring);
2396 return work_done;
2399 static int
2400 nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
2402 int count;
2403 if (netxen_api_lock(adapter))
2404 return -EIO;
2406 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2408 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2410 netxen_api_unlock(adapter);
2411 return count;
2414 static int
2415 nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
2417 int count, state;
2418 if (netxen_api_lock(adapter))
2419 return -EIO;
2421 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2422 WARN_ON(count == 0);
2424 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2425 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2427 if (count == 0 && state != NX_DEV_FAILED)
2428 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2430 netxen_api_unlock(adapter);
2431 return count;
2434 static int
2435 nx_dev_request_aer(struct netxen_adapter *adapter)
2437 u32 state;
2438 int ret = -EINVAL;
2440 if (netxen_api_lock(adapter))
2441 return ret;
2443 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2445 if (state == NX_DEV_NEED_AER)
2446 ret = 0;
2447 else if (state == NX_DEV_READY) {
2448 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_AER);
2449 ret = 0;
2452 netxen_api_unlock(adapter);
2453 return ret;
2457 nx_dev_request_reset(struct netxen_adapter *adapter)
2459 u32 state;
2460 int ret = -EINVAL;
2462 if (netxen_api_lock(adapter))
2463 return ret;
2465 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2467 if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED)
2468 ret = 0;
2469 else if (state != NX_DEV_INITALIZING && state != NX_DEV_NEED_AER) {
2470 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
2471 adapter->flags |= NETXEN_FW_RESET_OWNER;
2472 ret = 0;
2475 netxen_api_unlock(adapter);
2477 return ret;
2480 static int
2481 netxen_can_start_firmware(struct netxen_adapter *adapter)
2483 int count;
2484 int can_start = 0;
2486 if (netxen_api_lock(adapter)) {
2487 nx_incr_dev_ref_cnt(adapter);
2488 return -1;
2491 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2493 if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
2494 count = 0;
2496 if (count == 0) {
2497 can_start = 1;
2498 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
2501 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
2503 netxen_api_unlock(adapter);
2505 return can_start;
2508 static void
2509 netxen_schedule_work(struct netxen_adapter *adapter,
2510 work_func_t func, int delay)
2512 INIT_DELAYED_WORK(&adapter->fw_work, func);
2513 schedule_delayed_work(&adapter->fw_work, delay);
2516 static void
2517 netxen_cancel_fw_work(struct netxen_adapter *adapter)
2519 while (test_and_set_bit(__NX_RESETTING, &adapter->state))
2520 msleep(10);
2522 cancel_delayed_work_sync(&adapter->fw_work);
2525 static void
2526 netxen_attach_work(struct work_struct *work)
2528 struct netxen_adapter *adapter = container_of(work,
2529 struct netxen_adapter, fw_work.work);
2530 struct net_device *netdev = adapter->netdev;
2531 int err = 0;
2533 if (netif_running(netdev)) {
2534 err = netxen_nic_attach(adapter);
2535 if (err)
2536 goto done;
2538 err = netxen_nic_up(adapter, netdev);
2539 if (err) {
2540 netxen_nic_detach(adapter);
2541 goto done;
2544 netxen_restore_indev_addr(netdev, NETDEV_UP);
2547 netif_device_attach(netdev);
2549 done:
2550 adapter->fw_fail_cnt = 0;
2551 clear_bit(__NX_RESETTING, &adapter->state);
2552 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2555 static void
2556 netxen_fwinit_work(struct work_struct *work)
2558 struct netxen_adapter *adapter = container_of(work,
2559 struct netxen_adapter, fw_work.work);
2560 int dev_state;
2561 int count;
2562 dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
2563 if (adapter->flags & NETXEN_FW_RESET_OWNER) {
2564 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2565 WARN_ON(count == 0);
2566 if (count == 1) {
2567 if (adapter->mdump.md_enabled) {
2568 rtnl_lock();
2569 netxen_dump_fw(adapter);
2570 rtnl_unlock();
2572 adapter->flags &= ~NETXEN_FW_RESET_OWNER;
2573 if (netxen_api_lock(adapter)) {
2574 clear_bit(__NX_RESETTING, &adapter->state);
2575 NXWR32(adapter, NX_CRB_DEV_STATE,
2576 NX_DEV_FAILED);
2577 return;
2579 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
2580 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
2581 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
2582 dev_state = NX_DEV_COLD;
2583 netxen_api_unlock(adapter);
2587 switch (dev_state) {
2588 case NX_DEV_COLD:
2589 case NX_DEV_READY:
2590 if (!netxen_start_firmware(adapter)) {
2591 netxen_schedule_work(adapter, netxen_attach_work, 0);
2592 return;
2594 break;
2596 case NX_DEV_NEED_RESET:
2597 case NX_DEV_INITALIZING:
2598 netxen_schedule_work(adapter,
2599 netxen_fwinit_work, 2 * FW_POLL_DELAY);
2600 return;
2602 case NX_DEV_FAILED:
2603 default:
2604 nx_incr_dev_ref_cnt(adapter);
2605 break;
2608 if (netxen_api_lock(adapter)) {
2609 clear_bit(__NX_RESETTING, &adapter->state);
2610 return;
2612 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_FAILED);
2613 netxen_api_unlock(adapter);
2614 dev_err(&adapter->pdev->dev, "%s: Device initialization Failed\n",
2615 adapter->netdev->name);
2617 clear_bit(__NX_RESETTING, &adapter->state);
2620 static void
2621 netxen_detach_work(struct work_struct *work)
2623 struct netxen_adapter *adapter = container_of(work,
2624 struct netxen_adapter, fw_work.work);
2625 struct net_device *netdev = adapter->netdev;
2626 int ref_cnt = 0, delay;
2627 u32 status;
2629 netif_device_detach(netdev);
2631 netxen_nic_down(adapter, netdev);
2633 rtnl_lock();
2634 netxen_nic_detach(adapter);
2635 rtnl_unlock();
2637 status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2639 if (status & NX_RCODE_FATAL_ERROR)
2640 goto err_ret;
2642 if (adapter->temp == NX_TEMP_PANIC)
2643 goto err_ret;
2645 if (!(adapter->flags & NETXEN_FW_RESET_OWNER))
2646 ref_cnt = nx_decr_dev_ref_cnt(adapter);
2648 if (ref_cnt == -EIO)
2649 goto err_ret;
2651 delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
2653 adapter->fw_wait_cnt = 0;
2654 netxen_schedule_work(adapter, netxen_fwinit_work, delay);
2656 return;
2658 err_ret:
2659 clear_bit(__NX_RESETTING, &adapter->state);
2662 static int
2663 netxen_check_health(struct netxen_adapter *adapter)
2665 u32 state, heartbit;
2666 u32 peg_status;
2667 struct net_device *netdev = adapter->netdev;
2669 state = NXRD32(adapter, NX_CRB_DEV_STATE);
2670 if (state == NX_DEV_NEED_AER)
2671 return 0;
2673 if (netxen_nic_check_temp(adapter))
2674 goto detach;
2676 if (adapter->need_fw_reset) {
2677 if (nx_dev_request_reset(adapter))
2678 return 0;
2679 goto detach;
2682 /* NX_DEV_NEED_RESET, this state can be marked in two cases
2683 * 1. Tx timeout 2. Fw hang
2684 * Send request to destroy context in case of tx timeout only
2685 * and doesn't required in case of Fw hang
2687 if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED) {
2688 adapter->need_fw_reset = 1;
2689 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2690 goto detach;
2693 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2694 return 0;
2696 heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
2697 if (heartbit != adapter->heartbit) {
2698 adapter->heartbit = heartbit;
2699 adapter->fw_fail_cnt = 0;
2700 if (adapter->need_fw_reset)
2701 goto detach;
2702 return 0;
2705 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2706 return 0;
2708 if (nx_dev_request_reset(adapter))
2709 return 0;
2711 clear_bit(__NX_FW_ATTACHED, &adapter->state);
2713 dev_err(&netdev->dev, "firmware hang detected\n");
2714 peg_status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
2715 dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
2716 "PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
2717 "PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
2718 "PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
2719 "PEG_NET_4_PC: 0x%x\n",
2720 peg_status,
2721 NXRD32(adapter, NETXEN_PEG_HALT_STATUS2),
2722 NXRD32(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c),
2723 NXRD32(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c),
2724 NXRD32(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c),
2725 NXRD32(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c),
2726 NXRD32(adapter, NETXEN_CRB_PEG_NET_4 + 0x3c));
2727 if (NX_FWERROR_PEGSTAT1(peg_status) == 0x67)
2728 dev_err(&adapter->pdev->dev,
2729 "Firmware aborted with error code 0x00006700. "
2730 "Device is being reset.\n");
2731 detach:
2732 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
2733 !test_and_set_bit(__NX_RESETTING, &adapter->state))
2734 netxen_schedule_work(adapter, netxen_detach_work, 0);
2735 return 1;
2738 static void
2739 netxen_fw_poll_work(struct work_struct *work)
2741 struct netxen_adapter *adapter = container_of(work,
2742 struct netxen_adapter, fw_work.work);
2744 if (test_bit(__NX_RESETTING, &adapter->state))
2745 goto reschedule;
2747 if (test_bit(__NX_DEV_UP, &adapter->state) &&
2748 !(adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)) {
2749 if (!adapter->has_link_events) {
2751 netxen_nic_handle_phy_intr(adapter);
2753 if (adapter->link_changed)
2754 netxen_nic_set_link_parameters(adapter);
2758 if (netxen_check_health(adapter))
2759 return;
2761 reschedule:
2762 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
2765 static ssize_t
2766 netxen_store_bridged_mode(struct device *dev,
2767 struct device_attribute *attr, const char *buf, size_t len)
2769 struct net_device *net = to_net_dev(dev);
2770 struct netxen_adapter *adapter = netdev_priv(net);
2771 unsigned long new;
2772 int ret = -EINVAL;
2774 if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
2775 goto err_out;
2777 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
2778 goto err_out;
2780 if (kstrtoul(buf, 2, &new))
2781 goto err_out;
2783 if (!netxen_config_bridged_mode(adapter, !!new))
2784 ret = len;
2786 err_out:
2787 return ret;
2790 static ssize_t
2791 netxen_show_bridged_mode(struct device *dev,
2792 struct device_attribute *attr, char *buf)
2794 struct net_device *net = to_net_dev(dev);
2795 struct netxen_adapter *adapter;
2796 int bridged_mode = 0;
2798 adapter = netdev_priv(net);
2800 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
2801 bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
2803 return sprintf(buf, "%d\n", bridged_mode);
2806 static const struct device_attribute dev_attr_bridged_mode = {
2807 .attr = { .name = "bridged_mode", .mode = 0644 },
2808 .show = netxen_show_bridged_mode,
2809 .store = netxen_store_bridged_mode,
2812 static ssize_t
2813 netxen_store_diag_mode(struct device *dev,
2814 struct device_attribute *attr, const char *buf, size_t len)
2816 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2817 unsigned long new;
2819 if (kstrtoul(buf, 2, &new))
2820 return -EINVAL;
2822 if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2823 adapter->flags ^= NETXEN_NIC_DIAG_ENABLED;
2825 return len;
2828 static ssize_t
2829 netxen_show_diag_mode(struct device *dev,
2830 struct device_attribute *attr, char *buf)
2832 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2834 return sprintf(buf, "%d\n",
2835 !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED));
2838 static const struct device_attribute dev_attr_diag_mode = {
2839 .attr = { .name = "diag_mode", .mode = 0644 },
2840 .show = netxen_show_diag_mode,
2841 .store = netxen_store_diag_mode,
2844 static int
2845 netxen_sysfs_validate_crb(struct netxen_adapter *adapter,
2846 loff_t offset, size_t size)
2848 size_t crb_size = 4;
2850 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2851 return -EIO;
2853 if (offset < NETXEN_PCI_CRBSPACE) {
2854 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
2855 return -EINVAL;
2857 if (ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2858 NETXEN_PCI_CAMQM_2M_END))
2859 crb_size = 8;
2860 else
2861 return -EINVAL;
2864 if ((size != crb_size) || (offset & (crb_size-1)))
2865 return -EINVAL;
2867 return 0;
2870 static ssize_t
2871 netxen_sysfs_read_crb(struct file *filp, struct kobject *kobj,
2872 struct bin_attribute *attr,
2873 char *buf, loff_t offset, size_t size)
2875 struct device *dev = kobj_to_dev(kobj);
2876 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2877 u32 data;
2878 u64 qmdata;
2879 int ret;
2881 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2882 if (ret != 0)
2883 return ret;
2885 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
2886 ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2887 NETXEN_PCI_CAMQM_2M_END)) {
2888 netxen_pci_camqm_read_2M(adapter, offset, &qmdata);
2889 memcpy(buf, &qmdata, size);
2890 } else {
2891 data = NXRD32(adapter, offset);
2892 memcpy(buf, &data, size);
2895 return size;
2898 static ssize_t
2899 netxen_sysfs_write_crb(struct file *filp, struct kobject *kobj,
2900 struct bin_attribute *attr,
2901 char *buf, loff_t offset, size_t size)
2903 struct device *dev = kobj_to_dev(kobj);
2904 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2905 u32 data;
2906 u64 qmdata;
2907 int ret;
2909 ret = netxen_sysfs_validate_crb(adapter, offset, size);
2910 if (ret != 0)
2911 return ret;
2913 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
2914 ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
2915 NETXEN_PCI_CAMQM_2M_END)) {
2916 memcpy(&qmdata, buf, size);
2917 netxen_pci_camqm_write_2M(adapter, offset, qmdata);
2918 } else {
2919 memcpy(&data, buf, size);
2920 NXWR32(adapter, offset, data);
2923 return size;
2926 static int
2927 netxen_sysfs_validate_mem(struct netxen_adapter *adapter,
2928 loff_t offset, size_t size)
2930 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
2931 return -EIO;
2933 if ((size != 8) || (offset & 0x7))
2934 return -EIO;
2936 return 0;
2939 static ssize_t
2940 netxen_sysfs_read_mem(struct file *filp, struct kobject *kobj,
2941 struct bin_attribute *attr,
2942 char *buf, loff_t offset, size_t size)
2944 struct device *dev = kobj_to_dev(kobj);
2945 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2946 u64 data;
2947 int ret;
2949 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2950 if (ret != 0)
2951 return ret;
2953 if (adapter->pci_mem_read(adapter, offset, &data))
2954 return -EIO;
2956 memcpy(buf, &data, size);
2958 return size;
2961 static ssize_t netxen_sysfs_write_mem(struct file *filp, struct kobject *kobj,
2962 struct bin_attribute *attr, char *buf,
2963 loff_t offset, size_t size)
2965 struct device *dev = kobj_to_dev(kobj);
2966 struct netxen_adapter *adapter = dev_get_drvdata(dev);
2967 u64 data;
2968 int ret;
2970 ret = netxen_sysfs_validate_mem(adapter, offset, size);
2971 if (ret != 0)
2972 return ret;
2974 memcpy(&data, buf, size);
2976 if (adapter->pci_mem_write(adapter, offset, data))
2977 return -EIO;
2979 return size;
2983 static const struct bin_attribute bin_attr_crb = {
2984 .attr = { .name = "crb", .mode = 0644 },
2985 .size = 0,
2986 .read = netxen_sysfs_read_crb,
2987 .write = netxen_sysfs_write_crb,
2990 static const struct bin_attribute bin_attr_mem = {
2991 .attr = { .name = "mem", .mode = 0644 },
2992 .size = 0,
2993 .read = netxen_sysfs_read_mem,
2994 .write = netxen_sysfs_write_mem,
2997 static ssize_t
2998 netxen_sysfs_read_dimm(struct file *filp, struct kobject *kobj,
2999 struct bin_attribute *attr,
3000 char *buf, loff_t offset, size_t size)
3002 struct device *dev = kobj_to_dev(kobj);
3003 struct netxen_adapter *adapter = dev_get_drvdata(dev);
3004 struct net_device *netdev = adapter->netdev;
3005 struct netxen_dimm_cfg dimm;
3006 u8 dw, rows, cols, banks, ranks;
3007 u32 val;
3009 if (size < attr->size) {
3010 netdev_err(netdev, "Invalid size\n");
3011 return -EINVAL;
3014 memset(&dimm, 0, sizeof(struct netxen_dimm_cfg));
3015 val = NXRD32(adapter, NETXEN_DIMM_CAPABILITY);
3017 /* Checks if DIMM info is valid. */
3018 if (val & NETXEN_DIMM_VALID_FLAG) {
3019 netdev_err(netdev, "Invalid DIMM flag\n");
3020 dimm.presence = 0xff;
3021 goto out;
3024 rows = NETXEN_DIMM_NUMROWS(val);
3025 cols = NETXEN_DIMM_NUMCOLS(val);
3026 ranks = NETXEN_DIMM_NUMRANKS(val);
3027 banks = NETXEN_DIMM_NUMBANKS(val);
3028 dw = NETXEN_DIMM_DATAWIDTH(val);
3030 dimm.presence = (val & NETXEN_DIMM_PRESENT);
3032 /* Checks if DIMM info is present. */
3033 if (!dimm.presence) {
3034 netdev_err(netdev, "DIMM not present\n");
3035 goto out;
3038 dimm.dimm_type = NETXEN_DIMM_TYPE(val);
3040 switch (dimm.dimm_type) {
3041 case NETXEN_DIMM_TYPE_RDIMM:
3042 case NETXEN_DIMM_TYPE_UDIMM:
3043 case NETXEN_DIMM_TYPE_SO_DIMM:
3044 case NETXEN_DIMM_TYPE_Micro_DIMM:
3045 case NETXEN_DIMM_TYPE_Mini_RDIMM:
3046 case NETXEN_DIMM_TYPE_Mini_UDIMM:
3047 break;
3048 default:
3049 netdev_err(netdev, "Invalid DIMM type %x\n", dimm.dimm_type);
3050 goto out;
3053 if (val & NETXEN_DIMM_MEMTYPE_DDR2_SDRAM)
3054 dimm.mem_type = NETXEN_DIMM_MEM_DDR2_SDRAM;
3055 else
3056 dimm.mem_type = NETXEN_DIMM_MEMTYPE(val);
3058 if (val & NETXEN_DIMM_SIZE) {
3059 dimm.size = NETXEN_DIMM_STD_MEM_SIZE;
3060 goto out;
3063 if (!rows) {
3064 netdev_err(netdev, "Invalid no of rows %x\n", rows);
3065 goto out;
3068 if (!cols) {
3069 netdev_err(netdev, "Invalid no of columns %x\n", cols);
3070 goto out;
3073 if (!banks) {
3074 netdev_err(netdev, "Invalid no of banks %x\n", banks);
3075 goto out;
3078 ranks += 1;
3080 switch (dw) {
3081 case 0x0:
3082 dw = 32;
3083 break;
3084 case 0x1:
3085 dw = 33;
3086 break;
3087 case 0x2:
3088 dw = 36;
3089 break;
3090 case 0x3:
3091 dw = 64;
3092 break;
3093 case 0x4:
3094 dw = 72;
3095 break;
3096 case 0x5:
3097 dw = 80;
3098 break;
3099 case 0x6:
3100 dw = 128;
3101 break;
3102 case 0x7:
3103 dw = 144;
3104 break;
3105 default:
3106 netdev_err(netdev, "Invalid data-width %x\n", dw);
3107 goto out;
3110 dimm.size = ((1 << rows) * (1 << cols) * dw * banks * ranks) / 8;
3111 /* Size returned in MB. */
3112 dimm.size = (dimm.size) / 0x100000;
3113 out:
3114 memcpy(buf, &dimm, sizeof(struct netxen_dimm_cfg));
3115 return sizeof(struct netxen_dimm_cfg);
3119 static const struct bin_attribute bin_attr_dimm = {
3120 .attr = { .name = "dimm", .mode = 0644 },
3121 .size = sizeof(struct netxen_dimm_cfg),
3122 .read = netxen_sysfs_read_dimm,
3126 static void
3127 netxen_create_sysfs_entries(struct netxen_adapter *adapter)
3129 struct device *dev = &adapter->pdev->dev;
3131 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
3132 /* bridged_mode control */
3133 if (device_create_file(dev, &dev_attr_bridged_mode)) {
3134 dev_warn(dev,
3135 "failed to create bridged_mode sysfs entry\n");
3140 static void
3141 netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
3143 struct device *dev = &adapter->pdev->dev;
3145 if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
3146 device_remove_file(dev, &dev_attr_bridged_mode);
3149 static void
3150 netxen_create_diag_entries(struct netxen_adapter *adapter)
3152 struct pci_dev *pdev = adapter->pdev;
3153 struct device *dev;
3155 dev = &pdev->dev;
3156 if (device_create_file(dev, &dev_attr_diag_mode))
3157 dev_info(dev, "failed to create diag_mode sysfs entry\n");
3158 if (device_create_bin_file(dev, &bin_attr_crb))
3159 dev_info(dev, "failed to create crb sysfs entry\n");
3160 if (device_create_bin_file(dev, &bin_attr_mem))
3161 dev_info(dev, "failed to create mem sysfs entry\n");
3162 if (device_create_bin_file(dev, &bin_attr_dimm))
3163 dev_info(dev, "failed to create dimm sysfs entry\n");
3167 static void
3168 netxen_remove_diag_entries(struct netxen_adapter *adapter)
3170 struct pci_dev *pdev = adapter->pdev;
3171 struct device *dev = &pdev->dev;
3173 device_remove_file(dev, &dev_attr_diag_mode);
3174 device_remove_bin_file(dev, &bin_attr_crb);
3175 device_remove_bin_file(dev, &bin_attr_mem);
3176 device_remove_bin_file(dev, &bin_attr_dimm);
3179 #ifdef CONFIG_INET
3181 #define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
3183 static int
3184 netxen_destip_supported(struct netxen_adapter *adapter)
3186 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
3187 return 0;
3189 if (adapter->ahw.cut_through)
3190 return 0;
3192 return 1;
3195 static void
3196 netxen_free_ip_list(struct netxen_adapter *adapter, bool master)
3198 struct nx_ip_list *cur, *tmp_cur;
3200 list_for_each_entry_safe(cur, tmp_cur, &adapter->ip_list, list) {
3201 if (master) {
3202 if (cur->master) {
3203 netxen_config_ipaddr(adapter, cur->ip_addr,
3204 NX_IP_DOWN);
3205 list_del(&cur->list);
3206 kfree(cur);
3208 } else {
3209 netxen_config_ipaddr(adapter, cur->ip_addr, NX_IP_DOWN);
3210 list_del(&cur->list);
3211 kfree(cur);
3216 static bool
3217 netxen_list_config_ip(struct netxen_adapter *adapter,
3218 struct in_ifaddr *ifa, unsigned long event)
3220 struct net_device *dev;
3221 struct nx_ip_list *cur, *tmp_cur;
3222 struct list_head *head;
3223 bool ret = false;
3225 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
3227 if (dev == NULL)
3228 goto out;
3230 switch (event) {
3231 case NX_IP_UP:
3232 list_for_each(head, &adapter->ip_list) {
3233 cur = list_entry(head, struct nx_ip_list, list);
3235 if (cur->ip_addr == ifa->ifa_address)
3236 goto out;
3239 cur = kzalloc(sizeof(struct nx_ip_list), GFP_ATOMIC);
3240 if (cur == NULL)
3241 goto out;
3242 if (is_vlan_dev(dev))
3243 dev = vlan_dev_real_dev(dev);
3244 cur->master = !!netif_is_bond_master(dev);
3245 cur->ip_addr = ifa->ifa_address;
3246 list_add_tail(&cur->list, &adapter->ip_list);
3247 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
3248 ret = true;
3249 break;
3250 case NX_IP_DOWN:
3251 list_for_each_entry_safe(cur, tmp_cur,
3252 &adapter->ip_list, list) {
3253 if (cur->ip_addr == ifa->ifa_address) {
3254 list_del(&cur->list);
3255 kfree(cur);
3256 netxen_config_ipaddr(adapter, ifa->ifa_address,
3257 NX_IP_DOWN);
3258 ret = true;
3259 break;
3263 out:
3264 return ret;
3267 static void
3268 netxen_config_indev_addr(struct netxen_adapter *adapter,
3269 struct net_device *dev, unsigned long event)
3271 struct in_device *indev;
3273 if (!netxen_destip_supported(adapter))
3274 return;
3276 indev = in_dev_get(dev);
3277 if (!indev)
3278 return;
3280 for_ifa(indev) {
3281 switch (event) {
3282 case NETDEV_UP:
3283 netxen_list_config_ip(adapter, ifa, NX_IP_UP);
3284 break;
3285 case NETDEV_DOWN:
3286 netxen_list_config_ip(adapter, ifa, NX_IP_DOWN);
3287 break;
3288 default:
3289 break;
3291 } endfor_ifa(indev);
3293 in_dev_put(indev);
3296 static void
3297 netxen_restore_indev_addr(struct net_device *netdev, unsigned long event)
3300 struct netxen_adapter *adapter = netdev_priv(netdev);
3301 struct nx_ip_list *pos, *tmp_pos;
3302 unsigned long ip_event;
3304 ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
3305 netxen_config_indev_addr(adapter, netdev, event);
3307 list_for_each_entry_safe(pos, tmp_pos, &adapter->ip_list, list) {
3308 netxen_config_ipaddr(adapter, pos->ip_addr, ip_event);
3312 static inline bool
3313 netxen_config_checkdev(struct net_device *dev)
3315 struct netxen_adapter *adapter;
3317 if (!is_netxen_netdev(dev))
3318 return false;
3319 adapter = netdev_priv(dev);
3320 if (!adapter)
3321 return false;
3322 if (!netxen_destip_supported(adapter))
3323 return false;
3324 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
3325 return false;
3327 return true;
3331 * netxen_config_master - configure addresses based on master
3332 * @dev: netxen device
3333 * @event: netdev event
3335 static void netxen_config_master(struct net_device *dev, unsigned long event)
3337 struct net_device *master, *slave;
3338 struct netxen_adapter *adapter = netdev_priv(dev);
3340 rcu_read_lock();
3341 master = netdev_master_upper_dev_get_rcu(dev);
3343 * This is the case where the netxen nic is being
3344 * enslaved and is dev_open()ed in bond_enslave()
3345 * Now we should program the bond's (and its vlans')
3346 * addresses in the netxen NIC.
3348 if (master && netif_is_bond_master(master) &&
3349 !netif_is_bond_slave(dev)) {
3350 netxen_config_indev_addr(adapter, master, event);
3351 for_each_netdev_rcu(&init_net, slave)
3352 if (is_vlan_dev(slave) &&
3353 vlan_dev_real_dev(slave) == master)
3354 netxen_config_indev_addr(adapter, slave, event);
3356 rcu_read_unlock();
3358 * This is the case where the netxen nic is being
3359 * released and is dev_close()ed in bond_release()
3360 * just before IFF_BONDING is stripped.
3362 if (!master && dev->priv_flags & IFF_BONDING)
3363 netxen_free_ip_list(adapter, true);
3366 static int netxen_netdev_event(struct notifier_block *this,
3367 unsigned long event, void *ptr)
3369 struct netxen_adapter *adapter;
3370 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3371 struct net_device *orig_dev = dev;
3372 struct net_device *slave;
3374 recheck:
3375 if (dev == NULL)
3376 goto done;
3378 if (is_vlan_dev(dev)) {
3379 dev = vlan_dev_real_dev(dev);
3380 goto recheck;
3382 if (event == NETDEV_UP || event == NETDEV_DOWN) {
3383 /* If this is a bonding device, look for netxen-based slaves*/
3384 if (netif_is_bond_master(dev)) {
3385 rcu_read_lock();
3386 for_each_netdev_in_bond_rcu(dev, slave) {
3387 if (!netxen_config_checkdev(slave))
3388 continue;
3389 adapter = netdev_priv(slave);
3390 netxen_config_indev_addr(adapter,
3391 orig_dev, event);
3393 rcu_read_unlock();
3394 } else {
3395 if (!netxen_config_checkdev(dev))
3396 goto done;
3397 adapter = netdev_priv(dev);
3398 /* Act only if the actual netxen is the target */
3399 if (orig_dev == dev)
3400 netxen_config_master(dev, event);
3401 netxen_config_indev_addr(adapter, orig_dev, event);
3404 done:
3405 return NOTIFY_DONE;
3408 static int
3409 netxen_inetaddr_event(struct notifier_block *this,
3410 unsigned long event, void *ptr)
3412 struct netxen_adapter *adapter;
3413 struct net_device *dev, *slave;
3414 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
3415 unsigned long ip_event;
3417 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
3418 ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
3419 recheck:
3420 if (dev == NULL)
3421 goto done;
3423 if (is_vlan_dev(dev)) {
3424 dev = vlan_dev_real_dev(dev);
3425 goto recheck;
3427 if (event == NETDEV_UP || event == NETDEV_DOWN) {
3428 /* If this is a bonding device, look for netxen-based slaves*/
3429 if (netif_is_bond_master(dev)) {
3430 rcu_read_lock();
3431 for_each_netdev_in_bond_rcu(dev, slave) {
3432 if (!netxen_config_checkdev(slave))
3433 continue;
3434 adapter = netdev_priv(slave);
3435 netxen_list_config_ip(adapter, ifa, ip_event);
3437 rcu_read_unlock();
3438 } else {
3439 if (!netxen_config_checkdev(dev))
3440 goto done;
3441 adapter = netdev_priv(dev);
3442 netxen_list_config_ip(adapter, ifa, ip_event);
3445 done:
3446 return NOTIFY_DONE;
3449 static struct notifier_block netxen_netdev_cb = {
3450 .notifier_call = netxen_netdev_event,
3453 static struct notifier_block netxen_inetaddr_cb = {
3454 .notifier_call = netxen_inetaddr_event,
3456 #else
3457 static void
3458 netxen_restore_indev_addr(struct net_device *dev, unsigned long event)
3460 static void
3461 netxen_free_ip_list(struct netxen_adapter *adapter, bool master)
3463 #endif
3465 static const struct pci_error_handlers netxen_err_handler = {
3466 .error_detected = netxen_io_error_detected,
3467 .slot_reset = netxen_io_slot_reset,
3468 .resume = netxen_io_resume,
3471 static struct pci_driver netxen_driver = {
3472 .name = netxen_nic_driver_name,
3473 .id_table = netxen_pci_tbl,
3474 .probe = netxen_nic_probe,
3475 .remove = netxen_nic_remove,
3476 #ifdef CONFIG_PM
3477 .suspend = netxen_nic_suspend,
3478 .resume = netxen_nic_resume,
3479 #endif
3480 .shutdown = netxen_nic_shutdown,
3481 .err_handler = &netxen_err_handler
3484 static int __init netxen_init_module(void)
3486 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
3488 #ifdef CONFIG_INET
3489 register_netdevice_notifier(&netxen_netdev_cb);
3490 register_inetaddr_notifier(&netxen_inetaddr_cb);
3491 #endif
3492 return pci_register_driver(&netxen_driver);
3495 module_init(netxen_init_module);
3497 static void __exit netxen_exit_module(void)
3499 pci_unregister_driver(&netxen_driver);
3501 #ifdef CONFIG_INET
3502 unregister_inetaddr_notifier(&netxen_inetaddr_cb);
3503 unregister_netdevice_notifier(&netxen_netdev_cb);
3504 #endif
3507 module_exit(netxen_exit_module);