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