Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 - QLogic Corporation. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 18 | * MA 02111-1307, USA. |
| 19 | * |
| 20 | * The full GNU General Public License is included in this distribution |
| 21 | * in the file called "COPYING". |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/vmalloc.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | |
| 28 | #include "qlcnic.h" |
| 29 | |
| 30 | #include <linux/dma-mapping.h> |
| 31 | #include <linux/if_vlan.h> |
| 32 | #include <net/ip.h> |
| 33 | #include <linux/ipv6.h> |
| 34 | #include <linux/inetdevice.h> |
| 35 | #include <linux/sysfs.h> |
| 36 | |
| 37 | MODULE_DESCRIPTION("QLogic 10 GbE Converged Ethernet Driver"); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | MODULE_VERSION(QLCNIC_LINUX_VERSIONID); |
| 40 | MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME); |
| 41 | |
| 42 | char qlcnic_driver_name[] = "qlcnic"; |
| 43 | static const char qlcnic_driver_string[] = "QLogic Converged Ethernet Driver v" |
| 44 | QLCNIC_LINUX_VERSIONID; |
| 45 | |
| 46 | static int port_mode = QLCNIC_PORT_MODE_AUTO_NEG; |
| 47 | |
| 48 | /* Default to restricted 1G auto-neg mode */ |
| 49 | static int wol_port_mode = 5; |
| 50 | |
| 51 | static int use_msi = 1; |
| 52 | module_param(use_msi, int, 0644); |
| 53 | MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled"); |
| 54 | |
| 55 | static int use_msi_x = 1; |
| 56 | module_param(use_msi_x, int, 0644); |
| 57 | MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled"); |
| 58 | |
| 59 | static int auto_fw_reset = AUTO_FW_RESET_ENABLED; |
| 60 | module_param(auto_fw_reset, int, 0644); |
| 61 | MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled"); |
| 62 | |
| 63 | static int __devinit qlcnic_probe(struct pci_dev *pdev, |
| 64 | const struct pci_device_id *ent); |
| 65 | static void __devexit qlcnic_remove(struct pci_dev *pdev); |
| 66 | static int qlcnic_open(struct net_device *netdev); |
| 67 | static int qlcnic_close(struct net_device *netdev); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 68 | static void qlcnic_tx_timeout(struct net_device *netdev); |
| 69 | static void qlcnic_tx_timeout_task(struct work_struct *work); |
| 70 | static void qlcnic_attach_work(struct work_struct *work); |
| 71 | static void qlcnic_fwinit_work(struct work_struct *work); |
| 72 | static void qlcnic_fw_poll_work(struct work_struct *work); |
| 73 | static void qlcnic_schedule_work(struct qlcnic_adapter *adapter, |
| 74 | work_func_t func, int delay); |
| 75 | static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter); |
| 76 | static int qlcnic_poll(struct napi_struct *napi, int budget); |
| 77 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 78 | static void qlcnic_poll_controller(struct net_device *netdev); |
| 79 | #endif |
| 80 | |
| 81 | static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter); |
| 82 | static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter); |
| 83 | static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter); |
| 84 | static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter); |
| 85 | |
| 86 | static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter); |
| 87 | static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter); |
| 88 | |
Amit Kumar Salecha | 7eb9855 | 2010-02-01 05:24:59 +0000 | [diff] [blame] | 89 | static irqreturn_t qlcnic_tmp_intr(int irq, void *data); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 90 | static irqreturn_t qlcnic_intr(int irq, void *data); |
| 91 | static irqreturn_t qlcnic_msi_intr(int irq, void *data); |
| 92 | static irqreturn_t qlcnic_msix_intr(int irq, void *data); |
| 93 | |
| 94 | static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev); |
| 95 | static void qlcnic_config_indev_addr(struct net_device *dev, unsigned long); |
| 96 | |
| 97 | /* PCI Device ID Table */ |
| 98 | #define ENTRY(device) \ |
| 99 | {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \ |
| 100 | .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0} |
| 101 | |
| 102 | #define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020 |
| 103 | |
Amit Kumar Salecha | 6a90288 | 2010-02-01 05:24:54 +0000 | [diff] [blame] | 104 | static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = { |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 105 | ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X), |
| 106 | {0,} |
| 107 | }; |
| 108 | |
| 109 | MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl); |
| 110 | |
| 111 | |
| 112 | void |
| 113 | qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter, |
| 114 | struct qlcnic_host_tx_ring *tx_ring) |
| 115 | { |
| 116 | writel(tx_ring->producer, tx_ring->crb_cmd_producer); |
| 117 | |
| 118 | if (qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH) { |
| 119 | netif_stop_queue(adapter->netdev); |
| 120 | smp_mb(); |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 121 | adapter->stats.xmit_off++; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | static const u32 msi_tgt_status[8] = { |
| 126 | ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1, |
| 127 | ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3, |
| 128 | ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5, |
| 129 | ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7 |
| 130 | }; |
| 131 | |
| 132 | static const |
| 133 | struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG; |
| 134 | |
| 135 | static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring) |
| 136 | { |
| 137 | writel(0, sds_ring->crb_intr_mask); |
| 138 | } |
| 139 | |
| 140 | static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring) |
| 141 | { |
| 142 | struct qlcnic_adapter *adapter = sds_ring->adapter; |
| 143 | |
| 144 | writel(0x1, sds_ring->crb_intr_mask); |
| 145 | |
| 146 | if (!QLCNIC_IS_MSI_FAMILY(adapter)) |
| 147 | writel(0xfbff, adapter->tgt_mask_reg); |
| 148 | } |
| 149 | |
| 150 | static int |
| 151 | qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count) |
| 152 | { |
| 153 | int size = sizeof(struct qlcnic_host_sds_ring) * count; |
| 154 | |
| 155 | recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL); |
| 156 | |
| 157 | return (recv_ctx->sds_rings == NULL); |
| 158 | } |
| 159 | |
| 160 | static void |
| 161 | qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx) |
| 162 | { |
| 163 | if (recv_ctx->sds_rings != NULL) |
| 164 | kfree(recv_ctx->sds_rings); |
| 165 | |
| 166 | recv_ctx->sds_rings = NULL; |
| 167 | } |
| 168 | |
| 169 | static int |
| 170 | qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev) |
| 171 | { |
| 172 | int ring; |
| 173 | struct qlcnic_host_sds_ring *sds_ring; |
| 174 | struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx; |
| 175 | |
| 176 | if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings)) |
| 177 | return -ENOMEM; |
| 178 | |
| 179 | for (ring = 0; ring < adapter->max_sds_rings; ring++) { |
| 180 | sds_ring = &recv_ctx->sds_rings[ring]; |
| 181 | netif_napi_add(netdev, &sds_ring->napi, |
| 182 | qlcnic_poll, QLCNIC_NETDEV_WEIGHT); |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static void |
| 189 | qlcnic_napi_del(struct qlcnic_adapter *adapter) |
| 190 | { |
| 191 | int ring; |
| 192 | struct qlcnic_host_sds_ring *sds_ring; |
| 193 | struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx; |
| 194 | |
| 195 | for (ring = 0; ring < adapter->max_sds_rings; ring++) { |
| 196 | sds_ring = &recv_ctx->sds_rings[ring]; |
| 197 | netif_napi_del(&sds_ring->napi); |
| 198 | } |
| 199 | |
| 200 | qlcnic_free_sds_rings(&adapter->recv_ctx); |
| 201 | } |
| 202 | |
| 203 | static void |
| 204 | qlcnic_napi_enable(struct qlcnic_adapter *adapter) |
| 205 | { |
| 206 | int ring; |
| 207 | struct qlcnic_host_sds_ring *sds_ring; |
| 208 | struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx; |
| 209 | |
| 210 | for (ring = 0; ring < adapter->max_sds_rings; ring++) { |
| 211 | sds_ring = &recv_ctx->sds_rings[ring]; |
| 212 | napi_enable(&sds_ring->napi); |
| 213 | qlcnic_enable_int(sds_ring); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static void |
| 218 | qlcnic_napi_disable(struct qlcnic_adapter *adapter) |
| 219 | { |
| 220 | int ring; |
| 221 | struct qlcnic_host_sds_ring *sds_ring; |
| 222 | struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx; |
| 223 | |
| 224 | for (ring = 0; ring < adapter->max_sds_rings; ring++) { |
| 225 | sds_ring = &recv_ctx->sds_rings[ring]; |
| 226 | qlcnic_disable_int(sds_ring); |
| 227 | napi_synchronize(&sds_ring->napi); |
| 228 | napi_disable(&sds_ring->napi); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static void qlcnic_clear_stats(struct qlcnic_adapter *adapter) |
| 233 | { |
| 234 | memset(&adapter->stats, 0, sizeof(adapter->stats)); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | static int qlcnic_set_dma_mask(struct qlcnic_adapter *adapter) |
| 239 | { |
| 240 | struct pci_dev *pdev = adapter->pdev; |
| 241 | u64 mask, cmask; |
| 242 | |
| 243 | adapter->pci_using_dac = 0; |
| 244 | |
| 245 | mask = DMA_BIT_MASK(39); |
| 246 | cmask = mask; |
| 247 | |
| 248 | if (pci_set_dma_mask(pdev, mask) == 0 && |
| 249 | pci_set_consistent_dma_mask(pdev, cmask) == 0) { |
| 250 | adapter->pci_using_dac = 1; |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | return -EIO; |
| 255 | } |
| 256 | |
| 257 | /* Update addressable range if firmware supports it */ |
| 258 | static int |
| 259 | qlcnic_update_dma_mask(struct qlcnic_adapter *adapter) |
| 260 | { |
| 261 | int change, shift, err; |
| 262 | u64 mask, old_mask, old_cmask; |
| 263 | struct pci_dev *pdev = adapter->pdev; |
| 264 | |
| 265 | change = 0; |
| 266 | |
| 267 | shift = QLCRD32(adapter, CRB_DMA_SHIFT); |
| 268 | if (shift > 32) |
| 269 | return 0; |
| 270 | |
| 271 | if (shift > 9) |
| 272 | change = 1; |
| 273 | |
| 274 | if (change) { |
| 275 | old_mask = pdev->dma_mask; |
| 276 | old_cmask = pdev->dev.coherent_dma_mask; |
| 277 | |
| 278 | mask = DMA_BIT_MASK(32+shift); |
| 279 | |
| 280 | err = pci_set_dma_mask(pdev, mask); |
| 281 | if (err) |
| 282 | goto err_out; |
| 283 | |
| 284 | err = pci_set_consistent_dma_mask(pdev, mask); |
| 285 | if (err) |
| 286 | goto err_out; |
| 287 | dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift); |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | |
| 292 | err_out: |
| 293 | pci_set_dma_mask(pdev, old_mask); |
| 294 | pci_set_consistent_dma_mask(pdev, old_cmask); |
| 295 | return err; |
| 296 | } |
| 297 | |
| 298 | static void qlcnic_set_port_mode(struct qlcnic_adapter *adapter) |
| 299 | { |
| 300 | u32 val, data; |
| 301 | |
| 302 | val = adapter->ahw.board_type; |
| 303 | if ((val == QLCNIC_BRDTYPE_P3_HMEZ) || |
| 304 | (val == QLCNIC_BRDTYPE_P3_XG_LOM)) { |
| 305 | if (port_mode == QLCNIC_PORT_MODE_802_3_AP) { |
| 306 | data = QLCNIC_PORT_MODE_802_3_AP; |
| 307 | QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data); |
| 308 | } else if (port_mode == QLCNIC_PORT_MODE_XG) { |
| 309 | data = QLCNIC_PORT_MODE_XG; |
| 310 | QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data); |
| 311 | } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_1G) { |
| 312 | data = QLCNIC_PORT_MODE_AUTO_NEG_1G; |
| 313 | QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data); |
| 314 | } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_XG) { |
| 315 | data = QLCNIC_PORT_MODE_AUTO_NEG_XG; |
| 316 | QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data); |
| 317 | } else { |
| 318 | data = QLCNIC_PORT_MODE_AUTO_NEG; |
| 319 | QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data); |
| 320 | } |
| 321 | |
| 322 | if ((wol_port_mode != QLCNIC_PORT_MODE_802_3_AP) && |
| 323 | (wol_port_mode != QLCNIC_PORT_MODE_XG) && |
| 324 | (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_1G) && |
| 325 | (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_XG)) { |
| 326 | wol_port_mode = QLCNIC_PORT_MODE_AUTO_NEG; |
| 327 | } |
| 328 | QLCWR32(adapter, QLCNIC_WOL_PORT_MODE, wol_port_mode); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable) |
| 333 | { |
| 334 | u32 control; |
| 335 | int pos; |
| 336 | |
| 337 | pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX); |
| 338 | if (pos) { |
| 339 | pci_read_config_dword(pdev, pos, &control); |
| 340 | if (enable) |
| 341 | control |= PCI_MSIX_FLAGS_ENABLE; |
| 342 | else |
| 343 | control = 0; |
| 344 | pci_write_config_dword(pdev, pos, control); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count) |
| 349 | { |
| 350 | int i; |
| 351 | |
| 352 | for (i = 0; i < count; i++) |
| 353 | adapter->msix_entries[i].entry = i; |
| 354 | } |
| 355 | |
| 356 | static int |
| 357 | qlcnic_read_mac_addr(struct qlcnic_adapter *adapter) |
| 358 | { |
| 359 | int i; |
| 360 | unsigned char *p; |
| 361 | u64 mac_addr; |
| 362 | struct net_device *netdev = adapter->netdev; |
| 363 | struct pci_dev *pdev = adapter->pdev; |
| 364 | |
| 365 | if (qlcnic_get_mac_addr(adapter, &mac_addr) != 0) |
| 366 | return -EIO; |
| 367 | |
| 368 | p = (unsigned char *)&mac_addr; |
| 369 | for (i = 0; i < 6; i++) |
| 370 | netdev->dev_addr[i] = *(p + 5 - i); |
| 371 | |
| 372 | memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len); |
| 373 | memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len); |
| 374 | |
| 375 | /* set station address */ |
| 376 | |
| 377 | if (!is_valid_ether_addr(netdev->perm_addr)) |
| 378 | dev_warn(&pdev->dev, "Bad MAC address %pM.\n", |
| 379 | netdev->dev_addr); |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static int qlcnic_set_mac(struct net_device *netdev, void *p) |
| 385 | { |
| 386 | struct qlcnic_adapter *adapter = netdev_priv(netdev); |
| 387 | struct sockaddr *addr = p; |
| 388 | |
| 389 | if (!is_valid_ether_addr(addr->sa_data)) |
| 390 | return -EINVAL; |
| 391 | |
| 392 | if (netif_running(netdev)) { |
| 393 | netif_device_detach(netdev); |
| 394 | qlcnic_napi_disable(adapter); |
| 395 | } |
| 396 | |
| 397 | memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len); |
| 398 | memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); |
| 399 | qlcnic_set_multi(adapter->netdev); |
| 400 | |
| 401 | if (netif_running(netdev)) { |
| 402 | netif_device_attach(netdev); |
| 403 | qlcnic_napi_enable(adapter); |
| 404 | } |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static const struct net_device_ops qlcnic_netdev_ops = { |
| 409 | .ndo_open = qlcnic_open, |
| 410 | .ndo_stop = qlcnic_close, |
| 411 | .ndo_start_xmit = qlcnic_xmit_frame, |
| 412 | .ndo_get_stats = qlcnic_get_stats, |
| 413 | .ndo_validate_addr = eth_validate_addr, |
| 414 | .ndo_set_multicast_list = qlcnic_set_multi, |
| 415 | .ndo_set_mac_address = qlcnic_set_mac, |
| 416 | .ndo_change_mtu = qlcnic_change_mtu, |
| 417 | .ndo_tx_timeout = qlcnic_tx_timeout, |
| 418 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 419 | .ndo_poll_controller = qlcnic_poll_controller, |
| 420 | #endif |
| 421 | }; |
| 422 | |
| 423 | static void |
| 424 | qlcnic_setup_intr(struct qlcnic_adapter *adapter) |
| 425 | { |
| 426 | const struct qlcnic_legacy_intr_set *legacy_intrp; |
| 427 | struct pci_dev *pdev = adapter->pdev; |
| 428 | int err, num_msix; |
| 429 | |
| 430 | if (adapter->rss_supported) { |
| 431 | num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ? |
| 432 | MSIX_ENTRIES_PER_ADAPTER : 2; |
| 433 | } else |
| 434 | num_msix = 1; |
| 435 | |
| 436 | adapter->max_sds_rings = 1; |
| 437 | |
| 438 | adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED); |
| 439 | |
| 440 | legacy_intrp = &legacy_intr[adapter->ahw.pci_func]; |
| 441 | |
| 442 | adapter->int_vec_bit = legacy_intrp->int_vec_bit; |
| 443 | adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter, |
| 444 | legacy_intrp->tgt_status_reg); |
| 445 | adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter, |
| 446 | legacy_intrp->tgt_mask_reg); |
| 447 | adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR); |
| 448 | |
| 449 | adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter, |
| 450 | ISR_INT_STATE_REG); |
| 451 | |
| 452 | qlcnic_set_msix_bit(pdev, 0); |
| 453 | |
| 454 | if (adapter->msix_supported) { |
| 455 | |
| 456 | qlcnic_init_msix_entries(adapter, num_msix); |
| 457 | err = pci_enable_msix(pdev, adapter->msix_entries, num_msix); |
| 458 | if (err == 0) { |
| 459 | adapter->flags |= QLCNIC_MSIX_ENABLED; |
| 460 | qlcnic_set_msix_bit(pdev, 1); |
| 461 | |
| 462 | if (adapter->rss_supported) |
| 463 | adapter->max_sds_rings = num_msix; |
| 464 | |
| 465 | dev_info(&pdev->dev, "using msi-x interrupts\n"); |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | if (err > 0) |
| 470 | pci_disable_msix(pdev); |
| 471 | |
| 472 | /* fall through for msi */ |
| 473 | } |
| 474 | |
| 475 | if (use_msi && !pci_enable_msi(pdev)) { |
| 476 | adapter->flags |= QLCNIC_MSI_ENABLED; |
| 477 | adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter, |
| 478 | msi_tgt_status[adapter->ahw.pci_func]); |
| 479 | dev_info(&pdev->dev, "using msi interrupts\n"); |
| 480 | adapter->msix_entries[0].vector = pdev->irq; |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | dev_info(&pdev->dev, "using legacy interrupts\n"); |
| 485 | adapter->msix_entries[0].vector = pdev->irq; |
| 486 | } |
| 487 | |
| 488 | static void |
| 489 | qlcnic_teardown_intr(struct qlcnic_adapter *adapter) |
| 490 | { |
| 491 | if (adapter->flags & QLCNIC_MSIX_ENABLED) |
| 492 | pci_disable_msix(adapter->pdev); |
| 493 | if (adapter->flags & QLCNIC_MSI_ENABLED) |
| 494 | pci_disable_msi(adapter->pdev); |
| 495 | } |
| 496 | |
| 497 | static void |
| 498 | qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter) |
| 499 | { |
| 500 | if (adapter->ahw.pci_base0 != NULL) |
| 501 | iounmap(adapter->ahw.pci_base0); |
| 502 | } |
| 503 | |
| 504 | static int |
| 505 | qlcnic_setup_pci_map(struct qlcnic_adapter *adapter) |
| 506 | { |
| 507 | void __iomem *mem_ptr0 = NULL; |
| 508 | resource_size_t mem_base; |
| 509 | unsigned long mem_len, pci_len0 = 0; |
| 510 | |
| 511 | struct pci_dev *pdev = adapter->pdev; |
| 512 | int pci_func = adapter->ahw.pci_func; |
| 513 | |
| 514 | /* |
| 515 | * Set the CRB window to invalid. If any register in window 0 is |
| 516 | * accessed it should set the window to 0 and then reset it to 1. |
| 517 | */ |
| 518 | adapter->ahw.crb_win = -1; |
| 519 | adapter->ahw.ocm_win = -1; |
| 520 | |
| 521 | /* remap phys address */ |
| 522 | mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */ |
| 523 | mem_len = pci_resource_len(pdev, 0); |
| 524 | |
| 525 | if (mem_len == QLCNIC_PCI_2MB_SIZE) { |
| 526 | |
| 527 | mem_ptr0 = pci_ioremap_bar(pdev, 0); |
| 528 | if (mem_ptr0 == NULL) { |
| 529 | dev_err(&pdev->dev, "failed to map PCI bar 0\n"); |
| 530 | return -EIO; |
| 531 | } |
| 532 | pci_len0 = mem_len; |
| 533 | } else { |
| 534 | return -EIO; |
| 535 | } |
| 536 | |
| 537 | dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20)); |
| 538 | |
| 539 | adapter->ahw.pci_base0 = mem_ptr0; |
| 540 | adapter->ahw.pci_len0 = pci_len0; |
| 541 | |
| 542 | adapter->ahw.ocm_win_crb = qlcnic_get_ioaddr(adapter, |
| 543 | QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func))); |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static void get_brd_name(struct qlcnic_adapter *adapter, char *name) |
| 549 | { |
| 550 | struct pci_dev *pdev = adapter->pdev; |
| 551 | int i, found = 0; |
| 552 | |
| 553 | for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) { |
| 554 | if (qlcnic_boards[i].vendor == pdev->vendor && |
| 555 | qlcnic_boards[i].device == pdev->device && |
| 556 | qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor && |
| 557 | qlcnic_boards[i].sub_device == pdev->subsystem_device) { |
| 558 | strcpy(name, qlcnic_boards[i].short_name); |
| 559 | found = 1; |
| 560 | break; |
| 561 | } |
| 562 | |
| 563 | } |
| 564 | |
| 565 | if (!found) |
| 566 | name = "Unknown"; |
| 567 | } |
| 568 | |
| 569 | static void |
| 570 | qlcnic_check_options(struct qlcnic_adapter *adapter) |
| 571 | { |
| 572 | u32 fw_major, fw_minor, fw_build; |
| 573 | char brd_name[QLCNIC_MAX_BOARD_NAME_LEN]; |
| 574 | char serial_num[32]; |
| 575 | int i, offset, val; |
| 576 | int *ptr32; |
| 577 | struct pci_dev *pdev = adapter->pdev; |
| 578 | |
| 579 | adapter->driver_mismatch = 0; |
| 580 | |
| 581 | ptr32 = (int *)&serial_num; |
| 582 | offset = QLCNIC_FW_SERIAL_NUM_OFFSET; |
| 583 | for (i = 0; i < 8; i++) { |
| 584 | if (qlcnic_rom_fast_read(adapter, offset, &val) == -1) { |
| 585 | dev_err(&pdev->dev, "error reading board info\n"); |
| 586 | adapter->driver_mismatch = 1; |
| 587 | return; |
| 588 | } |
| 589 | ptr32[i] = cpu_to_le32(val); |
| 590 | offset += sizeof(u32); |
| 591 | } |
| 592 | |
| 593 | fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR); |
| 594 | fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR); |
| 595 | fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB); |
| 596 | |
| 597 | adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build); |
| 598 | |
| 599 | if (adapter->portnum == 0) { |
| 600 | get_brd_name(adapter, brd_name); |
| 601 | |
| 602 | pr_info("%s: %s Board Chip rev 0x%x\n", |
| 603 | module_name(THIS_MODULE), |
| 604 | brd_name, adapter->ahw.revision_id); |
| 605 | } |
| 606 | |
| 607 | if (adapter->fw_version < QLCNIC_VERSION_CODE(3, 4, 216)) { |
| 608 | adapter->driver_mismatch = 1; |
| 609 | dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n", |
| 610 | fw_major, fw_minor, fw_build); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | i = QLCRD32(adapter, QLCNIC_SRE_MISC); |
| 615 | adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0; |
| 616 | |
| 617 | dev_info(&pdev->dev, "firmware v%d.%d.%d [%s]\n", |
| 618 | fw_major, fw_minor, fw_build, |
| 619 | adapter->ahw.cut_through ? "cut-through" : "legacy"); |
| 620 | |
| 621 | if (adapter->fw_version >= QLCNIC_VERSION_CODE(4, 0, 222)) |
| 622 | adapter->capabilities = QLCRD32(adapter, CRB_FW_CAPABILITIES_1); |
| 623 | |
| 624 | adapter->flags &= ~QLCNIC_LRO_ENABLED; |
| 625 | |
| 626 | if (adapter->ahw.port_type == QLCNIC_XGBE) { |
| 627 | adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G; |
| 628 | adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G; |
| 629 | } else if (adapter->ahw.port_type == QLCNIC_GBE) { |
| 630 | adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G; |
| 631 | adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G; |
| 632 | } |
| 633 | |
| 634 | adapter->msix_supported = !!use_msi_x; |
| 635 | adapter->rss_supported = !!use_msi_x; |
| 636 | |
| 637 | adapter->num_txd = MAX_CMD_DESCRIPTORS; |
| 638 | |
| 639 | adapter->num_lro_rxd = 0; |
| 640 | adapter->max_rds_rings = 2; |
| 641 | } |
| 642 | |
| 643 | static int |
| 644 | qlcnic_start_firmware(struct qlcnic_adapter *adapter) |
| 645 | { |
| 646 | int val, err, first_boot; |
| 647 | |
| 648 | err = qlcnic_set_dma_mask(adapter); |
| 649 | if (err) |
| 650 | return err; |
| 651 | |
Sucheta Chakraborty | aa5e18c | 2010-04-01 19:01:32 +0000 | [diff] [blame] | 652 | err = qlcnic_can_start_firmware(adapter); |
| 653 | if (err < 0) |
| 654 | return err; |
| 655 | else if (!err) |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 656 | goto wait_init; |
| 657 | |
| 658 | first_boot = QLCRD32(adapter, QLCNIC_CAM_RAM(0x1fc)); |
| 659 | if (first_boot == 0x55555555) |
| 660 | /* This is the first boot after power up */ |
| 661 | QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC); |
| 662 | |
| 663 | qlcnic_request_firmware(adapter); |
| 664 | |
| 665 | err = qlcnic_need_fw_reset(adapter); |
| 666 | if (err < 0) |
| 667 | goto err_out; |
| 668 | if (err == 0) |
| 669 | goto wait_init; |
| 670 | |
| 671 | if (first_boot != 0x55555555) { |
| 672 | QLCWR32(adapter, CRB_CMDPEG_STATE, 0); |
| 673 | qlcnic_pinit_from_rom(adapter); |
| 674 | msleep(1); |
| 675 | } |
| 676 | |
| 677 | QLCWR32(adapter, CRB_DMA_SHIFT, 0x55555555); |
| 678 | QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS1, 0); |
| 679 | QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS2, 0); |
| 680 | |
| 681 | qlcnic_set_port_mode(adapter); |
| 682 | |
| 683 | err = qlcnic_load_firmware(adapter); |
| 684 | if (err) |
| 685 | goto err_out; |
| 686 | |
| 687 | qlcnic_release_firmware(adapter); |
| 688 | |
| 689 | val = (_QLCNIC_LINUX_MAJOR << 16) |
| 690 | | ((_QLCNIC_LINUX_MINOR << 8)) |
| 691 | | (_QLCNIC_LINUX_SUBVERSION); |
| 692 | QLCWR32(adapter, CRB_DRIVER_VERSION, val); |
| 693 | |
| 694 | wait_init: |
| 695 | /* Handshake with the card before we register the devices. */ |
| 696 | err = qlcnic_phantom_init(adapter); |
| 697 | if (err) |
| 698 | goto err_out; |
| 699 | |
| 700 | QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY); |
| 701 | |
| 702 | qlcnic_update_dma_mask(adapter); |
| 703 | |
| 704 | qlcnic_check_options(adapter); |
| 705 | |
| 706 | adapter->need_fw_reset = 0; |
| 707 | |
| 708 | /* fall through and release firmware */ |
| 709 | |
| 710 | err_out: |
| 711 | qlcnic_release_firmware(adapter); |
| 712 | return err; |
| 713 | } |
| 714 | |
| 715 | static int |
| 716 | qlcnic_request_irq(struct qlcnic_adapter *adapter) |
| 717 | { |
| 718 | irq_handler_t handler; |
| 719 | struct qlcnic_host_sds_ring *sds_ring; |
| 720 | int err, ring; |
| 721 | |
| 722 | unsigned long flags = 0; |
| 723 | struct net_device *netdev = adapter->netdev; |
| 724 | struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx; |
| 725 | |
Amit Kumar Salecha | 7eb9855 | 2010-02-01 05:24:59 +0000 | [diff] [blame] | 726 | if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) { |
| 727 | handler = qlcnic_tmp_intr; |
| 728 | if (!QLCNIC_IS_MSI_FAMILY(adapter)) |
| 729 | flags |= IRQF_SHARED; |
| 730 | |
| 731 | } else { |
| 732 | if (adapter->flags & QLCNIC_MSIX_ENABLED) |
| 733 | handler = qlcnic_msix_intr; |
| 734 | else if (adapter->flags & QLCNIC_MSI_ENABLED) |
| 735 | handler = qlcnic_msi_intr; |
| 736 | else { |
| 737 | flags |= IRQF_SHARED; |
| 738 | handler = qlcnic_intr; |
| 739 | } |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 740 | } |
| 741 | adapter->irq = netdev->irq; |
| 742 | |
| 743 | for (ring = 0; ring < adapter->max_sds_rings; ring++) { |
| 744 | sds_ring = &recv_ctx->sds_rings[ring]; |
| 745 | sprintf(sds_ring->name, "%s[%d]", netdev->name, ring); |
| 746 | err = request_irq(sds_ring->irq, handler, |
| 747 | flags, sds_ring->name, sds_ring); |
| 748 | if (err) |
| 749 | return err; |
| 750 | } |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | static void |
| 756 | qlcnic_free_irq(struct qlcnic_adapter *adapter) |
| 757 | { |
| 758 | int ring; |
| 759 | struct qlcnic_host_sds_ring *sds_ring; |
| 760 | |
| 761 | struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx; |
| 762 | |
| 763 | for (ring = 0; ring < adapter->max_sds_rings; ring++) { |
| 764 | sds_ring = &recv_ctx->sds_rings[ring]; |
| 765 | free_irq(sds_ring->irq, sds_ring); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | static void |
| 770 | qlcnic_init_coalesce_defaults(struct qlcnic_adapter *adapter) |
| 771 | { |
| 772 | adapter->coal.flags = QLCNIC_INTR_DEFAULT; |
| 773 | adapter->coal.normal.data.rx_time_us = |
| 774 | QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US; |
| 775 | adapter->coal.normal.data.rx_packets = |
| 776 | QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS; |
| 777 | adapter->coal.normal.data.tx_time_us = |
| 778 | QLCNIC_DEFAULT_INTR_COALESCE_TX_TIME_US; |
| 779 | adapter->coal.normal.data.tx_packets = |
| 780 | QLCNIC_DEFAULT_INTR_COALESCE_TX_PACKETS; |
| 781 | } |
| 782 | |
| 783 | static int |
| 784 | __qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev) |
| 785 | { |
| 786 | if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC) |
| 787 | return -EIO; |
| 788 | |
| 789 | qlcnic_set_multi(netdev); |
| 790 | qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu); |
| 791 | |
| 792 | adapter->ahw.linkup = 0; |
| 793 | |
| 794 | if (adapter->max_sds_rings > 1) |
| 795 | qlcnic_config_rss(adapter, 1); |
| 796 | |
| 797 | qlcnic_config_intr_coalesce(adapter); |
| 798 | |
| 799 | if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO) |
| 800 | qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED); |
| 801 | |
| 802 | qlcnic_napi_enable(adapter); |
| 803 | |
| 804 | qlcnic_linkevent_request(adapter, 1); |
| 805 | |
| 806 | set_bit(__QLCNIC_DEV_UP, &adapter->state); |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | /* Usage: During resume and firmware recovery module.*/ |
| 811 | |
| 812 | static int |
| 813 | qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev) |
| 814 | { |
| 815 | int err = 0; |
| 816 | |
| 817 | rtnl_lock(); |
| 818 | if (netif_running(netdev)) |
| 819 | err = __qlcnic_up(adapter, netdev); |
| 820 | rtnl_unlock(); |
| 821 | |
| 822 | return err; |
| 823 | } |
| 824 | |
| 825 | static void |
| 826 | __qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev) |
| 827 | { |
| 828 | if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC) |
| 829 | return; |
| 830 | |
| 831 | if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state)) |
| 832 | return; |
| 833 | |
| 834 | smp_mb(); |
| 835 | spin_lock(&adapter->tx_clean_lock); |
| 836 | netif_carrier_off(netdev); |
| 837 | netif_tx_disable(netdev); |
| 838 | |
| 839 | qlcnic_free_mac_list(adapter); |
| 840 | |
| 841 | qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE); |
| 842 | |
| 843 | qlcnic_napi_disable(adapter); |
| 844 | |
| 845 | qlcnic_release_tx_buffers(adapter); |
| 846 | spin_unlock(&adapter->tx_clean_lock); |
| 847 | } |
| 848 | |
| 849 | /* Usage: During suspend and firmware recovery module */ |
| 850 | |
| 851 | static void |
| 852 | qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev) |
| 853 | { |
| 854 | rtnl_lock(); |
| 855 | if (netif_running(netdev)) |
| 856 | __qlcnic_down(adapter, netdev); |
| 857 | rtnl_unlock(); |
| 858 | |
| 859 | } |
| 860 | |
| 861 | static int |
| 862 | qlcnic_attach(struct qlcnic_adapter *adapter) |
| 863 | { |
| 864 | struct net_device *netdev = adapter->netdev; |
| 865 | struct pci_dev *pdev = adapter->pdev; |
| 866 | int err, ring; |
| 867 | struct qlcnic_host_rds_ring *rds_ring; |
| 868 | |
| 869 | if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) |
| 870 | return 0; |
| 871 | |
| 872 | err = qlcnic_init_firmware(adapter); |
| 873 | if (err) |
| 874 | return err; |
| 875 | |
| 876 | err = qlcnic_napi_add(adapter, netdev); |
| 877 | if (err) |
| 878 | return err; |
| 879 | |
| 880 | err = qlcnic_alloc_sw_resources(adapter); |
| 881 | if (err) { |
| 882 | dev_err(&pdev->dev, "Error in setting sw resources\n"); |
| 883 | return err; |
| 884 | } |
| 885 | |
| 886 | err = qlcnic_alloc_hw_resources(adapter); |
| 887 | if (err) { |
| 888 | dev_err(&pdev->dev, "Error in setting hw resources\n"); |
| 889 | goto err_out_free_sw; |
| 890 | } |
| 891 | |
| 892 | |
| 893 | for (ring = 0; ring < adapter->max_rds_rings; ring++) { |
| 894 | rds_ring = &adapter->recv_ctx.rds_rings[ring]; |
| 895 | qlcnic_post_rx_buffers(adapter, ring, rds_ring); |
| 896 | } |
| 897 | |
| 898 | err = qlcnic_request_irq(adapter); |
| 899 | if (err) { |
| 900 | dev_err(&pdev->dev, "failed to setup interrupt\n"); |
| 901 | goto err_out_free_rxbuf; |
| 902 | } |
| 903 | |
| 904 | qlcnic_init_coalesce_defaults(adapter); |
| 905 | |
| 906 | qlcnic_create_sysfs_entries(adapter); |
| 907 | |
| 908 | adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC; |
| 909 | return 0; |
| 910 | |
| 911 | err_out_free_rxbuf: |
| 912 | qlcnic_release_rx_buffers(adapter); |
| 913 | qlcnic_free_hw_resources(adapter); |
| 914 | err_out_free_sw: |
| 915 | qlcnic_free_sw_resources(adapter); |
| 916 | return err; |
| 917 | } |
| 918 | |
| 919 | static void |
| 920 | qlcnic_detach(struct qlcnic_adapter *adapter) |
| 921 | { |
| 922 | if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC) |
| 923 | return; |
| 924 | |
| 925 | qlcnic_remove_sysfs_entries(adapter); |
| 926 | |
| 927 | qlcnic_free_hw_resources(adapter); |
| 928 | qlcnic_release_rx_buffers(adapter); |
| 929 | qlcnic_free_irq(adapter); |
| 930 | qlcnic_napi_del(adapter); |
| 931 | qlcnic_free_sw_resources(adapter); |
| 932 | |
| 933 | adapter->is_up = 0; |
| 934 | } |
| 935 | |
Amit Kumar Salecha | 7eb9855 | 2010-02-01 05:24:59 +0000 | [diff] [blame] | 936 | void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings) |
| 937 | { |
| 938 | struct qlcnic_adapter *adapter = netdev_priv(netdev); |
| 939 | struct qlcnic_host_sds_ring *sds_ring; |
| 940 | int ring; |
| 941 | |
Amit Kumar Salecha | cdaff18 | 2010-02-01 05:25:00 +0000 | [diff] [blame] | 942 | if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) { |
| 943 | for (ring = 0; ring < adapter->max_sds_rings; ring++) { |
| 944 | sds_ring = &adapter->recv_ctx.sds_rings[ring]; |
| 945 | qlcnic_disable_int(sds_ring); |
| 946 | } |
Amit Kumar Salecha | 7eb9855 | 2010-02-01 05:24:59 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | qlcnic_detach(adapter); |
| 950 | |
| 951 | adapter->diag_test = 0; |
| 952 | adapter->max_sds_rings = max_sds_rings; |
| 953 | |
| 954 | if (qlcnic_attach(adapter)) |
Amit Kumar Salecha | 34ce362 | 2010-04-01 19:01:34 +0000 | [diff] [blame] | 955 | goto out; |
Amit Kumar Salecha | 7eb9855 | 2010-02-01 05:24:59 +0000 | [diff] [blame] | 956 | |
| 957 | if (netif_running(netdev)) |
| 958 | __qlcnic_up(adapter, netdev); |
Amit Kumar Salecha | 34ce362 | 2010-04-01 19:01:34 +0000 | [diff] [blame] | 959 | out: |
Amit Kumar Salecha | 7eb9855 | 2010-02-01 05:24:59 +0000 | [diff] [blame] | 960 | netif_device_attach(netdev); |
| 961 | } |
| 962 | |
| 963 | int qlcnic_diag_alloc_res(struct net_device *netdev, int test) |
| 964 | { |
| 965 | struct qlcnic_adapter *adapter = netdev_priv(netdev); |
| 966 | struct qlcnic_host_sds_ring *sds_ring; |
| 967 | int ring; |
| 968 | int ret; |
| 969 | |
| 970 | netif_device_detach(netdev); |
| 971 | |
| 972 | if (netif_running(netdev)) |
| 973 | __qlcnic_down(adapter, netdev); |
| 974 | |
| 975 | qlcnic_detach(adapter); |
| 976 | |
| 977 | adapter->max_sds_rings = 1; |
| 978 | adapter->diag_test = test; |
| 979 | |
| 980 | ret = qlcnic_attach(adapter); |
Amit Kumar Salecha | 34ce362 | 2010-04-01 19:01:34 +0000 | [diff] [blame] | 981 | if (ret) { |
| 982 | netif_device_attach(netdev); |
Amit Kumar Salecha | 7eb9855 | 2010-02-01 05:24:59 +0000 | [diff] [blame] | 983 | return ret; |
Amit Kumar Salecha | 34ce362 | 2010-04-01 19:01:34 +0000 | [diff] [blame] | 984 | } |
Amit Kumar Salecha | 7eb9855 | 2010-02-01 05:24:59 +0000 | [diff] [blame] | 985 | |
Amit Kumar Salecha | cdaff18 | 2010-02-01 05:25:00 +0000 | [diff] [blame] | 986 | if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) { |
| 987 | for (ring = 0; ring < adapter->max_sds_rings; ring++) { |
| 988 | sds_ring = &adapter->recv_ctx.sds_rings[ring]; |
| 989 | qlcnic_enable_int(sds_ring); |
| 990 | } |
Amit Kumar Salecha | 7eb9855 | 2010-02-01 05:24:59 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | return 0; |
| 994 | } |
| 995 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 996 | int |
| 997 | qlcnic_reset_context(struct qlcnic_adapter *adapter) |
| 998 | { |
| 999 | int err = 0; |
| 1000 | struct net_device *netdev = adapter->netdev; |
| 1001 | |
| 1002 | if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) |
| 1003 | return -EBUSY; |
| 1004 | |
| 1005 | if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) { |
| 1006 | |
| 1007 | netif_device_detach(netdev); |
| 1008 | |
| 1009 | if (netif_running(netdev)) |
| 1010 | __qlcnic_down(adapter, netdev); |
| 1011 | |
| 1012 | qlcnic_detach(adapter); |
| 1013 | |
| 1014 | if (netif_running(netdev)) { |
| 1015 | err = qlcnic_attach(adapter); |
| 1016 | if (!err) |
Amit Kumar Salecha | 34ce362 | 2010-04-01 19:01:34 +0000 | [diff] [blame] | 1017 | __qlcnic_up(adapter, netdev); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | netif_device_attach(netdev); |
| 1021 | } |
| 1022 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1023 | clear_bit(__QLCNIC_RESETTING, &adapter->state); |
| 1024 | return err; |
| 1025 | } |
| 1026 | |
| 1027 | static int |
| 1028 | qlcnic_setup_netdev(struct qlcnic_adapter *adapter, |
| 1029 | struct net_device *netdev) |
| 1030 | { |
| 1031 | int err; |
| 1032 | struct pci_dev *pdev = adapter->pdev; |
| 1033 | |
| 1034 | adapter->rx_csum = 1; |
| 1035 | adapter->mc_enabled = 0; |
| 1036 | adapter->max_mc_count = 38; |
| 1037 | |
| 1038 | netdev->netdev_ops = &qlcnic_netdev_ops; |
| 1039 | netdev->watchdog_timeo = 2*HZ; |
| 1040 | |
| 1041 | qlcnic_change_mtu(netdev, netdev->mtu); |
| 1042 | |
| 1043 | SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops); |
| 1044 | |
| 1045 | netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO); |
| 1046 | netdev->features |= (NETIF_F_GRO); |
| 1047 | netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO); |
| 1048 | |
| 1049 | netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6); |
| 1050 | netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6); |
| 1051 | |
| 1052 | if (adapter->pci_using_dac) { |
| 1053 | netdev->features |= NETIF_F_HIGHDMA; |
| 1054 | netdev->vlan_features |= NETIF_F_HIGHDMA; |
| 1055 | } |
| 1056 | |
| 1057 | if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX) |
| 1058 | netdev->features |= (NETIF_F_HW_VLAN_TX); |
| 1059 | |
| 1060 | if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO) |
| 1061 | netdev->features |= NETIF_F_LRO; |
| 1062 | |
| 1063 | netdev->irq = adapter->msix_entries[0].vector; |
| 1064 | |
| 1065 | INIT_WORK(&adapter->tx_timeout_task, qlcnic_tx_timeout_task); |
| 1066 | |
| 1067 | if (qlcnic_read_mac_addr(adapter)) |
| 1068 | dev_warn(&pdev->dev, "failed to read mac addr\n"); |
| 1069 | |
| 1070 | netif_carrier_off(netdev); |
| 1071 | netif_stop_queue(netdev); |
| 1072 | |
| 1073 | err = register_netdev(netdev); |
| 1074 | if (err) { |
| 1075 | dev_err(&pdev->dev, "failed to register net device\n"); |
| 1076 | return err; |
| 1077 | } |
| 1078 | |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | static int __devinit |
| 1083 | qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 1084 | { |
| 1085 | struct net_device *netdev = NULL; |
| 1086 | struct qlcnic_adapter *adapter = NULL; |
| 1087 | int err; |
| 1088 | int pci_func_id = PCI_FUNC(pdev->devfn); |
| 1089 | uint8_t revision_id; |
| 1090 | |
| 1091 | err = pci_enable_device(pdev); |
| 1092 | if (err) |
| 1093 | return err; |
| 1094 | |
| 1095 | if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { |
| 1096 | err = -ENODEV; |
| 1097 | goto err_out_disable_pdev; |
| 1098 | } |
| 1099 | |
| 1100 | err = pci_request_regions(pdev, qlcnic_driver_name); |
| 1101 | if (err) |
| 1102 | goto err_out_disable_pdev; |
| 1103 | |
| 1104 | pci_set_master(pdev); |
| 1105 | |
| 1106 | netdev = alloc_etherdev(sizeof(struct qlcnic_adapter)); |
| 1107 | if (!netdev) { |
| 1108 | dev_err(&pdev->dev, "failed to allocate net_device\n"); |
| 1109 | err = -ENOMEM; |
| 1110 | goto err_out_free_res; |
| 1111 | } |
| 1112 | |
| 1113 | SET_NETDEV_DEV(netdev, &pdev->dev); |
| 1114 | |
| 1115 | adapter = netdev_priv(netdev); |
| 1116 | adapter->netdev = netdev; |
| 1117 | adapter->pdev = pdev; |
| 1118 | adapter->ahw.pci_func = pci_func_id; |
| 1119 | |
| 1120 | revision_id = pdev->revision; |
| 1121 | adapter->ahw.revision_id = revision_id; |
| 1122 | |
| 1123 | rwlock_init(&adapter->ahw.crb_lock); |
| 1124 | mutex_init(&adapter->ahw.mem_lock); |
| 1125 | |
| 1126 | spin_lock_init(&adapter->tx_clean_lock); |
| 1127 | INIT_LIST_HEAD(&adapter->mac_list); |
| 1128 | |
| 1129 | err = qlcnic_setup_pci_map(adapter); |
| 1130 | if (err) |
| 1131 | goto err_out_free_netdev; |
| 1132 | |
| 1133 | /* This will be reset for mezz cards */ |
| 1134 | adapter->portnum = pci_func_id; |
| 1135 | |
| 1136 | err = qlcnic_get_board_info(adapter); |
| 1137 | if (err) { |
| 1138 | dev_err(&pdev->dev, "Error getting board config info.\n"); |
| 1139 | goto err_out_iounmap; |
| 1140 | } |
| 1141 | |
Sucheta Chakraborty | aa5e18c | 2010-04-01 19:01:32 +0000 | [diff] [blame] | 1142 | qlcnic_setup_idc_param(adapter); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1143 | |
| 1144 | err = qlcnic_start_firmware(adapter); |
| 1145 | if (err) |
| 1146 | goto err_out_decr_ref; |
| 1147 | |
| 1148 | /* |
| 1149 | * See if the firmware gave us a virtual-physical port mapping. |
| 1150 | */ |
| 1151 | adapter->physical_port = adapter->portnum; |
| 1152 | |
| 1153 | qlcnic_clear_stats(adapter); |
| 1154 | |
| 1155 | qlcnic_setup_intr(adapter); |
| 1156 | |
| 1157 | err = qlcnic_setup_netdev(adapter, netdev); |
| 1158 | if (err) |
| 1159 | goto err_out_disable_msi; |
| 1160 | |
| 1161 | pci_set_drvdata(pdev, adapter); |
| 1162 | |
| 1163 | qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY); |
| 1164 | |
| 1165 | switch (adapter->ahw.port_type) { |
| 1166 | case QLCNIC_GBE: |
| 1167 | dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n", |
| 1168 | adapter->netdev->name); |
| 1169 | break; |
| 1170 | case QLCNIC_XGBE: |
| 1171 | dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n", |
| 1172 | adapter->netdev->name); |
| 1173 | break; |
| 1174 | } |
| 1175 | |
| 1176 | qlcnic_create_diag_entries(adapter); |
| 1177 | |
| 1178 | return 0; |
| 1179 | |
| 1180 | err_out_disable_msi: |
| 1181 | qlcnic_teardown_intr(adapter); |
| 1182 | |
| 1183 | err_out_decr_ref: |
| 1184 | qlcnic_clr_all_drv_state(adapter); |
| 1185 | |
| 1186 | err_out_iounmap: |
| 1187 | qlcnic_cleanup_pci_map(adapter); |
| 1188 | |
| 1189 | err_out_free_netdev: |
| 1190 | free_netdev(netdev); |
| 1191 | |
| 1192 | err_out_free_res: |
| 1193 | pci_release_regions(pdev); |
| 1194 | |
| 1195 | err_out_disable_pdev: |
| 1196 | pci_set_drvdata(pdev, NULL); |
| 1197 | pci_disable_device(pdev); |
| 1198 | return err; |
| 1199 | } |
| 1200 | |
| 1201 | static void __devexit qlcnic_remove(struct pci_dev *pdev) |
| 1202 | { |
| 1203 | struct qlcnic_adapter *adapter; |
| 1204 | struct net_device *netdev; |
| 1205 | |
| 1206 | adapter = pci_get_drvdata(pdev); |
| 1207 | if (adapter == NULL) |
| 1208 | return; |
| 1209 | |
| 1210 | netdev = adapter->netdev; |
| 1211 | |
| 1212 | qlcnic_cancel_fw_work(adapter); |
| 1213 | |
| 1214 | unregister_netdev(netdev); |
| 1215 | |
| 1216 | cancel_work_sync(&adapter->tx_timeout_task); |
| 1217 | |
| 1218 | qlcnic_detach(adapter); |
| 1219 | |
| 1220 | qlcnic_clr_all_drv_state(adapter); |
| 1221 | |
| 1222 | clear_bit(__QLCNIC_RESETTING, &adapter->state); |
| 1223 | |
| 1224 | qlcnic_teardown_intr(adapter); |
| 1225 | |
| 1226 | qlcnic_remove_diag_entries(adapter); |
| 1227 | |
| 1228 | qlcnic_cleanup_pci_map(adapter); |
| 1229 | |
| 1230 | qlcnic_release_firmware(adapter); |
| 1231 | |
| 1232 | pci_release_regions(pdev); |
| 1233 | pci_disable_device(pdev); |
| 1234 | pci_set_drvdata(pdev, NULL); |
| 1235 | |
| 1236 | free_netdev(netdev); |
| 1237 | } |
| 1238 | static int __qlcnic_shutdown(struct pci_dev *pdev) |
| 1239 | { |
| 1240 | struct qlcnic_adapter *adapter = pci_get_drvdata(pdev); |
| 1241 | struct net_device *netdev = adapter->netdev; |
| 1242 | int retval; |
| 1243 | |
| 1244 | netif_device_detach(netdev); |
| 1245 | |
| 1246 | qlcnic_cancel_fw_work(adapter); |
| 1247 | |
| 1248 | if (netif_running(netdev)) |
| 1249 | qlcnic_down(adapter, netdev); |
| 1250 | |
| 1251 | cancel_work_sync(&adapter->tx_timeout_task); |
| 1252 | |
| 1253 | qlcnic_detach(adapter); |
| 1254 | |
| 1255 | qlcnic_clr_all_drv_state(adapter); |
| 1256 | |
| 1257 | clear_bit(__QLCNIC_RESETTING, &adapter->state); |
| 1258 | |
| 1259 | retval = pci_save_state(pdev); |
| 1260 | if (retval) |
| 1261 | return retval; |
| 1262 | |
| 1263 | if (qlcnic_wol_supported(adapter)) { |
| 1264 | pci_enable_wake(pdev, PCI_D3cold, 1); |
| 1265 | pci_enable_wake(pdev, PCI_D3hot, 1); |
| 1266 | } |
| 1267 | |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | static void qlcnic_shutdown(struct pci_dev *pdev) |
| 1272 | { |
| 1273 | if (__qlcnic_shutdown(pdev)) |
| 1274 | return; |
| 1275 | |
| 1276 | pci_disable_device(pdev); |
| 1277 | } |
| 1278 | |
| 1279 | #ifdef CONFIG_PM |
| 1280 | static int |
| 1281 | qlcnic_suspend(struct pci_dev *pdev, pm_message_t state) |
| 1282 | { |
| 1283 | int retval; |
| 1284 | |
| 1285 | retval = __qlcnic_shutdown(pdev); |
| 1286 | if (retval) |
| 1287 | return retval; |
| 1288 | |
| 1289 | pci_set_power_state(pdev, pci_choose_state(pdev, state)); |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | static int |
| 1294 | qlcnic_resume(struct pci_dev *pdev) |
| 1295 | { |
| 1296 | struct qlcnic_adapter *adapter = pci_get_drvdata(pdev); |
| 1297 | struct net_device *netdev = adapter->netdev; |
| 1298 | int err; |
| 1299 | |
| 1300 | err = pci_enable_device(pdev); |
| 1301 | if (err) |
| 1302 | return err; |
| 1303 | |
| 1304 | pci_set_power_state(pdev, PCI_D0); |
| 1305 | pci_set_master(pdev); |
| 1306 | pci_restore_state(pdev); |
| 1307 | |
| 1308 | adapter->ahw.crb_win = -1; |
| 1309 | adapter->ahw.ocm_win = -1; |
| 1310 | |
| 1311 | err = qlcnic_start_firmware(adapter); |
| 1312 | if (err) { |
| 1313 | dev_err(&pdev->dev, "failed to start firmware\n"); |
| 1314 | return err; |
| 1315 | } |
| 1316 | |
| 1317 | if (netif_running(netdev)) { |
| 1318 | err = qlcnic_attach(adapter); |
| 1319 | if (err) |
| 1320 | goto err_out; |
| 1321 | |
| 1322 | err = qlcnic_up(adapter, netdev); |
| 1323 | if (err) |
| 1324 | goto err_out_detach; |
| 1325 | |
| 1326 | |
| 1327 | qlcnic_config_indev_addr(netdev, NETDEV_UP); |
| 1328 | } |
| 1329 | |
| 1330 | netif_device_attach(netdev); |
| 1331 | qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY); |
| 1332 | return 0; |
| 1333 | |
| 1334 | err_out_detach: |
| 1335 | qlcnic_detach(adapter); |
| 1336 | err_out: |
| 1337 | qlcnic_clr_all_drv_state(adapter); |
Amit Kumar Salecha | 34ce362 | 2010-04-01 19:01:34 +0000 | [diff] [blame] | 1338 | netif_device_attach(netdev); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1339 | return err; |
| 1340 | } |
| 1341 | #endif |
| 1342 | |
| 1343 | static int qlcnic_open(struct net_device *netdev) |
| 1344 | { |
| 1345 | struct qlcnic_adapter *adapter = netdev_priv(netdev); |
| 1346 | int err; |
| 1347 | |
| 1348 | if (adapter->driver_mismatch) |
| 1349 | return -EIO; |
| 1350 | |
| 1351 | err = qlcnic_attach(adapter); |
| 1352 | if (err) |
| 1353 | return err; |
| 1354 | |
| 1355 | err = __qlcnic_up(adapter, netdev); |
| 1356 | if (err) |
| 1357 | goto err_out; |
| 1358 | |
| 1359 | netif_start_queue(netdev); |
| 1360 | |
| 1361 | return 0; |
| 1362 | |
| 1363 | err_out: |
| 1364 | qlcnic_detach(adapter); |
| 1365 | return err; |
| 1366 | } |
| 1367 | |
| 1368 | /* |
| 1369 | * qlcnic_close - Disables a network interface entry point |
| 1370 | */ |
| 1371 | static int qlcnic_close(struct net_device *netdev) |
| 1372 | { |
| 1373 | struct qlcnic_adapter *adapter = netdev_priv(netdev); |
| 1374 | |
| 1375 | __qlcnic_down(adapter, netdev); |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | static void |
| 1380 | qlcnic_tso_check(struct net_device *netdev, |
| 1381 | struct qlcnic_host_tx_ring *tx_ring, |
| 1382 | struct cmd_desc_type0 *first_desc, |
| 1383 | struct sk_buff *skb) |
| 1384 | { |
| 1385 | u8 opcode = TX_ETHER_PKT; |
| 1386 | __be16 protocol = skb->protocol; |
| 1387 | u16 flags = 0, vid = 0; |
| 1388 | u32 producer; |
| 1389 | int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0; |
| 1390 | struct cmd_desc_type0 *hwdesc; |
| 1391 | struct vlan_ethhdr *vh; |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 1392 | struct qlcnic_adapter *adapter = netdev_priv(netdev); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1393 | |
| 1394 | if (protocol == cpu_to_be16(ETH_P_8021Q)) { |
| 1395 | |
| 1396 | vh = (struct vlan_ethhdr *)skb->data; |
| 1397 | protocol = vh->h_vlan_encapsulated_proto; |
| 1398 | flags = FLAGS_VLAN_TAGGED; |
| 1399 | |
| 1400 | } else if (vlan_tx_tag_present(skb)) { |
| 1401 | |
| 1402 | flags = FLAGS_VLAN_OOB; |
| 1403 | vid = vlan_tx_tag_get(skb); |
| 1404 | qlcnic_set_tx_vlan_tci(first_desc, vid); |
| 1405 | vlan_oob = 1; |
| 1406 | } |
| 1407 | |
| 1408 | if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) && |
| 1409 | skb_shinfo(skb)->gso_size > 0) { |
| 1410 | |
| 1411 | hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); |
| 1412 | |
| 1413 | first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size); |
| 1414 | first_desc->total_hdr_length = hdr_len; |
| 1415 | if (vlan_oob) { |
| 1416 | first_desc->total_hdr_length += VLAN_HLEN; |
| 1417 | first_desc->tcp_hdr_offset = VLAN_HLEN; |
| 1418 | first_desc->ip_hdr_offset = VLAN_HLEN; |
| 1419 | /* Only in case of TSO on vlan device */ |
| 1420 | flags |= FLAGS_VLAN_TAGGED; |
| 1421 | } |
| 1422 | |
| 1423 | opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ? |
| 1424 | TX_TCP_LSO6 : TX_TCP_LSO; |
| 1425 | tso = 1; |
| 1426 | |
| 1427 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1428 | u8 l4proto; |
| 1429 | |
| 1430 | if (protocol == cpu_to_be16(ETH_P_IP)) { |
| 1431 | l4proto = ip_hdr(skb)->protocol; |
| 1432 | |
| 1433 | if (l4proto == IPPROTO_TCP) |
| 1434 | opcode = TX_TCP_PKT; |
| 1435 | else if (l4proto == IPPROTO_UDP) |
| 1436 | opcode = TX_UDP_PKT; |
| 1437 | } else if (protocol == cpu_to_be16(ETH_P_IPV6)) { |
| 1438 | l4proto = ipv6_hdr(skb)->nexthdr; |
| 1439 | |
| 1440 | if (l4proto == IPPROTO_TCP) |
| 1441 | opcode = TX_TCPV6_PKT; |
| 1442 | else if (l4proto == IPPROTO_UDP) |
| 1443 | opcode = TX_UDPV6_PKT; |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | first_desc->tcp_hdr_offset += skb_transport_offset(skb); |
| 1448 | first_desc->ip_hdr_offset += skb_network_offset(skb); |
| 1449 | qlcnic_set_tx_flags_opcode(first_desc, flags, opcode); |
| 1450 | |
| 1451 | if (!tso) |
| 1452 | return; |
| 1453 | |
| 1454 | /* For LSO, we need to copy the MAC/IP/TCP headers into |
| 1455 | * the descriptor ring |
| 1456 | */ |
| 1457 | producer = tx_ring->producer; |
| 1458 | copied = 0; |
| 1459 | offset = 2; |
| 1460 | |
| 1461 | if (vlan_oob) { |
| 1462 | /* Create a TSO vlan header template for firmware */ |
| 1463 | |
| 1464 | hwdesc = &tx_ring->desc_head[producer]; |
| 1465 | tx_ring->cmd_buf_arr[producer].skb = NULL; |
| 1466 | |
| 1467 | copy_len = min((int)sizeof(struct cmd_desc_type0) - offset, |
| 1468 | hdr_len + VLAN_HLEN); |
| 1469 | |
| 1470 | vh = (struct vlan_ethhdr *)((char *)hwdesc + 2); |
| 1471 | skb_copy_from_linear_data(skb, vh, 12); |
| 1472 | vh->h_vlan_proto = htons(ETH_P_8021Q); |
| 1473 | vh->h_vlan_TCI = htons(vid); |
| 1474 | skb_copy_from_linear_data_offset(skb, 12, |
| 1475 | (char *)vh + 16, copy_len - 16); |
| 1476 | |
| 1477 | copied = copy_len - VLAN_HLEN; |
| 1478 | offset = 0; |
| 1479 | |
| 1480 | producer = get_next_index(producer, tx_ring->num_desc); |
| 1481 | } |
| 1482 | |
| 1483 | while (copied < hdr_len) { |
| 1484 | |
| 1485 | copy_len = min((int)sizeof(struct cmd_desc_type0) - offset, |
| 1486 | (hdr_len - copied)); |
| 1487 | |
| 1488 | hwdesc = &tx_ring->desc_head[producer]; |
| 1489 | tx_ring->cmd_buf_arr[producer].skb = NULL; |
| 1490 | |
| 1491 | skb_copy_from_linear_data_offset(skb, copied, |
| 1492 | (char *)hwdesc + offset, copy_len); |
| 1493 | |
| 1494 | copied += copy_len; |
| 1495 | offset = 0; |
| 1496 | |
| 1497 | producer = get_next_index(producer, tx_ring->num_desc); |
| 1498 | } |
| 1499 | |
| 1500 | tx_ring->producer = producer; |
| 1501 | barrier(); |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 1502 | adapter->stats.lso_frames++; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | static int |
| 1506 | qlcnic_map_tx_skb(struct pci_dev *pdev, |
| 1507 | struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf) |
| 1508 | { |
| 1509 | struct qlcnic_skb_frag *nf; |
| 1510 | struct skb_frag_struct *frag; |
| 1511 | int i, nr_frags; |
| 1512 | dma_addr_t map; |
| 1513 | |
| 1514 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 1515 | nf = &pbuf->frag_array[0]; |
| 1516 | |
| 1517 | map = pci_map_single(pdev, skb->data, |
| 1518 | skb_headlen(skb), PCI_DMA_TODEVICE); |
| 1519 | if (pci_dma_mapping_error(pdev, map)) |
| 1520 | goto out_err; |
| 1521 | |
| 1522 | nf->dma = map; |
| 1523 | nf->length = skb_headlen(skb); |
| 1524 | |
| 1525 | for (i = 0; i < nr_frags; i++) { |
| 1526 | frag = &skb_shinfo(skb)->frags[i]; |
| 1527 | nf = &pbuf->frag_array[i+1]; |
| 1528 | |
| 1529 | map = pci_map_page(pdev, frag->page, frag->page_offset, |
| 1530 | frag->size, PCI_DMA_TODEVICE); |
| 1531 | if (pci_dma_mapping_error(pdev, map)) |
| 1532 | goto unwind; |
| 1533 | |
| 1534 | nf->dma = map; |
| 1535 | nf->length = frag->size; |
| 1536 | } |
| 1537 | |
| 1538 | return 0; |
| 1539 | |
| 1540 | unwind: |
| 1541 | while (--i >= 0) { |
| 1542 | nf = &pbuf->frag_array[i+1]; |
| 1543 | pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE); |
| 1544 | } |
| 1545 | |
| 1546 | nf = &pbuf->frag_array[0]; |
| 1547 | pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE); |
| 1548 | |
| 1549 | out_err: |
| 1550 | return -ENOMEM; |
| 1551 | } |
| 1552 | |
| 1553 | static inline void |
| 1554 | qlcnic_clear_cmddesc(u64 *desc) |
| 1555 | { |
| 1556 | desc[0] = 0ULL; |
| 1557 | desc[2] = 0ULL; |
| 1558 | } |
| 1559 | |
Amit Kumar Salecha | cdaff18 | 2010-02-01 05:25:00 +0000 | [diff] [blame] | 1560 | netdev_tx_t |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1561 | qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev) |
| 1562 | { |
| 1563 | struct qlcnic_adapter *adapter = netdev_priv(netdev); |
| 1564 | struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring; |
| 1565 | struct qlcnic_cmd_buffer *pbuf; |
| 1566 | struct qlcnic_skb_frag *buffrag; |
| 1567 | struct cmd_desc_type0 *hwdesc, *first_desc; |
| 1568 | struct pci_dev *pdev; |
| 1569 | int i, k; |
| 1570 | |
| 1571 | u32 producer; |
| 1572 | int frag_count, no_of_desc; |
| 1573 | u32 num_txd = tx_ring->num_desc; |
| 1574 | |
| 1575 | frag_count = skb_shinfo(skb)->nr_frags + 1; |
| 1576 | |
| 1577 | /* 4 fragments per cmd des */ |
| 1578 | no_of_desc = (frag_count + 3) >> 2; |
| 1579 | |
| 1580 | if (unlikely(no_of_desc + 2 > qlcnic_tx_avail(tx_ring))) { |
| 1581 | netif_stop_queue(netdev); |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 1582 | adapter->stats.xmit_off++; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1583 | return NETDEV_TX_BUSY; |
| 1584 | } |
| 1585 | |
| 1586 | producer = tx_ring->producer; |
| 1587 | pbuf = &tx_ring->cmd_buf_arr[producer]; |
| 1588 | |
| 1589 | pdev = adapter->pdev; |
| 1590 | |
| 1591 | if (qlcnic_map_tx_skb(pdev, skb, pbuf)) |
| 1592 | goto drop_packet; |
| 1593 | |
| 1594 | pbuf->skb = skb; |
| 1595 | pbuf->frag_count = frag_count; |
| 1596 | |
| 1597 | first_desc = hwdesc = &tx_ring->desc_head[producer]; |
| 1598 | qlcnic_clear_cmddesc((u64 *)hwdesc); |
| 1599 | |
| 1600 | qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len); |
| 1601 | qlcnic_set_tx_port(first_desc, adapter->portnum); |
| 1602 | |
| 1603 | for (i = 0; i < frag_count; i++) { |
| 1604 | |
| 1605 | k = i % 4; |
| 1606 | |
| 1607 | if ((k == 0) && (i > 0)) { |
| 1608 | /* move to next desc.*/ |
| 1609 | producer = get_next_index(producer, num_txd); |
| 1610 | hwdesc = &tx_ring->desc_head[producer]; |
| 1611 | qlcnic_clear_cmddesc((u64 *)hwdesc); |
| 1612 | tx_ring->cmd_buf_arr[producer].skb = NULL; |
| 1613 | } |
| 1614 | |
| 1615 | buffrag = &pbuf->frag_array[i]; |
| 1616 | |
| 1617 | hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length); |
| 1618 | switch (k) { |
| 1619 | case 0: |
| 1620 | hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma); |
| 1621 | break; |
| 1622 | case 1: |
| 1623 | hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma); |
| 1624 | break; |
| 1625 | case 2: |
| 1626 | hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma); |
| 1627 | break; |
| 1628 | case 3: |
| 1629 | hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma); |
| 1630 | break; |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | tx_ring->producer = get_next_index(producer, num_txd); |
| 1635 | |
| 1636 | qlcnic_tso_check(netdev, tx_ring, first_desc, skb); |
| 1637 | |
| 1638 | qlcnic_update_cmd_producer(adapter, tx_ring); |
| 1639 | |
| 1640 | adapter->stats.txbytes += skb->len; |
| 1641 | adapter->stats.xmitcalled++; |
| 1642 | |
| 1643 | return NETDEV_TX_OK; |
| 1644 | |
| 1645 | drop_packet: |
| 1646 | adapter->stats.txdropped++; |
| 1647 | dev_kfree_skb_any(skb); |
| 1648 | return NETDEV_TX_OK; |
| 1649 | } |
| 1650 | |
| 1651 | static int qlcnic_check_temp(struct qlcnic_adapter *adapter) |
| 1652 | { |
| 1653 | struct net_device *netdev = adapter->netdev; |
| 1654 | u32 temp, temp_state, temp_val; |
| 1655 | int rv = 0; |
| 1656 | |
| 1657 | temp = QLCRD32(adapter, CRB_TEMP_STATE); |
| 1658 | |
| 1659 | temp_state = qlcnic_get_temp_state(temp); |
| 1660 | temp_val = qlcnic_get_temp_val(temp); |
| 1661 | |
| 1662 | if (temp_state == QLCNIC_TEMP_PANIC) { |
| 1663 | dev_err(&netdev->dev, |
| 1664 | "Device temperature %d degrees C exceeds" |
| 1665 | " maximum allowed. Hardware has been shut down.\n", |
| 1666 | temp_val); |
| 1667 | rv = 1; |
| 1668 | } else if (temp_state == QLCNIC_TEMP_WARN) { |
| 1669 | if (adapter->temp == QLCNIC_TEMP_NORMAL) { |
| 1670 | dev_err(&netdev->dev, |
| 1671 | "Device temperature %d degrees C " |
| 1672 | "exceeds operating range." |
| 1673 | " Immediate action needed.\n", |
| 1674 | temp_val); |
| 1675 | } |
| 1676 | } else { |
| 1677 | if (adapter->temp == QLCNIC_TEMP_WARN) { |
| 1678 | dev_info(&netdev->dev, |
| 1679 | "Device temperature is now %d degrees C" |
| 1680 | " in normal range.\n", temp_val); |
| 1681 | } |
| 1682 | } |
| 1683 | adapter->temp = temp_state; |
| 1684 | return rv; |
| 1685 | } |
| 1686 | |
| 1687 | void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup) |
| 1688 | { |
| 1689 | struct net_device *netdev = adapter->netdev; |
| 1690 | |
| 1691 | if (adapter->ahw.linkup && !linkup) { |
| 1692 | dev_info(&netdev->dev, "NIC Link is down\n"); |
| 1693 | adapter->ahw.linkup = 0; |
| 1694 | if (netif_running(netdev)) { |
| 1695 | netif_carrier_off(netdev); |
| 1696 | netif_stop_queue(netdev); |
| 1697 | } |
| 1698 | } else if (!adapter->ahw.linkup && linkup) { |
| 1699 | dev_info(&netdev->dev, "NIC Link is up\n"); |
| 1700 | adapter->ahw.linkup = 1; |
| 1701 | if (netif_running(netdev)) { |
| 1702 | netif_carrier_on(netdev); |
| 1703 | netif_wake_queue(netdev); |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | static void qlcnic_tx_timeout(struct net_device *netdev) |
| 1709 | { |
| 1710 | struct qlcnic_adapter *adapter = netdev_priv(netdev); |
| 1711 | |
| 1712 | if (test_bit(__QLCNIC_RESETTING, &adapter->state)) |
| 1713 | return; |
| 1714 | |
| 1715 | dev_err(&netdev->dev, "transmit timeout, resetting.\n"); |
| 1716 | schedule_work(&adapter->tx_timeout_task); |
| 1717 | } |
| 1718 | |
| 1719 | static void qlcnic_tx_timeout_task(struct work_struct *work) |
| 1720 | { |
| 1721 | struct qlcnic_adapter *adapter = |
| 1722 | container_of(work, struct qlcnic_adapter, tx_timeout_task); |
| 1723 | |
| 1724 | if (!netif_running(adapter->netdev)) |
| 1725 | return; |
| 1726 | |
| 1727 | if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) |
| 1728 | return; |
| 1729 | |
| 1730 | if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS) |
| 1731 | goto request_reset; |
| 1732 | |
| 1733 | clear_bit(__QLCNIC_RESETTING, &adapter->state); |
| 1734 | if (!qlcnic_reset_context(adapter)) { |
| 1735 | adapter->netdev->trans_start = jiffies; |
| 1736 | return; |
| 1737 | |
| 1738 | /* context reset failed, fall through for fw reset */ |
| 1739 | } |
| 1740 | |
| 1741 | request_reset: |
| 1742 | adapter->need_fw_reset = 1; |
| 1743 | clear_bit(__QLCNIC_RESETTING, &adapter->state); |
Amit Kumar Salecha | 65b5b42 | 2010-04-01 19:01:33 +0000 | [diff] [blame] | 1744 | QLCDB(adapter, DRV, "Resetting adapter\n"); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev) |
| 1748 | { |
| 1749 | struct qlcnic_adapter *adapter = netdev_priv(netdev); |
| 1750 | struct net_device_stats *stats = &netdev->stats; |
| 1751 | |
| 1752 | memset(stats, 0, sizeof(*stats)); |
| 1753 | |
| 1754 | stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts; |
| 1755 | stats->tx_packets = adapter->stats.xmitfinished; |
| 1756 | stats->rx_bytes = adapter->stats.rxbytes; |
| 1757 | stats->tx_bytes = adapter->stats.txbytes; |
| 1758 | stats->rx_dropped = adapter->stats.rxdropped; |
| 1759 | stats->tx_dropped = adapter->stats.txdropped; |
| 1760 | |
| 1761 | return stats; |
| 1762 | } |
| 1763 | |
Amit Kumar Salecha | 7eb9855 | 2010-02-01 05:24:59 +0000 | [diff] [blame] | 1764 | static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter) |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1765 | { |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1766 | u32 status; |
| 1767 | |
| 1768 | status = readl(adapter->isr_int_vec); |
| 1769 | |
| 1770 | if (!(status & adapter->int_vec_bit)) |
| 1771 | return IRQ_NONE; |
| 1772 | |
| 1773 | /* check interrupt state machine, to be sure */ |
| 1774 | status = readl(adapter->crb_int_state_reg); |
| 1775 | if (!ISR_LEGACY_INT_TRIGGERED(status)) |
| 1776 | return IRQ_NONE; |
| 1777 | |
| 1778 | writel(0xffffffff, adapter->tgt_status_reg); |
| 1779 | /* read twice to ensure write is flushed */ |
| 1780 | readl(adapter->isr_int_vec); |
| 1781 | readl(adapter->isr_int_vec); |
| 1782 | |
Amit Kumar Salecha | 7eb9855 | 2010-02-01 05:24:59 +0000 | [diff] [blame] | 1783 | return IRQ_HANDLED; |
| 1784 | } |
| 1785 | |
| 1786 | static irqreturn_t qlcnic_tmp_intr(int irq, void *data) |
| 1787 | { |
| 1788 | struct qlcnic_host_sds_ring *sds_ring = data; |
| 1789 | struct qlcnic_adapter *adapter = sds_ring->adapter; |
| 1790 | |
| 1791 | if (adapter->flags & QLCNIC_MSIX_ENABLED) |
| 1792 | goto done; |
| 1793 | else if (adapter->flags & QLCNIC_MSI_ENABLED) { |
| 1794 | writel(0xffffffff, adapter->tgt_status_reg); |
| 1795 | goto done; |
| 1796 | } |
| 1797 | |
| 1798 | if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE) |
| 1799 | return IRQ_NONE; |
| 1800 | |
| 1801 | done: |
| 1802 | adapter->diag_cnt++; |
| 1803 | qlcnic_enable_int(sds_ring); |
| 1804 | return IRQ_HANDLED; |
| 1805 | } |
| 1806 | |
| 1807 | static irqreturn_t qlcnic_intr(int irq, void *data) |
| 1808 | { |
| 1809 | struct qlcnic_host_sds_ring *sds_ring = data; |
| 1810 | struct qlcnic_adapter *adapter = sds_ring->adapter; |
| 1811 | |
| 1812 | if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE) |
| 1813 | return IRQ_NONE; |
| 1814 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1815 | napi_schedule(&sds_ring->napi); |
| 1816 | |
| 1817 | return IRQ_HANDLED; |
| 1818 | } |
| 1819 | |
| 1820 | static irqreturn_t qlcnic_msi_intr(int irq, void *data) |
| 1821 | { |
| 1822 | struct qlcnic_host_sds_ring *sds_ring = data; |
| 1823 | struct qlcnic_adapter *adapter = sds_ring->adapter; |
| 1824 | |
| 1825 | /* clear interrupt */ |
| 1826 | writel(0xffffffff, adapter->tgt_status_reg); |
| 1827 | |
| 1828 | napi_schedule(&sds_ring->napi); |
| 1829 | return IRQ_HANDLED; |
| 1830 | } |
| 1831 | |
| 1832 | static irqreturn_t qlcnic_msix_intr(int irq, void *data) |
| 1833 | { |
| 1834 | struct qlcnic_host_sds_ring *sds_ring = data; |
| 1835 | |
| 1836 | napi_schedule(&sds_ring->napi); |
| 1837 | return IRQ_HANDLED; |
| 1838 | } |
| 1839 | |
| 1840 | static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter) |
| 1841 | { |
| 1842 | u32 sw_consumer, hw_consumer; |
| 1843 | int count = 0, i; |
| 1844 | struct qlcnic_cmd_buffer *buffer; |
| 1845 | struct pci_dev *pdev = adapter->pdev; |
| 1846 | struct net_device *netdev = adapter->netdev; |
| 1847 | struct qlcnic_skb_frag *frag; |
| 1848 | int done; |
| 1849 | struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring; |
| 1850 | |
| 1851 | if (!spin_trylock(&adapter->tx_clean_lock)) |
| 1852 | return 1; |
| 1853 | |
| 1854 | sw_consumer = tx_ring->sw_consumer; |
| 1855 | hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer)); |
| 1856 | |
| 1857 | while (sw_consumer != hw_consumer) { |
| 1858 | buffer = &tx_ring->cmd_buf_arr[sw_consumer]; |
| 1859 | if (buffer->skb) { |
| 1860 | frag = &buffer->frag_array[0]; |
| 1861 | pci_unmap_single(pdev, frag->dma, frag->length, |
| 1862 | PCI_DMA_TODEVICE); |
| 1863 | frag->dma = 0ULL; |
| 1864 | for (i = 1; i < buffer->frag_count; i++) { |
| 1865 | frag++; |
| 1866 | pci_unmap_page(pdev, frag->dma, frag->length, |
| 1867 | PCI_DMA_TODEVICE); |
| 1868 | frag->dma = 0ULL; |
| 1869 | } |
| 1870 | |
| 1871 | adapter->stats.xmitfinished++; |
| 1872 | dev_kfree_skb_any(buffer->skb); |
| 1873 | buffer->skb = NULL; |
| 1874 | } |
| 1875 | |
| 1876 | sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc); |
| 1877 | if (++count >= MAX_STATUS_HANDLE) |
| 1878 | break; |
| 1879 | } |
| 1880 | |
| 1881 | if (count && netif_running(netdev)) { |
| 1882 | tx_ring->sw_consumer = sw_consumer; |
| 1883 | |
| 1884 | smp_mb(); |
| 1885 | |
| 1886 | if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) { |
| 1887 | __netif_tx_lock(tx_ring->txq, smp_processor_id()); |
| 1888 | if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) { |
| 1889 | netif_wake_queue(netdev); |
| 1890 | adapter->tx_timeo_cnt = 0; |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 1891 | adapter->stats.xmit_on++; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1892 | } |
| 1893 | __netif_tx_unlock(tx_ring->txq); |
| 1894 | } |
| 1895 | } |
| 1896 | /* |
| 1897 | * If everything is freed up to consumer then check if the ring is full |
| 1898 | * If the ring is full then check if more needs to be freed and |
| 1899 | * schedule the call back again. |
| 1900 | * |
| 1901 | * This happens when there are 2 CPUs. One could be freeing and the |
| 1902 | * other filling it. If the ring is full when we get out of here and |
| 1903 | * the card has already interrupted the host then the host can miss the |
| 1904 | * interrupt. |
| 1905 | * |
| 1906 | * There is still a possible race condition and the host could miss an |
| 1907 | * interrupt. The card has to take care of this. |
| 1908 | */ |
| 1909 | hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer)); |
| 1910 | done = (sw_consumer == hw_consumer); |
| 1911 | spin_unlock(&adapter->tx_clean_lock); |
| 1912 | |
| 1913 | return done; |
| 1914 | } |
| 1915 | |
| 1916 | static int qlcnic_poll(struct napi_struct *napi, int budget) |
| 1917 | { |
| 1918 | struct qlcnic_host_sds_ring *sds_ring = |
| 1919 | container_of(napi, struct qlcnic_host_sds_ring, napi); |
| 1920 | |
| 1921 | struct qlcnic_adapter *adapter = sds_ring->adapter; |
| 1922 | |
| 1923 | int tx_complete; |
| 1924 | int work_done; |
| 1925 | |
| 1926 | tx_complete = qlcnic_process_cmd_ring(adapter); |
| 1927 | |
| 1928 | work_done = qlcnic_process_rcv_ring(sds_ring, budget); |
| 1929 | |
| 1930 | if ((work_done < budget) && tx_complete) { |
| 1931 | napi_complete(&sds_ring->napi); |
| 1932 | if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) |
| 1933 | qlcnic_enable_int(sds_ring); |
| 1934 | } |
| 1935 | |
| 1936 | return work_done; |
| 1937 | } |
| 1938 | |
| 1939 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1940 | static void qlcnic_poll_controller(struct net_device *netdev) |
| 1941 | { |
| 1942 | struct qlcnic_adapter *adapter = netdev_priv(netdev); |
| 1943 | disable_irq(adapter->irq); |
| 1944 | qlcnic_intr(adapter->irq, adapter); |
| 1945 | enable_irq(adapter->irq); |
| 1946 | } |
| 1947 | #endif |
| 1948 | |
| 1949 | static void |
| 1950 | qlcnic_set_drv_state(struct qlcnic_adapter *adapter, int state) |
| 1951 | { |
| 1952 | u32 val; |
| 1953 | |
| 1954 | WARN_ON(state != QLCNIC_DEV_NEED_RESET && |
| 1955 | state != QLCNIC_DEV_NEED_QUISCENT); |
| 1956 | |
| 1957 | if (qlcnic_api_lock(adapter)) |
| 1958 | return ; |
| 1959 | |
| 1960 | val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE); |
| 1961 | |
| 1962 | if (state == QLCNIC_DEV_NEED_RESET) |
| 1963 | val |= ((u32)0x1 << (adapter->portnum * 4)); |
| 1964 | else if (state == QLCNIC_DEV_NEED_QUISCENT) |
| 1965 | val |= ((u32)0x1 << ((adapter->portnum * 4) + 1)); |
| 1966 | |
| 1967 | QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val); |
| 1968 | |
| 1969 | qlcnic_api_unlock(adapter); |
| 1970 | } |
| 1971 | |
Amit Kumar Salecha | 1b95a83 | 2010-02-01 05:24:56 +0000 | [diff] [blame] | 1972 | static int |
| 1973 | qlcnic_clr_drv_state(struct qlcnic_adapter *adapter) |
| 1974 | { |
| 1975 | u32 val; |
| 1976 | |
| 1977 | if (qlcnic_api_lock(adapter)) |
| 1978 | return -EBUSY; |
| 1979 | |
| 1980 | val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE); |
| 1981 | val &= ~((u32)0x3 << (adapter->portnum * 4)); |
| 1982 | QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val); |
| 1983 | |
| 1984 | qlcnic_api_unlock(adapter); |
| 1985 | |
| 1986 | return 0; |
| 1987 | } |
| 1988 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1989 | static void |
| 1990 | qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter) |
| 1991 | { |
| 1992 | u32 val; |
| 1993 | |
| 1994 | if (qlcnic_api_lock(adapter)) |
| 1995 | goto err; |
| 1996 | |
| 1997 | val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT); |
| 1998 | val &= ~((u32)0x1 << (adapter->portnum * 4)); |
| 1999 | QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val); |
| 2000 | |
| 2001 | if (!(val & 0x11111111)) |
| 2002 | QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD); |
| 2003 | |
| 2004 | val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE); |
| 2005 | val &= ~((u32)0x3 << (adapter->portnum * 4)); |
| 2006 | QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val); |
| 2007 | |
| 2008 | qlcnic_api_unlock(adapter); |
| 2009 | err: |
| 2010 | adapter->fw_fail_cnt = 0; |
| 2011 | clear_bit(__QLCNIC_START_FW, &adapter->state); |
| 2012 | clear_bit(__QLCNIC_RESETTING, &adapter->state); |
| 2013 | } |
| 2014 | |
| 2015 | static int |
| 2016 | qlcnic_check_drv_state(struct qlcnic_adapter *adapter) |
| 2017 | { |
| 2018 | int act, state; |
| 2019 | |
| 2020 | state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE); |
| 2021 | act = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT); |
| 2022 | |
| 2023 | if (((state & 0x11111111) == (act & 0x11111111)) || |
| 2024 | ((act & 0x11111111) == ((state >> 1) & 0x11111111))) |
| 2025 | return 0; |
| 2026 | else |
| 2027 | return 1; |
| 2028 | } |
| 2029 | |
| 2030 | static int |
| 2031 | qlcnic_can_start_firmware(struct qlcnic_adapter *adapter) |
| 2032 | { |
| 2033 | u32 val, prev_state; |
Sucheta Chakraborty | aa5e18c | 2010-04-01 19:01:32 +0000 | [diff] [blame] | 2034 | u8 dev_init_timeo = adapter->dev_init_timeo; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2035 | int portnum = adapter->portnum; |
| 2036 | |
| 2037 | if (qlcnic_api_lock(adapter)) |
| 2038 | return -1; |
| 2039 | |
| 2040 | val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT); |
| 2041 | if (!(val & ((int)0x1 << (portnum * 4)))) { |
| 2042 | val |= ((u32)0x1 << (portnum * 4)); |
| 2043 | QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val); |
| 2044 | } else if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state)) { |
| 2045 | goto start_fw; |
| 2046 | } |
| 2047 | |
| 2048 | prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE); |
Amit Kumar Salecha | 65b5b42 | 2010-04-01 19:01:33 +0000 | [diff] [blame] | 2049 | QLCDB(adapter, HW, "Device state = %u\n", prev_state); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2050 | |
| 2051 | switch (prev_state) { |
| 2052 | case QLCNIC_DEV_COLD: |
| 2053 | start_fw: |
| 2054 | QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITALIZING); |
| 2055 | qlcnic_api_unlock(adapter); |
| 2056 | return 1; |
| 2057 | |
| 2058 | case QLCNIC_DEV_READY: |
| 2059 | qlcnic_api_unlock(adapter); |
| 2060 | return 0; |
| 2061 | |
| 2062 | case QLCNIC_DEV_NEED_RESET: |
| 2063 | val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE); |
| 2064 | val |= ((u32)0x1 << (portnum * 4)); |
| 2065 | QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val); |
| 2066 | break; |
| 2067 | |
| 2068 | case QLCNIC_DEV_NEED_QUISCENT: |
| 2069 | val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE); |
| 2070 | val |= ((u32)0x1 << ((portnum * 4) + 1)); |
| 2071 | QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val); |
| 2072 | break; |
| 2073 | |
| 2074 | case QLCNIC_DEV_FAILED: |
| 2075 | qlcnic_api_unlock(adapter); |
| 2076 | return -1; |
| 2077 | } |
| 2078 | |
| 2079 | qlcnic_api_unlock(adapter); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2080 | |
Sucheta Chakraborty | aa5e18c | 2010-04-01 19:01:32 +0000 | [diff] [blame] | 2081 | do { |
| 2082 | msleep(1000); |
| 2083 | } while ((QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) != QLCNIC_DEV_READY) |
| 2084 | && --dev_init_timeo); |
| 2085 | |
Amit Kumar Salecha | 65b5b42 | 2010-04-01 19:01:33 +0000 | [diff] [blame] | 2086 | if (!dev_init_timeo) { |
| 2087 | dev_err(&adapter->pdev->dev, |
| 2088 | "Waiting for device to initialize timeout\n"); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2089 | return -1; |
Amit Kumar Salecha | 65b5b42 | 2010-04-01 19:01:33 +0000 | [diff] [blame] | 2090 | } |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2091 | |
| 2092 | if (qlcnic_api_lock(adapter)) |
| 2093 | return -1; |
| 2094 | |
| 2095 | val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE); |
| 2096 | val &= ~((u32)0x3 << (portnum * 4)); |
| 2097 | QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val); |
| 2098 | |
| 2099 | qlcnic_api_unlock(adapter); |
| 2100 | |
| 2101 | return 0; |
| 2102 | } |
| 2103 | |
| 2104 | static void |
| 2105 | qlcnic_fwinit_work(struct work_struct *work) |
| 2106 | { |
| 2107 | struct qlcnic_adapter *adapter = container_of(work, |
| 2108 | struct qlcnic_adapter, fw_work.work); |
| 2109 | int dev_state; |
| 2110 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2111 | if (test_bit(__QLCNIC_START_FW, &adapter->state)) { |
| 2112 | |
Sucheta Chakraborty | aa5e18c | 2010-04-01 19:01:32 +0000 | [diff] [blame] | 2113 | if (qlcnic_check_drv_state(adapter) && |
| 2114 | (adapter->fw_wait_cnt++ < adapter->reset_ack_timeo)) { |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2115 | qlcnic_schedule_work(adapter, |
| 2116 | qlcnic_fwinit_work, FW_POLL_DELAY); |
| 2117 | return; |
| 2118 | } |
| 2119 | |
Amit Kumar Salecha | 65b5b42 | 2010-04-01 19:01:33 +0000 | [diff] [blame] | 2120 | QLCDB(adapter, DRV, "Resetting FW\n"); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2121 | if (!qlcnic_start_firmware(adapter)) { |
| 2122 | qlcnic_schedule_work(adapter, qlcnic_attach_work, 0); |
| 2123 | return; |
| 2124 | } |
| 2125 | |
| 2126 | goto err_ret; |
| 2127 | } |
| 2128 | |
Amit Kumar Salecha | 65b5b42 | 2010-04-01 19:01:33 +0000 | [diff] [blame] | 2129 | if (adapter->fw_wait_cnt++ > (adapter->dev_init_timeo / 2)) { |
| 2130 | dev_err(&adapter->pdev->dev, |
| 2131 | "Waiting for device to reset timeout\n"); |
Sucheta Chakraborty | aa5e18c | 2010-04-01 19:01:32 +0000 | [diff] [blame] | 2132 | goto err_ret; |
Amit Kumar Salecha | 65b5b42 | 2010-04-01 19:01:33 +0000 | [diff] [blame] | 2133 | } |
Sucheta Chakraborty | aa5e18c | 2010-04-01 19:01:32 +0000 | [diff] [blame] | 2134 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2135 | dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE); |
Amit Kumar Salecha | 65b5b42 | 2010-04-01 19:01:33 +0000 | [diff] [blame] | 2136 | QLCDB(adapter, HW, "Func waiting: Device state=%d\n", dev_state); |
| 2137 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2138 | switch (dev_state) { |
| 2139 | case QLCNIC_DEV_READY: |
| 2140 | if (!qlcnic_start_firmware(adapter)) { |
| 2141 | qlcnic_schedule_work(adapter, qlcnic_attach_work, 0); |
| 2142 | return; |
| 2143 | } |
| 2144 | case QLCNIC_DEV_FAILED: |
| 2145 | break; |
| 2146 | |
| 2147 | default: |
| 2148 | qlcnic_schedule_work(adapter, |
| 2149 | qlcnic_fwinit_work, 2 * FW_POLL_DELAY); |
| 2150 | return; |
| 2151 | } |
| 2152 | |
| 2153 | err_ret: |
Amit Kumar Salecha | 34ce362 | 2010-04-01 19:01:34 +0000 | [diff] [blame] | 2154 | netif_device_attach(adapter->netdev); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2155 | qlcnic_clr_all_drv_state(adapter); |
| 2156 | } |
| 2157 | |
| 2158 | static void |
| 2159 | qlcnic_detach_work(struct work_struct *work) |
| 2160 | { |
| 2161 | struct qlcnic_adapter *adapter = container_of(work, |
| 2162 | struct qlcnic_adapter, fw_work.work); |
| 2163 | struct net_device *netdev = adapter->netdev; |
| 2164 | u32 status; |
| 2165 | |
| 2166 | netif_device_detach(netdev); |
| 2167 | |
| 2168 | qlcnic_down(adapter, netdev); |
| 2169 | |
Amit Kumar Salecha | ce66844 | 2010-02-01 05:24:57 +0000 | [diff] [blame] | 2170 | rtnl_lock(); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2171 | qlcnic_detach(adapter); |
Amit Kumar Salecha | ce66844 | 2010-02-01 05:24:57 +0000 | [diff] [blame] | 2172 | rtnl_unlock(); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2173 | |
| 2174 | status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1); |
| 2175 | |
| 2176 | if (status & QLCNIC_RCODE_FATAL_ERROR) |
| 2177 | goto err_ret; |
| 2178 | |
| 2179 | if (adapter->temp == QLCNIC_TEMP_PANIC) |
| 2180 | goto err_ret; |
| 2181 | |
| 2182 | qlcnic_set_drv_state(adapter, adapter->dev_state); |
| 2183 | |
| 2184 | adapter->fw_wait_cnt = 0; |
| 2185 | |
| 2186 | qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY); |
| 2187 | |
| 2188 | return; |
| 2189 | |
| 2190 | err_ret: |
Amit Kumar Salecha | 65b5b42 | 2010-04-01 19:01:33 +0000 | [diff] [blame] | 2191 | dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n", |
| 2192 | status, adapter->temp); |
Amit Kumar Salecha | 34ce362 | 2010-04-01 19:01:34 +0000 | [diff] [blame] | 2193 | netif_device_attach(netdev); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2194 | qlcnic_clr_all_drv_state(adapter); |
| 2195 | |
| 2196 | } |
| 2197 | |
| 2198 | static void |
| 2199 | qlcnic_dev_request_reset(struct qlcnic_adapter *adapter) |
| 2200 | { |
| 2201 | u32 state; |
| 2202 | |
| 2203 | if (qlcnic_api_lock(adapter)) |
| 2204 | return; |
| 2205 | |
| 2206 | state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE); |
| 2207 | |
| 2208 | if (state != QLCNIC_DEV_INITALIZING && state != QLCNIC_DEV_NEED_RESET) { |
| 2209 | QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET); |
| 2210 | set_bit(__QLCNIC_START_FW, &adapter->state); |
Amit Kumar Salecha | 65b5b42 | 2010-04-01 19:01:33 +0000 | [diff] [blame] | 2211 | QLCDB(adapter, DRV, "NEED_RESET state set\n"); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | qlcnic_api_unlock(adapter); |
| 2215 | } |
| 2216 | |
| 2217 | static void |
| 2218 | qlcnic_schedule_work(struct qlcnic_adapter *adapter, |
| 2219 | work_func_t func, int delay) |
| 2220 | { |
| 2221 | INIT_DELAYED_WORK(&adapter->fw_work, func); |
| 2222 | schedule_delayed_work(&adapter->fw_work, round_jiffies_relative(delay)); |
| 2223 | } |
| 2224 | |
| 2225 | static void |
| 2226 | qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter) |
| 2227 | { |
| 2228 | while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) |
| 2229 | msleep(10); |
| 2230 | |
| 2231 | cancel_delayed_work_sync(&adapter->fw_work); |
| 2232 | } |
| 2233 | |
| 2234 | static void |
| 2235 | qlcnic_attach_work(struct work_struct *work) |
| 2236 | { |
| 2237 | struct qlcnic_adapter *adapter = container_of(work, |
| 2238 | struct qlcnic_adapter, fw_work.work); |
| 2239 | struct net_device *netdev = adapter->netdev; |
| 2240 | int err; |
| 2241 | |
| 2242 | if (netif_running(netdev)) { |
| 2243 | err = qlcnic_attach(adapter); |
| 2244 | if (err) |
| 2245 | goto done; |
| 2246 | |
| 2247 | err = qlcnic_up(adapter, netdev); |
| 2248 | if (err) { |
| 2249 | qlcnic_detach(adapter); |
| 2250 | goto done; |
| 2251 | } |
| 2252 | |
| 2253 | qlcnic_config_indev_addr(netdev, NETDEV_UP); |
| 2254 | } |
| 2255 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2256 | done: |
Amit Kumar Salecha | 34ce362 | 2010-04-01 19:01:34 +0000 | [diff] [blame] | 2257 | netif_device_attach(netdev); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2258 | adapter->fw_fail_cnt = 0; |
| 2259 | clear_bit(__QLCNIC_RESETTING, &adapter->state); |
Amit Kumar Salecha | 1b95a83 | 2010-02-01 05:24:56 +0000 | [diff] [blame] | 2260 | |
| 2261 | if (!qlcnic_clr_drv_state(adapter)) |
| 2262 | qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, |
| 2263 | FW_POLL_DELAY); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | static int |
| 2267 | qlcnic_check_health(struct qlcnic_adapter *adapter) |
| 2268 | { |
| 2269 | u32 state = 0, heartbit; |
| 2270 | struct net_device *netdev = adapter->netdev; |
| 2271 | |
| 2272 | if (qlcnic_check_temp(adapter)) |
| 2273 | goto detach; |
| 2274 | |
| 2275 | if (adapter->need_fw_reset) { |
| 2276 | qlcnic_dev_request_reset(adapter); |
| 2277 | goto detach; |
| 2278 | } |
| 2279 | |
| 2280 | state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE); |
| 2281 | if (state == QLCNIC_DEV_NEED_RESET || state == QLCNIC_DEV_NEED_QUISCENT) |
| 2282 | adapter->need_fw_reset = 1; |
| 2283 | |
| 2284 | heartbit = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER); |
| 2285 | if (heartbit != adapter->heartbit) { |
| 2286 | adapter->heartbit = heartbit; |
| 2287 | adapter->fw_fail_cnt = 0; |
| 2288 | if (adapter->need_fw_reset) |
| 2289 | goto detach; |
| 2290 | return 0; |
| 2291 | } |
| 2292 | |
| 2293 | if (++adapter->fw_fail_cnt < FW_FAIL_THRESH) |
| 2294 | return 0; |
| 2295 | |
| 2296 | qlcnic_dev_request_reset(adapter); |
| 2297 | |
| 2298 | clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state); |
| 2299 | |
| 2300 | dev_info(&netdev->dev, "firmware hang detected\n"); |
| 2301 | |
| 2302 | detach: |
| 2303 | adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state : |
| 2304 | QLCNIC_DEV_NEED_RESET; |
| 2305 | |
| 2306 | if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) && |
Amit Kumar Salecha | 65b5b42 | 2010-04-01 19:01:33 +0000 | [diff] [blame] | 2307 | !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) { |
| 2308 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2309 | qlcnic_schedule_work(adapter, qlcnic_detach_work, 0); |
Amit Kumar Salecha | 65b5b42 | 2010-04-01 19:01:33 +0000 | [diff] [blame] | 2310 | QLCDB(adapter, DRV, "fw recovery scheduled.\n"); |
| 2311 | } |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2312 | |
| 2313 | return 1; |
| 2314 | } |
| 2315 | |
| 2316 | static void |
| 2317 | qlcnic_fw_poll_work(struct work_struct *work) |
| 2318 | { |
| 2319 | struct qlcnic_adapter *adapter = container_of(work, |
| 2320 | struct qlcnic_adapter, fw_work.work); |
| 2321 | |
| 2322 | if (test_bit(__QLCNIC_RESETTING, &adapter->state)) |
| 2323 | goto reschedule; |
| 2324 | |
| 2325 | |
| 2326 | if (qlcnic_check_health(adapter)) |
| 2327 | return; |
| 2328 | |
| 2329 | reschedule: |
| 2330 | qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY); |
| 2331 | } |
| 2332 | |
| 2333 | static ssize_t |
| 2334 | qlcnic_store_bridged_mode(struct device *dev, |
| 2335 | struct device_attribute *attr, const char *buf, size_t len) |
| 2336 | { |
| 2337 | struct qlcnic_adapter *adapter = dev_get_drvdata(dev); |
| 2338 | unsigned long new; |
| 2339 | int ret = -EINVAL; |
| 2340 | |
| 2341 | if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)) |
| 2342 | goto err_out; |
| 2343 | |
| 2344 | if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC) |
| 2345 | goto err_out; |
| 2346 | |
| 2347 | if (strict_strtoul(buf, 2, &new)) |
| 2348 | goto err_out; |
| 2349 | |
| 2350 | if (!qlcnic_config_bridged_mode(adapter, !!new)) |
| 2351 | ret = len; |
| 2352 | |
| 2353 | err_out: |
| 2354 | return ret; |
| 2355 | } |
| 2356 | |
| 2357 | static ssize_t |
| 2358 | qlcnic_show_bridged_mode(struct device *dev, |
| 2359 | struct device_attribute *attr, char *buf) |
| 2360 | { |
| 2361 | struct qlcnic_adapter *adapter = dev_get_drvdata(dev); |
| 2362 | int bridged_mode = 0; |
| 2363 | |
| 2364 | if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG) |
| 2365 | bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED); |
| 2366 | |
| 2367 | return sprintf(buf, "%d\n", bridged_mode); |
| 2368 | } |
| 2369 | |
| 2370 | static struct device_attribute dev_attr_bridged_mode = { |
| 2371 | .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)}, |
| 2372 | .show = qlcnic_show_bridged_mode, |
| 2373 | .store = qlcnic_store_bridged_mode, |
| 2374 | }; |
| 2375 | |
| 2376 | static ssize_t |
| 2377 | qlcnic_store_diag_mode(struct device *dev, |
| 2378 | struct device_attribute *attr, const char *buf, size_t len) |
| 2379 | { |
| 2380 | struct qlcnic_adapter *adapter = dev_get_drvdata(dev); |
| 2381 | unsigned long new; |
| 2382 | |
| 2383 | if (strict_strtoul(buf, 2, &new)) |
| 2384 | return -EINVAL; |
| 2385 | |
| 2386 | if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED)) |
| 2387 | adapter->flags ^= QLCNIC_DIAG_ENABLED; |
| 2388 | |
| 2389 | return len; |
| 2390 | } |
| 2391 | |
| 2392 | static ssize_t |
| 2393 | qlcnic_show_diag_mode(struct device *dev, |
| 2394 | struct device_attribute *attr, char *buf) |
| 2395 | { |
| 2396 | struct qlcnic_adapter *adapter = dev_get_drvdata(dev); |
| 2397 | |
| 2398 | return sprintf(buf, "%d\n", |
| 2399 | !!(adapter->flags & QLCNIC_DIAG_ENABLED)); |
| 2400 | } |
| 2401 | |
| 2402 | static struct device_attribute dev_attr_diag_mode = { |
| 2403 | .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)}, |
| 2404 | .show = qlcnic_show_diag_mode, |
| 2405 | .store = qlcnic_store_diag_mode, |
| 2406 | }; |
| 2407 | |
| 2408 | static int |
| 2409 | qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter, |
| 2410 | loff_t offset, size_t size) |
| 2411 | { |
Dhananjay Phadke | 897e8c7 | 2010-04-01 19:01:29 +0000 | [diff] [blame] | 2412 | size_t crb_size = 4; |
| 2413 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2414 | if (!(adapter->flags & QLCNIC_DIAG_ENABLED)) |
| 2415 | return -EIO; |
| 2416 | |
Dhananjay Phadke | 897e8c7 | 2010-04-01 19:01:29 +0000 | [diff] [blame] | 2417 | if (offset < QLCNIC_PCI_CRBSPACE) { |
| 2418 | if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, |
| 2419 | QLCNIC_PCI_CAMQM_END)) |
| 2420 | crb_size = 8; |
| 2421 | else |
| 2422 | return -EINVAL; |
| 2423 | } |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2424 | |
Dhananjay Phadke | 897e8c7 | 2010-04-01 19:01:29 +0000 | [diff] [blame] | 2425 | if ((size != crb_size) || (offset & (crb_size-1))) |
| 2426 | return -EINVAL; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2427 | |
| 2428 | return 0; |
| 2429 | } |
| 2430 | |
| 2431 | static ssize_t |
| 2432 | qlcnic_sysfs_read_crb(struct kobject *kobj, struct bin_attribute *attr, |
| 2433 | char *buf, loff_t offset, size_t size) |
| 2434 | { |
| 2435 | struct device *dev = container_of(kobj, struct device, kobj); |
| 2436 | struct qlcnic_adapter *adapter = dev_get_drvdata(dev); |
| 2437 | u32 data; |
Dhananjay Phadke | 897e8c7 | 2010-04-01 19:01:29 +0000 | [diff] [blame] | 2438 | u64 qmdata; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2439 | int ret; |
| 2440 | |
| 2441 | ret = qlcnic_sysfs_validate_crb(adapter, offset, size); |
| 2442 | if (ret != 0) |
| 2443 | return ret; |
| 2444 | |
Dhananjay Phadke | 897e8c7 | 2010-04-01 19:01:29 +0000 | [diff] [blame] | 2445 | if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) { |
| 2446 | qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata); |
| 2447 | memcpy(buf, &qmdata, size); |
| 2448 | } else { |
| 2449 | data = QLCRD32(adapter, offset); |
| 2450 | memcpy(buf, &data, size); |
| 2451 | } |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2452 | return size; |
| 2453 | } |
| 2454 | |
| 2455 | static ssize_t |
| 2456 | qlcnic_sysfs_write_crb(struct kobject *kobj, struct bin_attribute *attr, |
| 2457 | char *buf, loff_t offset, size_t size) |
| 2458 | { |
| 2459 | struct device *dev = container_of(kobj, struct device, kobj); |
| 2460 | struct qlcnic_adapter *adapter = dev_get_drvdata(dev); |
| 2461 | u32 data; |
Dhananjay Phadke | 897e8c7 | 2010-04-01 19:01:29 +0000 | [diff] [blame] | 2462 | u64 qmdata; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2463 | int ret; |
| 2464 | |
| 2465 | ret = qlcnic_sysfs_validate_crb(adapter, offset, size); |
| 2466 | if (ret != 0) |
| 2467 | return ret; |
| 2468 | |
Dhananjay Phadke | 897e8c7 | 2010-04-01 19:01:29 +0000 | [diff] [blame] | 2469 | if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) { |
| 2470 | memcpy(&qmdata, buf, size); |
| 2471 | qlcnic_pci_camqm_write_2M(adapter, offset, qmdata); |
| 2472 | } else { |
| 2473 | memcpy(&data, buf, size); |
| 2474 | QLCWR32(adapter, offset, data); |
| 2475 | } |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 2476 | return size; |
| 2477 | } |
| 2478 | |
| 2479 | static int |
| 2480 | qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter, |
| 2481 | loff_t offset, size_t size) |
| 2482 | { |
| 2483 | if (!(adapter->flags & QLCNIC_DIAG_ENABLED)) |
| 2484 | return -EIO; |
| 2485 | |
| 2486 | if ((size != 8) || (offset & 0x7)) |
| 2487 | return -EIO; |
| 2488 | |
| 2489 | return 0; |
| 2490 | } |
| 2491 | |
| 2492 | static ssize_t |
| 2493 | qlcnic_sysfs_read_mem(struct kobject *kobj, struct bin_attribute *attr, |
| 2494 | char *buf, loff_t offset, size_t size) |
| 2495 | { |
| 2496 | struct device *dev = container_of(kobj, struct device, kobj); |
| 2497 | struct qlcnic_adapter *adapter = dev_get_drvdata(dev); |
| 2498 | u64 data; |
| 2499 | int ret; |
| 2500 | |
| 2501 | ret = qlcnic_sysfs_validate_mem(adapter, offset, size); |
| 2502 | if (ret != 0) |
| 2503 | return ret; |
| 2504 | |
| 2505 | if (qlcnic_pci_mem_read_2M(adapter, offset, &data)) |
| 2506 | return -EIO; |
| 2507 | |
| 2508 | memcpy(buf, &data, size); |
| 2509 | |
| 2510 | return size; |
| 2511 | } |
| 2512 | |
| 2513 | static ssize_t |
| 2514 | qlcnic_sysfs_write_mem(struct kobject *kobj, struct bin_attribute *attr, |
| 2515 | char *buf, loff_t offset, size_t size) |
| 2516 | { |
| 2517 | struct device *dev = container_of(kobj, struct device, kobj); |
| 2518 | struct qlcnic_adapter *adapter = dev_get_drvdata(dev); |
| 2519 | u64 data; |
| 2520 | int ret; |
| 2521 | |
| 2522 | ret = qlcnic_sysfs_validate_mem(adapter, offset, size); |
| 2523 | if (ret != 0) |
| 2524 | return ret; |
| 2525 | |
| 2526 | memcpy(&data, buf, size); |
| 2527 | |
| 2528 | if (qlcnic_pci_mem_write_2M(adapter, offset, data)) |
| 2529 | return -EIO; |
| 2530 | |
| 2531 | return size; |
| 2532 | } |
| 2533 | |
| 2534 | |
| 2535 | static struct bin_attribute bin_attr_crb = { |
| 2536 | .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)}, |
| 2537 | .size = 0, |
| 2538 | .read = qlcnic_sysfs_read_crb, |
| 2539 | .write = qlcnic_sysfs_write_crb, |
| 2540 | }; |
| 2541 | |
| 2542 | static struct bin_attribute bin_attr_mem = { |
| 2543 | .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)}, |
| 2544 | .size = 0, |
| 2545 | .read = qlcnic_sysfs_read_mem, |
| 2546 | .write = qlcnic_sysfs_write_mem, |
| 2547 | }; |
| 2548 | |
| 2549 | static void |
| 2550 | qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter) |
| 2551 | { |
| 2552 | struct device *dev = &adapter->pdev->dev; |
| 2553 | |
| 2554 | if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG) |
| 2555 | if (device_create_file(dev, &dev_attr_bridged_mode)) |
| 2556 | dev_warn(dev, |
| 2557 | "failed to create bridged_mode sysfs entry\n"); |
| 2558 | } |
| 2559 | |
| 2560 | static void |
| 2561 | qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter) |
| 2562 | { |
| 2563 | struct device *dev = &adapter->pdev->dev; |
| 2564 | |
| 2565 | if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG) |
| 2566 | device_remove_file(dev, &dev_attr_bridged_mode); |
| 2567 | } |
| 2568 | |
| 2569 | static void |
| 2570 | qlcnic_create_diag_entries(struct qlcnic_adapter *adapter) |
| 2571 | { |
| 2572 | struct device *dev = &adapter->pdev->dev; |
| 2573 | |
| 2574 | if (device_create_file(dev, &dev_attr_diag_mode)) |
| 2575 | dev_info(dev, "failed to create diag_mode sysfs entry\n"); |
| 2576 | if (device_create_bin_file(dev, &bin_attr_crb)) |
| 2577 | dev_info(dev, "failed to create crb sysfs entry\n"); |
| 2578 | if (device_create_bin_file(dev, &bin_attr_mem)) |
| 2579 | dev_info(dev, "failed to create mem sysfs entry\n"); |
| 2580 | } |
| 2581 | |
| 2582 | |
| 2583 | static void |
| 2584 | qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter) |
| 2585 | { |
| 2586 | struct device *dev = &adapter->pdev->dev; |
| 2587 | |
| 2588 | device_remove_file(dev, &dev_attr_diag_mode); |
| 2589 | device_remove_bin_file(dev, &bin_attr_crb); |
| 2590 | device_remove_bin_file(dev, &bin_attr_mem); |
| 2591 | } |
| 2592 | |
| 2593 | #ifdef CONFIG_INET |
| 2594 | |
| 2595 | #define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops) |
| 2596 | |
| 2597 | static int |
| 2598 | qlcnic_destip_supported(struct qlcnic_adapter *adapter) |
| 2599 | { |
| 2600 | if (adapter->ahw.cut_through) |
| 2601 | return 0; |
| 2602 | |
| 2603 | return 1; |
| 2604 | } |
| 2605 | |
| 2606 | static void |
| 2607 | qlcnic_config_indev_addr(struct net_device *dev, unsigned long event) |
| 2608 | { |
| 2609 | struct in_device *indev; |
| 2610 | struct qlcnic_adapter *adapter = netdev_priv(dev); |
| 2611 | |
| 2612 | if (!qlcnic_destip_supported(adapter)) |
| 2613 | return; |
| 2614 | |
| 2615 | indev = in_dev_get(dev); |
| 2616 | if (!indev) |
| 2617 | return; |
| 2618 | |
| 2619 | for_ifa(indev) { |
| 2620 | switch (event) { |
| 2621 | case NETDEV_UP: |
| 2622 | qlcnic_config_ipaddr(adapter, |
| 2623 | ifa->ifa_address, QLCNIC_IP_UP); |
| 2624 | break; |
| 2625 | case NETDEV_DOWN: |
| 2626 | qlcnic_config_ipaddr(adapter, |
| 2627 | ifa->ifa_address, QLCNIC_IP_DOWN); |
| 2628 | break; |
| 2629 | default: |
| 2630 | break; |
| 2631 | } |
| 2632 | } endfor_ifa(indev); |
| 2633 | |
| 2634 | in_dev_put(indev); |
| 2635 | return; |
| 2636 | } |
| 2637 | |
| 2638 | static int qlcnic_netdev_event(struct notifier_block *this, |
| 2639 | unsigned long event, void *ptr) |
| 2640 | { |
| 2641 | struct qlcnic_adapter *adapter; |
| 2642 | struct net_device *dev = (struct net_device *)ptr; |
| 2643 | |
| 2644 | recheck: |
| 2645 | if (dev == NULL) |
| 2646 | goto done; |
| 2647 | |
| 2648 | if (dev->priv_flags & IFF_802_1Q_VLAN) { |
| 2649 | dev = vlan_dev_real_dev(dev); |
| 2650 | goto recheck; |
| 2651 | } |
| 2652 | |
| 2653 | if (!is_qlcnic_netdev(dev)) |
| 2654 | goto done; |
| 2655 | |
| 2656 | adapter = netdev_priv(dev); |
| 2657 | |
| 2658 | if (!adapter) |
| 2659 | goto done; |
| 2660 | |
| 2661 | if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC) |
| 2662 | goto done; |
| 2663 | |
| 2664 | qlcnic_config_indev_addr(dev, event); |
| 2665 | done: |
| 2666 | return NOTIFY_DONE; |
| 2667 | } |
| 2668 | |
| 2669 | static int |
| 2670 | qlcnic_inetaddr_event(struct notifier_block *this, |
| 2671 | unsigned long event, void *ptr) |
| 2672 | { |
| 2673 | struct qlcnic_adapter *adapter; |
| 2674 | struct net_device *dev; |
| 2675 | |
| 2676 | struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; |
| 2677 | |
| 2678 | dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL; |
| 2679 | |
| 2680 | recheck: |
| 2681 | if (dev == NULL || !netif_running(dev)) |
| 2682 | goto done; |
| 2683 | |
| 2684 | if (dev->priv_flags & IFF_802_1Q_VLAN) { |
| 2685 | dev = vlan_dev_real_dev(dev); |
| 2686 | goto recheck; |
| 2687 | } |
| 2688 | |
| 2689 | if (!is_qlcnic_netdev(dev)) |
| 2690 | goto done; |
| 2691 | |
| 2692 | adapter = netdev_priv(dev); |
| 2693 | |
| 2694 | if (!adapter || !qlcnic_destip_supported(adapter)) |
| 2695 | goto done; |
| 2696 | |
| 2697 | if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC) |
| 2698 | goto done; |
| 2699 | |
| 2700 | switch (event) { |
| 2701 | case NETDEV_UP: |
| 2702 | qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP); |
| 2703 | break; |
| 2704 | case NETDEV_DOWN: |
| 2705 | qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN); |
| 2706 | break; |
| 2707 | default: |
| 2708 | break; |
| 2709 | } |
| 2710 | |
| 2711 | done: |
| 2712 | return NOTIFY_DONE; |
| 2713 | } |
| 2714 | |
| 2715 | static struct notifier_block qlcnic_netdev_cb = { |
| 2716 | .notifier_call = qlcnic_netdev_event, |
| 2717 | }; |
| 2718 | |
| 2719 | static struct notifier_block qlcnic_inetaddr_cb = { |
| 2720 | .notifier_call = qlcnic_inetaddr_event, |
| 2721 | }; |
| 2722 | #else |
| 2723 | static void |
| 2724 | qlcnic_config_indev_addr(struct net_device *dev, unsigned long event) |
| 2725 | { } |
| 2726 | #endif |
| 2727 | |
| 2728 | static struct pci_driver qlcnic_driver = { |
| 2729 | .name = qlcnic_driver_name, |
| 2730 | .id_table = qlcnic_pci_tbl, |
| 2731 | .probe = qlcnic_probe, |
| 2732 | .remove = __devexit_p(qlcnic_remove), |
| 2733 | #ifdef CONFIG_PM |
| 2734 | .suspend = qlcnic_suspend, |
| 2735 | .resume = qlcnic_resume, |
| 2736 | #endif |
| 2737 | .shutdown = qlcnic_shutdown |
| 2738 | }; |
| 2739 | |
| 2740 | static int __init qlcnic_init_module(void) |
| 2741 | { |
| 2742 | |
| 2743 | printk(KERN_INFO "%s\n", qlcnic_driver_string); |
| 2744 | |
| 2745 | #ifdef CONFIG_INET |
| 2746 | register_netdevice_notifier(&qlcnic_netdev_cb); |
| 2747 | register_inetaddr_notifier(&qlcnic_inetaddr_cb); |
| 2748 | #endif |
| 2749 | |
| 2750 | |
| 2751 | return pci_register_driver(&qlcnic_driver); |
| 2752 | } |
| 2753 | |
| 2754 | module_init(qlcnic_init_module); |
| 2755 | |
| 2756 | static void __exit qlcnic_exit_module(void) |
| 2757 | { |
| 2758 | |
| 2759 | pci_unregister_driver(&qlcnic_driver); |
| 2760 | |
| 2761 | #ifdef CONFIG_INET |
| 2762 | unregister_inetaddr_notifier(&qlcnic_inetaddr_cb); |
| 2763 | unregister_netdevice_notifier(&qlcnic_netdev_cb); |
| 2764 | #endif |
| 2765 | } |
| 2766 | |
| 2767 | module_exit(qlcnic_exit_module); |