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 */ 7 8 #include <linux/slab.h> 9 #include <linux/vmalloc.h> 10 #include <linux/interrupt.h> 11 #include "netxen_nic_hw.h" 12 13 #include "netxen_nic.h" 14 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> 22 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); 27 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; 31 32 static int port_mode = NETXEN_PORT_MODE_AUTO_NEG; 33 34 /* Default to restricted 1G auto-neg mode */ 35 static int wol_port_mode = 5; 36 37 static int use_msi = 1; 38 39 static int use_msi_x = 1; 40 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"); 44 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); 59 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); 67 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); 71 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); 77 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} 82 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,} 93 }; 94 95 MODULE_DEVICE_TABLE(pci, netxen_pci_tbl); 96 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 100 }; 101 102 void 103 netxen_nic_update_cmd_producer(struct netxen_adapter *adapter, 104 struct nx_host_tx_ring *tx_ring) 105 { 106 NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer); 107 } 108 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 112 }; 113 114 static inline void 115 netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter, 116 struct nx_host_tx_ring *tx_ring) 117 { 118 NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer); 119 } 120 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 126 }; 127 128 static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG; 129 130 static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring) 131 { 132 struct netxen_adapter *adapter = sds_ring->adapter; 133 134 NXWRIO(adapter, sds_ring->crb_intr_mask, 0); 135 } 136 137 static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring) 138 { 139 struct netxen_adapter *adapter = sds_ring->adapter; 140 141 NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1); 142 143 if (!NETXEN_IS_MSI_FAMILY(adapter)) 144 NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff); 145 } 146 147 static int 148 netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count) 149 { 150 int size = sizeof(struct nx_host_sds_ring) * count; 151 152 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL); 153 154 return recv_ctx->sds_rings == NULL; 155 } 156 157 static void 158 netxen_free_sds_rings(struct netxen_recv_context *recv_ctx) 159 { 160 kfree(recv_ctx->sds_rings); 161 recv_ctx->sds_rings = NULL; 162 } 163 164 static int 165 netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev) 166 { 167 int ring; 168 struct nx_host_sds_ring *sds_ring; 169 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx; 170 171 if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings)) 172 return -ENOMEM; 173 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); 178 } 179 180 return 0; 181 } 182 183 static void 184 netxen_napi_del(struct netxen_adapter *adapter) 185 { 186 int ring; 187 struct nx_host_sds_ring *sds_ring; 188 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx; 189 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); 193 } 194 195 netxen_free_sds_rings(&adapter->recv_ctx); 196 } 197 198 static void 199 netxen_napi_enable(struct netxen_adapter *adapter) 200 { 201 int ring; 202 struct nx_host_sds_ring *sds_ring; 203 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx; 204 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); 209 } 210 } 211 212 static void 213 netxen_napi_disable(struct netxen_adapter *adapter) 214 { 215 int ring; 216 struct nx_host_sds_ring *sds_ring; 217 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx; 218 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); 224 } 225 } 226 227 static int nx_set_dma_mask(struct netxen_adapter *adapter) 228 { 229 struct pci_dev *pdev = adapter->pdev; 230 uint64_t mask, cmask; 231 232 adapter->pci_using_dac = 0; 233 234 mask = DMA_BIT_MASK(32); 235 cmask = DMA_BIT_MASK(32); 236 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; 244 } 245 246 if (dma_set_mask(&pdev->dev, mask) == 0 && 247 dma_set_coherent_mask(&pdev->dev, cmask) == 0) { 248 adapter->pci_using_dac = 1; 249 return 0; 250 } 251 252 return -EIO; 253 } 254 255 /* Update addressable range if firmware supports it */ 256 static int 257 nx_update_dma_mask(struct netxen_adapter *adapter) 258 { 259 int change, shift, err; 260 uint64_t mask, old_mask, old_cmask; 261 struct pci_dev *pdev = adapter->pdev; 262 263 change = 0; 264 265 shift = NXRD32(adapter, CRB_DMA_SHIFT); 266 if (shift > 32) 267 return 0; 268 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; 273 274 if (change) { 275 old_mask = pdev->dma_mask; 276 old_cmask = pdev->dev.coherent_dma_mask; 277 278 mask = DMA_BIT_MASK(32+shift); 279 280 err = dma_set_mask(&pdev->dev, mask); 281 if (err) 282 goto err_out; 283 284 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) { 285 286 err = dma_set_coherent_mask(&pdev->dev, mask); 287 if (err) 288 goto err_out; 289 } 290 dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift); 291 } 292 293 return 0; 294 295 err_out: 296 dma_set_mask(&pdev->dev, old_mask); 297 dma_set_coherent_mask(&pdev->dev, old_cmask); 298 return err; 299 } 300 301 static int 302 netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot) 303 { 304 u32 val, timeout; 305 306 if (first_boot == 0x55555555) { 307 /* This is the first boot after power up */ 308 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC); 309 310 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id)) 311 return 0; 312 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)); 319 } 320 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; 327 } 328 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)); 336 337 if (++timeout > 5000) 338 return -EIO; 339 340 } while (val == NETXEN_BDINFO_MAGIC); 341 } 342 return 0; 343 } 344 345 static void netxen_set_port_mode(struct netxen_adapter *adapter) 346 { 347 u32 val, data; 348 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); 367 } 368 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; 374 } 375 NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode); 376 } 377 } 378 379 #define PCI_CAP_ID_GEN 0x10 380 381 static void netxen_pcie_strap_init(struct netxen_adapter *adapter) 382 { 383 u32 pdevfuncsave; 384 u32 c8c9value = 0; 385 u32 chicken = 0; 386 u32 control = 0; 387 int i, pos; 388 struct pci_dev *pdev; 389 390 pdev = adapter->pdev; 391 392 chicken = NXRD32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3)); 393 /* clear chicken3.25:24 */ 394 chicken &= 0xFCFFFFFF; 395 /* 396 * if gen1 and B0, set F1020 - if gen 2, do nothing 397 * if gen2 set to F1000 398 */ 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; 405 } 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; 416 } 417 418 NXWR32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3), chicken); 419 420 if (!c8c9value) 421 return; 422 423 pdevfuncsave = pdev->devfn; 424 if (pdevfuncsave & 0x07) 425 return; 426 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++; 432 } 433 pdev->devfn = pdevfuncsave; 434 } 435 436 static void netxen_set_msix_bit(struct pci_dev *pdev, int enable) 437 { 438 u32 control; 439 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); 447 } 448 } 449 450 static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count) 451 { 452 int i; 453 454 for (i = 0; i < count; i++) 455 adapter->msix_entries[i].entry = i; 456 } 457 458 static int 459 netxen_read_mac_addr(struct netxen_adapter *adapter) 460 { 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; 466 u8 addr[ETH_ALEN]; 467 468 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) { 469 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0) 470 return -EIO; 471 } else { 472 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0) 473 return -EIO; 474 } 475 476 p = (unsigned char *)&mac_addr; 477 for (i = 0; i < 6; i++) 478 addr[i] = *(p + 5 - i); 479 eth_hw_addr_set(netdev, addr); 480 481 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len); 482 483 /* set station address */ 484 485 if (!is_valid_ether_addr(netdev->dev_addr)) 486 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr); 487 488 return 0; 489 } 490 491 static int netxen_nic_set_mac(struct net_device *netdev, void *p) 492 { 493 struct netxen_adapter *adapter = netdev_priv(netdev); 494 struct sockaddr *addr = p; 495 496 if (!is_valid_ether_addr(addr->sa_data)) 497 return -EADDRNOTAVAIL; 498 499 if (netif_running(netdev)) { 500 netif_device_detach(netdev); 501 netxen_napi_disable(adapter); 502 } 503 504 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len); 505 eth_hw_addr_set(netdev, addr->sa_data); 506 adapter->macaddr_set(adapter, addr->sa_data); 507 508 if (netif_running(netdev)) { 509 netif_device_attach(netdev); 510 netxen_napi_enable(adapter); 511 } 512 return 0; 513 } 514 515 static void netxen_set_multicast_list(struct net_device *dev) 516 { 517 struct netxen_adapter *adapter = netdev_priv(dev); 518 519 adapter->set_multi(dev); 520 } 521 522 static netdev_features_t netxen_fix_features(struct net_device *dev, 523 netdev_features_t features) 524 { 525 if (!(features & NETIF_F_RXCSUM)) { 526 netdev_info(dev, "disabling LRO as RXCSUM is off\n"); 527 528 features &= ~NETIF_F_LRO; 529 } 530 531 return features; 532 } 533 534 static int netxen_set_features(struct net_device *dev, 535 netdev_features_t features) 536 { 537 struct netxen_adapter *adapter = netdev_priv(dev); 538 int hw_lro; 539 540 if (!((dev->features ^ features) & NETIF_F_LRO)) 541 return 0; 542 543 hw_lro = (features & NETIF_F_LRO) ? NETXEN_NIC_LRO_ENABLED 544 : NETXEN_NIC_LRO_DISABLED; 545 546 if (netxen_config_hw_lro(adapter, hw_lro)) 547 return -EIO; 548 549 if (!(features & NETIF_F_LRO) && netxen_send_lro_cleanup(adapter)) 550 return -EIO; 551 552 return 0; 553 } 554 555 static const struct net_device_ops netxen_netdev_ops = { 556 .ndo_open = netxen_nic_open, 557 .ndo_stop = netxen_nic_close, 558 .ndo_start_xmit = netxen_nic_xmit_frame, 559 .ndo_get_stats64 = netxen_nic_get_stats, 560 .ndo_validate_addr = eth_validate_addr, 561 .ndo_set_rx_mode = netxen_set_multicast_list, 562 .ndo_set_mac_address = netxen_nic_set_mac, 563 .ndo_change_mtu = netxen_nic_change_mtu, 564 .ndo_tx_timeout = netxen_tx_timeout, 565 .ndo_fix_features = netxen_fix_features, 566 .ndo_set_features = netxen_set_features, 567 }; 568 569 static inline void netxen_set_interrupt_mode(struct netxen_adapter *adapter, 570 u32 mode) 571 { 572 NXWR32(adapter, NETXEN_INTR_MODE_REG, mode); 573 } 574 575 static inline u32 netxen_get_interrupt_mode(struct netxen_adapter *adapter) 576 { 577 return NXRD32(adapter, NETXEN_INTR_MODE_REG); 578 } 579 580 static void 581 netxen_initialize_interrupt_registers(struct netxen_adapter *adapter) 582 { 583 struct netxen_legacy_intr_set *legacy_intrp; 584 u32 tgt_status_reg, int_state_reg; 585 586 if (adapter->ahw.revision_id >= NX_P3_B0) 587 legacy_intrp = &legacy_intr[adapter->ahw.pci_func]; 588 else 589 legacy_intrp = &legacy_intr[0]; 590 591 tgt_status_reg = legacy_intrp->tgt_status_reg; 592 int_state_reg = ISR_INT_STATE_REG; 593 594 adapter->int_vec_bit = legacy_intrp->int_vec_bit; 595 adapter->tgt_status_reg = netxen_get_ioaddr(adapter, tgt_status_reg); 596 adapter->tgt_mask_reg = netxen_get_ioaddr(adapter, 597 legacy_intrp->tgt_mask_reg); 598 adapter->pci_int_reg = netxen_get_ioaddr(adapter, 599 legacy_intrp->pci_int_reg); 600 adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR); 601 602 if (adapter->ahw.revision_id >= NX_P3_B1) 603 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter, 604 int_state_reg); 605 else 606 adapter->crb_int_state_reg = netxen_get_ioaddr(adapter, 607 CRB_INT_VECTOR); 608 } 609 610 static int netxen_setup_msi_interrupts(struct netxen_adapter *adapter, 611 int num_msix) 612 { 613 struct pci_dev *pdev = adapter->pdev; 614 u32 value; 615 int err; 616 617 if (adapter->msix_supported) { 618 netxen_init_msix_entries(adapter, num_msix); 619 err = pci_enable_msix_range(pdev, adapter->msix_entries, 620 num_msix, num_msix); 621 if (err > 0) { 622 adapter->flags |= NETXEN_NIC_MSIX_ENABLED; 623 netxen_set_msix_bit(pdev, 1); 624 625 if (adapter->rss_supported) 626 adapter->max_sds_rings = num_msix; 627 628 dev_info(&pdev->dev, "using msi-x interrupts\n"); 629 return 0; 630 } 631 /* fall through for msi */ 632 } 633 634 if (use_msi && !pci_enable_msi(pdev)) { 635 value = msi_tgt_status[adapter->ahw.pci_func]; 636 adapter->flags |= NETXEN_NIC_MSI_ENABLED; 637 adapter->tgt_status_reg = netxen_get_ioaddr(adapter, value); 638 adapter->msix_entries[0].vector = pdev->irq; 639 dev_info(&pdev->dev, "using msi interrupts\n"); 640 return 0; 641 } 642 643 dev_err(&pdev->dev, "Failed to acquire MSI-X/MSI interrupt vector\n"); 644 return -EIO; 645 } 646 647 static int netxen_setup_intr(struct netxen_adapter *adapter) 648 { 649 struct pci_dev *pdev = adapter->pdev; 650 int num_msix; 651 652 if (adapter->rss_supported) 653 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ? 654 MSIX_ENTRIES_PER_ADAPTER : 2; 655 else 656 num_msix = 1; 657 658 adapter->max_sds_rings = 1; 659 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED); 660 661 netxen_initialize_interrupt_registers(adapter); 662 netxen_set_msix_bit(pdev, 0); 663 664 if (adapter->portnum == 0) { 665 if (!netxen_setup_msi_interrupts(adapter, num_msix)) 666 netxen_set_interrupt_mode(adapter, NETXEN_MSI_MODE); 667 else 668 netxen_set_interrupt_mode(adapter, NETXEN_INTX_MODE); 669 } else { 670 if (netxen_get_interrupt_mode(adapter) == NETXEN_MSI_MODE && 671 netxen_setup_msi_interrupts(adapter, num_msix)) { 672 dev_err(&pdev->dev, "Co-existence of MSI-X/MSI and INTx interrupts is not supported\n"); 673 return -EIO; 674 } 675 } 676 677 if (!NETXEN_IS_MSI_FAMILY(adapter)) { 678 adapter->msix_entries[0].vector = pdev->irq; 679 dev_info(&pdev->dev, "using legacy interrupts\n"); 680 } 681 return 0; 682 } 683 684 static void 685 netxen_teardown_intr(struct netxen_adapter *adapter) 686 { 687 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED) 688 pci_disable_msix(adapter->pdev); 689 if (adapter->flags & NETXEN_NIC_MSI_ENABLED) 690 pci_disable_msi(adapter->pdev); 691 } 692 693 static void 694 netxen_cleanup_pci_map(struct netxen_adapter *adapter) 695 { 696 if (adapter->ahw.db_base != NULL) 697 iounmap(adapter->ahw.db_base); 698 if (adapter->ahw.pci_base0 != NULL) 699 iounmap(adapter->ahw.pci_base0); 700 if (adapter->ahw.pci_base1 != NULL) 701 iounmap(adapter->ahw.pci_base1); 702 if (adapter->ahw.pci_base2 != NULL) 703 iounmap(adapter->ahw.pci_base2); 704 } 705 706 static int 707 netxen_setup_pci_map(struct netxen_adapter *adapter) 708 { 709 void __iomem *db_ptr = NULL; 710 711 resource_size_t mem_base, db_base; 712 unsigned long mem_len, db_len = 0; 713 714 struct pci_dev *pdev = adapter->pdev; 715 int pci_func = adapter->ahw.pci_func; 716 struct netxen_hardware_context *ahw = &adapter->ahw; 717 718 int err = 0; 719 720 /* 721 * Set the CRB window to invalid. If any register in window 0 is 722 * accessed it should set the window to 0 and then reset it to 1. 723 */ 724 adapter->ahw.crb_win = -1; 725 adapter->ahw.ocm_win = -1; 726 727 /* remap phys address */ 728 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */ 729 mem_len = pci_resource_len(pdev, 0); 730 731 /* 128 Meg of memory */ 732 if (mem_len == NETXEN_PCI_128MB_SIZE) { 733 734 ahw->pci_base0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE); 735 ahw->pci_base1 = ioremap(mem_base + SECOND_PAGE_GROUP_START, 736 SECOND_PAGE_GROUP_SIZE); 737 ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START, 738 THIRD_PAGE_GROUP_SIZE); 739 if (ahw->pci_base0 == NULL || ahw->pci_base1 == NULL || 740 ahw->pci_base2 == NULL) { 741 dev_err(&pdev->dev, "failed to map PCI bar 0\n"); 742 err = -EIO; 743 goto err_out; 744 } 745 746 ahw->pci_len0 = FIRST_PAGE_GROUP_SIZE; 747 748 } else if (mem_len == NETXEN_PCI_32MB_SIZE) { 749 750 ahw->pci_base1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE); 751 ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START - 752 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE); 753 if (ahw->pci_base1 == NULL || ahw->pci_base2 == NULL) { 754 dev_err(&pdev->dev, "failed to map PCI bar 0\n"); 755 err = -EIO; 756 goto err_out; 757 } 758 759 } else if (mem_len == NETXEN_PCI_2MB_SIZE) { 760 761 ahw->pci_base0 = pci_ioremap_bar(pdev, 0); 762 if (ahw->pci_base0 == NULL) { 763 dev_err(&pdev->dev, "failed to map PCI bar 0\n"); 764 return -EIO; 765 } 766 ahw->pci_len0 = mem_len; 767 } else { 768 return -EIO; 769 } 770 771 netxen_setup_hwops(adapter); 772 773 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20)); 774 775 if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) { 776 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter, 777 NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func))); 778 779 } else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) { 780 adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter, 781 NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func))); 782 } 783 784 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) 785 goto skip_doorbell; 786 787 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */ 788 db_len = pci_resource_len(pdev, 4); 789 790 if (db_len == 0) { 791 printk(KERN_ERR "%s: doorbell is disabled\n", 792 netxen_nic_driver_name); 793 err = -EIO; 794 goto err_out; 795 } 796 797 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES); 798 if (!db_ptr) { 799 printk(KERN_ERR "%s: Failed to allocate doorbell map.", 800 netxen_nic_driver_name); 801 err = -EIO; 802 goto err_out; 803 } 804 805 skip_doorbell: 806 adapter->ahw.db_base = db_ptr; 807 adapter->ahw.db_len = db_len; 808 return 0; 809 810 err_out: 811 netxen_cleanup_pci_map(adapter); 812 return err; 813 } 814 815 static void 816 netxen_check_options(struct netxen_adapter *adapter) 817 { 818 u32 fw_major, fw_minor, fw_build, prev_fw_version; 819 char brd_name[NETXEN_MAX_SHORT_NAME]; 820 char serial_num[32]; 821 int i, offset, val, err; 822 __le32 *ptr32; 823 struct pci_dev *pdev = adapter->pdev; 824 825 adapter->driver_mismatch = 0; 826 827 ptr32 = (__le32 *)&serial_num; 828 offset = NX_FW_SERIAL_NUM_OFFSET; 829 for (i = 0; i < 8; i++) { 830 err = netxen_rom_fast_read(adapter, offset, &val); 831 if (err) { 832 dev_err(&pdev->dev, "error reading board info\n"); 833 adapter->driver_mismatch = 1; 834 return; 835 } 836 ptr32[i] = cpu_to_le32(val); 837 offset += sizeof(u32); 838 } 839 840 fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR); 841 fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR); 842 fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB); 843 prev_fw_version = adapter->fw_version; 844 adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build); 845 846 /* Get FW Mini Coredump template and store it */ 847 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) { 848 if (adapter->mdump.md_template == NULL || 849 adapter->fw_version > prev_fw_version) { 850 kfree(adapter->mdump.md_template); 851 adapter->mdump.md_template = NULL; 852 err = netxen_setup_minidump(adapter); 853 if (err) 854 dev_err(&adapter->pdev->dev, 855 "Failed to setup minidump rcode = %d\n", err); 856 } 857 } 858 859 if (adapter->portnum == 0) { 860 if (netxen_nic_get_brd_name_by_type(adapter->ahw.board_type, 861 brd_name)) 862 strcpy(serial_num, "Unknown"); 863 864 pr_info("%s: %s Board S/N %s Chip rev 0x%x\n", 865 module_name(THIS_MODULE), 866 brd_name, serial_num, adapter->ahw.revision_id); 867 } 868 869 if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) { 870 adapter->driver_mismatch = 1; 871 dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n", 872 fw_major, fw_minor, fw_build); 873 return; 874 } 875 876 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) { 877 i = NXRD32(adapter, NETXEN_SRE_MISC); 878 adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0; 879 } 880 881 dev_info(&pdev->dev, "Driver v%s, firmware v%d.%d.%d [%s]\n", 882 NETXEN_NIC_LINUX_VERSIONID, fw_major, fw_minor, fw_build, 883 adapter->ahw.cut_through ? "cut-through" : "legacy"); 884 885 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222)) 886 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1); 887 888 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) { 889 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G; 890 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G; 891 } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) { 892 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G; 893 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G; 894 } 895 896 adapter->msix_supported = 0; 897 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) { 898 adapter->msix_supported = !!use_msi_x; 899 adapter->rss_supported = !!use_msi_x; 900 } else { 901 u32 flashed_ver = 0; 902 netxen_rom_fast_read(adapter, 903 NX_FW_VERSION_OFFSET, (int *)&flashed_ver); 904 flashed_ver = NETXEN_DECODE_VERSION(flashed_ver); 905 906 if (flashed_ver >= NETXEN_VERSION_CODE(3, 4, 336)) { 907 switch (adapter->ahw.board_type) { 908 case NETXEN_BRDTYPE_P2_SB31_10G: 909 case NETXEN_BRDTYPE_P2_SB31_10G_CX4: 910 adapter->msix_supported = !!use_msi_x; 911 adapter->rss_supported = !!use_msi_x; 912 break; 913 default: 914 break; 915 } 916 } 917 } 918 919 adapter->num_txd = MAX_CMD_DESCRIPTORS; 920 921 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) { 922 adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS; 923 adapter->max_rds_rings = 3; 924 } else { 925 adapter->num_lro_rxd = 0; 926 adapter->max_rds_rings = 2; 927 } 928 } 929 930 static int 931 netxen_start_firmware(struct netxen_adapter *adapter) 932 { 933 int val, err, first_boot; 934 struct pci_dev *pdev = adapter->pdev; 935 936 /* required for NX2031 dummy dma */ 937 err = nx_set_dma_mask(adapter); 938 if (err) 939 return err; 940 941 err = netxen_can_start_firmware(adapter); 942 943 if (err < 0) 944 return err; 945 946 if (!err) 947 goto wait_init; 948 949 first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc)); 950 951 err = netxen_check_hw_init(adapter, first_boot); 952 if (err) { 953 dev_err(&pdev->dev, "error in init HW init sequence\n"); 954 return err; 955 } 956 957 netxen_request_firmware(adapter); 958 959 err = netxen_need_fw_reset(adapter); 960 if (err < 0) 961 goto err_out; 962 if (err == 0) 963 goto pcie_strap_init; 964 965 if (first_boot != 0x55555555) { 966 NXWR32(adapter, CRB_CMDPEG_STATE, 0); 967 netxen_pinit_from_rom(adapter); 968 msleep(1); 969 } 970 971 NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555); 972 NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0); 973 NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0); 974 975 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) 976 netxen_set_port_mode(adapter); 977 978 err = netxen_load_firmware(adapter); 979 if (err) 980 goto err_out; 981 982 netxen_release_firmware(adapter); 983 984 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) { 985 986 /* Initialize multicast addr pool owners */ 987 val = 0x7654; 988 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) 989 val |= 0x0f000000; 990 NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val); 991 992 } 993 994 err = netxen_init_dummy_dma(adapter); 995 if (err) 996 goto err_out; 997 998 /* 999 * Tell the hardware our version number. 1000 */ 1001 val = (_NETXEN_NIC_LINUX_MAJOR << 16) 1002 | ((_NETXEN_NIC_LINUX_MINOR << 8)) 1003 | (_NETXEN_NIC_LINUX_SUBVERSION); 1004 NXWR32(adapter, CRB_DRIVER_VERSION, val); 1005 1006 pcie_strap_init: 1007 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) 1008 netxen_pcie_strap_init(adapter); 1009 1010 wait_init: 1011 /* Handshake with the card before we register the devices. */ 1012 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE); 1013 if (err) { 1014 netxen_free_dummy_dma(adapter); 1015 goto err_out; 1016 } 1017 1018 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY); 1019 1020 nx_update_dma_mask(adapter); 1021 1022 netxen_check_options(adapter); 1023 1024 adapter->need_fw_reset = 0; 1025 1026 /* fall through and release firmware */ 1027 1028 err_out: 1029 netxen_release_firmware(adapter); 1030 return err; 1031 } 1032 1033 static int 1034 netxen_nic_request_irq(struct netxen_adapter *adapter) 1035 { 1036 irq_handler_t handler; 1037 struct nx_host_sds_ring *sds_ring; 1038 int err, ring; 1039 1040 unsigned long flags = 0; 1041 struct net_device *netdev = adapter->netdev; 1042 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx; 1043 1044 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED) 1045 handler = netxen_msix_intr; 1046 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED) 1047 handler = netxen_msi_intr; 1048 else { 1049 flags |= IRQF_SHARED; 1050 handler = netxen_intr; 1051 } 1052 adapter->irq = netdev->irq; 1053 1054 for (ring = 0; ring < adapter->max_sds_rings; ring++) { 1055 sds_ring = &recv_ctx->sds_rings[ring]; 1056 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring); 1057 err = request_irq(sds_ring->irq, handler, 1058 flags, sds_ring->name, sds_ring); 1059 if (err) 1060 return err; 1061 } 1062 1063 return 0; 1064 } 1065 1066 static void 1067 netxen_nic_free_irq(struct netxen_adapter *adapter) 1068 { 1069 int ring; 1070 struct nx_host_sds_ring *sds_ring; 1071 1072 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx; 1073 1074 for (ring = 0; ring < adapter->max_sds_rings; ring++) { 1075 sds_ring = &recv_ctx->sds_rings[ring]; 1076 free_irq(sds_ring->irq, sds_ring); 1077 } 1078 } 1079 1080 static void 1081 netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter) 1082 { 1083 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT; 1084 adapter->coal.normal.data.rx_time_us = 1085 NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US; 1086 adapter->coal.normal.data.rx_packets = 1087 NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS; 1088 adapter->coal.normal.data.tx_time_us = 1089 NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US; 1090 adapter->coal.normal.data.tx_packets = 1091 NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS; 1092 } 1093 1094 /* with rtnl_lock */ 1095 static int 1096 __netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev) 1097 { 1098 int err; 1099 1100 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) 1101 return -EIO; 1102 1103 err = adapter->init_port(adapter, adapter->physical_port); 1104 if (err) { 1105 printk(KERN_ERR "%s: Failed to initialize port %d\n", 1106 netxen_nic_driver_name, adapter->portnum); 1107 return err; 1108 } 1109 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) 1110 adapter->macaddr_set(adapter, adapter->mac_addr); 1111 1112 adapter->set_multi(netdev); 1113 adapter->set_mtu(adapter, netdev->mtu); 1114 1115 adapter->ahw.linkup = 0; 1116 1117 if (adapter->max_sds_rings > 1) 1118 netxen_config_rss(adapter, 1); 1119 1120 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) 1121 netxen_config_intr_coalesce(adapter); 1122 1123 if (netdev->features & NETIF_F_LRO) 1124 netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED); 1125 1126 netxen_napi_enable(adapter); 1127 1128 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION) 1129 netxen_linkevent_request(adapter, 1); 1130 else 1131 netxen_nic_set_link_parameters(adapter); 1132 1133 set_bit(__NX_DEV_UP, &adapter->state); 1134 return 0; 1135 } 1136 1137 /* Usage: During resume and firmware recovery module.*/ 1138 1139 static inline int 1140 netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev) 1141 { 1142 int err = 0; 1143 1144 rtnl_lock(); 1145 if (netif_running(netdev)) 1146 err = __netxen_nic_up(adapter, netdev); 1147 rtnl_unlock(); 1148 1149 return err; 1150 } 1151 1152 /* with rtnl_lock */ 1153 static void 1154 __netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev) 1155 { 1156 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) 1157 return; 1158 1159 if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state)) 1160 return; 1161 1162 smp_mb(); 1163 netif_carrier_off(netdev); 1164 netif_tx_disable(netdev); 1165 1166 if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION) 1167 netxen_linkevent_request(adapter, 0); 1168 1169 if (adapter->stop_port) 1170 adapter->stop_port(adapter); 1171 1172 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) 1173 netxen_p3_free_mac_list(adapter); 1174 1175 adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE); 1176 1177 netxen_napi_disable(adapter); 1178 1179 netxen_release_tx_buffers(adapter); 1180 } 1181 1182 /* Usage: During suspend and firmware recovery module */ 1183 1184 static inline void 1185 netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev) 1186 { 1187 rtnl_lock(); 1188 if (netif_running(netdev)) 1189 __netxen_nic_down(adapter, netdev); 1190 rtnl_unlock(); 1191 1192 } 1193 1194 static int 1195 netxen_nic_attach(struct netxen_adapter *adapter) 1196 { 1197 struct net_device *netdev = adapter->netdev; 1198 struct pci_dev *pdev = adapter->pdev; 1199 int err, ring; 1200 struct nx_host_rds_ring *rds_ring; 1201 struct nx_host_tx_ring *tx_ring; 1202 u32 capab2; 1203 1204 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) 1205 return 0; 1206 1207 err = netxen_init_firmware(adapter); 1208 if (err) 1209 return err; 1210 1211 adapter->flags &= ~NETXEN_FW_MSS_CAP; 1212 if (adapter->capabilities & NX_FW_CAPABILITY_MORE_CAPS) { 1213 capab2 = NXRD32(adapter, CRB_FW_CAPABILITIES_2); 1214 if (capab2 & NX_FW_CAPABILITY_2_LRO_MAX_TCP_SEG) 1215 adapter->flags |= NETXEN_FW_MSS_CAP; 1216 } 1217 1218 err = netxen_napi_add(adapter, netdev); 1219 if (err) 1220 return err; 1221 1222 err = netxen_alloc_sw_resources(adapter); 1223 if (err) { 1224 printk(KERN_ERR "%s: Error in setting sw resources\n", 1225 netdev->name); 1226 return err; 1227 } 1228 1229 err = netxen_alloc_hw_resources(adapter); 1230 if (err) { 1231 printk(KERN_ERR "%s: Error in setting hw resources\n", 1232 netdev->name); 1233 goto err_out_free_sw; 1234 } 1235 1236 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) { 1237 tx_ring = adapter->tx_ring; 1238 tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter, 1239 crb_cmd_producer[adapter->portnum]); 1240 tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter, 1241 crb_cmd_consumer[adapter->portnum]); 1242 1243 tx_ring->producer = 0; 1244 tx_ring->sw_consumer = 0; 1245 1246 netxen_nic_update_cmd_producer(adapter, tx_ring); 1247 netxen_nic_update_cmd_consumer(adapter, tx_ring); 1248 } 1249 1250 for (ring = 0; ring < adapter->max_rds_rings; ring++) { 1251 rds_ring = &adapter->recv_ctx.rds_rings[ring]; 1252 netxen_post_rx_buffers(adapter, ring, rds_ring); 1253 } 1254 1255 err = netxen_nic_request_irq(adapter); 1256 if (err) { 1257 dev_err(&pdev->dev, "%s: failed to setup interrupt\n", 1258 netdev->name); 1259 goto err_out_free_rxbuf; 1260 } 1261 1262 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) 1263 netxen_nic_init_coalesce_defaults(adapter); 1264 1265 netxen_create_sysfs_entries(adapter); 1266 1267 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC; 1268 return 0; 1269 1270 err_out_free_rxbuf: 1271 netxen_release_rx_buffers(adapter); 1272 netxen_free_hw_resources(adapter); 1273 err_out_free_sw: 1274 netxen_free_sw_resources(adapter); 1275 return err; 1276 } 1277 1278 static void 1279 netxen_nic_detach(struct netxen_adapter *adapter) 1280 { 1281 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) 1282 return; 1283 1284 netxen_remove_sysfs_entries(adapter); 1285 1286 netxen_free_hw_resources(adapter); 1287 netxen_release_rx_buffers(adapter); 1288 netxen_nic_free_irq(adapter); 1289 netxen_napi_del(adapter); 1290 netxen_free_sw_resources(adapter); 1291 1292 adapter->is_up = 0; 1293 } 1294 1295 int 1296 netxen_nic_reset_context(struct netxen_adapter *adapter) 1297 { 1298 int err = 0; 1299 struct net_device *netdev = adapter->netdev; 1300 1301 if (test_and_set_bit(__NX_RESETTING, &adapter->state)) 1302 return -EBUSY; 1303 1304 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) { 1305 1306 netif_device_detach(netdev); 1307 1308 if (netif_running(netdev)) 1309 __netxen_nic_down(adapter, netdev); 1310 1311 netxen_nic_detach(adapter); 1312 1313 if (netif_running(netdev)) { 1314 err = netxen_nic_attach(adapter); 1315 if (!err) 1316 err = __netxen_nic_up(adapter, netdev); 1317 1318 if (err) 1319 goto done; 1320 } 1321 1322 netif_device_attach(netdev); 1323 } 1324 1325 done: 1326 clear_bit(__NX_RESETTING, &adapter->state); 1327 return err; 1328 } 1329 1330 static int 1331 netxen_setup_netdev(struct netxen_adapter *adapter, 1332 struct net_device *netdev) 1333 { 1334 int err = 0; 1335 struct pci_dev *pdev = adapter->pdev; 1336 1337 adapter->mc_enabled = 0; 1338 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) 1339 adapter->max_mc_count = 38; 1340 else 1341 adapter->max_mc_count = 16; 1342 1343 netdev->netdev_ops = &netxen_netdev_ops; 1344 netdev->watchdog_timeo = 5*HZ; 1345 1346 netxen_nic_change_mtu(netdev, netdev->mtu); 1347 1348 netdev->ethtool_ops = &netxen_nic_ethtool_ops; 1349 1350 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | 1351 NETIF_F_RXCSUM; 1352 1353 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) 1354 netdev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6; 1355 1356 netdev->vlan_features |= netdev->hw_features; 1357 1358 if (adapter->pci_using_dac) { 1359 netdev->features |= NETIF_F_HIGHDMA; 1360 netdev->vlan_features |= NETIF_F_HIGHDMA; 1361 } 1362 1363 if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX) 1364 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX; 1365 1366 if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO) 1367 netdev->hw_features |= NETIF_F_LRO; 1368 1369 netdev->features |= netdev->hw_features; 1370 1371 netdev->irq = adapter->msix_entries[0].vector; 1372 1373 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task); 1374 1375 if (netxen_read_mac_addr(adapter)) 1376 dev_warn(&pdev->dev, "failed to read mac addr\n"); 1377 1378 netif_carrier_off(netdev); 1379 1380 err = register_netdev(netdev); 1381 if (err) { 1382 dev_err(&pdev->dev, "failed to register net device\n"); 1383 return err; 1384 } 1385 1386 return 0; 1387 } 1388 1389 #define NETXEN_ULA_ADAPTER_KEY (0xdaddad01) 1390 #define NETXEN_NON_ULA_ADAPTER_KEY (0xdaddad00) 1391 1392 static void netxen_read_ula_info(struct netxen_adapter *adapter) 1393 { 1394 u32 temp; 1395 1396 /* Print ULA info only once for an adapter */ 1397 if (adapter->portnum != 0) 1398 return; 1399 1400 temp = NXRD32(adapter, NETXEN_ULA_KEY); 1401 switch (temp) { 1402 case NETXEN_ULA_ADAPTER_KEY: 1403 dev_info(&adapter->pdev->dev, "ULA adapter"); 1404 break; 1405 case NETXEN_NON_ULA_ADAPTER_KEY: 1406 dev_info(&adapter->pdev->dev, "non ULA adapter"); 1407 break; 1408 default: 1409 break; 1410 } 1411 1412 return; 1413 } 1414 1415 #ifdef CONFIG_PCIEAER 1416 static void netxen_mask_aer_correctable(struct netxen_adapter *adapter) 1417 { 1418 struct pci_dev *pdev = adapter->pdev; 1419 struct pci_dev *root = pdev->bus->self; 1420 u32 aer_pos; 1421 1422 /* root bus? */ 1423 if (!root) 1424 return; 1425 1426 if (adapter->ahw.board_type != NETXEN_BRDTYPE_P3_4_GB_MM && 1427 adapter->ahw.board_type != NETXEN_BRDTYPE_P3_10G_TP) 1428 return; 1429 1430 if (pci_pcie_type(root) != PCI_EXP_TYPE_ROOT_PORT) 1431 return; 1432 1433 aer_pos = pci_find_ext_capability(root, PCI_EXT_CAP_ID_ERR); 1434 if (!aer_pos) 1435 return; 1436 1437 pci_write_config_dword(root, aer_pos + PCI_ERR_COR_MASK, 0xffff); 1438 } 1439 #endif 1440 1441 static int 1442 netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 1443 { 1444 struct net_device *netdev = NULL; 1445 struct netxen_adapter *adapter = NULL; 1446 int i = 0, err; 1447 int pci_func_id = PCI_FUNC(pdev->devfn); 1448 uint8_t revision_id; 1449 u32 val; 1450 1451 if (pdev->revision >= NX_P3_A0 && pdev->revision <= NX_P3_B1) { 1452 pr_warn("%s: chip revisions between 0x%x-0x%x will not be enabled\n", 1453 module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1); 1454 return -ENODEV; 1455 } 1456 1457 if ((err = pci_enable_device(pdev))) 1458 return err; 1459 1460 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { 1461 err = -ENODEV; 1462 goto err_out_disable_pdev; 1463 } 1464 1465 if ((err = pci_request_regions(pdev, netxen_nic_driver_name))) 1466 goto err_out_disable_pdev; 1467 1468 if (NX_IS_REVISION_P3(pdev->revision)) 1469 pci_enable_pcie_error_reporting(pdev); 1470 1471 pci_set_master(pdev); 1472 1473 netdev = alloc_etherdev(sizeof(struct netxen_adapter)); 1474 if(!netdev) { 1475 err = -ENOMEM; 1476 goto err_out_free_res; 1477 } 1478 1479 SET_NETDEV_DEV(netdev, &pdev->dev); 1480 1481 adapter = netdev_priv(netdev); 1482 adapter->netdev = netdev; 1483 adapter->pdev = pdev; 1484 adapter->ahw.pci_func = pci_func_id; 1485 1486 revision_id = pdev->revision; 1487 adapter->ahw.revision_id = revision_id; 1488 1489 rwlock_init(&adapter->ahw.crb_lock); 1490 spin_lock_init(&adapter->ahw.mem_lock); 1491 1492 spin_lock_init(&adapter->tx_clean_lock); 1493 INIT_LIST_HEAD(&adapter->mac_list); 1494 INIT_LIST_HEAD(&adapter->ip_list); 1495 1496 err = netxen_setup_pci_map(adapter); 1497 if (err) 1498 goto err_out_free_netdev; 1499 1500 /* This will be reset for mezz cards */ 1501 adapter->portnum = pci_func_id; 1502 1503 err = netxen_nic_get_board_info(adapter); 1504 if (err) { 1505 dev_err(&pdev->dev, "Error getting board config info.\n"); 1506 goto err_out_iounmap; 1507 } 1508 1509 #ifdef CONFIG_PCIEAER 1510 netxen_mask_aer_correctable(adapter); 1511 #endif 1512 1513 /* Mezz cards have PCI function 0,2,3 enabled */ 1514 switch (adapter->ahw.board_type) { 1515 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ: 1516 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ: 1517 if (pci_func_id >= 2) 1518 adapter->portnum = pci_func_id - 2; 1519 break; 1520 default: 1521 break; 1522 } 1523 1524 err = netxen_check_flash_fw_compatibility(adapter); 1525 if (err) 1526 goto err_out_iounmap; 1527 1528 if (adapter->portnum == 0) { 1529 val = NXRD32(adapter, NX_CRB_DEV_REF_COUNT); 1530 if (val != 0xffffffff && val != 0) { 1531 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, 0); 1532 adapter->need_fw_reset = 1; 1533 } 1534 } 1535 1536 err = netxen_start_firmware(adapter); 1537 if (err) 1538 goto err_out_decr_ref; 1539 1540 /* 1541 * See if the firmware gave us a virtual-physical port mapping. 1542 */ 1543 adapter->physical_port = adapter->portnum; 1544 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) { 1545 i = NXRD32(adapter, CRB_V2P(adapter->portnum)); 1546 if (i != 0x55555555) 1547 adapter->physical_port = i; 1548 } 1549 1550 /* MTU range: 0 - 8000 (P2) or 9600 (P3) */ 1551 netdev->min_mtu = 0; 1552 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) 1553 netdev->max_mtu = P3_MAX_MTU; 1554 else 1555 netdev->max_mtu = P2_MAX_MTU; 1556 1557 netxen_nic_clear_stats(adapter); 1558 1559 err = netxen_setup_intr(adapter); 1560 1561 if (err) { 1562 dev_err(&adapter->pdev->dev, 1563 "Failed to setup interrupts, error = %d\n", err); 1564 goto err_out_disable_msi; 1565 } 1566 1567 netxen_read_ula_info(adapter); 1568 1569 err = netxen_setup_netdev(adapter, netdev); 1570 if (err) 1571 goto err_out_disable_msi; 1572 1573 pci_set_drvdata(pdev, adapter); 1574 1575 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY); 1576 1577 switch (adapter->ahw.port_type) { 1578 case NETXEN_NIC_GBE: 1579 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n", 1580 adapter->netdev->name); 1581 break; 1582 case NETXEN_NIC_XGBE: 1583 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n", 1584 adapter->netdev->name); 1585 break; 1586 } 1587 1588 netxen_create_diag_entries(adapter); 1589 1590 return 0; 1591 1592 err_out_disable_msi: 1593 netxen_teardown_intr(adapter); 1594 1595 netxen_free_dummy_dma(adapter); 1596 1597 err_out_decr_ref: 1598 nx_decr_dev_ref_cnt(adapter); 1599 1600 err_out_iounmap: 1601 netxen_cleanup_pci_map(adapter); 1602 1603 err_out_free_netdev: 1604 free_netdev(netdev); 1605 1606 err_out_free_res: 1607 if (NX_IS_REVISION_P3(pdev->revision)) 1608 pci_disable_pcie_error_reporting(pdev); 1609 pci_release_regions(pdev); 1610 1611 err_out_disable_pdev: 1612 pci_disable_device(pdev); 1613 return err; 1614 } 1615 1616 static 1617 void netxen_cleanup_minidump(struct netxen_adapter *adapter) 1618 { 1619 kfree(adapter->mdump.md_template); 1620 adapter->mdump.md_template = NULL; 1621 1622 if (adapter->mdump.md_capture_buff) { 1623 vfree(adapter->mdump.md_capture_buff); 1624 adapter->mdump.md_capture_buff = NULL; 1625 } 1626 } 1627 1628 static void netxen_nic_remove(struct pci_dev *pdev) 1629 { 1630 struct netxen_adapter *adapter; 1631 struct net_device *netdev; 1632 1633 adapter = pci_get_drvdata(pdev); 1634 if (adapter == NULL) 1635 return; 1636 1637 netdev = adapter->netdev; 1638 1639 netxen_cancel_fw_work(adapter); 1640 1641 unregister_netdev(netdev); 1642 1643 cancel_work_sync(&adapter->tx_timeout_task); 1644 1645 netxen_free_ip_list(adapter, false); 1646 netxen_nic_detach(adapter); 1647 1648 nx_decr_dev_ref_cnt(adapter); 1649 1650 if (adapter->portnum == 0) 1651 netxen_free_dummy_dma(adapter); 1652 1653 clear_bit(__NX_RESETTING, &adapter->state); 1654 1655 netxen_teardown_intr(adapter); 1656 netxen_set_interrupt_mode(adapter, 0); 1657 netxen_remove_diag_entries(adapter); 1658 1659 netxen_cleanup_pci_map(adapter); 1660 1661 netxen_release_firmware(adapter); 1662 1663 if (NX_IS_REVISION_P3(pdev->revision)) { 1664 netxen_cleanup_minidump(adapter); 1665 pci_disable_pcie_error_reporting(pdev); 1666 } 1667 1668 pci_release_regions(pdev); 1669 pci_disable_device(pdev); 1670 1671 free_netdev(netdev); 1672 } 1673 1674 static void netxen_nic_detach_func(struct netxen_adapter *adapter) 1675 { 1676 struct net_device *netdev = adapter->netdev; 1677 1678 netif_device_detach(netdev); 1679 1680 netxen_cancel_fw_work(adapter); 1681 1682 if (netif_running(netdev)) 1683 netxen_nic_down(adapter, netdev); 1684 1685 cancel_work_sync(&adapter->tx_timeout_task); 1686 1687 netxen_nic_detach(adapter); 1688 1689 if (adapter->portnum == 0) 1690 netxen_free_dummy_dma(adapter); 1691 1692 nx_decr_dev_ref_cnt(adapter); 1693 1694 clear_bit(__NX_RESETTING, &adapter->state); 1695 } 1696 1697 static int netxen_nic_attach_late_func(struct pci_dev *pdev) 1698 { 1699 struct netxen_adapter *adapter = pci_get_drvdata(pdev); 1700 struct net_device *netdev = adapter->netdev; 1701 int err; 1702 1703 pci_set_master(pdev); 1704 1705 adapter->ahw.crb_win = -1; 1706 adapter->ahw.ocm_win = -1; 1707 1708 err = netxen_start_firmware(adapter); 1709 if (err) { 1710 dev_err(&pdev->dev, "failed to start firmware\n"); 1711 return err; 1712 } 1713 1714 if (netif_running(netdev)) { 1715 err = netxen_nic_attach(adapter); 1716 if (err) 1717 goto err_out; 1718 1719 err = netxen_nic_up(adapter, netdev); 1720 if (err) 1721 goto err_out_detach; 1722 1723 netxen_restore_indev_addr(netdev, NETDEV_UP); 1724 } 1725 1726 netif_device_attach(netdev); 1727 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY); 1728 return 0; 1729 1730 err_out_detach: 1731 netxen_nic_detach(adapter); 1732 err_out: 1733 nx_decr_dev_ref_cnt(adapter); 1734 return err; 1735 } 1736 1737 static int netxen_nic_attach_func(struct pci_dev *pdev) 1738 { 1739 int err; 1740 1741 err = pci_enable_device(pdev); 1742 if (err) 1743 return err; 1744 1745 pci_set_power_state(pdev, PCI_D0); 1746 pci_restore_state(pdev); 1747 1748 return netxen_nic_attach_late_func(pdev); 1749 } 1750 1751 static pci_ers_result_t netxen_io_error_detected(struct pci_dev *pdev, 1752 pci_channel_state_t state) 1753 { 1754 struct netxen_adapter *adapter = pci_get_drvdata(pdev); 1755 1756 if (state == pci_channel_io_perm_failure) 1757 return PCI_ERS_RESULT_DISCONNECT; 1758 1759 if (nx_dev_request_aer(adapter)) 1760 return PCI_ERS_RESULT_RECOVERED; 1761 1762 netxen_nic_detach_func(adapter); 1763 1764 pci_disable_device(pdev); 1765 1766 return PCI_ERS_RESULT_NEED_RESET; 1767 } 1768 1769 static pci_ers_result_t netxen_io_slot_reset(struct pci_dev *pdev) 1770 { 1771 int err = 0; 1772 1773 err = netxen_nic_attach_func(pdev); 1774 1775 return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED; 1776 } 1777 1778 static void netxen_nic_shutdown(struct pci_dev *pdev) 1779 { 1780 struct netxen_adapter *adapter = pci_get_drvdata(pdev); 1781 1782 netxen_nic_detach_func(adapter); 1783 1784 if (pci_save_state(pdev)) 1785 return; 1786 1787 if (netxen_nic_wol_supported(adapter)) { 1788 pci_enable_wake(pdev, PCI_D3cold, 1); 1789 pci_enable_wake(pdev, PCI_D3hot, 1); 1790 } 1791 1792 pci_disable_device(pdev); 1793 } 1794 1795 static int __maybe_unused 1796 netxen_nic_suspend(struct device *dev_d) 1797 { 1798 struct netxen_adapter *adapter = dev_get_drvdata(dev_d); 1799 1800 netxen_nic_detach_func(adapter); 1801 1802 if (netxen_nic_wol_supported(adapter)) 1803 device_wakeup_enable(dev_d); 1804 1805 return 0; 1806 } 1807 1808 static int __maybe_unused 1809 netxen_nic_resume(struct device *dev_d) 1810 { 1811 return netxen_nic_attach_late_func(to_pci_dev(dev_d)); 1812 } 1813 1814 static int netxen_nic_open(struct net_device *netdev) 1815 { 1816 struct netxen_adapter *adapter = netdev_priv(netdev); 1817 int err = 0; 1818 1819 if (adapter->driver_mismatch) 1820 return -EIO; 1821 1822 err = netxen_nic_attach(adapter); 1823 if (err) 1824 return err; 1825 1826 err = __netxen_nic_up(adapter, netdev); 1827 if (err) 1828 goto err_out; 1829 1830 netif_start_queue(netdev); 1831 1832 return 0; 1833 1834 err_out: 1835 netxen_nic_detach(adapter); 1836 return err; 1837 } 1838 1839 /* 1840 * netxen_nic_close - Disables a network interface entry point 1841 */ 1842 static int netxen_nic_close(struct net_device *netdev) 1843 { 1844 struct netxen_adapter *adapter = netdev_priv(netdev); 1845 1846 __netxen_nic_down(adapter, netdev); 1847 return 0; 1848 } 1849 1850 static void 1851 netxen_tso_check(struct net_device *netdev, 1852 struct nx_host_tx_ring *tx_ring, 1853 struct cmd_desc_type0 *first_desc, 1854 struct sk_buff *skb) 1855 { 1856 u8 opcode = TX_ETHER_PKT; 1857 __be16 protocol = skb->protocol; 1858 u16 flags = 0, vid = 0; 1859 u32 producer; 1860 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0; 1861 struct cmd_desc_type0 *hwdesc; 1862 struct vlan_ethhdr *vh; 1863 1864 if (protocol == cpu_to_be16(ETH_P_8021Q)) { 1865 1866 vh = (struct vlan_ethhdr *)skb->data; 1867 protocol = vh->h_vlan_encapsulated_proto; 1868 flags = FLAGS_VLAN_TAGGED; 1869 1870 } else if (skb_vlan_tag_present(skb)) { 1871 flags = FLAGS_VLAN_OOB; 1872 vid = skb_vlan_tag_get(skb); 1873 netxen_set_tx_vlan_tci(first_desc, vid); 1874 vlan_oob = 1; 1875 } 1876 1877 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) && 1878 skb_shinfo(skb)->gso_size > 0) { 1879 1880 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 1881 1882 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size); 1883 first_desc->total_hdr_length = hdr_len; 1884 if (vlan_oob) { 1885 first_desc->total_hdr_length += VLAN_HLEN; 1886 first_desc->tcp_hdr_offset = VLAN_HLEN; 1887 first_desc->ip_hdr_offset = VLAN_HLEN; 1888 /* Only in case of TSO on vlan device */ 1889 flags |= FLAGS_VLAN_TAGGED; 1890 } 1891 1892 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ? 1893 TX_TCP_LSO6 : TX_TCP_LSO; 1894 tso = 1; 1895 1896 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 1897 u8 l4proto; 1898 1899 if (protocol == cpu_to_be16(ETH_P_IP)) { 1900 l4proto = ip_hdr(skb)->protocol; 1901 1902 if (l4proto == IPPROTO_TCP) 1903 opcode = TX_TCP_PKT; 1904 else if(l4proto == IPPROTO_UDP) 1905 opcode = TX_UDP_PKT; 1906 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) { 1907 l4proto = ipv6_hdr(skb)->nexthdr; 1908 1909 if (l4proto == IPPROTO_TCP) 1910 opcode = TX_TCPV6_PKT; 1911 else if(l4proto == IPPROTO_UDP) 1912 opcode = TX_UDPV6_PKT; 1913 } 1914 } 1915 1916 first_desc->tcp_hdr_offset += skb_transport_offset(skb); 1917 first_desc->ip_hdr_offset += skb_network_offset(skb); 1918 netxen_set_tx_flags_opcode(first_desc, flags, opcode); 1919 1920 if (!tso) 1921 return; 1922 1923 /* For LSO, we need to copy the MAC/IP/TCP headers into 1924 * the descriptor ring 1925 */ 1926 producer = tx_ring->producer; 1927 copied = 0; 1928 offset = 2; 1929 1930 if (vlan_oob) { 1931 /* Create a TSO vlan header template for firmware */ 1932 1933 hwdesc = &tx_ring->desc_head[producer]; 1934 tx_ring->cmd_buf_arr[producer].skb = NULL; 1935 1936 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset, 1937 hdr_len + VLAN_HLEN); 1938 1939 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2); 1940 skb_copy_from_linear_data(skb, vh, 12); 1941 vh->h_vlan_proto = htons(ETH_P_8021Q); 1942 vh->h_vlan_TCI = htons(vid); 1943 skb_copy_from_linear_data_offset(skb, 12, 1944 (char *)vh + 16, copy_len - 16); 1945 1946 copied = copy_len - VLAN_HLEN; 1947 offset = 0; 1948 1949 producer = get_next_index(producer, tx_ring->num_desc); 1950 } 1951 1952 while (copied < hdr_len) { 1953 1954 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset, 1955 (hdr_len - copied)); 1956 1957 hwdesc = &tx_ring->desc_head[producer]; 1958 tx_ring->cmd_buf_arr[producer].skb = NULL; 1959 1960 skb_copy_from_linear_data_offset(skb, copied, 1961 (char *)hwdesc + offset, copy_len); 1962 1963 copied += copy_len; 1964 offset = 0; 1965 1966 producer = get_next_index(producer, tx_ring->num_desc); 1967 } 1968 1969 tx_ring->producer = producer; 1970 barrier(); 1971 } 1972 1973 static int 1974 netxen_map_tx_skb(struct pci_dev *pdev, 1975 struct sk_buff *skb, struct netxen_cmd_buffer *pbuf) 1976 { 1977 struct netxen_skb_frag *nf; 1978 skb_frag_t *frag; 1979 int i, nr_frags; 1980 dma_addr_t map; 1981 1982 nr_frags = skb_shinfo(skb)->nr_frags; 1983 nf = &pbuf->frag_array[0]; 1984 1985 map = dma_map_single(&pdev->dev, skb->data, skb_headlen(skb), 1986 DMA_TO_DEVICE); 1987 if (dma_mapping_error(&pdev->dev, map)) 1988 goto out_err; 1989 1990 nf->dma = map; 1991 nf->length = skb_headlen(skb); 1992 1993 for (i = 0; i < nr_frags; i++) { 1994 frag = &skb_shinfo(skb)->frags[i]; 1995 nf = &pbuf->frag_array[i+1]; 1996 1997 map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag), 1998 DMA_TO_DEVICE); 1999 if (dma_mapping_error(&pdev->dev, map)) 2000 goto unwind; 2001 2002 nf->dma = map; 2003 nf->length = skb_frag_size(frag); 2004 } 2005 2006 return 0; 2007 2008 unwind: 2009 while (--i >= 0) { 2010 nf = &pbuf->frag_array[i+1]; 2011 dma_unmap_page(&pdev->dev, nf->dma, nf->length, DMA_TO_DEVICE); 2012 nf->dma = 0ULL; 2013 } 2014 2015 nf = &pbuf->frag_array[0]; 2016 dma_unmap_single(&pdev->dev, nf->dma, skb_headlen(skb), DMA_TO_DEVICE); 2017 nf->dma = 0ULL; 2018 2019 out_err: 2020 return -ENOMEM; 2021 } 2022 2023 static inline void 2024 netxen_clear_cmddesc(u64 *desc) 2025 { 2026 desc[0] = 0ULL; 2027 desc[2] = 0ULL; 2028 } 2029 2030 static netdev_tx_t 2031 netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev) 2032 { 2033 struct netxen_adapter *adapter = netdev_priv(netdev); 2034 struct nx_host_tx_ring *tx_ring = adapter->tx_ring; 2035 struct netxen_cmd_buffer *pbuf; 2036 struct netxen_skb_frag *buffrag; 2037 struct cmd_desc_type0 *hwdesc, *first_desc; 2038 struct pci_dev *pdev; 2039 int i, k; 2040 int delta = 0; 2041 skb_frag_t *frag; 2042 2043 u32 producer; 2044 int frag_count; 2045 u32 num_txd = tx_ring->num_desc; 2046 2047 frag_count = skb_shinfo(skb)->nr_frags + 1; 2048 2049 /* 14 frags supported for normal packet and 2050 * 32 frags supported for TSO packet 2051 */ 2052 if (!skb_is_gso(skb) && frag_count > NETXEN_MAX_FRAGS_PER_TX) { 2053 2054 for (i = 0; i < (frag_count - NETXEN_MAX_FRAGS_PER_TX); i++) { 2055 frag = &skb_shinfo(skb)->frags[i]; 2056 delta += skb_frag_size(frag); 2057 } 2058 2059 if (!__pskb_pull_tail(skb, delta)) 2060 goto drop_packet; 2061 2062 frag_count = 1 + skb_shinfo(skb)->nr_frags; 2063 } 2064 2065 if (unlikely(netxen_tx_avail(tx_ring) <= TX_STOP_THRESH)) { 2066 netif_stop_queue(netdev); 2067 smp_mb(); 2068 if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH) 2069 netif_start_queue(netdev); 2070 else 2071 return NETDEV_TX_BUSY; 2072 } 2073 2074 producer = tx_ring->producer; 2075 pbuf = &tx_ring->cmd_buf_arr[producer]; 2076 2077 pdev = adapter->pdev; 2078 2079 if (netxen_map_tx_skb(pdev, skb, pbuf)) 2080 goto drop_packet; 2081 2082 pbuf->skb = skb; 2083 pbuf->frag_count = frag_count; 2084 2085 first_desc = hwdesc = &tx_ring->desc_head[producer]; 2086 netxen_clear_cmddesc((u64 *)hwdesc); 2087 2088 netxen_set_tx_frags_len(first_desc, frag_count, skb->len); 2089 netxen_set_tx_port(first_desc, adapter->portnum); 2090 2091 for (i = 0; i < frag_count; i++) { 2092 2093 k = i % 4; 2094 2095 if ((k == 0) && (i > 0)) { 2096 /* move to next desc.*/ 2097 producer = get_next_index(producer, num_txd); 2098 hwdesc = &tx_ring->desc_head[producer]; 2099 netxen_clear_cmddesc((u64 *)hwdesc); 2100 tx_ring->cmd_buf_arr[producer].skb = NULL; 2101 } 2102 2103 buffrag = &pbuf->frag_array[i]; 2104 2105 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length); 2106 switch (k) { 2107 case 0: 2108 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma); 2109 break; 2110 case 1: 2111 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma); 2112 break; 2113 case 2: 2114 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma); 2115 break; 2116 case 3: 2117 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma); 2118 break; 2119 } 2120 } 2121 2122 tx_ring->producer = get_next_index(producer, num_txd); 2123 2124 netxen_tso_check(netdev, tx_ring, first_desc, skb); 2125 2126 adapter->stats.txbytes += skb->len; 2127 adapter->stats.xmitcalled++; 2128 2129 netxen_nic_update_cmd_producer(adapter, tx_ring); 2130 2131 return NETDEV_TX_OK; 2132 2133 drop_packet: 2134 adapter->stats.txdropped++; 2135 dev_kfree_skb_any(skb); 2136 return NETDEV_TX_OK; 2137 } 2138 2139 static int netxen_nic_check_temp(struct netxen_adapter *adapter) 2140 { 2141 struct net_device *netdev = adapter->netdev; 2142 uint32_t temp, temp_state, temp_val; 2143 int rv = 0; 2144 2145 temp = NXRD32(adapter, CRB_TEMP_STATE); 2146 2147 temp_state = nx_get_temp_state(temp); 2148 temp_val = nx_get_temp_val(temp); 2149 2150 if (temp_state == NX_TEMP_PANIC) { 2151 printk(KERN_ALERT 2152 "%s: Device temperature %d degrees C exceeds" 2153 " maximum allowed. Hardware has been shut down.\n", 2154 netdev->name, temp_val); 2155 rv = 1; 2156 } else if (temp_state == NX_TEMP_WARN) { 2157 if (adapter->temp == NX_TEMP_NORMAL) { 2158 printk(KERN_ALERT 2159 "%s: Device temperature %d degrees C " 2160 "exceeds operating range." 2161 " Immediate action needed.\n", 2162 netdev->name, temp_val); 2163 } 2164 } else { 2165 if (adapter->temp == NX_TEMP_WARN) { 2166 printk(KERN_INFO 2167 "%s: Device temperature is now %d degrees C" 2168 " in normal range.\n", netdev->name, 2169 temp_val); 2170 } 2171 } 2172 adapter->temp = temp_state; 2173 return rv; 2174 } 2175 2176 void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup) 2177 { 2178 struct net_device *netdev = adapter->netdev; 2179 2180 if (adapter->ahw.linkup && !linkup) { 2181 printk(KERN_INFO "%s: %s NIC Link is down\n", 2182 netxen_nic_driver_name, netdev->name); 2183 adapter->ahw.linkup = 0; 2184 if (netif_running(netdev)) { 2185 netif_carrier_off(netdev); 2186 netif_stop_queue(netdev); 2187 } 2188 adapter->link_changed = !adapter->has_link_events; 2189 } else if (!adapter->ahw.linkup && linkup) { 2190 printk(KERN_INFO "%s: %s NIC Link is up\n", 2191 netxen_nic_driver_name, netdev->name); 2192 adapter->ahw.linkup = 1; 2193 if (netif_running(netdev)) { 2194 netif_carrier_on(netdev); 2195 netif_wake_queue(netdev); 2196 } 2197 adapter->link_changed = !adapter->has_link_events; 2198 } 2199 } 2200 2201 static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter) 2202 { 2203 u32 val, port, linkup; 2204 2205 port = adapter->physical_port; 2206 2207 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) { 2208 val = NXRD32(adapter, CRB_XG_STATE_P3); 2209 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val); 2210 linkup = (val == XG_LINK_UP_P3); 2211 } else { 2212 val = NXRD32(adapter, CRB_XG_STATE); 2213 val = (val >> port*8) & 0xff; 2214 linkup = (val == XG_LINK_UP); 2215 } 2216 2217 netxen_advert_link_change(adapter, linkup); 2218 } 2219 2220 static void netxen_tx_timeout(struct net_device *netdev, unsigned int txqueue) 2221 { 2222 struct netxen_adapter *adapter = netdev_priv(netdev); 2223 2224 if (test_bit(__NX_RESETTING, &adapter->state)) 2225 return; 2226 2227 dev_err(&netdev->dev, "transmit timeout, resetting.\n"); 2228 schedule_work(&adapter->tx_timeout_task); 2229 } 2230 2231 static void netxen_tx_timeout_task(struct work_struct *work) 2232 { 2233 struct netxen_adapter *adapter = 2234 container_of(work, struct netxen_adapter, tx_timeout_task); 2235 2236 if (!netif_running(adapter->netdev)) 2237 return; 2238 2239 if (test_and_set_bit(__NX_RESETTING, &adapter->state)) 2240 return; 2241 2242 if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS) 2243 goto request_reset; 2244 2245 rtnl_lock(); 2246 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) { 2247 /* try to scrub interrupt */ 2248 netxen_napi_disable(adapter); 2249 2250 netxen_napi_enable(adapter); 2251 2252 netif_wake_queue(adapter->netdev); 2253 2254 clear_bit(__NX_RESETTING, &adapter->state); 2255 } else { 2256 clear_bit(__NX_RESETTING, &adapter->state); 2257 if (netxen_nic_reset_context(adapter)) { 2258 rtnl_unlock(); 2259 goto request_reset; 2260 } 2261 } 2262 netif_trans_update(adapter->netdev); 2263 rtnl_unlock(); 2264 return; 2265 2266 request_reset: 2267 adapter->need_fw_reset = 1; 2268 clear_bit(__NX_RESETTING, &adapter->state); 2269 } 2270 2271 static void netxen_nic_get_stats(struct net_device *netdev, 2272 struct rtnl_link_stats64 *stats) 2273 { 2274 struct netxen_adapter *adapter = netdev_priv(netdev); 2275 2276 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts; 2277 stats->tx_packets = adapter->stats.xmitfinished; 2278 stats->rx_bytes = adapter->stats.rxbytes; 2279 stats->tx_bytes = adapter->stats.txbytes; 2280 stats->rx_dropped = adapter->stats.rxdropped; 2281 stats->tx_dropped = adapter->stats.txdropped; 2282 } 2283 2284 static irqreturn_t netxen_intr(int irq, void *data) 2285 { 2286 struct nx_host_sds_ring *sds_ring = data; 2287 struct netxen_adapter *adapter = sds_ring->adapter; 2288 u32 status = 0; 2289 2290 status = readl(adapter->isr_int_vec); 2291 2292 if (!(status & adapter->int_vec_bit)) 2293 return IRQ_NONE; 2294 2295 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) { 2296 /* check interrupt state machine, to be sure */ 2297 status = readl(adapter->crb_int_state_reg); 2298 if (!ISR_LEGACY_INT_TRIGGERED(status)) 2299 return IRQ_NONE; 2300 2301 } else { 2302 unsigned long our_int = 0; 2303 2304 our_int = readl(adapter->crb_int_state_reg); 2305 2306 /* not our interrupt */ 2307 if (!test_and_clear_bit((7 + adapter->portnum), &our_int)) 2308 return IRQ_NONE; 2309 2310 /* claim interrupt */ 2311 writel((our_int & 0xffffffff), adapter->crb_int_state_reg); 2312 2313 /* clear interrupt */ 2314 netxen_nic_disable_int(sds_ring); 2315 } 2316 2317 writel(0xffffffff, adapter->tgt_status_reg); 2318 /* read twice to ensure write is flushed */ 2319 readl(adapter->isr_int_vec); 2320 readl(adapter->isr_int_vec); 2321 2322 napi_schedule(&sds_ring->napi); 2323 2324 return IRQ_HANDLED; 2325 } 2326 2327 static irqreturn_t netxen_msi_intr(int irq, void *data) 2328 { 2329 struct nx_host_sds_ring *sds_ring = data; 2330 struct netxen_adapter *adapter = sds_ring->adapter; 2331 2332 /* clear interrupt */ 2333 writel(0xffffffff, adapter->tgt_status_reg); 2334 2335 napi_schedule(&sds_ring->napi); 2336 return IRQ_HANDLED; 2337 } 2338 2339 static irqreturn_t netxen_msix_intr(int irq, void *data) 2340 { 2341 struct nx_host_sds_ring *sds_ring = data; 2342 2343 napi_schedule(&sds_ring->napi); 2344 return IRQ_HANDLED; 2345 } 2346 2347 static int netxen_nic_poll(struct napi_struct *napi, int budget) 2348 { 2349 struct nx_host_sds_ring *sds_ring = 2350 container_of(napi, struct nx_host_sds_ring, napi); 2351 2352 struct netxen_adapter *adapter = sds_ring->adapter; 2353 2354 int tx_complete; 2355 int work_done; 2356 2357 tx_complete = netxen_process_cmd_ring(adapter); 2358 2359 work_done = netxen_process_rcv_ring(sds_ring, budget); 2360 2361 if (!tx_complete) 2362 work_done = budget; 2363 2364 if (work_done < budget) { 2365 napi_complete_done(&sds_ring->napi, work_done); 2366 if (test_bit(__NX_DEV_UP, &adapter->state)) 2367 netxen_nic_enable_int(sds_ring); 2368 } 2369 2370 return work_done; 2371 } 2372 2373 static int 2374 nx_incr_dev_ref_cnt(struct netxen_adapter *adapter) 2375 { 2376 int count; 2377 if (netxen_api_lock(adapter)) 2378 return -EIO; 2379 2380 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT); 2381 2382 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count); 2383 2384 netxen_api_unlock(adapter); 2385 return count; 2386 } 2387 2388 static int 2389 nx_decr_dev_ref_cnt(struct netxen_adapter *adapter) 2390 { 2391 int count, state; 2392 if (netxen_api_lock(adapter)) 2393 return -EIO; 2394 2395 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT); 2396 WARN_ON(count == 0); 2397 2398 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count); 2399 state = NXRD32(adapter, NX_CRB_DEV_STATE); 2400 2401 if (count == 0 && state != NX_DEV_FAILED) 2402 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD); 2403 2404 netxen_api_unlock(adapter); 2405 return count; 2406 } 2407 2408 static int 2409 nx_dev_request_aer(struct netxen_adapter *adapter) 2410 { 2411 u32 state; 2412 int ret = -EINVAL; 2413 2414 if (netxen_api_lock(adapter)) 2415 return ret; 2416 2417 state = NXRD32(adapter, NX_CRB_DEV_STATE); 2418 2419 if (state == NX_DEV_NEED_AER) 2420 ret = 0; 2421 else if (state == NX_DEV_READY) { 2422 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_AER); 2423 ret = 0; 2424 } 2425 2426 netxen_api_unlock(adapter); 2427 return ret; 2428 } 2429 2430 int 2431 nx_dev_request_reset(struct netxen_adapter *adapter) 2432 { 2433 u32 state; 2434 int ret = -EINVAL; 2435 2436 if (netxen_api_lock(adapter)) 2437 return ret; 2438 2439 state = NXRD32(adapter, NX_CRB_DEV_STATE); 2440 2441 if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED) 2442 ret = 0; 2443 else if (state != NX_DEV_INITALIZING && state != NX_DEV_NEED_AER) { 2444 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET); 2445 adapter->flags |= NETXEN_FW_RESET_OWNER; 2446 ret = 0; 2447 } 2448 2449 netxen_api_unlock(adapter); 2450 2451 return ret; 2452 } 2453 2454 static int 2455 netxen_can_start_firmware(struct netxen_adapter *adapter) 2456 { 2457 int count; 2458 int can_start = 0; 2459 2460 if (netxen_api_lock(adapter)) { 2461 nx_incr_dev_ref_cnt(adapter); 2462 return -1; 2463 } 2464 2465 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT); 2466 2467 if ((count < 0) || (count >= NX_MAX_PCI_FUNC)) 2468 count = 0; 2469 2470 if (count == 0) { 2471 can_start = 1; 2472 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING); 2473 } 2474 2475 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count); 2476 2477 netxen_api_unlock(adapter); 2478 2479 return can_start; 2480 } 2481 2482 static void 2483 netxen_schedule_work(struct netxen_adapter *adapter, 2484 work_func_t func, int delay) 2485 { 2486 INIT_DELAYED_WORK(&adapter->fw_work, func); 2487 schedule_delayed_work(&adapter->fw_work, delay); 2488 } 2489 2490 static void 2491 netxen_cancel_fw_work(struct netxen_adapter *adapter) 2492 { 2493 while (test_and_set_bit(__NX_RESETTING, &adapter->state)) 2494 msleep(10); 2495 2496 cancel_delayed_work_sync(&adapter->fw_work); 2497 } 2498 2499 static void 2500 netxen_attach_work(struct work_struct *work) 2501 { 2502 struct netxen_adapter *adapter = container_of(work, 2503 struct netxen_adapter, fw_work.work); 2504 struct net_device *netdev = adapter->netdev; 2505 int err = 0; 2506 2507 if (netif_running(netdev)) { 2508 err = netxen_nic_attach(adapter); 2509 if (err) 2510 goto done; 2511 2512 err = netxen_nic_up(adapter, netdev); 2513 if (err) { 2514 netxen_nic_detach(adapter); 2515 goto done; 2516 } 2517 2518 netxen_restore_indev_addr(netdev, NETDEV_UP); 2519 } 2520 2521 netif_device_attach(netdev); 2522 2523 done: 2524 adapter->fw_fail_cnt = 0; 2525 clear_bit(__NX_RESETTING, &adapter->state); 2526 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY); 2527 } 2528 2529 static void 2530 netxen_fwinit_work(struct work_struct *work) 2531 { 2532 struct netxen_adapter *adapter = container_of(work, 2533 struct netxen_adapter, fw_work.work); 2534 int dev_state; 2535 int count; 2536 dev_state = NXRD32(adapter, NX_CRB_DEV_STATE); 2537 if (adapter->flags & NETXEN_FW_RESET_OWNER) { 2538 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT); 2539 WARN_ON(count == 0); 2540 if (count == 1) { 2541 if (adapter->mdump.md_enabled) { 2542 rtnl_lock(); 2543 netxen_dump_fw(adapter); 2544 rtnl_unlock(); 2545 } 2546 adapter->flags &= ~NETXEN_FW_RESET_OWNER; 2547 if (netxen_api_lock(adapter)) { 2548 clear_bit(__NX_RESETTING, &adapter->state); 2549 NXWR32(adapter, NX_CRB_DEV_STATE, 2550 NX_DEV_FAILED); 2551 return; 2552 } 2553 count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT); 2554 NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count); 2555 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD); 2556 dev_state = NX_DEV_COLD; 2557 netxen_api_unlock(adapter); 2558 } 2559 } 2560 2561 switch (dev_state) { 2562 case NX_DEV_COLD: 2563 case NX_DEV_READY: 2564 if (!netxen_start_firmware(adapter)) { 2565 netxen_schedule_work(adapter, netxen_attach_work, 0); 2566 return; 2567 } 2568 break; 2569 2570 case NX_DEV_NEED_RESET: 2571 case NX_DEV_INITALIZING: 2572 netxen_schedule_work(adapter, 2573 netxen_fwinit_work, 2 * FW_POLL_DELAY); 2574 return; 2575 2576 case NX_DEV_FAILED: 2577 default: 2578 nx_incr_dev_ref_cnt(adapter); 2579 break; 2580 } 2581 2582 if (netxen_api_lock(adapter)) { 2583 clear_bit(__NX_RESETTING, &adapter->state); 2584 return; 2585 } 2586 NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_FAILED); 2587 netxen_api_unlock(adapter); 2588 dev_err(&adapter->pdev->dev, "%s: Device initialization Failed\n", 2589 adapter->netdev->name); 2590 2591 clear_bit(__NX_RESETTING, &adapter->state); 2592 } 2593 2594 static void 2595 netxen_detach_work(struct work_struct *work) 2596 { 2597 struct netxen_adapter *adapter = container_of(work, 2598 struct netxen_adapter, fw_work.work); 2599 struct net_device *netdev = adapter->netdev; 2600 int ref_cnt = 0, delay; 2601 u32 status; 2602 2603 netif_device_detach(netdev); 2604 2605 netxen_nic_down(adapter, netdev); 2606 2607 rtnl_lock(); 2608 netxen_nic_detach(adapter); 2609 rtnl_unlock(); 2610 2611 status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1); 2612 2613 if (status & NX_RCODE_FATAL_ERROR) 2614 goto err_ret; 2615 2616 if (adapter->temp == NX_TEMP_PANIC) 2617 goto err_ret; 2618 2619 if (!(adapter->flags & NETXEN_FW_RESET_OWNER)) 2620 ref_cnt = nx_decr_dev_ref_cnt(adapter); 2621 2622 if (ref_cnt == -EIO) 2623 goto err_ret; 2624 2625 delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY); 2626 2627 adapter->fw_wait_cnt = 0; 2628 netxen_schedule_work(adapter, netxen_fwinit_work, delay); 2629 2630 return; 2631 2632 err_ret: 2633 clear_bit(__NX_RESETTING, &adapter->state); 2634 } 2635 2636 static int 2637 netxen_check_health(struct netxen_adapter *adapter) 2638 { 2639 u32 state, heartbit; 2640 u32 peg_status; 2641 struct net_device *netdev = adapter->netdev; 2642 2643 state = NXRD32(adapter, NX_CRB_DEV_STATE); 2644 if (state == NX_DEV_NEED_AER) 2645 return 0; 2646 2647 if (netxen_nic_check_temp(adapter)) 2648 goto detach; 2649 2650 if (adapter->need_fw_reset) { 2651 if (nx_dev_request_reset(adapter)) 2652 return 0; 2653 goto detach; 2654 } 2655 2656 /* NX_DEV_NEED_RESET, this state can be marked in two cases 2657 * 1. Tx timeout 2. Fw hang 2658 * Send request to destroy context in case of tx timeout only 2659 * and doesn't required in case of Fw hang 2660 */ 2661 if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED) { 2662 adapter->need_fw_reset = 1; 2663 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) 2664 goto detach; 2665 } 2666 2667 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) 2668 return 0; 2669 2670 heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER); 2671 if (heartbit != adapter->heartbit) { 2672 adapter->heartbit = heartbit; 2673 adapter->fw_fail_cnt = 0; 2674 if (adapter->need_fw_reset) 2675 goto detach; 2676 return 0; 2677 } 2678 2679 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH) 2680 return 0; 2681 2682 if (nx_dev_request_reset(adapter)) 2683 return 0; 2684 2685 clear_bit(__NX_FW_ATTACHED, &adapter->state); 2686 2687 dev_err(&netdev->dev, "firmware hang detected\n"); 2688 peg_status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1); 2689 dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n" 2690 "PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n" 2691 "PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n" 2692 "PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n" 2693 "PEG_NET_4_PC: 0x%x\n", 2694 peg_status, 2695 NXRD32(adapter, NETXEN_PEG_HALT_STATUS2), 2696 NXRD32(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c), 2697 NXRD32(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c), 2698 NXRD32(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c), 2699 NXRD32(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c), 2700 NXRD32(adapter, NETXEN_CRB_PEG_NET_4 + 0x3c)); 2701 if (NX_FWERROR_PEGSTAT1(peg_status) == 0x67) 2702 dev_err(&adapter->pdev->dev, 2703 "Firmware aborted with error code 0x00006700. " 2704 "Device is being reset.\n"); 2705 detach: 2706 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) && 2707 !test_and_set_bit(__NX_RESETTING, &adapter->state)) 2708 netxen_schedule_work(adapter, netxen_detach_work, 0); 2709 return 1; 2710 } 2711 2712 static void 2713 netxen_fw_poll_work(struct work_struct *work) 2714 { 2715 struct netxen_adapter *adapter = container_of(work, 2716 struct netxen_adapter, fw_work.work); 2717 2718 if (test_bit(__NX_RESETTING, &adapter->state)) 2719 goto reschedule; 2720 2721 if (test_bit(__NX_DEV_UP, &adapter->state) && 2722 !(adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)) { 2723 if (!adapter->has_link_events) { 2724 2725 netxen_nic_handle_phy_intr(adapter); 2726 2727 if (adapter->link_changed) 2728 netxen_nic_set_link_parameters(adapter); 2729 } 2730 } 2731 2732 if (netxen_check_health(adapter)) 2733 return; 2734 2735 reschedule: 2736 netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY); 2737 } 2738 2739 static ssize_t 2740 netxen_store_bridged_mode(struct device *dev, 2741 struct device_attribute *attr, const char *buf, size_t len) 2742 { 2743 struct net_device *net = to_net_dev(dev); 2744 struct netxen_adapter *adapter = netdev_priv(net); 2745 unsigned long new; 2746 int ret = -EINVAL; 2747 2748 if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG)) 2749 goto err_out; 2750 2751 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) 2752 goto err_out; 2753 2754 if (kstrtoul(buf, 2, &new)) 2755 goto err_out; 2756 2757 if (!netxen_config_bridged_mode(adapter, !!new)) 2758 ret = len; 2759 2760 err_out: 2761 return ret; 2762 } 2763 2764 static ssize_t 2765 netxen_show_bridged_mode(struct device *dev, 2766 struct device_attribute *attr, char *buf) 2767 { 2768 struct net_device *net = to_net_dev(dev); 2769 struct netxen_adapter *adapter; 2770 int bridged_mode = 0; 2771 2772 adapter = netdev_priv(net); 2773 2774 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) 2775 bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED); 2776 2777 return sprintf(buf, "%d\n", bridged_mode); 2778 } 2779 2780 static const struct device_attribute dev_attr_bridged_mode = { 2781 .attr = { .name = "bridged_mode", .mode = 0644 }, 2782 .show = netxen_show_bridged_mode, 2783 .store = netxen_store_bridged_mode, 2784 }; 2785 2786 static ssize_t 2787 netxen_store_diag_mode(struct device *dev, 2788 struct device_attribute *attr, const char *buf, size_t len) 2789 { 2790 struct netxen_adapter *adapter = dev_get_drvdata(dev); 2791 unsigned long new; 2792 2793 if (kstrtoul(buf, 2, &new)) 2794 return -EINVAL; 2795 2796 if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED)) 2797 adapter->flags ^= NETXEN_NIC_DIAG_ENABLED; 2798 2799 return len; 2800 } 2801 2802 static ssize_t 2803 netxen_show_diag_mode(struct device *dev, 2804 struct device_attribute *attr, char *buf) 2805 { 2806 struct netxen_adapter *adapter = dev_get_drvdata(dev); 2807 2808 return sprintf(buf, "%d\n", 2809 !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED)); 2810 } 2811 2812 static const struct device_attribute dev_attr_diag_mode = { 2813 .attr = { .name = "diag_mode", .mode = 0644 }, 2814 .show = netxen_show_diag_mode, 2815 .store = netxen_store_diag_mode, 2816 }; 2817 2818 static int 2819 netxen_sysfs_validate_crb(struct netxen_adapter *adapter, 2820 loff_t offset, size_t size) 2821 { 2822 size_t crb_size = 4; 2823 2824 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED)) 2825 return -EIO; 2826 2827 if (offset < NETXEN_PCI_CRBSPACE) { 2828 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) 2829 return -EINVAL; 2830 2831 if (ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM, 2832 NETXEN_PCI_CAMQM_2M_END)) 2833 crb_size = 8; 2834 else 2835 return -EINVAL; 2836 } 2837 2838 if ((size != crb_size) || (offset & (crb_size-1))) 2839 return -EINVAL; 2840 2841 return 0; 2842 } 2843 2844 static ssize_t 2845 netxen_sysfs_read_crb(struct file *filp, struct kobject *kobj, 2846 struct bin_attribute *attr, 2847 char *buf, loff_t offset, size_t size) 2848 { 2849 struct device *dev = kobj_to_dev(kobj); 2850 struct netxen_adapter *adapter = dev_get_drvdata(dev); 2851 u32 data; 2852 u64 qmdata; 2853 int ret; 2854 2855 ret = netxen_sysfs_validate_crb(adapter, offset, size); 2856 if (ret != 0) 2857 return ret; 2858 2859 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && 2860 ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM, 2861 NETXEN_PCI_CAMQM_2M_END)) { 2862 netxen_pci_camqm_read_2M(adapter, offset, &qmdata); 2863 memcpy(buf, &qmdata, size); 2864 } else { 2865 data = NXRD32(adapter, offset); 2866 memcpy(buf, &data, size); 2867 } 2868 2869 return size; 2870 } 2871 2872 static ssize_t 2873 netxen_sysfs_write_crb(struct file *filp, struct kobject *kobj, 2874 struct bin_attribute *attr, 2875 char *buf, loff_t offset, size_t size) 2876 { 2877 struct device *dev = kobj_to_dev(kobj); 2878 struct netxen_adapter *adapter = dev_get_drvdata(dev); 2879 u32 data; 2880 u64 qmdata; 2881 int ret; 2882 2883 ret = netxen_sysfs_validate_crb(adapter, offset, size); 2884 if (ret != 0) 2885 return ret; 2886 2887 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && 2888 ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM, 2889 NETXEN_PCI_CAMQM_2M_END)) { 2890 memcpy(&qmdata, buf, size); 2891 netxen_pci_camqm_write_2M(adapter, offset, qmdata); 2892 } else { 2893 memcpy(&data, buf, size); 2894 NXWR32(adapter, offset, data); 2895 } 2896 2897 return size; 2898 } 2899 2900 static int 2901 netxen_sysfs_validate_mem(struct netxen_adapter *adapter, 2902 loff_t offset, size_t size) 2903 { 2904 if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED)) 2905 return -EIO; 2906 2907 if ((size != 8) || (offset & 0x7)) 2908 return -EIO; 2909 2910 return 0; 2911 } 2912 2913 static ssize_t 2914 netxen_sysfs_read_mem(struct file *filp, struct kobject *kobj, 2915 struct bin_attribute *attr, 2916 char *buf, loff_t offset, size_t size) 2917 { 2918 struct device *dev = kobj_to_dev(kobj); 2919 struct netxen_adapter *adapter = dev_get_drvdata(dev); 2920 u64 data; 2921 int ret; 2922 2923 ret = netxen_sysfs_validate_mem(adapter, offset, size); 2924 if (ret != 0) 2925 return ret; 2926 2927 if (adapter->pci_mem_read(adapter, offset, &data)) 2928 return -EIO; 2929 2930 memcpy(buf, &data, size); 2931 2932 return size; 2933 } 2934 2935 static ssize_t netxen_sysfs_write_mem(struct file *filp, struct kobject *kobj, 2936 struct bin_attribute *attr, char *buf, 2937 loff_t offset, size_t size) 2938 { 2939 struct device *dev = kobj_to_dev(kobj); 2940 struct netxen_adapter *adapter = dev_get_drvdata(dev); 2941 u64 data; 2942 int ret; 2943 2944 ret = netxen_sysfs_validate_mem(adapter, offset, size); 2945 if (ret != 0) 2946 return ret; 2947 2948 memcpy(&data, buf, size); 2949 2950 if (adapter->pci_mem_write(adapter, offset, data)) 2951 return -EIO; 2952 2953 return size; 2954 } 2955 2956 2957 static const struct bin_attribute bin_attr_crb = { 2958 .attr = { .name = "crb", .mode = 0644 }, 2959 .size = 0, 2960 .read = netxen_sysfs_read_crb, 2961 .write = netxen_sysfs_write_crb, 2962 }; 2963 2964 static const struct bin_attribute bin_attr_mem = { 2965 .attr = { .name = "mem", .mode = 0644 }, 2966 .size = 0, 2967 .read = netxen_sysfs_read_mem, 2968 .write = netxen_sysfs_write_mem, 2969 }; 2970 2971 static ssize_t 2972 netxen_sysfs_read_dimm(struct file *filp, struct kobject *kobj, 2973 struct bin_attribute *attr, 2974 char *buf, loff_t offset, size_t size) 2975 { 2976 struct device *dev = kobj_to_dev(kobj); 2977 struct netxen_adapter *adapter = dev_get_drvdata(dev); 2978 struct net_device *netdev = adapter->netdev; 2979 struct netxen_dimm_cfg dimm; 2980 u8 dw, rows, cols, banks, ranks; 2981 u32 val; 2982 2983 if (size < attr->size) { 2984 netdev_err(netdev, "Invalid size\n"); 2985 return -EINVAL; 2986 } 2987 2988 memset(&dimm, 0, sizeof(struct netxen_dimm_cfg)); 2989 val = NXRD32(adapter, NETXEN_DIMM_CAPABILITY); 2990 2991 /* Checks if DIMM info is valid. */ 2992 if (val & NETXEN_DIMM_VALID_FLAG) { 2993 netdev_err(netdev, "Invalid DIMM flag\n"); 2994 dimm.presence = 0xff; 2995 goto out; 2996 } 2997 2998 rows = NETXEN_DIMM_NUMROWS(val); 2999 cols = NETXEN_DIMM_NUMCOLS(val); 3000 ranks = NETXEN_DIMM_NUMRANKS(val); 3001 banks = NETXEN_DIMM_NUMBANKS(val); 3002 dw = NETXEN_DIMM_DATAWIDTH(val); 3003 3004 dimm.presence = (val & NETXEN_DIMM_PRESENT); 3005 3006 /* Checks if DIMM info is present. */ 3007 if (!dimm.presence) { 3008 netdev_err(netdev, "DIMM not present\n"); 3009 goto out; 3010 } 3011 3012 dimm.dimm_type = NETXEN_DIMM_TYPE(val); 3013 3014 switch (dimm.dimm_type) { 3015 case NETXEN_DIMM_TYPE_RDIMM: 3016 case NETXEN_DIMM_TYPE_UDIMM: 3017 case NETXEN_DIMM_TYPE_SO_DIMM: 3018 case NETXEN_DIMM_TYPE_Micro_DIMM: 3019 case NETXEN_DIMM_TYPE_Mini_RDIMM: 3020 case NETXEN_DIMM_TYPE_Mini_UDIMM: 3021 break; 3022 default: 3023 netdev_err(netdev, "Invalid DIMM type %x\n", dimm.dimm_type); 3024 goto out; 3025 } 3026 3027 if (val & NETXEN_DIMM_MEMTYPE_DDR2_SDRAM) 3028 dimm.mem_type = NETXEN_DIMM_MEM_DDR2_SDRAM; 3029 else 3030 dimm.mem_type = NETXEN_DIMM_MEMTYPE(val); 3031 3032 if (val & NETXEN_DIMM_SIZE) { 3033 dimm.size = NETXEN_DIMM_STD_MEM_SIZE; 3034 goto out; 3035 } 3036 3037 if (!rows) { 3038 netdev_err(netdev, "Invalid no of rows %x\n", rows); 3039 goto out; 3040 } 3041 3042 if (!cols) { 3043 netdev_err(netdev, "Invalid no of columns %x\n", cols); 3044 goto out; 3045 } 3046 3047 if (!banks) { 3048 netdev_err(netdev, "Invalid no of banks %x\n", banks); 3049 goto out; 3050 } 3051 3052 ranks += 1; 3053 3054 switch (dw) { 3055 case 0x0: 3056 dw = 32; 3057 break; 3058 case 0x1: 3059 dw = 33; 3060 break; 3061 case 0x2: 3062 dw = 36; 3063 break; 3064 case 0x3: 3065 dw = 64; 3066 break; 3067 case 0x4: 3068 dw = 72; 3069 break; 3070 case 0x5: 3071 dw = 80; 3072 break; 3073 case 0x6: 3074 dw = 128; 3075 break; 3076 case 0x7: 3077 dw = 144; 3078 break; 3079 default: 3080 netdev_err(netdev, "Invalid data-width %x\n", dw); 3081 goto out; 3082 } 3083 3084 dimm.size = ((1 << rows) * (1 << cols) * dw * banks * ranks) / 8; 3085 /* Size returned in MB. */ 3086 dimm.size = (dimm.size) / 0x100000; 3087 out: 3088 memcpy(buf, &dimm, sizeof(struct netxen_dimm_cfg)); 3089 return sizeof(struct netxen_dimm_cfg); 3090 3091 } 3092 3093 static const struct bin_attribute bin_attr_dimm = { 3094 .attr = { .name = "dimm", .mode = 0644 }, 3095 .size = sizeof(struct netxen_dimm_cfg), 3096 .read = netxen_sysfs_read_dimm, 3097 }; 3098 3099 3100 static void 3101 netxen_create_sysfs_entries(struct netxen_adapter *adapter) 3102 { 3103 struct device *dev = &adapter->pdev->dev; 3104 3105 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) { 3106 /* bridged_mode control */ 3107 if (device_create_file(dev, &dev_attr_bridged_mode)) { 3108 dev_warn(dev, 3109 "failed to create bridged_mode sysfs entry\n"); 3110 } 3111 } 3112 } 3113 3114 static void 3115 netxen_remove_sysfs_entries(struct netxen_adapter *adapter) 3116 { 3117 struct device *dev = &adapter->pdev->dev; 3118 3119 if (adapter->capabilities & NX_FW_CAPABILITY_BDG) 3120 device_remove_file(dev, &dev_attr_bridged_mode); 3121 } 3122 3123 static void 3124 netxen_create_diag_entries(struct netxen_adapter *adapter) 3125 { 3126 struct pci_dev *pdev = adapter->pdev; 3127 struct device *dev; 3128 3129 dev = &pdev->dev; 3130 if (device_create_file(dev, &dev_attr_diag_mode)) 3131 dev_info(dev, "failed to create diag_mode sysfs entry\n"); 3132 if (device_create_bin_file(dev, &bin_attr_crb)) 3133 dev_info(dev, "failed to create crb sysfs entry\n"); 3134 if (device_create_bin_file(dev, &bin_attr_mem)) 3135 dev_info(dev, "failed to create mem sysfs entry\n"); 3136 if (device_create_bin_file(dev, &bin_attr_dimm)) 3137 dev_info(dev, "failed to create dimm sysfs entry\n"); 3138 } 3139 3140 3141 static void 3142 netxen_remove_diag_entries(struct netxen_adapter *adapter) 3143 { 3144 struct pci_dev *pdev = adapter->pdev; 3145 struct device *dev = &pdev->dev; 3146 3147 device_remove_file(dev, &dev_attr_diag_mode); 3148 device_remove_bin_file(dev, &bin_attr_crb); 3149 device_remove_bin_file(dev, &bin_attr_mem); 3150 device_remove_bin_file(dev, &bin_attr_dimm); 3151 } 3152 3153 #ifdef CONFIG_INET 3154 3155 #define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops) 3156 3157 static int 3158 netxen_destip_supported(struct netxen_adapter *adapter) 3159 { 3160 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) 3161 return 0; 3162 3163 if (adapter->ahw.cut_through) 3164 return 0; 3165 3166 return 1; 3167 } 3168 3169 static void 3170 netxen_free_ip_list(struct netxen_adapter *adapter, bool master) 3171 { 3172 struct nx_ip_list *cur, *tmp_cur; 3173 3174 list_for_each_entry_safe(cur, tmp_cur, &adapter->ip_list, list) { 3175 if (master) { 3176 if (cur->master) { 3177 netxen_config_ipaddr(adapter, cur->ip_addr, 3178 NX_IP_DOWN); 3179 list_del(&cur->list); 3180 kfree(cur); 3181 } 3182 } else { 3183 netxen_config_ipaddr(adapter, cur->ip_addr, NX_IP_DOWN); 3184 list_del(&cur->list); 3185 kfree(cur); 3186 } 3187 } 3188 } 3189 3190 static bool 3191 netxen_list_config_ip(struct netxen_adapter *adapter, 3192 struct in_ifaddr *ifa, unsigned long event) 3193 { 3194 struct net_device *dev; 3195 struct nx_ip_list *cur, *tmp_cur; 3196 struct list_head *head; 3197 bool ret = false; 3198 3199 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL; 3200 3201 if (dev == NULL) 3202 goto out; 3203 3204 switch (event) { 3205 case NX_IP_UP: 3206 list_for_each(head, &adapter->ip_list) { 3207 cur = list_entry(head, struct nx_ip_list, list); 3208 3209 if (cur->ip_addr == ifa->ifa_address) 3210 goto out; 3211 } 3212 3213 cur = kzalloc(sizeof(struct nx_ip_list), GFP_ATOMIC); 3214 if (cur == NULL) 3215 goto out; 3216 if (is_vlan_dev(dev)) 3217 dev = vlan_dev_real_dev(dev); 3218 cur->master = !!netif_is_bond_master(dev); 3219 cur->ip_addr = ifa->ifa_address; 3220 list_add_tail(&cur->list, &adapter->ip_list); 3221 netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP); 3222 ret = true; 3223 break; 3224 case NX_IP_DOWN: 3225 list_for_each_entry_safe(cur, tmp_cur, 3226 &adapter->ip_list, list) { 3227 if (cur->ip_addr == ifa->ifa_address) { 3228 list_del(&cur->list); 3229 kfree(cur); 3230 netxen_config_ipaddr(adapter, ifa->ifa_address, 3231 NX_IP_DOWN); 3232 ret = true; 3233 break; 3234 } 3235 } 3236 } 3237 out: 3238 return ret; 3239 } 3240 3241 static void 3242 netxen_config_indev_addr(struct netxen_adapter *adapter, 3243 struct net_device *dev, unsigned long event) 3244 { 3245 struct in_device *indev; 3246 struct in_ifaddr *ifa; 3247 3248 if (!netxen_destip_supported(adapter)) 3249 return; 3250 3251 indev = in_dev_get(dev); 3252 if (!indev) 3253 return; 3254 3255 rcu_read_lock(); 3256 in_dev_for_each_ifa_rcu(ifa, indev) { 3257 switch (event) { 3258 case NETDEV_UP: 3259 netxen_list_config_ip(adapter, ifa, NX_IP_UP); 3260 break; 3261 case NETDEV_DOWN: 3262 netxen_list_config_ip(adapter, ifa, NX_IP_DOWN); 3263 break; 3264 default: 3265 break; 3266 } 3267 } 3268 rcu_read_unlock(); 3269 in_dev_put(indev); 3270 } 3271 3272 static void 3273 netxen_restore_indev_addr(struct net_device *netdev, unsigned long event) 3274 3275 { 3276 struct netxen_adapter *adapter = netdev_priv(netdev); 3277 struct nx_ip_list *pos, *tmp_pos; 3278 unsigned long ip_event; 3279 3280 ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN; 3281 netxen_config_indev_addr(adapter, netdev, event); 3282 3283 list_for_each_entry_safe(pos, tmp_pos, &adapter->ip_list, list) { 3284 netxen_config_ipaddr(adapter, pos->ip_addr, ip_event); 3285 } 3286 } 3287 3288 static inline bool 3289 netxen_config_checkdev(struct net_device *dev) 3290 { 3291 struct netxen_adapter *adapter; 3292 3293 if (!is_netxen_netdev(dev)) 3294 return false; 3295 adapter = netdev_priv(dev); 3296 if (!adapter) 3297 return false; 3298 if (!netxen_destip_supported(adapter)) 3299 return false; 3300 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) 3301 return false; 3302 3303 return true; 3304 } 3305 3306 /** 3307 * netxen_config_master - configure addresses based on master 3308 * @dev: netxen device 3309 * @event: netdev event 3310 */ 3311 static void netxen_config_master(struct net_device *dev, unsigned long event) 3312 { 3313 struct net_device *master, *slave; 3314 struct netxen_adapter *adapter = netdev_priv(dev); 3315 3316 rcu_read_lock(); 3317 master = netdev_master_upper_dev_get_rcu(dev); 3318 /* 3319 * This is the case where the netxen nic is being 3320 * enslaved and is dev_open()ed in bond_enslave() 3321 * Now we should program the bond's (and its vlans') 3322 * addresses in the netxen NIC. 3323 */ 3324 if (master && netif_is_bond_master(master) && 3325 !netif_is_bond_slave(dev)) { 3326 netxen_config_indev_addr(adapter, master, event); 3327 for_each_netdev_rcu(&init_net, slave) 3328 if (is_vlan_dev(slave) && 3329 vlan_dev_real_dev(slave) == master) 3330 netxen_config_indev_addr(adapter, slave, event); 3331 } 3332 rcu_read_unlock(); 3333 /* 3334 * This is the case where the netxen nic is being 3335 * released and is dev_close()ed in bond_release() 3336 * just before IFF_BONDING is stripped. 3337 */ 3338 if (!master && dev->priv_flags & IFF_BONDING) 3339 netxen_free_ip_list(adapter, true); 3340 } 3341 3342 static int netxen_netdev_event(struct notifier_block *this, 3343 unsigned long event, void *ptr) 3344 { 3345 struct netxen_adapter *adapter; 3346 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3347 struct net_device *orig_dev = dev; 3348 struct net_device *slave; 3349 3350 recheck: 3351 if (dev == NULL) 3352 goto done; 3353 3354 if (is_vlan_dev(dev)) { 3355 dev = vlan_dev_real_dev(dev); 3356 goto recheck; 3357 } 3358 if (event == NETDEV_UP || event == NETDEV_DOWN) { 3359 /* If this is a bonding device, look for netxen-based slaves*/ 3360 if (netif_is_bond_master(dev)) { 3361 rcu_read_lock(); 3362 for_each_netdev_in_bond_rcu(dev, slave) { 3363 if (!netxen_config_checkdev(slave)) 3364 continue; 3365 adapter = netdev_priv(slave); 3366 netxen_config_indev_addr(adapter, 3367 orig_dev, event); 3368 } 3369 rcu_read_unlock(); 3370 } else { 3371 if (!netxen_config_checkdev(dev)) 3372 goto done; 3373 adapter = netdev_priv(dev); 3374 /* Act only if the actual netxen is the target */ 3375 if (orig_dev == dev) 3376 netxen_config_master(dev, event); 3377 netxen_config_indev_addr(adapter, orig_dev, event); 3378 } 3379 } 3380 done: 3381 return NOTIFY_DONE; 3382 } 3383 3384 static int 3385 netxen_inetaddr_event(struct notifier_block *this, 3386 unsigned long event, void *ptr) 3387 { 3388 struct netxen_adapter *adapter; 3389 struct net_device *dev, *slave; 3390 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; 3391 unsigned long ip_event; 3392 3393 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL; 3394 ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN; 3395 recheck: 3396 if (dev == NULL) 3397 goto done; 3398 3399 if (is_vlan_dev(dev)) { 3400 dev = vlan_dev_real_dev(dev); 3401 goto recheck; 3402 } 3403 if (event == NETDEV_UP || event == NETDEV_DOWN) { 3404 /* If this is a bonding device, look for netxen-based slaves*/ 3405 if (netif_is_bond_master(dev)) { 3406 rcu_read_lock(); 3407 for_each_netdev_in_bond_rcu(dev, slave) { 3408 if (!netxen_config_checkdev(slave)) 3409 continue; 3410 adapter = netdev_priv(slave); 3411 netxen_list_config_ip(adapter, ifa, ip_event); 3412 } 3413 rcu_read_unlock(); 3414 } else { 3415 if (!netxen_config_checkdev(dev)) 3416 goto done; 3417 adapter = netdev_priv(dev); 3418 netxen_list_config_ip(adapter, ifa, ip_event); 3419 } 3420 } 3421 done: 3422 return NOTIFY_DONE; 3423 } 3424 3425 static struct notifier_block netxen_netdev_cb = { 3426 .notifier_call = netxen_netdev_event, 3427 }; 3428 3429 static struct notifier_block netxen_inetaddr_cb = { 3430 .notifier_call = netxen_inetaddr_event, 3431 }; 3432 #else 3433 static void 3434 netxen_restore_indev_addr(struct net_device *dev, unsigned long event) 3435 { } 3436 static void 3437 netxen_free_ip_list(struct netxen_adapter *adapter, bool master) 3438 { } 3439 #endif 3440 3441 static const struct pci_error_handlers netxen_err_handler = { 3442 .error_detected = netxen_io_error_detected, 3443 .slot_reset = netxen_io_slot_reset, 3444 }; 3445 3446 static SIMPLE_DEV_PM_OPS(netxen_nic_pm_ops, 3447 netxen_nic_suspend, 3448 netxen_nic_resume); 3449 3450 static struct pci_driver netxen_driver = { 3451 .name = netxen_nic_driver_name, 3452 .id_table = netxen_pci_tbl, 3453 .probe = netxen_nic_probe, 3454 .remove = netxen_nic_remove, 3455 .driver.pm = &netxen_nic_pm_ops, 3456 .shutdown = netxen_nic_shutdown, 3457 .err_handler = &netxen_err_handler 3458 }; 3459 3460 static int __init netxen_init_module(void) 3461 { 3462 printk(KERN_INFO "%s\n", netxen_nic_driver_string); 3463 3464 #ifdef CONFIG_INET 3465 register_netdevice_notifier(&netxen_netdev_cb); 3466 register_inetaddr_notifier(&netxen_inetaddr_cb); 3467 #endif 3468 return pci_register_driver(&netxen_driver); 3469 } 3470 3471 module_init(netxen_init_module); 3472 3473 static void __exit netxen_exit_module(void) 3474 { 3475 pci_unregister_driver(&netxen_driver); 3476 3477 #ifdef CONFIG_INET 3478 unregister_inetaddr_notifier(&netxen_inetaddr_cb); 3479 unregister_netdevice_notifier(&netxen_netdev_cb); 3480 #endif 3481 } 3482 3483 module_exit(netxen_exit_module); 3484