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