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