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