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