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