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