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/netdevice.h> |
| 26 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 28 | #include "qlcnic.h" |
| 29 | |
| 30 | struct crb_addr_pair { |
| 31 | u32 addr; |
| 32 | u32 data; |
| 33 | }; |
| 34 | |
| 35 | #define QLCNIC_MAX_CRB_XFORM 60 |
| 36 | static unsigned int crb_addr_xform[QLCNIC_MAX_CRB_XFORM]; |
| 37 | |
| 38 | #define crb_addr_transform(name) \ |
| 39 | (crb_addr_xform[QLCNIC_HW_PX_MAP_CRB_##name] = \ |
| 40 | QLCNIC_HW_CRB_HUB_AGT_ADR_##name << 20) |
| 41 | |
| 42 | #define QLCNIC_ADDR_ERROR (0xffffffff) |
| 43 | |
| 44 | static void |
| 45 | qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter, |
| 46 | struct qlcnic_host_rds_ring *rds_ring); |
| 47 | |
| 48 | static void crb_addr_transform_setup(void) |
| 49 | { |
| 50 | crb_addr_transform(XDMA); |
| 51 | crb_addr_transform(TIMR); |
| 52 | crb_addr_transform(SRE); |
| 53 | crb_addr_transform(SQN3); |
| 54 | crb_addr_transform(SQN2); |
| 55 | crb_addr_transform(SQN1); |
| 56 | crb_addr_transform(SQN0); |
| 57 | crb_addr_transform(SQS3); |
| 58 | crb_addr_transform(SQS2); |
| 59 | crb_addr_transform(SQS1); |
| 60 | crb_addr_transform(SQS0); |
| 61 | crb_addr_transform(RPMX7); |
| 62 | crb_addr_transform(RPMX6); |
| 63 | crb_addr_transform(RPMX5); |
| 64 | crb_addr_transform(RPMX4); |
| 65 | crb_addr_transform(RPMX3); |
| 66 | crb_addr_transform(RPMX2); |
| 67 | crb_addr_transform(RPMX1); |
| 68 | crb_addr_transform(RPMX0); |
| 69 | crb_addr_transform(ROMUSB); |
| 70 | crb_addr_transform(SN); |
| 71 | crb_addr_transform(QMN); |
| 72 | crb_addr_transform(QMS); |
| 73 | crb_addr_transform(PGNI); |
| 74 | crb_addr_transform(PGND); |
| 75 | crb_addr_transform(PGN3); |
| 76 | crb_addr_transform(PGN2); |
| 77 | crb_addr_transform(PGN1); |
| 78 | crb_addr_transform(PGN0); |
| 79 | crb_addr_transform(PGSI); |
| 80 | crb_addr_transform(PGSD); |
| 81 | crb_addr_transform(PGS3); |
| 82 | crb_addr_transform(PGS2); |
| 83 | crb_addr_transform(PGS1); |
| 84 | crb_addr_transform(PGS0); |
| 85 | crb_addr_transform(PS); |
| 86 | crb_addr_transform(PH); |
| 87 | crb_addr_transform(NIU); |
| 88 | crb_addr_transform(I2Q); |
| 89 | crb_addr_transform(EG); |
| 90 | crb_addr_transform(MN); |
| 91 | crb_addr_transform(MS); |
| 92 | crb_addr_transform(CAS2); |
| 93 | crb_addr_transform(CAS1); |
| 94 | crb_addr_transform(CAS0); |
| 95 | crb_addr_transform(CAM); |
| 96 | crb_addr_transform(C2C1); |
| 97 | crb_addr_transform(C2C0); |
| 98 | crb_addr_transform(SMB); |
| 99 | crb_addr_transform(OCM0); |
| 100 | crb_addr_transform(I2C0); |
| 101 | } |
| 102 | |
| 103 | void qlcnic_release_rx_buffers(struct qlcnic_adapter *adapter) |
| 104 | { |
| 105 | struct qlcnic_recv_context *recv_ctx; |
| 106 | struct qlcnic_host_rds_ring *rds_ring; |
| 107 | struct qlcnic_rx_buffer *rx_buf; |
| 108 | int i, ring; |
| 109 | |
| 110 | recv_ctx = &adapter->recv_ctx; |
| 111 | for (ring = 0; ring < adapter->max_rds_rings; ring++) { |
| 112 | rds_ring = &recv_ctx->rds_rings[ring]; |
| 113 | for (i = 0; i < rds_ring->num_desc; ++i) { |
| 114 | rx_buf = &(rds_ring->rx_buf_arr[i]); |
| 115 | if (rx_buf->state == QLCNIC_BUFFER_FREE) |
| 116 | continue; |
| 117 | pci_unmap_single(adapter->pdev, |
| 118 | rx_buf->dma, |
| 119 | rds_ring->dma_size, |
| 120 | PCI_DMA_FROMDEVICE); |
| 121 | if (rx_buf->skb != NULL) |
| 122 | dev_kfree_skb_any(rx_buf->skb); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void qlcnic_release_tx_buffers(struct qlcnic_adapter *adapter) |
| 128 | { |
| 129 | struct qlcnic_cmd_buffer *cmd_buf; |
| 130 | struct qlcnic_skb_frag *buffrag; |
| 131 | int i, j; |
| 132 | struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring; |
| 133 | |
| 134 | cmd_buf = tx_ring->cmd_buf_arr; |
| 135 | for (i = 0; i < tx_ring->num_desc; i++) { |
| 136 | buffrag = cmd_buf->frag_array; |
| 137 | if (buffrag->dma) { |
| 138 | pci_unmap_single(adapter->pdev, buffrag->dma, |
| 139 | buffrag->length, PCI_DMA_TODEVICE); |
| 140 | buffrag->dma = 0ULL; |
| 141 | } |
| 142 | for (j = 0; j < cmd_buf->frag_count; j++) { |
| 143 | buffrag++; |
| 144 | if (buffrag->dma) { |
| 145 | pci_unmap_page(adapter->pdev, buffrag->dma, |
| 146 | buffrag->length, |
| 147 | PCI_DMA_TODEVICE); |
| 148 | buffrag->dma = 0ULL; |
| 149 | } |
| 150 | } |
| 151 | if (cmd_buf->skb) { |
| 152 | dev_kfree_skb_any(cmd_buf->skb); |
| 153 | cmd_buf->skb = NULL; |
| 154 | } |
| 155 | cmd_buf++; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void qlcnic_free_sw_resources(struct qlcnic_adapter *adapter) |
| 160 | { |
| 161 | struct qlcnic_recv_context *recv_ctx; |
| 162 | struct qlcnic_host_rds_ring *rds_ring; |
| 163 | struct qlcnic_host_tx_ring *tx_ring; |
| 164 | int ring; |
| 165 | |
| 166 | recv_ctx = &adapter->recv_ctx; |
| 167 | |
| 168 | if (recv_ctx->rds_rings == NULL) |
| 169 | goto skip_rds; |
| 170 | |
| 171 | for (ring = 0; ring < adapter->max_rds_rings; ring++) { |
| 172 | rds_ring = &recv_ctx->rds_rings[ring]; |
| 173 | vfree(rds_ring->rx_buf_arr); |
| 174 | rds_ring->rx_buf_arr = NULL; |
| 175 | } |
| 176 | kfree(recv_ctx->rds_rings); |
| 177 | |
| 178 | skip_rds: |
| 179 | if (adapter->tx_ring == NULL) |
| 180 | return; |
| 181 | |
| 182 | tx_ring = adapter->tx_ring; |
| 183 | vfree(tx_ring->cmd_buf_arr); |
| 184 | kfree(adapter->tx_ring); |
| 185 | } |
| 186 | |
| 187 | int qlcnic_alloc_sw_resources(struct qlcnic_adapter *adapter) |
| 188 | { |
| 189 | struct qlcnic_recv_context *recv_ctx; |
| 190 | struct qlcnic_host_rds_ring *rds_ring; |
| 191 | struct qlcnic_host_sds_ring *sds_ring; |
| 192 | struct qlcnic_host_tx_ring *tx_ring; |
| 193 | struct qlcnic_rx_buffer *rx_buf; |
| 194 | int ring, i, size; |
| 195 | |
| 196 | struct qlcnic_cmd_buffer *cmd_buf_arr; |
| 197 | struct net_device *netdev = adapter->netdev; |
| 198 | |
| 199 | size = sizeof(struct qlcnic_host_tx_ring); |
| 200 | tx_ring = kzalloc(size, GFP_KERNEL); |
| 201 | if (tx_ring == NULL) { |
| 202 | dev_err(&netdev->dev, "failed to allocate tx ring struct\n"); |
| 203 | return -ENOMEM; |
| 204 | } |
| 205 | adapter->tx_ring = tx_ring; |
| 206 | |
| 207 | tx_ring->num_desc = adapter->num_txd; |
| 208 | tx_ring->txq = netdev_get_tx_queue(netdev, 0); |
| 209 | |
| 210 | cmd_buf_arr = vmalloc(TX_BUFF_RINGSIZE(tx_ring)); |
| 211 | if (cmd_buf_arr == NULL) { |
| 212 | dev_err(&netdev->dev, "failed to allocate cmd buffer ring\n"); |
| 213 | return -ENOMEM; |
| 214 | } |
| 215 | memset(cmd_buf_arr, 0, TX_BUFF_RINGSIZE(tx_ring)); |
| 216 | tx_ring->cmd_buf_arr = cmd_buf_arr; |
| 217 | |
| 218 | recv_ctx = &adapter->recv_ctx; |
| 219 | |
| 220 | size = adapter->max_rds_rings * sizeof(struct qlcnic_host_rds_ring); |
| 221 | rds_ring = kzalloc(size, GFP_KERNEL); |
| 222 | if (rds_ring == NULL) { |
| 223 | dev_err(&netdev->dev, "failed to allocate rds ring struct\n"); |
| 224 | return -ENOMEM; |
| 225 | } |
| 226 | recv_ctx->rds_rings = rds_ring; |
| 227 | |
| 228 | for (ring = 0; ring < adapter->max_rds_rings; ring++) { |
| 229 | rds_ring = &recv_ctx->rds_rings[ring]; |
| 230 | switch (ring) { |
| 231 | case RCV_RING_NORMAL: |
| 232 | rds_ring->num_desc = adapter->num_rxd; |
Amit Kumar Salecha | 251a84c | 2010-05-13 03:07:46 +0000 | [diff] [blame^] | 233 | rds_ring->dma_size = QLCNIC_P3_RX_BUF_MAX_LEN; |
| 234 | rds_ring->skb_size = rds_ring->dma_size + NET_IP_ALIGN; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 235 | break; |
| 236 | |
| 237 | case RCV_RING_JUMBO: |
| 238 | rds_ring->num_desc = adapter->num_jumbo_rxd; |
| 239 | rds_ring->dma_size = |
| 240 | QLCNIC_P3_RX_JUMBO_BUF_MAX_LEN; |
| 241 | |
| 242 | if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO) |
| 243 | rds_ring->dma_size += QLCNIC_LRO_BUFFER_EXTRA; |
| 244 | |
| 245 | rds_ring->skb_size = |
| 246 | rds_ring->dma_size + NET_IP_ALIGN; |
| 247 | break; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 248 | } |
| 249 | rds_ring->rx_buf_arr = (struct qlcnic_rx_buffer *) |
| 250 | vmalloc(RCV_BUFF_RINGSIZE(rds_ring)); |
| 251 | if (rds_ring->rx_buf_arr == NULL) { |
| 252 | dev_err(&netdev->dev, "Failed to allocate " |
| 253 | "rx buffer ring %d\n", ring); |
| 254 | goto err_out; |
| 255 | } |
| 256 | memset(rds_ring->rx_buf_arr, 0, RCV_BUFF_RINGSIZE(rds_ring)); |
| 257 | INIT_LIST_HEAD(&rds_ring->free_list); |
| 258 | /* |
| 259 | * Now go through all of them, set reference handles |
| 260 | * and put them in the queues. |
| 261 | */ |
| 262 | rx_buf = rds_ring->rx_buf_arr; |
| 263 | for (i = 0; i < rds_ring->num_desc; i++) { |
| 264 | list_add_tail(&rx_buf->list, |
| 265 | &rds_ring->free_list); |
| 266 | rx_buf->ref_handle = i; |
| 267 | rx_buf->state = QLCNIC_BUFFER_FREE; |
| 268 | rx_buf++; |
| 269 | } |
| 270 | spin_lock_init(&rds_ring->lock); |
| 271 | } |
| 272 | |
| 273 | for (ring = 0; ring < adapter->max_sds_rings; ring++) { |
| 274 | sds_ring = &recv_ctx->sds_rings[ring]; |
| 275 | sds_ring->irq = adapter->msix_entries[ring].vector; |
| 276 | sds_ring->adapter = adapter; |
| 277 | sds_ring->num_desc = adapter->num_rxd; |
| 278 | |
| 279 | for (i = 0; i < NUM_RCV_DESC_RINGS; i++) |
| 280 | INIT_LIST_HEAD(&sds_ring->free_list[i]); |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | |
| 285 | err_out: |
| 286 | qlcnic_free_sw_resources(adapter); |
| 287 | return -ENOMEM; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Utility to translate from internal Phantom CRB address |
| 292 | * to external PCI CRB address. |
| 293 | */ |
| 294 | static u32 qlcnic_decode_crb_addr(u32 addr) |
| 295 | { |
| 296 | int i; |
| 297 | u32 base_addr, offset, pci_base; |
| 298 | |
| 299 | crb_addr_transform_setup(); |
| 300 | |
| 301 | pci_base = QLCNIC_ADDR_ERROR; |
| 302 | base_addr = addr & 0xfff00000; |
| 303 | offset = addr & 0x000fffff; |
| 304 | |
| 305 | for (i = 0; i < QLCNIC_MAX_CRB_XFORM; i++) { |
| 306 | if (crb_addr_xform[i] == base_addr) { |
| 307 | pci_base = i << 20; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | if (pci_base == QLCNIC_ADDR_ERROR) |
| 312 | return pci_base; |
| 313 | else |
| 314 | return pci_base + offset; |
| 315 | } |
| 316 | |
| 317 | #define QLCNIC_MAX_ROM_WAIT_USEC 100 |
| 318 | |
| 319 | static int qlcnic_wait_rom_done(struct qlcnic_adapter *adapter) |
| 320 | { |
| 321 | long timeout = 0; |
| 322 | long done = 0; |
| 323 | |
| 324 | cond_resched(); |
| 325 | |
| 326 | while (done == 0) { |
| 327 | done = QLCRD32(adapter, QLCNIC_ROMUSB_GLB_STATUS); |
| 328 | done &= 2; |
| 329 | if (++timeout >= QLCNIC_MAX_ROM_WAIT_USEC) { |
| 330 | dev_err(&adapter->pdev->dev, |
| 331 | "Timeout reached waiting for rom done"); |
| 332 | return -EIO; |
| 333 | } |
| 334 | udelay(1); |
| 335 | } |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static int do_rom_fast_read(struct qlcnic_adapter *adapter, |
| 340 | int addr, int *valp) |
| 341 | { |
| 342 | QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ADDRESS, addr); |
| 343 | QLCWR32(adapter, QLCNIC_ROMUSB_ROM_DUMMY_BYTE_CNT, 0); |
| 344 | QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ABYTE_CNT, 3); |
| 345 | QLCWR32(adapter, QLCNIC_ROMUSB_ROM_INSTR_OPCODE, 0xb); |
| 346 | if (qlcnic_wait_rom_done(adapter)) { |
| 347 | dev_err(&adapter->pdev->dev, "Error waiting for rom done\n"); |
| 348 | return -EIO; |
| 349 | } |
| 350 | /* reset abyte_cnt and dummy_byte_cnt */ |
| 351 | QLCWR32(adapter, QLCNIC_ROMUSB_ROM_ABYTE_CNT, 0); |
| 352 | udelay(10); |
| 353 | QLCWR32(adapter, QLCNIC_ROMUSB_ROM_DUMMY_BYTE_CNT, 0); |
| 354 | |
| 355 | *valp = QLCRD32(adapter, QLCNIC_ROMUSB_ROM_RDATA); |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static int do_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr, |
| 360 | u8 *bytes, size_t size) |
| 361 | { |
| 362 | int addridx; |
| 363 | int ret = 0; |
| 364 | |
| 365 | for (addridx = addr; addridx < (addr + size); addridx += 4) { |
| 366 | int v; |
| 367 | ret = do_rom_fast_read(adapter, addridx, &v); |
| 368 | if (ret != 0) |
| 369 | break; |
| 370 | *(__le32 *)bytes = cpu_to_le32(v); |
| 371 | bytes += 4; |
| 372 | } |
| 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | int |
| 378 | qlcnic_rom_fast_read_words(struct qlcnic_adapter *adapter, int addr, |
| 379 | u8 *bytes, size_t size) |
| 380 | { |
| 381 | int ret; |
| 382 | |
| 383 | ret = qlcnic_rom_lock(adapter); |
| 384 | if (ret < 0) |
| 385 | return ret; |
| 386 | |
| 387 | ret = do_rom_fast_read_words(adapter, addr, bytes, size); |
| 388 | |
| 389 | qlcnic_rom_unlock(adapter); |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | int qlcnic_rom_fast_read(struct qlcnic_adapter *adapter, int addr, int *valp) |
| 394 | { |
| 395 | int ret; |
| 396 | |
| 397 | if (qlcnic_rom_lock(adapter) != 0) |
| 398 | return -EIO; |
| 399 | |
| 400 | ret = do_rom_fast_read(adapter, addr, valp); |
| 401 | qlcnic_rom_unlock(adapter); |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | int qlcnic_pinit_from_rom(struct qlcnic_adapter *adapter) |
| 406 | { |
| 407 | int addr, val; |
| 408 | int i, n, init_delay; |
| 409 | struct crb_addr_pair *buf; |
| 410 | unsigned offset; |
| 411 | u32 off; |
| 412 | struct pci_dev *pdev = adapter->pdev; |
| 413 | |
| 414 | /* resetall */ |
| 415 | qlcnic_rom_lock(adapter); |
| 416 | QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0xffffffff); |
| 417 | qlcnic_rom_unlock(adapter); |
| 418 | |
| 419 | if (qlcnic_rom_fast_read(adapter, 0, &n) != 0 || (n != 0xcafecafe) || |
| 420 | qlcnic_rom_fast_read(adapter, 4, &n) != 0) { |
| 421 | dev_err(&pdev->dev, "ERROR Reading crb_init area: val:%x\n", n); |
| 422 | return -EIO; |
| 423 | } |
| 424 | offset = n & 0xffffU; |
| 425 | n = (n >> 16) & 0xffffU; |
| 426 | |
| 427 | if (n >= 1024) { |
| 428 | dev_err(&pdev->dev, "QLOGIC card flash not initialized.\n"); |
| 429 | return -EIO; |
| 430 | } |
| 431 | |
| 432 | buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL); |
| 433 | if (buf == NULL) { |
| 434 | dev_err(&pdev->dev, "Unable to calloc memory for rom read.\n"); |
| 435 | return -ENOMEM; |
| 436 | } |
| 437 | |
| 438 | for (i = 0; i < n; i++) { |
| 439 | if (qlcnic_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 || |
| 440 | qlcnic_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) { |
| 441 | kfree(buf); |
| 442 | return -EIO; |
| 443 | } |
| 444 | |
| 445 | buf[i].addr = addr; |
| 446 | buf[i].data = val; |
| 447 | } |
| 448 | |
| 449 | for (i = 0; i < n; i++) { |
| 450 | |
| 451 | off = qlcnic_decode_crb_addr(buf[i].addr); |
| 452 | if (off == QLCNIC_ADDR_ERROR) { |
| 453 | dev_err(&pdev->dev, "CRB init value out of range %x\n", |
| 454 | buf[i].addr); |
| 455 | continue; |
| 456 | } |
| 457 | off += QLCNIC_PCI_CRBSPACE; |
| 458 | |
| 459 | if (off & 1) |
| 460 | continue; |
| 461 | |
| 462 | /* skipping cold reboot MAGIC */ |
| 463 | if (off == QLCNIC_CAM_RAM(0x1fc)) |
| 464 | continue; |
| 465 | if (off == (QLCNIC_CRB_I2C0 + 0x1c)) |
| 466 | continue; |
| 467 | if (off == (ROMUSB_GLB + 0xbc)) /* do not reset PCI */ |
| 468 | continue; |
| 469 | if (off == (ROMUSB_GLB + 0xa8)) |
| 470 | continue; |
| 471 | if (off == (ROMUSB_GLB + 0xc8)) /* core clock */ |
| 472 | continue; |
| 473 | if (off == (ROMUSB_GLB + 0x24)) /* MN clock */ |
| 474 | continue; |
| 475 | if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */ |
| 476 | continue; |
| 477 | if ((off & 0x0ff00000) == QLCNIC_CRB_DDR_NET) |
| 478 | continue; |
| 479 | /* skip the function enable register */ |
| 480 | if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION)) |
| 481 | continue; |
| 482 | if (off == QLCNIC_PCIE_REG(PCIE_SETUP_FUNCTION2)) |
| 483 | continue; |
| 484 | if ((off & 0x0ff00000) == QLCNIC_CRB_SMB) |
| 485 | continue; |
| 486 | |
| 487 | init_delay = 1; |
| 488 | /* After writing this register, HW needs time for CRB */ |
| 489 | /* to quiet down (else crb_window returns 0xffffffff) */ |
| 490 | if (off == QLCNIC_ROMUSB_GLB_SW_RESET) |
| 491 | init_delay = 1000; |
| 492 | |
| 493 | QLCWR32(adapter, off, buf[i].data); |
| 494 | |
| 495 | msleep(init_delay); |
| 496 | } |
| 497 | kfree(buf); |
| 498 | |
| 499 | /* p2dn replyCount */ |
| 500 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0xec, 0x1e); |
| 501 | /* disable_peg_cache 0 & 1*/ |
| 502 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_D + 0x4c, 8); |
| 503 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_I + 0x4c, 8); |
| 504 | |
| 505 | /* peg_clr_all */ |
| 506 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x8, 0); |
| 507 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0xc, 0); |
| 508 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x8, 0); |
| 509 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_1 + 0xc, 0); |
| 510 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x8, 0); |
| 511 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_2 + 0xc, 0); |
| 512 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x8, 0); |
| 513 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_3 + 0xc, 0); |
| 514 | return 0; |
| 515 | } |
| 516 | |
Sucheta Chakraborty | aa5e18c | 2010-04-01 19:01:32 +0000 | [diff] [blame] | 517 | void |
| 518 | qlcnic_setup_idc_param(struct qlcnic_adapter *adapter) { |
| 519 | |
| 520 | int timeo; |
| 521 | |
| 522 | if (qlcnic_rom_fast_read(adapter, QLCNIC_ROM_DEV_INIT_TIMEOUT, &timeo)) |
| 523 | timeo = 30; |
| 524 | |
| 525 | adapter->dev_init_timeo = timeo; |
| 526 | |
| 527 | if (qlcnic_rom_fast_read(adapter, QLCNIC_ROM_DRV_RESET_TIMEOUT, &timeo)) |
| 528 | timeo = 10; |
| 529 | |
| 530 | adapter->reset_ack_timeo = timeo; |
| 531 | } |
| 532 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 533 | static int |
| 534 | qlcnic_has_mn(struct qlcnic_adapter *adapter) |
| 535 | { |
| 536 | u32 capability, flashed_ver; |
| 537 | capability = 0; |
| 538 | |
| 539 | qlcnic_rom_fast_read(adapter, |
| 540 | QLCNIC_FW_VERSION_OFFSET, (int *)&flashed_ver); |
| 541 | flashed_ver = QLCNIC_DECODE_VERSION(flashed_ver); |
| 542 | |
Amit Kumar Salecha | 251a84c | 2010-05-13 03:07:46 +0000 | [diff] [blame^] | 543 | capability = QLCRD32(adapter, QLCNIC_PEG_TUNE_CAPABILITY); |
| 544 | if (capability & QLCNIC_PEG_TUNE_MN_PRESENT) |
| 545 | return 1; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 546 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static |
| 551 | struct uni_table_desc *qlcnic_get_table_desc(const u8 *unirom, int section) |
| 552 | { |
| 553 | u32 i; |
| 554 | struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0]; |
| 555 | __le32 entries = cpu_to_le32(directory->num_entries); |
| 556 | |
| 557 | for (i = 0; i < entries; i++) { |
| 558 | |
| 559 | __le32 offs = cpu_to_le32(directory->findex) + |
| 560 | (i * cpu_to_le32(directory->entry_size)); |
| 561 | __le32 tab_type = cpu_to_le32(*((u32 *)&unirom[offs] + 8)); |
| 562 | |
| 563 | if (tab_type == section) |
| 564 | return (struct uni_table_desc *) &unirom[offs]; |
| 565 | } |
| 566 | |
| 567 | return NULL; |
| 568 | } |
| 569 | |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 570 | #define FILEHEADER_SIZE (14 * 4) |
| 571 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 572 | static int |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 573 | qlcnic_validate_header(struct qlcnic_adapter *adapter) |
| 574 | { |
| 575 | const u8 *unirom = adapter->fw->data; |
| 576 | struct uni_table_desc *directory = (struct uni_table_desc *) &unirom[0]; |
| 577 | __le32 fw_file_size = adapter->fw->size; |
| 578 | __le32 entries; |
| 579 | __le32 entry_size; |
| 580 | __le32 tab_size; |
| 581 | |
| 582 | if (fw_file_size < FILEHEADER_SIZE) |
| 583 | return -EINVAL; |
| 584 | |
| 585 | entries = cpu_to_le32(directory->num_entries); |
| 586 | entry_size = cpu_to_le32(directory->entry_size); |
| 587 | tab_size = cpu_to_le32(directory->findex) + (entries * entry_size); |
| 588 | |
| 589 | if (fw_file_size < tab_size) |
| 590 | return -EINVAL; |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | static int |
| 596 | qlcnic_validate_bootld(struct qlcnic_adapter *adapter) |
| 597 | { |
| 598 | struct uni_table_desc *tab_desc; |
| 599 | struct uni_data_desc *descr; |
| 600 | const u8 *unirom = adapter->fw->data; |
| 601 | int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] + |
| 602 | QLCNIC_UNI_BOOTLD_IDX_OFF)); |
| 603 | __le32 offs; |
| 604 | __le32 tab_size; |
| 605 | __le32 data_size; |
| 606 | |
| 607 | tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_BOOTLD); |
| 608 | |
| 609 | if (!tab_desc) |
| 610 | return -EINVAL; |
| 611 | |
| 612 | tab_size = cpu_to_le32(tab_desc->findex) + |
Amit Kumar Salecha | 22dfaa8 | 2010-04-01 19:01:35 +0000 | [diff] [blame] | 613 | (cpu_to_le32(tab_desc->entry_size) * (idx + 1)); |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 614 | |
| 615 | if (adapter->fw->size < tab_size) |
| 616 | return -EINVAL; |
| 617 | |
| 618 | offs = cpu_to_le32(tab_desc->findex) + |
| 619 | (cpu_to_le32(tab_desc->entry_size) * (idx)); |
| 620 | descr = (struct uni_data_desc *)&unirom[offs]; |
| 621 | |
Amit Kumar Salecha | 22dfaa8 | 2010-04-01 19:01:35 +0000 | [diff] [blame] | 622 | data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size); |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 623 | |
| 624 | if (adapter->fw->size < data_size) |
| 625 | return -EINVAL; |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static int |
| 631 | qlcnic_validate_fw(struct qlcnic_adapter *adapter) |
| 632 | { |
| 633 | struct uni_table_desc *tab_desc; |
| 634 | struct uni_data_desc *descr; |
| 635 | const u8 *unirom = adapter->fw->data; |
| 636 | int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] + |
| 637 | QLCNIC_UNI_FIRMWARE_IDX_OFF)); |
| 638 | __le32 offs; |
| 639 | __le32 tab_size; |
| 640 | __le32 data_size; |
| 641 | |
| 642 | tab_desc = qlcnic_get_table_desc(unirom, QLCNIC_UNI_DIR_SECT_FW); |
| 643 | |
| 644 | if (!tab_desc) |
| 645 | return -EINVAL; |
| 646 | |
| 647 | tab_size = cpu_to_le32(tab_desc->findex) + |
Amit Kumar Salecha | 22dfaa8 | 2010-04-01 19:01:35 +0000 | [diff] [blame] | 648 | (cpu_to_le32(tab_desc->entry_size) * (idx + 1)); |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 649 | |
| 650 | if (adapter->fw->size < tab_size) |
| 651 | return -EINVAL; |
| 652 | |
| 653 | offs = cpu_to_le32(tab_desc->findex) + |
| 654 | (cpu_to_le32(tab_desc->entry_size) * (idx)); |
| 655 | descr = (struct uni_data_desc *)&unirom[offs]; |
Amit Kumar Salecha | 22dfaa8 | 2010-04-01 19:01:35 +0000 | [diff] [blame] | 656 | data_size = cpu_to_le32(descr->findex) + cpu_to_le32(descr->size); |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 657 | |
| 658 | if (adapter->fw->size < data_size) |
| 659 | return -EINVAL; |
| 660 | |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | static int |
| 665 | qlcnic_validate_product_offs(struct qlcnic_adapter *adapter) |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 666 | { |
| 667 | struct uni_table_desc *ptab_descr; |
| 668 | const u8 *unirom = adapter->fw->data; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 669 | int mn_present = qlcnic_has_mn(adapter); |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 670 | __le32 entries; |
| 671 | __le32 entry_size; |
| 672 | __le32 tab_size; |
| 673 | u32 i; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 674 | |
| 675 | ptab_descr = qlcnic_get_table_desc(unirom, |
| 676 | QLCNIC_UNI_DIR_SECT_PRODUCT_TBL); |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 677 | if (!ptab_descr) |
| 678 | return -EINVAL; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 679 | |
| 680 | entries = cpu_to_le32(ptab_descr->num_entries); |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 681 | entry_size = cpu_to_le32(ptab_descr->entry_size); |
| 682 | tab_size = cpu_to_le32(ptab_descr->findex) + (entries * entry_size); |
| 683 | |
| 684 | if (adapter->fw->size < tab_size) |
| 685 | return -EINVAL; |
| 686 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 687 | nomn: |
| 688 | for (i = 0; i < entries; i++) { |
| 689 | |
| 690 | __le32 flags, file_chiprev, offs; |
| 691 | u8 chiprev = adapter->ahw.revision_id; |
| 692 | u32 flagbit; |
| 693 | |
| 694 | offs = cpu_to_le32(ptab_descr->findex) + |
| 695 | (i * cpu_to_le32(ptab_descr->entry_size)); |
| 696 | flags = cpu_to_le32(*((int *)&unirom[offs] + |
| 697 | QLCNIC_UNI_FLAGS_OFF)); |
| 698 | file_chiprev = cpu_to_le32(*((int *)&unirom[offs] + |
| 699 | QLCNIC_UNI_CHIP_REV_OFF)); |
| 700 | |
| 701 | flagbit = mn_present ? 1 : 2; |
| 702 | |
| 703 | if ((chiprev == file_chiprev) && |
| 704 | ((1ULL << flagbit) & flags)) { |
| 705 | adapter->file_prd_off = offs; |
| 706 | return 0; |
| 707 | } |
| 708 | } |
| 709 | if (mn_present) { |
| 710 | mn_present = 0; |
| 711 | goto nomn; |
| 712 | } |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 713 | return -EINVAL; |
| 714 | } |
| 715 | |
| 716 | static int |
| 717 | qlcnic_validate_unified_romimage(struct qlcnic_adapter *adapter) |
| 718 | { |
| 719 | if (qlcnic_validate_header(adapter)) { |
| 720 | dev_err(&adapter->pdev->dev, |
| 721 | "unified image: header validation failed\n"); |
| 722 | return -EINVAL; |
| 723 | } |
| 724 | |
| 725 | if (qlcnic_validate_product_offs(adapter)) { |
| 726 | dev_err(&adapter->pdev->dev, |
| 727 | "unified image: product validation failed\n"); |
| 728 | return -EINVAL; |
| 729 | } |
| 730 | |
| 731 | if (qlcnic_validate_bootld(adapter)) { |
| 732 | dev_err(&adapter->pdev->dev, |
| 733 | "unified image: bootld validation failed\n"); |
| 734 | return -EINVAL; |
| 735 | } |
| 736 | |
| 737 | if (qlcnic_validate_fw(adapter)) { |
| 738 | dev_err(&adapter->pdev->dev, |
| 739 | "unified image: firmware validation failed\n"); |
| 740 | return -EINVAL; |
| 741 | } |
| 742 | |
| 743 | return 0; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | static |
| 747 | struct uni_data_desc *qlcnic_get_data_desc(struct qlcnic_adapter *adapter, |
| 748 | u32 section, u32 idx_offset) |
| 749 | { |
| 750 | const u8 *unirom = adapter->fw->data; |
| 751 | int idx = cpu_to_le32(*((int *)&unirom[adapter->file_prd_off] + |
| 752 | idx_offset)); |
| 753 | struct uni_table_desc *tab_desc; |
| 754 | __le32 offs; |
| 755 | |
| 756 | tab_desc = qlcnic_get_table_desc(unirom, section); |
| 757 | |
| 758 | if (tab_desc == NULL) |
| 759 | return NULL; |
| 760 | |
| 761 | offs = cpu_to_le32(tab_desc->findex) + |
| 762 | (cpu_to_le32(tab_desc->entry_size) * idx); |
| 763 | |
| 764 | return (struct uni_data_desc *)&unirom[offs]; |
| 765 | } |
| 766 | |
| 767 | static u8 * |
| 768 | qlcnic_get_bootld_offs(struct qlcnic_adapter *adapter) |
| 769 | { |
| 770 | u32 offs = QLCNIC_BOOTLD_START; |
| 771 | |
| 772 | if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE) |
| 773 | offs = cpu_to_le32((qlcnic_get_data_desc(adapter, |
| 774 | QLCNIC_UNI_DIR_SECT_BOOTLD, |
| 775 | QLCNIC_UNI_BOOTLD_IDX_OFF))->findex); |
| 776 | |
| 777 | return (u8 *)&adapter->fw->data[offs]; |
| 778 | } |
| 779 | |
| 780 | static u8 * |
| 781 | qlcnic_get_fw_offs(struct qlcnic_adapter *adapter) |
| 782 | { |
| 783 | u32 offs = QLCNIC_IMAGE_START; |
| 784 | |
| 785 | if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE) |
| 786 | offs = cpu_to_le32((qlcnic_get_data_desc(adapter, |
| 787 | QLCNIC_UNI_DIR_SECT_FW, |
| 788 | QLCNIC_UNI_FIRMWARE_IDX_OFF))->findex); |
| 789 | |
| 790 | return (u8 *)&adapter->fw->data[offs]; |
| 791 | } |
| 792 | |
| 793 | static __le32 |
| 794 | qlcnic_get_fw_size(struct qlcnic_adapter *adapter) |
| 795 | { |
| 796 | if (adapter->fw_type == QLCNIC_UNIFIED_ROMIMAGE) |
| 797 | return cpu_to_le32((qlcnic_get_data_desc(adapter, |
| 798 | QLCNIC_UNI_DIR_SECT_FW, |
| 799 | QLCNIC_UNI_FIRMWARE_IDX_OFF))->size); |
| 800 | else |
| 801 | return cpu_to_le32( |
| 802 | *(u32 *)&adapter->fw->data[QLCNIC_FW_SIZE_OFFSET]); |
| 803 | } |
| 804 | |
| 805 | static __le32 |
| 806 | qlcnic_get_fw_version(struct qlcnic_adapter *adapter) |
| 807 | { |
| 808 | struct uni_data_desc *fw_data_desc; |
| 809 | const struct firmware *fw = adapter->fw; |
| 810 | __le32 major, minor, sub; |
| 811 | const u8 *ver_str; |
| 812 | int i, ret; |
| 813 | |
| 814 | if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE) |
| 815 | return cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_VERSION_OFFSET]); |
| 816 | |
| 817 | fw_data_desc = qlcnic_get_data_desc(adapter, QLCNIC_UNI_DIR_SECT_FW, |
| 818 | QLCNIC_UNI_FIRMWARE_IDX_OFF); |
| 819 | ver_str = fw->data + cpu_to_le32(fw_data_desc->findex) + |
| 820 | cpu_to_le32(fw_data_desc->size) - 17; |
| 821 | |
| 822 | for (i = 0; i < 12; i++) { |
| 823 | if (!strncmp(&ver_str[i], "REV=", 4)) { |
| 824 | ret = sscanf(&ver_str[i+4], "%u.%u.%u ", |
| 825 | &major, &minor, &sub); |
| 826 | if (ret != 3) |
| 827 | return 0; |
| 828 | else |
| 829 | return major + (minor << 8) + (sub << 16); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | static __le32 |
| 837 | qlcnic_get_bios_version(struct qlcnic_adapter *adapter) |
| 838 | { |
| 839 | const struct firmware *fw = adapter->fw; |
| 840 | __le32 bios_ver, prd_off = adapter->file_prd_off; |
| 841 | |
| 842 | if (adapter->fw_type != QLCNIC_UNIFIED_ROMIMAGE) |
| 843 | return cpu_to_le32( |
| 844 | *(u32 *)&fw->data[QLCNIC_BIOS_VERSION_OFFSET]); |
| 845 | |
| 846 | bios_ver = cpu_to_le32(*((u32 *) (&fw->data[prd_off]) |
| 847 | + QLCNIC_UNI_BIOS_VERSION_OFF)); |
| 848 | |
Amit Kumar Salecha | addd5ab | 2010-03-08 00:14:49 +0000 | [diff] [blame] | 849 | return (bios_ver << 16) + ((bios_ver >> 8) & 0xff00) + (bios_ver >> 24); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | int |
| 853 | qlcnic_need_fw_reset(struct qlcnic_adapter *adapter) |
| 854 | { |
| 855 | u32 count, old_count; |
| 856 | u32 val, version, major, minor, build; |
| 857 | int i, timeout; |
| 858 | |
| 859 | if (adapter->need_fw_reset) |
| 860 | return 1; |
| 861 | |
| 862 | /* last attempt had failed */ |
| 863 | if (QLCRD32(adapter, CRB_CMDPEG_STATE) == PHAN_INITIALIZE_FAILED) |
| 864 | return 1; |
| 865 | |
| 866 | old_count = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER); |
| 867 | |
| 868 | for (i = 0; i < 10; i++) { |
| 869 | |
| 870 | timeout = msleep_interruptible(200); |
| 871 | if (timeout) { |
| 872 | QLCWR32(adapter, CRB_CMDPEG_STATE, |
| 873 | PHAN_INITIALIZE_FAILED); |
| 874 | return -EINTR; |
| 875 | } |
| 876 | |
| 877 | count = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER); |
| 878 | if (count != old_count) |
| 879 | break; |
| 880 | } |
| 881 | |
| 882 | /* firmware is dead */ |
| 883 | if (count == old_count) |
| 884 | return 1; |
| 885 | |
| 886 | /* check if we have got newer or different file firmware */ |
| 887 | if (adapter->fw) { |
| 888 | |
| 889 | val = qlcnic_get_fw_version(adapter); |
| 890 | |
| 891 | version = QLCNIC_DECODE_VERSION(val); |
| 892 | |
| 893 | major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR); |
| 894 | minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR); |
| 895 | build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB); |
| 896 | |
| 897 | if (version > QLCNIC_VERSION_CODE(major, minor, build)) |
| 898 | return 1; |
| 899 | } |
| 900 | |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | static const char *fw_name[] = { |
| 905 | QLCNIC_UNIFIED_ROMIMAGE_NAME, |
| 906 | QLCNIC_FLASH_ROMIMAGE_NAME, |
| 907 | }; |
| 908 | |
| 909 | int |
| 910 | qlcnic_load_firmware(struct qlcnic_adapter *adapter) |
| 911 | { |
| 912 | u64 *ptr64; |
| 913 | u32 i, flashaddr, size; |
| 914 | const struct firmware *fw = adapter->fw; |
| 915 | struct pci_dev *pdev = adapter->pdev; |
| 916 | |
| 917 | dev_info(&pdev->dev, "loading firmware from %s\n", |
| 918 | fw_name[adapter->fw_type]); |
| 919 | |
| 920 | if (fw) { |
| 921 | __le64 data; |
| 922 | |
| 923 | size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8; |
| 924 | |
| 925 | ptr64 = (u64 *)qlcnic_get_bootld_offs(adapter); |
| 926 | flashaddr = QLCNIC_BOOTLD_START; |
| 927 | |
| 928 | for (i = 0; i < size; i++) { |
| 929 | data = cpu_to_le64(ptr64[i]); |
| 930 | |
| 931 | if (qlcnic_pci_mem_write_2M(adapter, flashaddr, data)) |
| 932 | return -EIO; |
| 933 | |
| 934 | flashaddr += 8; |
| 935 | } |
| 936 | |
| 937 | size = (__force u32)qlcnic_get_fw_size(adapter) / 8; |
| 938 | |
| 939 | ptr64 = (u64 *)qlcnic_get_fw_offs(adapter); |
| 940 | flashaddr = QLCNIC_IMAGE_START; |
| 941 | |
| 942 | for (i = 0; i < size; i++) { |
| 943 | data = cpu_to_le64(ptr64[i]); |
| 944 | |
| 945 | if (qlcnic_pci_mem_write_2M(adapter, |
| 946 | flashaddr, data)) |
| 947 | return -EIO; |
| 948 | |
| 949 | flashaddr += 8; |
| 950 | } |
Amit Kumar Salecha | 0bc92b5 | 2010-04-01 19:01:28 +0000 | [diff] [blame] | 951 | |
| 952 | size = (__force u32)qlcnic_get_fw_size(adapter) % 8; |
| 953 | if (size) { |
| 954 | data = cpu_to_le64(ptr64[i]); |
| 955 | |
| 956 | if (qlcnic_pci_mem_write_2M(adapter, |
| 957 | flashaddr, data)) |
| 958 | return -EIO; |
| 959 | } |
| 960 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 961 | } else { |
| 962 | u64 data; |
| 963 | u32 hi, lo; |
| 964 | |
| 965 | size = (QLCNIC_IMAGE_START - QLCNIC_BOOTLD_START) / 8; |
| 966 | flashaddr = QLCNIC_BOOTLD_START; |
| 967 | |
| 968 | for (i = 0; i < size; i++) { |
| 969 | if (qlcnic_rom_fast_read(adapter, |
| 970 | flashaddr, (int *)&lo) != 0) |
| 971 | return -EIO; |
| 972 | if (qlcnic_rom_fast_read(adapter, |
| 973 | flashaddr + 4, (int *)&hi) != 0) |
| 974 | return -EIO; |
| 975 | |
| 976 | data = (((u64)hi << 32) | lo); |
| 977 | |
| 978 | if (qlcnic_pci_mem_write_2M(adapter, |
| 979 | flashaddr, data)) |
| 980 | return -EIO; |
| 981 | |
| 982 | flashaddr += 8; |
| 983 | } |
| 984 | } |
| 985 | msleep(1); |
| 986 | |
| 987 | QLCWR32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x18, 0x1020); |
| 988 | QLCWR32(adapter, QLCNIC_ROMUSB_GLB_SW_RESET, 0x80001e); |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | static int |
| 993 | qlcnic_validate_firmware(struct qlcnic_adapter *adapter) |
| 994 | { |
| 995 | __le32 val; |
| 996 | u32 ver, min_ver, bios, min_size; |
| 997 | struct pci_dev *pdev = adapter->pdev; |
| 998 | const struct firmware *fw = adapter->fw; |
| 999 | u8 fw_type = adapter->fw_type; |
| 1000 | |
| 1001 | if (fw_type == QLCNIC_UNIFIED_ROMIMAGE) { |
Sucheta Chakraborty | b7eff10 | 2010-03-08 00:14:48 +0000 | [diff] [blame] | 1002 | if (qlcnic_validate_unified_romimage(adapter)) |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1003 | return -EINVAL; |
| 1004 | |
| 1005 | min_size = QLCNIC_UNI_FW_MIN_SIZE; |
| 1006 | } else { |
| 1007 | val = cpu_to_le32(*(u32 *)&fw->data[QLCNIC_FW_MAGIC_OFFSET]); |
| 1008 | if ((__force u32)val != QLCNIC_BDINFO_MAGIC) |
| 1009 | return -EINVAL; |
| 1010 | |
| 1011 | min_size = QLCNIC_FW_MIN_SIZE; |
| 1012 | } |
| 1013 | |
| 1014 | if (fw->size < min_size) |
| 1015 | return -EINVAL; |
| 1016 | |
| 1017 | val = qlcnic_get_fw_version(adapter); |
| 1018 | |
| 1019 | min_ver = QLCNIC_VERSION_CODE(4, 0, 216); |
| 1020 | |
| 1021 | ver = QLCNIC_DECODE_VERSION(val); |
| 1022 | |
| 1023 | if ((_major(ver) > _QLCNIC_LINUX_MAJOR) || (ver < min_ver)) { |
| 1024 | dev_err(&pdev->dev, |
| 1025 | "%s: firmware version %d.%d.%d unsupported\n", |
| 1026 | fw_name[fw_type], _major(ver), _minor(ver), _build(ver)); |
| 1027 | return -EINVAL; |
| 1028 | } |
| 1029 | |
| 1030 | val = qlcnic_get_bios_version(adapter); |
| 1031 | qlcnic_rom_fast_read(adapter, QLCNIC_BIOS_VERSION_OFFSET, (int *)&bios); |
| 1032 | if ((__force u32)val != bios) { |
| 1033 | dev_err(&pdev->dev, "%s: firmware bios is incompatible\n", |
| 1034 | fw_name[fw_type]); |
| 1035 | return -EINVAL; |
| 1036 | } |
| 1037 | |
| 1038 | /* check if flashed firmware is newer */ |
| 1039 | if (qlcnic_rom_fast_read(adapter, |
| 1040 | QLCNIC_FW_VERSION_OFFSET, (int *)&val)) |
| 1041 | return -EIO; |
| 1042 | |
| 1043 | val = QLCNIC_DECODE_VERSION(val); |
| 1044 | if (val > ver) { |
| 1045 | dev_info(&pdev->dev, "%s: firmware is older than flash\n", |
| 1046 | fw_name[fw_type]); |
| 1047 | return -EINVAL; |
| 1048 | } |
| 1049 | |
| 1050 | QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC); |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | static void |
| 1055 | qlcnic_get_next_fwtype(struct qlcnic_adapter *adapter) |
| 1056 | { |
| 1057 | u8 fw_type; |
| 1058 | |
| 1059 | switch (adapter->fw_type) { |
| 1060 | case QLCNIC_UNKNOWN_ROMIMAGE: |
| 1061 | fw_type = QLCNIC_UNIFIED_ROMIMAGE; |
| 1062 | break; |
| 1063 | |
| 1064 | case QLCNIC_UNIFIED_ROMIMAGE: |
| 1065 | default: |
| 1066 | fw_type = QLCNIC_FLASH_ROMIMAGE; |
| 1067 | break; |
| 1068 | } |
| 1069 | |
| 1070 | adapter->fw_type = fw_type; |
| 1071 | } |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | void qlcnic_request_firmware(struct qlcnic_adapter *adapter) |
| 1076 | { |
| 1077 | struct pci_dev *pdev = adapter->pdev; |
| 1078 | int rc; |
| 1079 | |
| 1080 | adapter->fw_type = QLCNIC_UNKNOWN_ROMIMAGE; |
| 1081 | |
| 1082 | next: |
| 1083 | qlcnic_get_next_fwtype(adapter); |
| 1084 | |
| 1085 | if (adapter->fw_type == QLCNIC_FLASH_ROMIMAGE) { |
| 1086 | adapter->fw = NULL; |
| 1087 | } else { |
| 1088 | rc = request_firmware(&adapter->fw, |
| 1089 | fw_name[adapter->fw_type], &pdev->dev); |
| 1090 | if (rc != 0) |
| 1091 | goto next; |
| 1092 | |
| 1093 | rc = qlcnic_validate_firmware(adapter); |
| 1094 | if (rc != 0) { |
| 1095 | release_firmware(adapter->fw); |
| 1096 | msleep(1); |
| 1097 | goto next; |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | |
| 1103 | void |
| 1104 | qlcnic_release_firmware(struct qlcnic_adapter *adapter) |
| 1105 | { |
| 1106 | if (adapter->fw) |
| 1107 | release_firmware(adapter->fw); |
| 1108 | adapter->fw = NULL; |
| 1109 | } |
| 1110 | |
| 1111 | int qlcnic_phantom_init(struct qlcnic_adapter *adapter) |
| 1112 | { |
| 1113 | u32 val; |
| 1114 | int retries = 60; |
| 1115 | |
| 1116 | do { |
| 1117 | val = QLCRD32(adapter, CRB_CMDPEG_STATE); |
| 1118 | |
| 1119 | switch (val) { |
| 1120 | case PHAN_INITIALIZE_COMPLETE: |
| 1121 | case PHAN_INITIALIZE_ACK: |
| 1122 | return 0; |
| 1123 | case PHAN_INITIALIZE_FAILED: |
| 1124 | goto out_err; |
| 1125 | default: |
| 1126 | break; |
| 1127 | } |
| 1128 | |
| 1129 | msleep(500); |
| 1130 | |
| 1131 | } while (--retries); |
| 1132 | |
| 1133 | QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED); |
| 1134 | |
| 1135 | out_err: |
| 1136 | dev_err(&adapter->pdev->dev, "firmware init failed\n"); |
| 1137 | return -EIO; |
| 1138 | } |
| 1139 | |
| 1140 | static int |
| 1141 | qlcnic_receive_peg_ready(struct qlcnic_adapter *adapter) |
| 1142 | { |
| 1143 | u32 val; |
| 1144 | int retries = 2000; |
| 1145 | |
| 1146 | do { |
| 1147 | val = QLCRD32(adapter, CRB_RCVPEG_STATE); |
| 1148 | |
| 1149 | if (val == PHAN_PEG_RCV_INITIALIZED) |
| 1150 | return 0; |
| 1151 | |
| 1152 | msleep(10); |
| 1153 | |
| 1154 | } while (--retries); |
| 1155 | |
| 1156 | if (!retries) { |
| 1157 | dev_err(&adapter->pdev->dev, "Receive Peg initialization not " |
| 1158 | "complete, state: 0x%x.\n", val); |
| 1159 | return -EIO; |
| 1160 | } |
| 1161 | |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | int qlcnic_init_firmware(struct qlcnic_adapter *adapter) |
| 1166 | { |
| 1167 | int err; |
| 1168 | |
| 1169 | err = qlcnic_receive_peg_ready(adapter); |
| 1170 | if (err) |
| 1171 | return err; |
| 1172 | |
| 1173 | QLCWR32(adapter, CRB_NIC_CAPABILITIES_HOST, INTR_SCHEME_PERPORT); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1174 | QLCWR32(adapter, CRB_MPORT_MODE, MPORT_MULTI_FUNCTION_MODE); |
| 1175 | QLCWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK); |
| 1176 | |
| 1177 | return err; |
| 1178 | } |
| 1179 | |
| 1180 | static void |
| 1181 | qlcnic_handle_linkevent(struct qlcnic_adapter *adapter, |
| 1182 | struct qlcnic_fw_msg *msg) |
| 1183 | { |
| 1184 | u32 cable_OUI; |
| 1185 | u16 cable_len; |
| 1186 | u16 link_speed; |
| 1187 | u8 link_status, module, duplex, autoneg; |
| 1188 | struct net_device *netdev = adapter->netdev; |
| 1189 | |
| 1190 | adapter->has_link_events = 1; |
| 1191 | |
| 1192 | cable_OUI = msg->body[1] & 0xffffffff; |
| 1193 | cable_len = (msg->body[1] >> 32) & 0xffff; |
| 1194 | link_speed = (msg->body[1] >> 48) & 0xffff; |
| 1195 | |
| 1196 | link_status = msg->body[2] & 0xff; |
| 1197 | duplex = (msg->body[2] >> 16) & 0xff; |
| 1198 | autoneg = (msg->body[2] >> 24) & 0xff; |
| 1199 | |
| 1200 | module = (msg->body[2] >> 8) & 0xff; |
| 1201 | if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE) |
| 1202 | dev_info(&netdev->dev, "unsupported cable: OUI 0x%x, " |
| 1203 | "length %d\n", cable_OUI, cable_len); |
| 1204 | else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN) |
| 1205 | dev_info(&netdev->dev, "unsupported cable length %d\n", |
| 1206 | cable_len); |
| 1207 | |
| 1208 | qlcnic_advert_link_change(adapter, link_status); |
| 1209 | |
| 1210 | if (duplex == LINKEVENT_FULL_DUPLEX) |
| 1211 | adapter->link_duplex = DUPLEX_FULL; |
| 1212 | else |
| 1213 | adapter->link_duplex = DUPLEX_HALF; |
| 1214 | |
| 1215 | adapter->module_type = module; |
| 1216 | adapter->link_autoneg = autoneg; |
| 1217 | adapter->link_speed = link_speed; |
| 1218 | } |
| 1219 | |
| 1220 | static void |
| 1221 | qlcnic_handle_fw_message(int desc_cnt, int index, |
| 1222 | struct qlcnic_host_sds_ring *sds_ring) |
| 1223 | { |
| 1224 | struct qlcnic_fw_msg msg; |
| 1225 | struct status_desc *desc; |
| 1226 | int i = 0, opcode; |
| 1227 | |
| 1228 | while (desc_cnt > 0 && i < 8) { |
| 1229 | desc = &sds_ring->desc_head[index]; |
| 1230 | msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]); |
| 1231 | msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]); |
| 1232 | |
| 1233 | index = get_next_index(index, sds_ring->num_desc); |
| 1234 | desc_cnt--; |
| 1235 | } |
| 1236 | |
| 1237 | opcode = qlcnic_get_nic_msg_opcode(msg.body[0]); |
| 1238 | switch (opcode) { |
| 1239 | case QLCNIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE: |
| 1240 | qlcnic_handle_linkevent(sds_ring->adapter, &msg); |
| 1241 | break; |
| 1242 | default: |
| 1243 | break; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | static int |
| 1248 | qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter, |
| 1249 | struct qlcnic_host_rds_ring *rds_ring, |
| 1250 | struct qlcnic_rx_buffer *buffer) |
| 1251 | { |
| 1252 | struct sk_buff *skb; |
| 1253 | dma_addr_t dma; |
| 1254 | struct pci_dev *pdev = adapter->pdev; |
| 1255 | |
| 1256 | buffer->skb = dev_alloc_skb(rds_ring->skb_size); |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 1257 | if (!buffer->skb) { |
| 1258 | adapter->stats.skb_alloc_failure++; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1259 | return -ENOMEM; |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 1260 | } |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1261 | |
| 1262 | skb = buffer->skb; |
| 1263 | |
Amit Kumar Salecha | 251a84c | 2010-05-13 03:07:46 +0000 | [diff] [blame^] | 1264 | skb_reserve(skb, 2); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1265 | |
| 1266 | dma = pci_map_single(pdev, skb->data, |
| 1267 | rds_ring->dma_size, PCI_DMA_FROMDEVICE); |
| 1268 | |
| 1269 | if (pci_dma_mapping_error(pdev, dma)) { |
Amit Kumar Salecha | 8ae6df9 | 2010-04-22 02:51:35 +0000 | [diff] [blame] | 1270 | adapter->stats.rx_dma_map_error++; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1271 | dev_kfree_skb_any(skb); |
| 1272 | buffer->skb = NULL; |
| 1273 | return -ENOMEM; |
| 1274 | } |
| 1275 | |
| 1276 | buffer->skb = skb; |
| 1277 | buffer->dma = dma; |
| 1278 | buffer->state = QLCNIC_BUFFER_BUSY; |
| 1279 | |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | static struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter, |
| 1284 | struct qlcnic_host_rds_ring *rds_ring, u16 index, u16 cksum) |
| 1285 | { |
| 1286 | struct qlcnic_rx_buffer *buffer; |
| 1287 | struct sk_buff *skb; |
| 1288 | |
| 1289 | buffer = &rds_ring->rx_buf_arr[index]; |
| 1290 | |
| 1291 | pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size, |
| 1292 | PCI_DMA_FROMDEVICE); |
| 1293 | |
| 1294 | skb = buffer->skb; |
Amit Kumar Salecha | 8ae6df9 | 2010-04-22 02:51:35 +0000 | [diff] [blame] | 1295 | if (!skb) { |
| 1296 | adapter->stats.null_skb++; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1297 | goto no_skb; |
Amit Kumar Salecha | 8ae6df9 | 2010-04-22 02:51:35 +0000 | [diff] [blame] | 1298 | } |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1299 | |
| 1300 | if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) { |
| 1301 | adapter->stats.csummed++; |
| 1302 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1303 | } else { |
| 1304 | skb->ip_summed = CHECKSUM_NONE; |
| 1305 | } |
| 1306 | |
| 1307 | skb->dev = adapter->netdev; |
| 1308 | |
| 1309 | buffer->skb = NULL; |
| 1310 | no_skb: |
| 1311 | buffer->state = QLCNIC_BUFFER_FREE; |
| 1312 | return skb; |
| 1313 | } |
| 1314 | |
| 1315 | static struct qlcnic_rx_buffer * |
| 1316 | qlcnic_process_rcv(struct qlcnic_adapter *adapter, |
| 1317 | struct qlcnic_host_sds_ring *sds_ring, |
| 1318 | int ring, u64 sts_data0) |
| 1319 | { |
| 1320 | struct net_device *netdev = adapter->netdev; |
| 1321 | struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx; |
| 1322 | struct qlcnic_rx_buffer *buffer; |
| 1323 | struct sk_buff *skb; |
| 1324 | struct qlcnic_host_rds_ring *rds_ring; |
| 1325 | int index, length, cksum, pkt_offset; |
| 1326 | |
| 1327 | if (unlikely(ring >= adapter->max_rds_rings)) |
| 1328 | return NULL; |
| 1329 | |
| 1330 | rds_ring = &recv_ctx->rds_rings[ring]; |
| 1331 | |
| 1332 | index = qlcnic_get_sts_refhandle(sts_data0); |
| 1333 | if (unlikely(index >= rds_ring->num_desc)) |
| 1334 | return NULL; |
| 1335 | |
| 1336 | buffer = &rds_ring->rx_buf_arr[index]; |
| 1337 | |
| 1338 | length = qlcnic_get_sts_totallength(sts_data0); |
| 1339 | cksum = qlcnic_get_sts_status(sts_data0); |
| 1340 | pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0); |
| 1341 | |
| 1342 | skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum); |
| 1343 | if (!skb) |
| 1344 | return buffer; |
| 1345 | |
| 1346 | if (length > rds_ring->skb_size) |
| 1347 | skb_put(skb, rds_ring->skb_size); |
| 1348 | else |
| 1349 | skb_put(skb, length); |
| 1350 | |
| 1351 | if (pkt_offset) |
| 1352 | skb_pull(skb, pkt_offset); |
| 1353 | |
| 1354 | skb->truesize = skb->len + sizeof(struct sk_buff); |
| 1355 | skb->protocol = eth_type_trans(skb, netdev); |
| 1356 | |
| 1357 | napi_gro_receive(&sds_ring->napi, skb); |
| 1358 | |
| 1359 | adapter->stats.rx_pkts++; |
| 1360 | adapter->stats.rxbytes += length; |
| 1361 | |
| 1362 | return buffer; |
| 1363 | } |
| 1364 | |
| 1365 | #define QLC_TCP_HDR_SIZE 20 |
| 1366 | #define QLC_TCP_TS_OPTION_SIZE 12 |
| 1367 | #define QLC_TCP_TS_HDR_SIZE (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE) |
| 1368 | |
| 1369 | static struct qlcnic_rx_buffer * |
| 1370 | qlcnic_process_lro(struct qlcnic_adapter *adapter, |
| 1371 | struct qlcnic_host_sds_ring *sds_ring, |
| 1372 | int ring, u64 sts_data0, u64 sts_data1) |
| 1373 | { |
| 1374 | struct net_device *netdev = adapter->netdev; |
| 1375 | struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx; |
| 1376 | struct qlcnic_rx_buffer *buffer; |
| 1377 | struct sk_buff *skb; |
| 1378 | struct qlcnic_host_rds_ring *rds_ring; |
| 1379 | struct iphdr *iph; |
| 1380 | struct tcphdr *th; |
| 1381 | bool push, timestamp; |
| 1382 | int l2_hdr_offset, l4_hdr_offset; |
| 1383 | int index; |
| 1384 | u16 lro_length, length, data_offset; |
| 1385 | u32 seq_number; |
| 1386 | |
| 1387 | if (unlikely(ring > adapter->max_rds_rings)) |
| 1388 | return NULL; |
| 1389 | |
| 1390 | rds_ring = &recv_ctx->rds_rings[ring]; |
| 1391 | |
| 1392 | index = qlcnic_get_lro_sts_refhandle(sts_data0); |
| 1393 | if (unlikely(index > rds_ring->num_desc)) |
| 1394 | return NULL; |
| 1395 | |
| 1396 | buffer = &rds_ring->rx_buf_arr[index]; |
| 1397 | |
| 1398 | timestamp = qlcnic_get_lro_sts_timestamp(sts_data0); |
| 1399 | lro_length = qlcnic_get_lro_sts_length(sts_data0); |
| 1400 | l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0); |
| 1401 | l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0); |
| 1402 | push = qlcnic_get_lro_sts_push_flag(sts_data0); |
| 1403 | seq_number = qlcnic_get_lro_sts_seq_number(sts_data1); |
| 1404 | |
| 1405 | skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK); |
| 1406 | if (!skb) |
| 1407 | return buffer; |
| 1408 | |
| 1409 | if (timestamp) |
| 1410 | data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE; |
| 1411 | else |
| 1412 | data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE; |
| 1413 | |
| 1414 | skb_put(skb, lro_length + data_offset); |
| 1415 | |
| 1416 | skb->truesize = skb->len + sizeof(struct sk_buff) + skb_headroom(skb); |
| 1417 | |
| 1418 | skb_pull(skb, l2_hdr_offset); |
| 1419 | skb->protocol = eth_type_trans(skb, netdev); |
| 1420 | |
| 1421 | iph = (struct iphdr *)skb->data; |
| 1422 | th = (struct tcphdr *)(skb->data + (iph->ihl << 2)); |
| 1423 | |
| 1424 | length = (iph->ihl << 2) + (th->doff << 2) + lro_length; |
| 1425 | iph->tot_len = htons(length); |
| 1426 | iph->check = 0; |
| 1427 | iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); |
| 1428 | th->psh = push; |
| 1429 | th->seq = htonl(seq_number); |
| 1430 | |
| 1431 | length = skb->len; |
| 1432 | |
| 1433 | netif_receive_skb(skb); |
| 1434 | |
| 1435 | adapter->stats.lro_pkts++; |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 1436 | adapter->stats.lrobytes += length; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1437 | |
| 1438 | return buffer; |
| 1439 | } |
| 1440 | |
| 1441 | int |
| 1442 | qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring, int max) |
| 1443 | { |
| 1444 | struct qlcnic_adapter *adapter = sds_ring->adapter; |
| 1445 | struct list_head *cur; |
| 1446 | struct status_desc *desc; |
| 1447 | struct qlcnic_rx_buffer *rxbuf; |
| 1448 | u64 sts_data0, sts_data1; |
| 1449 | |
| 1450 | int count = 0; |
| 1451 | int opcode, ring, desc_cnt; |
| 1452 | u32 consumer = sds_ring->consumer; |
| 1453 | |
| 1454 | while (count < max) { |
| 1455 | desc = &sds_ring->desc_head[consumer]; |
| 1456 | sts_data0 = le64_to_cpu(desc->status_desc_data[0]); |
| 1457 | |
| 1458 | if (!(sts_data0 & STATUS_OWNER_HOST)) |
| 1459 | break; |
| 1460 | |
| 1461 | desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0); |
| 1462 | opcode = qlcnic_get_sts_opcode(sts_data0); |
| 1463 | |
| 1464 | switch (opcode) { |
| 1465 | case QLCNIC_RXPKT_DESC: |
| 1466 | case QLCNIC_OLD_RXPKT_DESC: |
| 1467 | case QLCNIC_SYN_OFFLOAD: |
| 1468 | ring = qlcnic_get_sts_type(sts_data0); |
| 1469 | rxbuf = qlcnic_process_rcv(adapter, sds_ring, |
| 1470 | ring, sts_data0); |
| 1471 | break; |
| 1472 | case QLCNIC_LRO_DESC: |
| 1473 | ring = qlcnic_get_lro_sts_type(sts_data0); |
| 1474 | sts_data1 = le64_to_cpu(desc->status_desc_data[1]); |
| 1475 | rxbuf = qlcnic_process_lro(adapter, sds_ring, |
| 1476 | ring, sts_data0, sts_data1); |
| 1477 | break; |
| 1478 | case QLCNIC_RESPONSE_DESC: |
| 1479 | qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring); |
| 1480 | default: |
| 1481 | goto skip; |
| 1482 | } |
| 1483 | |
| 1484 | WARN_ON(desc_cnt > 1); |
| 1485 | |
| 1486 | if (rxbuf) |
| 1487 | list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]); |
Amit Kumar Salecha | 8ae6df9 | 2010-04-22 02:51:35 +0000 | [diff] [blame] | 1488 | else |
| 1489 | adapter->stats.null_rxbuf++; |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1490 | |
| 1491 | skip: |
| 1492 | for (; desc_cnt > 0; desc_cnt--) { |
| 1493 | desc = &sds_ring->desc_head[consumer]; |
| 1494 | desc->status_desc_data[0] = |
| 1495 | cpu_to_le64(STATUS_OWNER_PHANTOM); |
| 1496 | consumer = get_next_index(consumer, sds_ring->num_desc); |
| 1497 | } |
| 1498 | count++; |
| 1499 | } |
| 1500 | |
| 1501 | for (ring = 0; ring < adapter->max_rds_rings; ring++) { |
| 1502 | struct qlcnic_host_rds_ring *rds_ring = |
| 1503 | &adapter->recv_ctx.rds_rings[ring]; |
| 1504 | |
| 1505 | if (!list_empty(&sds_ring->free_list[ring])) { |
| 1506 | list_for_each(cur, &sds_ring->free_list[ring]) { |
| 1507 | rxbuf = list_entry(cur, |
| 1508 | struct qlcnic_rx_buffer, list); |
| 1509 | qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf); |
| 1510 | } |
| 1511 | spin_lock(&rds_ring->lock); |
| 1512 | list_splice_tail_init(&sds_ring->free_list[ring], |
| 1513 | &rds_ring->free_list); |
| 1514 | spin_unlock(&rds_ring->lock); |
| 1515 | } |
| 1516 | |
| 1517 | qlcnic_post_rx_buffers_nodb(adapter, rds_ring); |
| 1518 | } |
| 1519 | |
| 1520 | if (count) { |
| 1521 | sds_ring->consumer = consumer; |
| 1522 | writel(consumer, sds_ring->crb_sts_consumer); |
| 1523 | } |
| 1524 | |
| 1525 | return count; |
| 1526 | } |
| 1527 | |
| 1528 | void |
| 1529 | qlcnic_post_rx_buffers(struct qlcnic_adapter *adapter, u32 ringid, |
| 1530 | struct qlcnic_host_rds_ring *rds_ring) |
| 1531 | { |
| 1532 | struct rcv_desc *pdesc; |
| 1533 | struct qlcnic_rx_buffer *buffer; |
| 1534 | int producer, count = 0; |
| 1535 | struct list_head *head; |
| 1536 | |
Amit Kumar Salecha | ed6f135 | 2010-04-22 02:51:40 +0000 | [diff] [blame] | 1537 | spin_lock(&rds_ring->lock); |
| 1538 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1539 | producer = rds_ring->producer; |
| 1540 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1541 | head = &rds_ring->free_list; |
| 1542 | while (!list_empty(head)) { |
| 1543 | |
| 1544 | buffer = list_entry(head->next, struct qlcnic_rx_buffer, list); |
| 1545 | |
| 1546 | if (!buffer->skb) { |
| 1547 | if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer)) |
| 1548 | break; |
| 1549 | } |
| 1550 | |
| 1551 | count++; |
| 1552 | list_del(&buffer->list); |
| 1553 | |
| 1554 | /* make a rcv descriptor */ |
| 1555 | pdesc = &rds_ring->desc_head[producer]; |
| 1556 | pdesc->addr_buffer = cpu_to_le64(buffer->dma); |
| 1557 | pdesc->reference_handle = cpu_to_le16(buffer->ref_handle); |
| 1558 | pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size); |
| 1559 | |
| 1560 | producer = get_next_index(producer, rds_ring->num_desc); |
| 1561 | } |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1562 | |
| 1563 | if (count) { |
| 1564 | rds_ring->producer = producer; |
| 1565 | writel((producer-1) & (rds_ring->num_desc-1), |
| 1566 | rds_ring->crb_rcv_producer); |
| 1567 | } |
Amit Kumar Salecha | ed6f135 | 2010-04-22 02:51:40 +0000 | [diff] [blame] | 1568 | spin_unlock(&rds_ring->lock); |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | static void |
| 1572 | qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter, |
| 1573 | struct qlcnic_host_rds_ring *rds_ring) |
| 1574 | { |
| 1575 | struct rcv_desc *pdesc; |
| 1576 | struct qlcnic_rx_buffer *buffer; |
| 1577 | int producer, count = 0; |
| 1578 | struct list_head *head; |
| 1579 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1580 | if (!spin_trylock(&rds_ring->lock)) |
| 1581 | return; |
| 1582 | |
Amit Kumar Salecha | ed6f135 | 2010-04-22 02:51:40 +0000 | [diff] [blame] | 1583 | producer = rds_ring->producer; |
| 1584 | |
Amit Kumar Salecha | af19b49 | 2010-01-13 00:37:25 +0000 | [diff] [blame] | 1585 | head = &rds_ring->free_list; |
| 1586 | while (!list_empty(head)) { |
| 1587 | |
| 1588 | buffer = list_entry(head->next, struct qlcnic_rx_buffer, list); |
| 1589 | |
| 1590 | if (!buffer->skb) { |
| 1591 | if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer)) |
| 1592 | break; |
| 1593 | } |
| 1594 | |
| 1595 | count++; |
| 1596 | list_del(&buffer->list); |
| 1597 | |
| 1598 | /* make a rcv descriptor */ |
| 1599 | pdesc = &rds_ring->desc_head[producer]; |
| 1600 | pdesc->reference_handle = cpu_to_le16(buffer->ref_handle); |
| 1601 | pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size); |
| 1602 | pdesc->addr_buffer = cpu_to_le64(buffer->dma); |
| 1603 | |
| 1604 | producer = get_next_index(producer, rds_ring->num_desc); |
| 1605 | } |
| 1606 | |
| 1607 | if (count) { |
| 1608 | rds_ring->producer = producer; |
| 1609 | writel((producer - 1) & (rds_ring->num_desc - 1), |
| 1610 | rds_ring->crb_rcv_producer); |
| 1611 | } |
| 1612 | spin_unlock(&rds_ring->lock); |
| 1613 | } |
| 1614 | |
Amit Kumar Salecha | cdaff18 | 2010-02-01 05:25:00 +0000 | [diff] [blame] | 1615 | static struct qlcnic_rx_buffer * |
| 1616 | qlcnic_process_rcv_diag(struct qlcnic_adapter *adapter, |
| 1617 | struct qlcnic_host_sds_ring *sds_ring, |
| 1618 | int ring, u64 sts_data0) |
| 1619 | { |
| 1620 | struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx; |
| 1621 | struct qlcnic_rx_buffer *buffer; |
| 1622 | struct sk_buff *skb; |
| 1623 | struct qlcnic_host_rds_ring *rds_ring; |
| 1624 | int index, length, cksum, pkt_offset; |
| 1625 | |
| 1626 | if (unlikely(ring >= adapter->max_rds_rings)) |
| 1627 | return NULL; |
| 1628 | |
| 1629 | rds_ring = &recv_ctx->rds_rings[ring]; |
| 1630 | |
| 1631 | index = qlcnic_get_sts_refhandle(sts_data0); |
| 1632 | if (unlikely(index >= rds_ring->num_desc)) |
| 1633 | return NULL; |
| 1634 | |
| 1635 | buffer = &rds_ring->rx_buf_arr[index]; |
| 1636 | |
| 1637 | length = qlcnic_get_sts_totallength(sts_data0); |
| 1638 | cksum = qlcnic_get_sts_status(sts_data0); |
| 1639 | pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0); |
| 1640 | |
| 1641 | skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum); |
| 1642 | if (!skb) |
| 1643 | return buffer; |
| 1644 | |
| 1645 | skb_put(skb, rds_ring->skb_size); |
| 1646 | |
| 1647 | if (pkt_offset) |
| 1648 | skb_pull(skb, pkt_offset); |
| 1649 | |
| 1650 | skb->truesize = skb->len + sizeof(struct sk_buff); |
| 1651 | |
| 1652 | if (!qlcnic_check_loopback_buff(skb->data)) |
| 1653 | adapter->diag_cnt++; |
| 1654 | |
| 1655 | dev_kfree_skb_any(skb); |
Sucheta Chakraborty | 8bfe8b9 | 2010-03-08 00:14:46 +0000 | [diff] [blame] | 1656 | adapter->stats.rx_pkts++; |
| 1657 | adapter->stats.rxbytes += length; |
Amit Kumar Salecha | cdaff18 | 2010-02-01 05:25:00 +0000 | [diff] [blame] | 1658 | |
| 1659 | return buffer; |
| 1660 | } |
| 1661 | |
| 1662 | void |
| 1663 | qlcnic_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring) |
| 1664 | { |
| 1665 | struct qlcnic_adapter *adapter = sds_ring->adapter; |
| 1666 | struct status_desc *desc; |
| 1667 | struct qlcnic_rx_buffer *rxbuf; |
| 1668 | u64 sts_data0; |
| 1669 | |
| 1670 | int opcode, ring, desc_cnt; |
| 1671 | u32 consumer = sds_ring->consumer; |
| 1672 | |
| 1673 | desc = &sds_ring->desc_head[consumer]; |
| 1674 | sts_data0 = le64_to_cpu(desc->status_desc_data[0]); |
| 1675 | |
| 1676 | if (!(sts_data0 & STATUS_OWNER_HOST)) |
| 1677 | return; |
| 1678 | |
| 1679 | desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0); |
| 1680 | opcode = qlcnic_get_sts_opcode(sts_data0); |
| 1681 | |
| 1682 | ring = qlcnic_get_sts_type(sts_data0); |
| 1683 | rxbuf = qlcnic_process_rcv_diag(adapter, sds_ring, |
| 1684 | ring, sts_data0); |
| 1685 | |
| 1686 | desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM); |
| 1687 | consumer = get_next_index(consumer, sds_ring->num_desc); |
| 1688 | |
| 1689 | sds_ring->consumer = consumer; |
| 1690 | writel(consumer, sds_ring->crb_sts_consumer); |
| 1691 | } |