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